Opencv Image Basic
OpenCV provides rich image processing and computer vision capabilities, including image reading, display, color space conversion, filtering, edge detection, contour detection, and more.
This chapter introduces the basic concepts and common features of OpenCV.
### Image Representation and Processing
OpenCV represents image data through (#). Each image is a multi-dimensional array where each element corresponds to a pixel in the image. The image dimensions and color mode can also be represented by the array shape.
**Basic Image Properties:**
* **Image Dimensions (Width, Height)**: Can be obtained via `img.shape`.
* **Color Channels**: Typically RGB (three channels), or grayscale (single channel).
* **Data Type**: Common types include `uint8` (0-255 range), or `float32` and others.
**Reading Images:**
import cv2 img = cv2.imread('image.jpg')
* `cv2.imread()` reads an image file and returns a NumPy array.
* Returns `None` if the image path is incorrect or the file does not exist.
**Displaying Images:**
# Display image cv2.imshow("Display Window", image)# Wait for key input cv2.waitKey(0)# Close all windows cv2.destroyAllWindows()
`cv2.imshow()` is used to display images, `cv2.waitKey(0)` waits for a key event, and `cv2.destroyAllWindows()` closes all windows.
Saving Images:
# Save image cv2.imwrite("output_image.jpg", image)
`cv2.imwrite()` saves the image to the specified path.
### Basic Image Operations
**1. Accessing and Modifying Pixel Values**
# Get pixel value (BGR format) pixel_value = image[100, 100] # Get pixel value at (100, 100)# Modify pixel value image[100, 100] = [255, 255, 255] # Set pixel at (100, 100) to white
**2. Image ROI (Region of Interest)**
# Get ROI roi = image[50:150, 50:150] # Get region from (50,50) to (150,150)# Modify ROI image[50:150, 50:150] = [0, 255, 0] # Set ROI region to green
**3. Image Channel Splitting and Merging**
# Split channels b, g, r = cv2.split(image)# Merge channels merged_image = cv2.merge([b, g, r])
**4. Image Scaling, Rotation, Translation, and Flipping**
# Scaling resized_image = cv2.resize(image, (new_width, new_height))# Rotation rotation_matrix = cv2.getRotationMatrix2D((center_x, center_y), angle, scale) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))# Translation translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]]) # tx, ty are translation distances translated_image = cv2.warpAffine(image, translation_matrix, (width, height))# Flipping flipped_image = cv2.flip(image, flip_code) # flip_code: 0 (vertical flip), 1 (horizontal flip), -1 (both directions)
### Image Arithmetic Operations
**1. Image Addition**
result = cv2.add(image1, image2)
**2. Image Subtraction**
result = cv2.subtract(image1, image2)
**3. Image Blending**
result = cv2.addWeighted(image1, alpha, image2, beta, gamma)
`alpha` and `beta` are weights, `gamma` is a scalar value.
### Image Thresholding
**1. Simple Thresholding**
ret, thresholded_image = cv2.threshold(image, thresh, maxval, cv2.THRESH_BINARY)
`thresh` is the threshold value, `maxval` is the maximum value.
**2. Adaptive Thresholding**
thresholded_image = cv2.adaptiveThreshold(image, maxval, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block_size, C)
**3. Otsu's Binarization**
ret, thresholded_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
### Image Smoothing
**1. Mean Filtering**
blurred_image = cv2.blur(image, (kernel_size, kernel_size))
**2. Gaussian Filtering**
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigmaX)
**3. Median Filtering**
blurred_image = cv2.medianBlur(image, kernel_size)
**4. Bilateral Filtering**
blurred_image = cv2.bilateralFilter(image, d, sigmaColor, sigmaSpace)
### Image Color Space and Conversion
OpenCV supports conversion between multiple color spaces, such as from RGB to grayscale, HSV, etc.
Color space conversion is very important in many image processing tasks.
Converting from RGB to Grayscale:
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Converting from BGR to HSV:
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
Converting from RGB to YUV:
yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
Color space conversion is a fundamental operation in image processing, used for different algorithms and visual effects.
### Image Resizing and Cropping
OpenCV provides multiple ways to resize images and crop image regions.
Resizing images:
resized_img = cv2.resize(img, (width, height))
Cropping images:
cropped_img = img[y1:y2, x1:x2]
Specific regions of the image can be cropped using slicing operations.
### Image Smoothing and Denoising (Blurring)
Image smoothing can reduce noise. Common blurring algorithms include Gaussian blur, mean blur, etc.
Gaussian blur:
blurred_img = cv2.GaussianBlur(img, (5, 5), 0)
Mean blur:
blurred_img = cv2.blur(img, (5, 5))
Median blur:
blurred_img = cv2.medianBlur(img, 5)
### Image Edge Detection
Edge detection is a commonly used image processing technique for detecting edges in images. The most commonly used method is Canny edge detection.
**Canny Edge Detection:**
edges = cv2.Canny(img, 100, 200)
The Canny algorithm finds edges by calculating gradients in the image, returning a binary image where edges are white and other regions are black.
**Sobel Operator:**
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5) sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
**Laplacian Operator:**
laplacian = cv2.Laplacian(image, cv2.CV_64F)
### Morphological Operations
Morphological operations are commonly used for binary image processing. Common operations include erosion, dilation, opening, and closing.
**Erosion:** Shrinks white regions in the image.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) eroded_img = cv2.erode(img, kernel, iterations=1)
**Dilation:** Expands white regions in the image.
dilated_img = cv2.dilate(img, kernel, iterations=1)
**Opening and Closing:**
* Opening (erosion followed by dilation): Used to remove small objects.
* Closing (dilation followed by erosion): Used to fill small holes in the image.
opening_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) closing_img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
### Image Contour Detection
OpenCV provides powerful contour detection capabilities that can be used for object recognition, image segmentation, and other applications.
Detecting contours:
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, threshold_img = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(threshold_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Drawing contours:
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
### Video Processing
OpenCV also supports video processing, allowing you to read video files, capture video streams, and perform real-time processing.
Reading video:
## Example
cap = cv2.VideoCapture('video.mp4')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process each frame
cv2.imshow('Video', frame)
if cv2.waitKey(1)&0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
YouTip