杰瑞科技汇

Python OpenCV cv2怎么用?

Of course! Here is a comprehensive guide to using OpenCV (cv2) in Python, covering everything from installation to core functionalities and practical examples.

Python OpenCV cv2怎么用?-图1
(图片来源网络,侵删)

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It contains over 2500 optimized algorithms, which makes it incredibly popular for a wide range of applications, including:

  • Image and video processing
  • Object detection and recognition
  • Face detection
  • Motion tracking
  • Augmented reality
  • Medical image analysis

In Python, you interact with OpenCV using the cv2 module.


Installation

First, you need to install the OpenCV library. The easiest way is using pip.

# Install the main OpenCV package
pip install opencv-python
# (Optional but recommended) Install additional libraries for optimized operations
pip install opencv-contrib-python
# (Optional) Install libraries for handling image formats like PNG with transparency
pip install opencv-python-headless
  • opencv-python: The core package.
  • opencv-contrib-python: Includes the core package plus extra, experimental modules (like SIFT, SURF, etc.). It's generally a good idea to install this one.
  • opencv-python-headless: A version of OpenCV that doesn't require a GUI backend. Useful for server environments or when you only need to do image processing without displaying windows.

Reading, Displaying, and Saving Images

These are the most fundamental operations.

Python OpenCV cv2怎么用?-图2
(图片来源网络,侵删)

cv2.imread(): Read an Image

This function reads an image from a file and returns it as a NumPy array.

import cv2
# The second argument specifies the color mode:
# cv2.IMREAD_COLOR (1): Loads a color image. (Default)
# cv2.IMREAD_GRAYSCALE (0): Loads an image in grayscale.
# cv2.IMREAD_UNCHANGED (-1): Loads the image as-is, including the alpha channel (transparency).
image_color = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_COLOR)
image_gray = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)
# Always check if the image was loaded successfully!
if image_color is None:
    print("Error: Could not read the image.")
else:
    print(f"Color image shape: {image_color.shape}") # Shape is (height, width, channels)

cv2.imshow(): Display an Image

This function displays an image in a window. The window will close automatically if you don't add a waitKey() call.

cv2.imshow('Color Image', image_color)
cv2.imshow('Grayscale Image', image_gray)
# The waitKey() function is crucial.
# It waits for a key press for a specified number of milliseconds.
# 0 means it will wait indefinitely until a key is pressed.
cv2.waitKey(0) 
# After you're done, destroy all windows to free up resources.
cv2.destroyAllWindows()

cv2.imwrite(): Save an Image

This function saves an image to a file.

# Save the grayscale image
success = cv2.imwrite('grayscale_image.jpg', image_gray)
if success:
    print("Image saved successfully.")
else:
    print("Error: Could not save the image.")

Basic Image Operations

OpenCV images are NumPy arrays. This means you can use NumPy for powerful operations.

Python OpenCV cv2怎么用?-图3
(图片来源网络,侵删)

Accessing and Modifying Pixel Values

You can access a pixel by its row and column coordinates. Remember, the order is [row, column] (which is [y, x]).

# Get the pixel value at (100, 50) in the color image
pixel_bgr = image_color[100, 50]
print(f"BGR value at (100, 50): {pixel_bgr}") # e.g., [B, G, R]
# Modify the pixel value
image_color[100, 50] = [255, 255, 255] # Set it to white

Resizing an Image

Use cv2.resize() to change the dimensions of an image.

# Resize to a specific size (width, height)
resized_image = cv2.resize(image_color, (500, 300))
# Resize by a scale factor
# fx and fy are scale factors for width and height, respectively
scaled_image = cv2.resize(image_color, None, fx=0.5, fy=0.5)
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Cropping an Image

Cropping is just slicing the NumPy array.

# Crop the image: [y_start:y_end, x_start:x_end]
cropped_image = image_color[100:400, 200:500]
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing on Images

You can draw shapes, text, and other objects directly on an image.

# Create a blank black image
blank_image = np.zeros((500, 500, 3), dtype=np.uint8)
# 1. Draw a line
# cv2.line(image, start_point, end_point, color, thickness)
cv2.line(blank_image, (0, 0), (499, 499), (0, 255, 0), 5) # Green line
# 2. Draw a rectangle
# cv2.rectangle(image, top_left_corner, bottom_right_corner, color, thickness)
# Use -1 for thickness to fill the rectangle
cv2.rectangle(blank_image, (50, 50), (200, 200), (0, 0, 255), -1) # Filled red rectangle
# 3. Draw a circle
# cv2.circle(image, center, radius, color, thickness)
cv2.circle(blank_image, (400, 100), 50, (255, 0, 0), 3) # Blue circle
# 4. Add text
# cv2.putText(image, text, bottom_left_corner, font, font_scale, color, thickness)
cv2.putText(blank_image, 'Hello OpenCV!', (150, 450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.imshow('Drawings', blank_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Working with Video

You can process video from a camera or a video file.

From a Webcam (Camera)

import cv2
# Create a VideoCapture object. 0 is usually the default webcam.
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Error: Could not open video stream.")
    exit()
while True:
    # Read a frame from the camera
    # ret is a boolean that is True if a frame was read successfully
    # frame is the image itself
    ret, frame = cap.read()
    if not ret:
        print("Error: Failed to capture frame.")
        break
    # Display the frame
    cv2.imshow('Webcam Feed', frame)
    # Wait for 'q' key to be pressed to break the loop
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# Release the capture and destroy all windows
cap.release()
cv2.destroyAllWindows()

From a Video File

The process is very similar, just replace 0 with the path to your video file.

cap = cv2.VideoCapture('path/to/your/video.mp4')
# The rest of the code is the same as the webcam example...

A Complete Practical Example: Face Detection

This is one of the most common uses of OpenCV. We'll use the Haar Cascade classifier, which is a pre-trained model.

Step 1: Get the Haar Cascade XML file. Download haarcascade_frontalface_default.xml from the OpenCV GitHub repository and place it in the same directory as your script.

Step 2: Write the Python script.

import cv2
import numpy as np
# 1. Load the Haar Cascade classifier for face detection
# Make sure 'haarcascade_frontalface_default.xml' is in the same folder
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# 2. Read the image
image = cv2.imread('people.jpg')
if image is None:
    print("Error: Could not read the image.")
    exit()
# 3. Convert the image to grayscale (face detection works on grayscale)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 4. Detect faces
# The detectMultiScale function detects objects of different sizes in the input image.
# The detected objects are returned as a list of rectangles.
faces = face_cascade.detectMultiScale(
    gray_image,
    scaleFactor=1.1,  # How much the image size is reduced at each image scale
    minNeighbors=5,   # How many neighbors each candidate rectangle should have to retain it
    minSize=(30, 30)  # Minimum possible object size
)
# 5. Draw rectangles around the detected faces
print(f"Found {len(faces)} faces.")
for (x, y, w, h) in faces:
    # (x, y) is the top-left corner
    # (x+w, y+h) is the bottom-right corner
    # (0, 255, 0) is the color (Green)
    # 2 is the thickness of the rectangle
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 6. Display the result
cv2.imshow('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Key Takeaways and Best Practices

  1. OpenCV uses BGR, not RGB. When you read an image with cv2.imread(), the color channels are in Blue-Green-Red order. If you use a library like Matplotlib to display the image, it will look strange because Matplotlib expects RGB. You'll need to convert it: cv2.cvtColor(image, cv2.COLOR_BGR2RGB).
  2. Always check for None. Always check if cv2.imread() or cv2.VideoCapture() returned None before proceeding.
  3. Use waitKey(0) for static images. This pauses the script until you press a key. For video streams, use waitKey(1) to process the next frame quickly.
  4. Release resources. Always call cap.release() for video capture and cv2.destroyAllWindows() when you are done with windows to prevent resource leaks.
  5. Leverage NumPy. Remember that images are NumPy arrays. This unlocks a massive ecosystem for fast and efficient array manipulation.
分享:
扫描分享到社交APP
上一篇
下一篇