Article Directory
1. HSV
Compared with the RGB space, the HSV space can express the lightness, hue, and vividness of the color very intuitively, which is convenient for the comparison between colors.
H - Hue (dominant wavelength).
S - Saturation (purity/shade of the color).
V value (strength).
Display the HSV image:
import cv2 #opencv读取的格式是BGR
img=cv2.imread('D:\cat.jpg')
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv", hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
2. Image Threshold
ret, dst = cv2.threshold(src, thresh, maxval, type)
Parameter description:
src : Input image, only single-channel image can be input, usually a grayscale image
dst : Output image
thresh : You want to set the threshold
maxval : When the pixel value exceeds the threshold (or is less than the threshold, it depends on the type) ), the assigned value is generally 255
type : the type of binarization operation, including the following 5 types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV
cv2.THRESH_BINARY : Take maxval (maximum value) for the part exceeding the threshold, otherwise take 0
cv2.THRESH_BINARY_INV : It is the inversion of THRESH_BINARY, take 0 for the part exceeding the threshold), otherwise take the maximum value
cv2.THRESH_TRUNC : Set the part greater than the threshold as the threshold, otherwise Unchanged
cv2.THRESH_TOZERO : The part greater than the threshold does not change, otherwise it is set to 0
cv2.THRESH_TOZERO_INV : It is the inversion of THRESH_TOZERO, the part less than the threshold does not change, otherwise it is set to 0
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
img=cv2.imread('D:\cat.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#五种不同的阈值操作
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
Effect:
3. Image smoothing
Mean filtering : simple average convolution operation, (3, 3) is the size of the convolution kernel
blur = cv2.blur(img, (3, 3))
Box filter : basically the same as the mean value. When normalize=True is normalized, the effect is the same as the mean value filter. When normalize=False crosses the boundary, the maximum value is 255
box = cv2.boxFilter(img,-1,(3,3), normalize=True)
Gaussian filtering : The value in the convolution kernel of Gaussian blur satisfies the Gaussian distribution, which is equivalent to paying more attention to the middle.
aussian = cv2.GaussianBlur(img, (5, 5), 1)
Median filtering : It is equivalent to replacing with the median value. The basic idea is to replace the gray value of the pixel with the median value of the gray value of the neighborhood of the pixel.
median = cv2.medianBlur(img, 5)
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
#原图
img = cv2.imread('D:\zaoying.jpg')
#均值滤波
blur = cv2.blur(img, (3, 3))#(3,3)为卷积核的大小
#方框滤波
box = cv2.boxFilter(img,-1,(3,3), normalize=True)#normalize=True)即归一化后效果和均值滤波一样
#高斯滤波
aussian = cv2.GaussianBlur(img, (5, 5), 1)
#中值滤波
median = cv2.medianBlur(img, 5)
#图像拼接
res = np.hstack((aussian,median))
#展示高斯滤波和中值滤波
cv2.imshow('aussian vs median', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
Contrast effect:
4. Morphology-corrosion operation
The image for corrosion operation is generally binary. The principle of corrosion operation is to use a kernel to convolve (scan) the image. A point in the kernel is defined as an anchor point, and then extract the pixel minimum value of the kernel coverage area ( black) to replace the pixel value at the anchor point, so the black becomes more after scanning.
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
img = cv2.imread('D:\Benwei.png')
cv2.imshow('img', img)
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel : the size of the convolution kernel
iterations : the number of iterations
original image:
effect:
5. Morphology-expansion operation
Like corrosion, expansion is also relative to white, which means that the pixels with high value (white) become more (expanded). Contrary to corrosion, expansion is to extract the maximum value (white) of the kernel area to replace the pixel at the anchor point value.
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
dige_dilate = cv2.dilate(img,kernel,iterations = 1)
cv2.imshow('dige_dilate', dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
6. Opening operation and closing operation
The opening operation and the closing operation are actually a summary of the previous corrosion and expansion, but the order of execution is different. The opening operation first corrodes and then expands, and the closing operation first expands and then corrodes. The open operation is cv2.MORPH_OPEN, and the closed operation is cv2.MORPH_CLOSE.
Open operation :
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('opening', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
Closing operation :
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
7. Gradient operation
The gradient operation is actually the result of image expansion minus image erosion, and finally we get a graph similar to the outline of the image.
import cv2 #opencv读取的格式是BGR
import numpy as np
pie = cv2.imread('D:\pie.png')
kernel = np.ones((7,7),np.uint8)
dilate = cv2.dilate(pie,kernel,iterations = 5)
erosion = cv2.erode(pie,kernel,iterations = 5)
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
8. Top hat and black hat
The top hat is equivalent to subtracting the result of the opening operation from the original input, and the black hat is the result of subtracting the original input from the closing operation.
hat :
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect:
black hat :
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat )
cv2.waitKey(0)
cv2.destroyAllWindows()
Effect (basically nothing to see):