本文为11月3日 OpenCV 实战基础学习笔记——图像基本处理,分为七个章节:
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
replicate = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_WRAP)
constant = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv.BORDER_CONSTANT, value=0)
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
plt.show()
cat.shape
>>> (414, 500, 3)
dog.shape
>>> (429, 499, 3)
dog = cv.resize(dog, (500, 414))
dog.shape
>>> (414, 500, 3)
res = cv.addWeighted(cat, 0.4, dog, 0.6, 0) # \alpha=0.4, \beta=0.6
plt.imshow(res)
ret, dst = cv2.threshold(src, thresh, maxval, type)
ret, thresh1 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY)
ret, thresh2 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY_INV)
ret, thresh3 = cv.threshold(img_gray, 127, 255, cv.THRESH_TRUNC)
ret, thresh4 = cv.threshold(img_gray, 127, 255, cv.THRESH_TOZERO)
ret, thresh5 = cv.threshold(img_gray, 127, 255, cv.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], cmap='gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
img = cv.imread('lenaNoise.png')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(img)
平均卷积操作。
# 均值滤波:平均卷积操作
blur = cv.blur(img, (3, 3))
cv_show('blur', blur)
基本和均值一样,可以选择归一化,容易越界。
box = cv.boxFilter(img, -1, (3, 3), normalize=True)
cv_show('box', box)
卷积核中的数值满足高斯分布,相当于更重视中间的。
gaussian = cv.GaussianBlur(img, (5, 5), 1)
cv_show('gaussian', gaussian)
相当于用中值代替。
medium = cv.medianBlur(img, 5)
cv_show('medium', medium)
img = cv.imread('dige.png')
cv_show('img', img)
kernel = np.ones((5, 5), np.uint8)
erosion = cv.erode(img, kernel, iterations=1)
cv_show('erosion', erosion)
kernel = np.ones((3, 3), np.uint8)
dige_dilate = cv.dilate(erosion, kernel, iterations=1)
cv_show('dilate', dige_dilate)
kernel = np.ones((5, 5), np.uint8)
opening = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)
cv_show("opening", opening)
closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)
cv_show("closing", closing)
梯度 = 膨胀 - 腐蚀。
pie = cv.imread('pie.png')
kernel = np.ones((7, 7), np.uint8)
dilate = cv.dilate(pie, kernel, iterations=5)
erosion = cv.erode(pie, kernel, iterations=5)
res = np.hstack((dilate, erosion))
cv_show('res', res)
gradient = cv.morphologyEx(pie, cv.MORPH_GRADIENT, kernel)
cv_show('gradient', gradient)
img = cv.imread('dige.png')
tophat = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel)
cv_show('tophat', tophat)
blackhat = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)
cv_show('blackhat', blackhat)
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
pie_sobel_x = cv.Sobel(pie, cv.CV_64F, 1, 0, ksize=3) # 1, 0: 只算水平方向
cv_show('pie_sobel_x', pie_sobel_x)
# 白到黑是正数,黑到白是负数,所有的负数会被截断成 0,所以要取绝对值
pie_sobel_x = cv.convertScaleAbs(pie_sobel_x)
cv_show('pie_sobel_x', pie_sobel_x)
# 分别计算 G_x 和 G_y,再求和
pie_sobel_y = cv.Sobel(pie, cv.CV_64F, 0, 1, ksize=3) # 0, 1: 只算竖直方向
pie_sobel_y = cv.convertScaleAbs(pie_sobel_y)
cv_show('pie_sobel_y', pie_sobel_y)
pie_sobel_xy = cv.addWeighted(pie_sobel_x, 0.5, pie_sobel_y, 0.5, 0)
cv_show('pie_sobel_xy', pie_sobel_xy)
# 不建议直接计算,会有重影
pie_sobel_xy_2 = cv.Sobel(pie, cv.CV_64F, 1, 1, ksize=3)
pie_sobel_xy_2 = cv.convertScaleAbs(pie_sobel_xy_2)
cv_show('pie_sobel_xy_2', pie_sobel_xy_2)
lena = cv.imread('lena.jpg', cv.IMREAD_GRAYSCALE)
cv_show('lena', lena)
lena_sobel_x = cv.Sobel(lena, cv.CV_64F, 1, 0, ksize=3)
lena_sobel_x = cv.convertScaleAbs(lena_sobel_x)
lena_sobel_y = cv.Sobel(lena, cv.CV_64F, 0, 1, ksize=3)
lena_sobel_y = cv.convertScaleAbs(lena_sobel_y)
lena_sobel_xy = cv.addWeighted(lena_sobel_x, 0.5, lena_sobel_y, 0.5, 0)
cv_show('lena_sobel_xy', lena_sobel_xy)
# 不同算子的差异
lena_scharr_x = cv.Scharr(lena, cv.CV_64F, 1, 0)
lena_scharr_x = cv.convertScaleAbs(lena_scharr_x)
lena_scharr_y = cv.Scharr(lena, cv.CV_64F, 0, 1)
lena_scharr_y = cv.convertScaleAbs(lena_scharr_y)
lena_scharr_xy = cv.addWeighted(lena_scharr_x, 0.5, lena_scharr_y, 0.5, 0)
lena_lap = cv.Laplacian(lena, cv.CV_64F)
lena_lap = cv.convertScaleAbs(lena_lap)
res = np.hstack((lena_scharr_xy, lena_lap))
cv_show('res', res)
步骤:
v1 = cv.Canny(lena, 80, 150) # min value 和 max value
v2 = cv.Canny(lena, 50, 100)
res = np.hstack((v1, v2))
cv_show('res', res)
car = cv.imread('car.png', cv.IMREAD_GRAYSCALE)
v1 = cv.Canny(car, 120, 250) # min value 和 max value
v2 = cv.Canny(car, 50, 100)
res = np.hstack((v1, v2))
cv_show('res', res)