I. What is OpenCV?¶
OpenCV (Open Source Computer Vision Library) is an open-source computer vision library that provides a large number of functions for tasks such as image processing, object detection, and facial recognition. For programming beginners, learning image processing with Python combined with OpenCV is very friendly, as it allows us to quickly implement basic functions like “making computers ‘understand’ images.”
II. Installing OpenCV¶
To start using OpenCV, you first need to install the corresponding Python library. The simplest way is to use pip:
pip install opencv-python
If the installation is slow, you can try a domestic mirror source:
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
After installation, open a Python environment (such as PyCharm, Jupyter Notebook, or the command line) and enter the following code to verify the installation:
import cv2
print(cv2.__version__) # If the version number (e.g., 4.8.0) is printed, the installation is successful
III. Reading an Image¶
In computer vision, “reading an image” is the most basic operation, and OpenCV provides the cv2.imread() function to achieve this.
1. Function Introduction¶
The syntax of cv2.imread() is:
img = cv2.imread(image_path, flags)
image_path: The path to the image file (relative or absolute path).flags(optional parameter): The way to read the image. Common values include:cv2.IMREAD_COLOR(default, integer 1): Reads a color image (includes RGB channels).cv2.IMREAD_GRAYSCALE(integer 0): Reads a grayscale image (only brightness information, no color).cv2.IMREAD_UNCHANGED(integer -1): Reads the original image (includes an alpha channel if present).
2. Code Example: Reading an Image¶
Assume we have an image file named test.jpg in the same folder as the Python code. The code is as follows:
import cv2 # Import the OpenCV library
# Read the image from the current folder (path: test.jpg)
img = cv2.imread('test.jpg')
# Check if the image was read successfully (if the path is wrong or the image does not exist, img will return None)
if img is None:
print("Image reading failed! Please check the path.")
else:
print("Image read successfully!")
IV. Displaying an Image¶
After reading an image, you need to use the cv2.imshow() function to display it in a window.
1. Function Introduction¶
The syntax of cv2.imshow() is:
cv2.imshow(window_name, image)
window_name: The name of the window (a string, e.g., “Image”).image: The image object to display (the variable returned bycv2.imread()).
When displaying an image, the window will remain open until the user presses any key to close it. To prevent the window from “flashing by,” you need to use cv2.waitKey():
- cv2.waitKey(0): Waits for keyboard input, with the parameter being the number of milliseconds (0 means infinite wait, and any key press closes the window).
- cv2.destroyAllWindows(): Closes all windows created by OpenCV and releases memory.
2. Code Example: Displaying an Image¶
Combine the reading and display steps:
import cv2
# Step 1: Read the image
img = cv2.imread('test.jpg') # Replace with your image path
# Step 2: Check if the image is valid
if img is None:
print("Image reading failed! Please check the path.")
else:
# Step 3: Display the image
cv2.imshow('Original Image', img)
# Step 4: Wait for a key press (any key closes the window)
cv2.waitKey(0)
# Step 5: Close all windows
cv2.destroyAllWindows()
V. Complete Example and Common Issues¶
1. Complete Code (Reading + Displaying a Color Image)¶
import cv2
# Read the image (in color mode)
img = cv2.imread('test.jpg') # Replace with your image path
# Check if the image was read successfully
if img is None:
print("Error: Image reading failed! Please confirm the path is correct.")
else:
# Display the image
cv2.imshow('Color Image', img)
# Wait for a key press (any key closes the window)
cv2.waitKey(0)
# Close all windows
cv2.destroyAllWindows()
2. Common Issues and Solutions¶
- Issue 1: Image is black or color is abnormal
Cause: OpenCV reads images in BGR order (not RGB by default). Directly displaying a color image may cause “red becomes blue” etc.
Solution: To get an RGB image, usecv2.cvtColor()for conversion:
import cv2
img = cv2.imread('test.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
cv2.imshow('RGB Image', img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
- Issue 2: Image cannot be displayed due to incorrect path
Cause:cv2.imread()returnsNonebecause the path is wrong.
Solution: - Use an absolute path (e.g.,
C:/Users/YourName/Pictures/test.jpg) to avoid relative path confusion. - Confirm the image format is supported (e.g., JPG, PNG) and the image is not corrupted.
VI. Summary¶
This article introduces the basic operations of Python OpenCV through three core steps: “Install OpenCV → Read Image → Display Image.” Key points are:
1. cv2.imread() is used to read images, note the path and color channel order.
2. cv2.imshow() is used to display images, and must be paired with cv2.waitKey() and cv2.destroyAllWindows() to avoid window abnormalities.
3. If the image fails to display, first check if the path is correct.
Practice is the best way to learn! Try replacing the image path in the code to read and display your own images.