introduction
OpenCV is an open source computer vision library that is widely used in the fields of image processing and computer vision. Python provides binding to OpenCV through the cv2 module, allowing developers to use Python to perform image processing and computer vision tasks conveniently. This article will introduce in detail how to use OpenCV binding library in Python and provide rich sample code.
1. Install OpenCV
First, you need to install the OpenCV library:
pip install opencv-python
If additional functions are needed (such as patented algorithms such as SIFT, SURF, etc.), you can install:
pip install opencv-contrib-python
2. Basic image operation
1. Read and display images
import cv2 # Read the imageimg = ('') # Default BGR format # Show image('Image', img) # Wait for the button and close the window(0) ()
2. Save the image
('', img) # Save asJPEGFormat
3. Obtain image information
print(f"Image shape: {}") # (height, width, number of channels)print(f"Image size: {} byte") print(f"Image data type: {}") # Usuallyuint8
3. Basic image processing
1. Color space conversion
# BGR to grayscalegray = (img, cv2.COLOR_BGR2GRAY) # BGR to RGBrgb = (img, cv2.COLOR_BGR2RGB) # Show results('Gray', gray) ('RGB', rgb) (0)
2. Image zoom
# Scale to the specified sizeresized = (img, (300, 200)) # (width, height) # Scalescale_percent = 50 # Scale to 50%width = int([1] * scale_percent / 100) height = int([0] * scale_percent / 100) resized = (img, (width, height)) ('Resized', resized) (0)
3. Image Cropping
# Crop the image (y1:y2, x1:x2)cropped = img[100:400, 200:500] ('Cropped', cropped) (0)
4. Image rotation
# Get Image Center(h, w) = [:2] center = (w // 2, h // 2) # Rotation matrixM = cv2.getRotationMatrix2D(center, 45, 1.0) # Rotate 45 degrees, zoom 1.0 # Apply rotationrotated = (img, M, (w, h)) ('Rotated', rotated) (0)
IV. Image filtering
1. Mean fuzzy
blurred = (img, (5, 5)) #5x5 core size('Blurred', blurred) (0)
2. Gaussian blur
gaussian = (img, (5, 5), 0) # kernel size 5x5, standard deviation 0('Gaussian', gaussian) (0)
3. Median Fuzzy
median = (img, 5) # core size 5('Median', median) (0)
4. Bilateral filtering
bilateral = (img, 9, 75, 75) # kernel size 9, color and space sigma('Bilateral', bilateral) (0)
5. Edge detection
1. Canny edge detection
gray = (img, cv2.COLOR_BGR2GRAY) edges = (gray, 100, 200) # Thresholds 100 and 200 ('Edges', edges) (0)
2. Sobel operator
grad_x = (gray, cv2.CV_64F, 1, 0, ksize=3) # x directiongrad_y = (gray, cv2.CV_64F, 0, 1, ksize=3) # y direction # Merge gradientsabs_grad_x = (grad_x) abs_grad_y = (grad_y) grad = (abs_grad_x, 0.5, abs_grad_y, 0.5, 0) ('Sobel', grad) (0)
6. Morphological operation
1. Expansion and corrosion
# Binary images_, binary = (gray, 127, 255, cv2.THRESH_BINARY) # Define the kernelkernel = (cv2.MORPH_RECT, (5, 5)) # Expansiondilated = (binary, kernel, iterations=1) # Corrosioneroded = (binary, kernel, iterations=1) ('Dilated', dilated) ('Eroded', eroded) (0)
2. Open and closed operations
# Start the operation (corrosion first and then expansion)opening = (binary, cv2.MORPH_OPEN, kernel) # Closed operation (expand first and then corrosion)closing = (binary, cv2.MORPH_CLOSE, kernel) ('Opening', opening) ('Closing', closing) (0)
7. Feature detection and matching
1. Harris corner detection
gray = (img, cv2.COLOR_BGR2GRAY) # Harris Corner Detectioncorners = (gray, 2, 3, 0.04) # Results visualizationimg_corners = () img_corners[corners > 0.01 * ()] = [0, 0, 255] ('Harris Corners', img_corners) (0)
2. SIFT feature detection
# Make sure you have opencv-contrib-python installedsift = cv2.SIFT_create() # Detect key points and descriptorskeypoints, descriptors = (gray, None) # Draw key pointsimg_sift = (img, keypoints, None, color=(0, 255, 0)) ('SIFT Keypoints', img_sift) (0)
3. Feature Matching
# Read the second imageimg2 = ('') gray2 = (img2, cv2.COLOR_BGR2GRAY) # Detect key points and descriptorskeypoints2, descriptors2 = (gray2, None) # Use FLANN matcherFLANN_INDEX_KDTREE = 1 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) flann = (index_params, search_params) matches = (descriptors, descriptors2, k=2) # Application ratio testgood = [] for m, n in matches: if < 0.7 * : (m) # Draw the matching resultimg_matches = (img, keypoints, img2, keypoints2, good, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) ('Feature Matches', img_matches) (0)
8. Video processing
1. Read and display video
cap = ('video.mp4') # or use 0 to read the camera while (): ret, frame = () if not ret: break ('Video', frame) if (25) & 0xFF == ord('q'): break () ()
2. Video writing
cap = (0) # Read the camerafourcc = cv2.VideoWriter_fourcc(*'XVID') out = ('', fourcc, 20.0, (640, 480)) while (): ret, frame = () if not ret: break # Process frames (for example, convert to grayscale) gray = (frame, cv2.COLOR_BGR2GRAY) ((gray, cv2.COLOR_GRAY2BGR)) # Need to convert back to BGR ('Video', frame) if (1) & 0xFF == ord('q'): break () () ()
9. Image segmentation
1. Threshold segmentation
gray = (img, cv2.COLOR_BGR2GRAY) # Fixed threshold_, thresh = (gray, 127, 255, cv2.THRESH_BINARY) # Adaptive Thresholdthresh_adapt = (gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) ('Threshold', thresh) ('Adaptive Threshold', thresh_adapt) (0)
2. Contour detection
# Binary images_, binary = (gray, 127, 255, cv2.THRESH_BINARY) # Find out the outlinecontours, _ = (binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Draw outlinesimg_contours = () (img_contours, contours, -1, (0, 255, 0), 2) ('Contours', img_contours) (0)
10. Advanced example: Face detection
# Load the pretrained face detection modelface_cascade = ( + 'haarcascade_frontalface_default.xml') # Read the imageimg = ('') gray = (img, cv2.COLOR_BGR2GRAY) # Detect facesfaces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # Draw a rectangle boxfor (x, y, w, h) in faces: (img, (x, y), (x+w, y+h), (255, 0, 0), 2) ('Face Detection', img) (0)
11. Performance optimization skills
Using NumPy operations instead of loops:
# Not recommendedfor i in range(rows): for j in range(cols): img[i,j] = [255, 255, 255] if some_condition else [0, 0, 0] # recommendcondition = some_condition_array img = (condition[..., None], [255, 255, 255], [0, 0, 0])
Use inRange for color segmentation:
# Create a masklower = ([0, 100, 100]) upper = ([10, 255, 255]) mask = (hsv_img, lower, upper)
Accelerated calculation using integral image:
# Calculate integral imagesintegral = (gray) # Quickly calculate rectangular areas andsum_rect = integral[x2,y2] - integral[x1-1,y2] - integral[x2,y1-1] + integral[x1-1,y1-1]
The above is a detailed explanation of how to use the OpenCV binding library in Python. For more information about the use of the OpenCV binding library in Python, please follow my other related articles!