目录
显示图片
img2 = cv2.imread('cat.jpg', cv2.IMREAD_COLOR)
img3 = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE) # 灰度
cv2.show(img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
显示视频
vc = cv2.VideoCapture('video.mp4')
if vc.isOpened(): # 检查是否打开正确
open, frame = vc.read() # open是bool值, frame是一帧
print(open)
else:
open = False
while open:
ret, frame = vc.read()
if frame is None:
break
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度处理
cv2.imshow('result', gray)
if cv2.waitKey(100) & 0xFF == 27: # 退出键退出
break
vc.release()
cv2.destroyAllWindows()
截取部分图像数据
img = cv2.imread('cat.jpg')
cat = img[0:100, 0:200]
cv2.imshow('cat', cat)
cv2.waitKey(500)
颜色通道提取
img = cv2.imread('cat.jpg')
b, g, r = cv2.split(img)
print(b, g, r)
# bgr合成
img = cv2.merge((b, g, r))
# 只保留r
cur_img = img.copy()
cur_img[:, :, 0] = 0
cur_img[:, :, 1] = 0
cv_show('cat', cur_img)
边界填充
img = cv2.imread('cat.jpg')
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_CONSTANT, value = 5)
# 放在图表里好比较
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('REFLECT101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
plt.show()
改变大小
res = cv2.resize(img, (0, 0), fx=2, fy=1) # 长度扩大三倍
res2 = cv2.resize(img, (500, 500)) # 改变为括号的大小
plt.imshow(res2)
plt.show()
``
## 数值计算
```python
cat = cv2.imread("cat.jpg")
dog = cv2.imread("dog.jpg")
dog = cv2.resize(dog, (500, 414)) # 需要数据大小相同才能相加
# 两种不同的加法
cat_dog = cat + dog
cat_dog2 = cv2.add(cat, dog)
print(cat_dog[:5, :, 0])
print(cat_dog2[:5, :, 0])
图像融合
注意两张图片要大小一样
res = cv2.addWeighted(cat, 0.3, dog, 0.7, 0) # z = x*a + y*b + c
cv_show("result", res)
腐蚀操作
img = cv2.imread('dige.png')
kernel = np.ones((3, 3), np.uint8) # 可以理解为腐蚀框的大小 越大腐蚀越强
erosion = cv2.erode(img, kernel, iterations=1) # iterations表示腐蚀次数
res = np.hstack((erosion, img))
cv_show('res', res)
膨胀操作
dilation = cv2.dilate(img, kernel, iterations=1)
res = np.hstack((dilation, img))
cv_show('res', res)
开运算与闭运算
开:先腐蚀再膨胀; 闭:先膨胀再腐蚀
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
res = np.hstack((opening, closing))
cv_show('res', res)
梯度运算
梯度=膨胀-腐蚀
erosion = cv2.erode(img, kernel, iterations=1)
dilation = cv2.dilate(img, kernel, iterations=1)
res = dilation - erosion
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=2)
res = np.hstack((res, gradient))
cv_show('res', res)
礼帽和黑帽
礼帽=原始输入-开运算结果; 黑帽=闭运算-原始输入
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
res = np.hstack((tophat, blackhat))
cv_show('res', res)
图像梯度-sobel算子
矩阵与图片相乘,使图片上下或者左右进行运算 用于得到边界
dst = cv2.sobel(src, ddepth, dx, dy, ksize)
分别对横向和纵行处理图像叠加比同时进行横向和纵向处理的效果要好
img = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx) # 补全另一半
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
plt.subplot(221)
plt.imshow(cv2.cvtColor(sobelx, code=cv2.COLOR_BGR2RGB))
plt.subplot(222)
plt.imshow(cv2.cvtColor(sobely, code=cv2.COLOR_BGR2RGB))
plt.subplot(223)
plt.imshow(cv2.cvtColor(sobelxy, code=cv2.COLOR_BGR2RGB))
plt.show()
Scharr、laplacian、sobel算子比较
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx) # 补全另一半
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharrx = cv2.convertScaleAbs(scharrx) # 补全另一半
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian) # 补全另一半
plt.subplot(221)
plt.imshow(sobelxy, 'gray')
plt.subplot(222)
plt.imshow(scharrxy, 'gray')
plt.subplot(223)
plt.imshow(laplacian, 'gray')
plt.show()
图像平滑处理
图像降噪
# 均值滤波 简单的平均卷积操作
blur = cv2.blur(img, (3, 3))
# 方框滤波 基本和均值一样,可以选择归一化,容易越界
box1 = cv2.boxFilter(img, -1, (3, 3))
box2 = cv2.boxFilter(img, -1, (3, 3), normalize=True)
# 高斯滤波 采用高斯分布,不以像素点位置为中心
gaussian = cv2.GaussianBlur(img, (5, 5), 1)
# 中值滤波 效果最好
median = cv2.medianBlur(img, 5)
# 画图对比一下
plt.subplot(231), plt.imshow(cv2.cvtColor(blur, code=cv2.COLOR_BGR2RGB))
plt.subplot(232), plt.imshow(cv2.cvtColor(box1, code=cv2.COLOR_BGR2RGB))
plt.subplot(233), plt.imshow(cv2.cvtColor(box2, cv2.COLOR_BGR2RGB))
plt.subplot(234), plt.imshow(cv2.cvtColor(gaussian, cv2.COLOR_BGR2RGB))
plt.subplot(235), plt.imshow(cv2.cvtColor(median, cv2.COLOR_BGR2RGB))
plt.show()
图像阈值
只能输入单通道图像
img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_OTSU)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_MASK)
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, thresh6 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh7 = cv2.threshold(img, 127, 255, cv2.THRESH_TRIANGLE)
ret, thresh8 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5, thresh6, thresh7, thresh8]
titiles = ["Original","Binary", "Otsu", "Mask", "Trunc", "Tozero", "BinaryINV", "Triangle", "TozeroINV"]
for i in range(9):
plt.subplot(3, 3, i+1)
plt.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
plt.title(titiles[i])
plt.show()