3 hours proficient in common operations of opencv images

3 hours proficient in common operations of opencv(2) pictures

Reference video resources: 3 hours proficient in Opencv-Python

Grayscale image

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', imgGray)
cv2.waitKey(0)

The default image channel in opencv is BGR, so it is COLOR_BGR2GRAY

Gaussian filter

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
cv2.imshow('blur', imgBlur)
cv2.waitKey(0)

Gaussian filtering is suitable for eliminating Gaussian noise and is widely used in the noise reduction process of image processing

Usually the first three parameters are used:

  • src: input image
  • ksize Gaussian kernel size, must be odd
  • sigmaX Gaussian kernel standard deviation in the X direction

For the specific Gaussian kernel calculation process, refer to this blog post
A brief description of Gaussian filtering: Use a template (or convolution, mask) to scan each pixel in the image, and use the weighted average gray value of the pixels in the neighborhood determined by the template to replace the value of the pixel in the center of the template.

edge detection

import cv2

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
imgCanny = cv2.Canny(img, 150, 200)
imgCanny2 = cv2.Canny(img, 35, 50)
cv2.imshow('img', img)
cv2.imshow('Canny', imgCanny)
cv2.imshow('Canny2', imgCanny2)
cv2.waitKey(0)


Usually the first three parameters are used:

  • image: input image
  • threshold1 represents the first threshold during processing
  • threshold2 represents the second threshold during processing.
    • Pixels below the threshold 1 are considered not to be edges;
    • Pixels above threshold 2 are considered edges;
    • Pixels between threshold 1 and threshold 2 are considered to be edges if they are adjacent to the edge pixels obtained in step 2, otherwise they are not considered to be edges.

Canny edge detection details reference blog post

Smaller values ​​of threshold1 and threshold2 will capture more edge information.

expansion function

import cv2
import numpy as np

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
imgCanny = cv2.Canny(img, 150, 200)
kernel = np.ones((5, 5), np.uint8)
imgDilation = cv2.dilate(imgCanny, kernel, iterations=1)  # expand

cv2.imshow('img', img)
cv2.imshow('Canny', imgCanny)
cv2.imshow('Dilation', imgDilation)

cv2.waitKey(0)


Commonly used parameters:

  • src: input image
  • kernel The kernel for the operation, the default is a 3×3 matrix
  • iterations Corrosion times, the default is 1

The principle of dilation is to take a local maximum in a small area of ​​the original image
Use the kernel to scan each pixel of the image; use the kernel and the binary image covered by the "AND" operation; if both are 0, the pixel of the resulting image is 0; otherwise it is 1. Make the binary image a circle
Corrosion and Dilation Reference Blog Post

corrosion function

import cv2
import numpy as np

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
imgCanny = cv2.Canny(img, 150, 200)
kernel = np.ones((5, 5), np.uint8)
imgDilation = cv2.dilate(imgCanny, kernel, iterations=1)  # expand
imgEroded = cv2.erode(imgDilation, kernel, iterations=1)  # corrosion

cv2.imshow('img', img)
cv2.imshow('Canny', imgCanny)
cv2.imshow('Eroded', imgEroded )

cv2.waitKey(0)


Commonly used parameters:

  • src: input image
  • kernel The kernel for the operation, the default is a 3×3 matrix
  • iterations Corrosion times, the default is 1

The principle of erosion is to take a local minimum in a small area of ​​the original image
Use the kernel to scan each pixel of the image; use the kernel and the binary image covered by the "AND" operation; if they are all 1, the pixel of the resulting image is 1; otherwise, it is 0. Reduce the binary image by one circle
Corrosion and Dilation Reference Blog Post

overall code

import cv2
import numpy as np

# The default image channel in opencv is BGR
img = cv2.imread('Resources/lena.png')
kernel = np.ones((5, 5), np.uint8)

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 0)
imgCanny = cv2.Canny(img, 150, 200)
imgDilation = cv2.dilate(imgCanny, kernel, iterations=1)  # expand
imgEroded = cv2.erode(imgDilation, kernel, iterations=1)  # corrosion
# cv2.imshow('gray', imgGray)
# cv2.imshow('blur', imgBlur)
cv2.imshow('Canny Image', imgCanny)
cv2.imshow('Dilation Image', imgDilation)
cv2.imshow('Erode Image', imgEroded)

cv2.waitKey(0)

Tags: OpenCV Computer Vision Deep Learning image processing

Posted by wakmp on Sun, 22 Jan 2023 02:31:27 +0300