图像基本操作
一、边界填充
二、图像融合
三、图像阈值
四、图像平滑
五、形态学预算
1、腐蚀操作
2、膨胀操作
3、开闭运算操作
4、梯度运算
5、顶帽运算
6、黑帽运算
一、边界填充
- cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=?)
(1)cv2.BORDER_REPLICATE,复制法,赋值最边缘像素
(2)cv2.BORDER_REFLECT,反射法,对感兴趣图像中的像素在两边进行复制,如hgfedcba|abcdefgh|hgfedcba
(3)cv2.BORDER_REFLECT101,反射法,以最边缘级像素为轴对称,hgfedcb|abcdefgh|gfedcba
(4)cv2.BORDER_WRAP,外包装法,abcdefgh|abcdefgh|abcdefgh
(5)cv2.BORDER_CONSTANT,常量值填充。
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
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)
relect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT)
relect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT, value=0)
plt.subplot(231), plt.imshow(img, cmap='gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, cmap='gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(relect, cmap='gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(relect101, cmap='gray'), plt.title('REFLECT101')
plt.subplot(235), plt.imshow(wrap, cmap='gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, cmap='gray'), plt.title('CONSTANT')
plt.show()
二、图像融合
- cv2.addWeighted(img1, alpha, img2, 1-alpha, gamma)
import cv2
img1 = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('dog.jpg', cv2.IMREAD_GRAYSCALE)
print(img1.shape)
print(img2.shape)
img_dog = cv2.resize(img2, (256, 256))
res = cv2.addWeighted(img1, 0.4, img_dog, 0.6, 0)
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、图像阈值
ret, dst = cv2.threshold(src, thresh, maxval, type)
- src:输入图,只能输入单通道图像,通常来说为灰度图
- dst:输出图
- threshold:阈值
- maxval:当像素值超过了阈值(或者小于阈值,根据type来决定)所赋予的值
- type:二值化操作的类型包含以下五种:
- cv2.THRESH_BINARY 超过阈值的部分取maxval
- cv2.THRESH_BINARY_INV,cv2.THRESH_BINARY的反转
- cv2.THRESH_TRUNC,大于阈值部分设为maxval,否则不变
- cv2.THRESH_TOZERO,大于阈值的部分不变,否则为0
- cv2.THRESH_TOZERO_INV,cv2.THRESH_TOZERO的反转
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
ret, thresh1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img1, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Origin', 'binary', 'binary_inv', 'trunc', 'tozero', 'tozero_inv']
images = [img1, 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()
四、图像平滑
- 均值滤波,cv2.blur(img, (kernel_size, kernel_size))
- 方框滤波,cv2.boxFilter(img, -1, (kernel_size, kernel_size), normaliza=True),是否求平均。基本与均值滤波相同
- 高斯滤波:cv2.GaussianBlur(img, (kernel_size, kernel_size), sigma)
- 中值滤波:cv2.medianBlur(img, 5)
import cv2
import numpy as np
img = cv2.imread('lena.jpg')
cv2.waitKey(0)
cv2.destroyAllWindows()
blur = cv2.blur(img, (3, 3))
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
median = cv2.medianBlur(img, 5)
cv2.imshow('median', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
gaussian = cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('gaussian', gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
res = np.hstack((blur, median, gaussian))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
五、形态学运算
就上图而言,膨胀和腐蚀都是针对白色部分而不是黑色部分,膨胀就是白色区域扩大,腐蚀就是白色区域缩小
1、腐蚀操作,cv2.erode(src, kernel, interation=?)
import cv2
import numpy as np
img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel = np.ones((5, 5), np.uint8)
erosion1 = cv2.erode(img, kernel, iterations=1)
erosion2 = cv2.erode(img, kernel, iterations=2)
erosion3 = cv2.erode(img, kernel, iterations=3)
res = np.hstack((erosion1, erosion2, erosion3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
2、膨胀操作,cv2.dilate(src, kernel, interation=?)
import cv2
import numpy as np
img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel = np.ones((5, 5), np.uint8)
dilate1 = cv2.dilate(img, kernel, iterations=1)
dilate2 = cv2.dilate(img, kernel, iterations=2)
dilate3 = cv2.dilate(img, kernel, iterations=3)
res = np.hstack((dilate1, dilate2, dilate3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、开闭运算,cv2.morphologyEx(img, type, kernel, iteration=?)
开运算:先腐蚀后膨胀,type=cv2.MORPH_OPEN
闭运算:先膨胀后腐蚀,type=cv2.MORPH_CLOSED
import cv2
import numpy as np
img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel = np.ones((5, 5), np.uint8)
open1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)
open2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=2)
open3 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=3)
res1 = np.hstack((open1, open2, open3))
cv2.imshow('res', res1)
cv2.waitKey(0)
cv2.destroyAllWindows()
closed1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=1)
closed2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=2)
closed3 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=3)
res2 = np.hstack((closed1, closed1, closed1))
cv2.imshow('res', res2)
cv2.waitKey(0)
cv2.destroyAllWindows()
4、梯度运算 = 膨胀 - 腐蚀,cv2.morphologyEx(img, 2v.MORPH_GRADIENT, kernel, iteration=?)
gradient1 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=1)
gradient2 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=2)
gradient3 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=3)
res3 = np.hstack((gradient1, gradient2, gradient3))
cv2.imshow('res', res3)
cv2.waitKey(0)
cv2.destroyAllWindows()
5、顶帽运算 = 原始输入 - 开运算,cv2.morphologyEx(img, 2v.MORPH_TOPHAT, kernel, iteration=?)
tophat1 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=1)
tophat2 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=2)
tophat3 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=3)
res4 = np.hstack((tophat1, tophat2, tophat3))
cv2.imshow('res', res4)
cv2.waitKey(0)
cv2.destroyAllWindows()
6、黑帽运算 = 闭运算 - 原始输入,cv2.morphologyEx(img, 2v.MORPH_BLACKHAT, kernel, iteration=?)
blackhat1 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=1)
blackhat2 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=2)
blackhat3 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=3)
res5 = np.hstack((blackhat1, blackhat2, blackhat3))
cv2.imshow('res', res5)
cv2.waitKey(0)
cv2.destroyAllWindows()