1. Image filling
First define the image display function:
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image read:
img_cat = cv2.imread('cat.jpg')
Define the size of the image padding:
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
Next, use the copy method, reflection method, outer packaging method, and constant method to fill.
Copy method:
# 复制法,复制最边缘像素
replicate = cv2.copyMakeBorder(img_cat, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
Reflection method:
# 反射法,对感兴趣的图像中的像素在两边进行复制
reflect = cv2.copyMakeBorder(img_cat, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT)
second reflection method
# 反射法,也就是以最边缘像素为周,对称
reflect101 = cv2.copyMakeBorder(img_cat, top_size, bottom_size, left_size, right_size,
borderType=cv2.BORDER_REFLECT_101)
Packaging method:
# 外包装法
warp = cv2.copyMakeBorder(img_cat, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP)
Constant method:
# 常量法,常数值填充,此处填充灰色
constant = cv2.copyMakeBorder(img_cat, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT,
value=0)
The output results are compared as follows:
2. Morphological operation
First read in the raw image:
pie = cv2.imread('pie.jfif')
Define the convolution kernel:
kernel = np.ones((10, 10), np.uint8)
Corrosion function:
dige_erosion = cv2.erode(pie, kernel=kernel, iterations=1)
The output is as follows:
Inflation function:
dige_dilate = cv2.dilate(pie, kernel=kernel, iterations=1)
The output is as follows:
Open operation: corrode first and then dilate
opening = cv2.morphologyEx(pie, cv2.MORPH_OPEN, kernel=kernel)
The output is as follows:
Closed operation: expand first and then corrode
closing = cv2.morphologyEx(pie, cv2.MORPH_CLOSE, kernel=kernel)
The output is as follows:
Gradient: Dilation-Erosion
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
The output is as follows:
Top hat: original input - open result
tophat = cv2.morphologyEx(pie, cv2.MORPH_TOPHAT, kernel)
The output is as follows:
Black Hat: Closed Operation - Raw Input
blackhat = cv2.morphologyEx(pie, cv2.MORPH_BLACKHAT, kernel)
The output is as follows:
3. Image smoothing
Pass in raw image:
Mean Filtering: A Simple Average Convolution Operation
blur = cv2.blur(cat, (3, 3))
Box filtering: basically the same as the mean value, you can choose normalization, normalize when normalize is true, the same as the mean value filter
box = cv2.boxFilter(cat, -1, (3, 3), normalize=True)
When normalize is False, normalization is not performed, and mean value processing is not performed, and 255 is taken after crossing the boundary
box = cv2.boxFilter(cat, -1, (3, 3), normalize=False)
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
gaussian = cv2.GaussianBlur(cat, (3, 3), 1)
Median filtering: equivalent to replacing with median
median = cv2.medianBlur(cat, 3)