文章目录
- 1.腐蚀操作
- 2.膨胀操作
- 3.开运算与闭运算
- 4.梯度运算
- 5.礼帽与黑帽
- 6.计算图像梯度
-
- 6.1. Sobel算子
- 6.2. Scharr算子与 laplacian算子
- 7.图像平滑处理
-
- 7.1.均值滤波
- 7.2.方框滤波
- 7.3.高斯滤波
- 7.4.中值滤波
- 8.图像阈值
- 9.图像灰度化
- 10.Canny边缘检测
1.腐蚀操作
img = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
(4,4)表示一次操作所腐蚀的范围,iterations:表示腐蚀操作进行多少次
kernel=np.ones((4,4),np.uint8)
erosion=cv2.erode(img,kernel,iterations=5)
plt.imshow(erosion)
plt.show()
2.膨胀操作
kernel=np.ones((4,4),np.uint8)
img_dilate=cv2.dilate(img,kernel,iterations=6)
plt.imshow(img_dilate)
plt.show()
3.开运算与闭运算
kernel=np.ones((4,4),np.uint8)
opening=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
kernel = np.ones((4, 4), np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
4.梯度运算
pie=cv2.imread('img1.jpg')
kernel=np.ones((20,20),np.uint8)
erode=cv2.erode(pie,kernel,iterations=2)
dilate=cv2.dilate(pie,kernel,iterations=2)
res=np.hstack((pie,erode,dilate))
plt.imshow(res)
plt.show()
gradient=cv2.morphologyEx(pie,cv.MORPH_GRADIENT,kernel)
plt.imshow(gradient)
plt.show()
5.礼帽与黑帽
tophat=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
plt.imshow(tophat)
plt.show()
blackhat=cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
plt.imshow(blackhat)
plt.show()
6.计算图像梯度
6.1. Sobel算子
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
plt.imshow(sobelx)
plt.show()
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx=cv2.convertScaleAbs(sobelx)
plt.imshow(sobelx)
plt.show()
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx=cv2.convertScaleAbs(sobelx)
plt.imshow(sobelx)
plt.show()
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
plt.imshow(sobely)
plt.show()
算出x,y 然后将x,y一起算
sobelxy=cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
plt.imshow(sobelxy)
plt.show()
6.2. Scharr算子与 laplacian算子
scharrx=cv2.Scharr(img,cv2.CV_64F,1,0)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrx=cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy=cv2.addWeighted(scharrx,0.5,scharry,0.5,0)
plt.imshow(sobelxy)
plt.show()
laplacian=cv2.Laplacian(img,cv2.CV_64F)
laplacian=cv2.convertScaleAbs(laplacian)
plt.imshow(laplacian)
plt.show()
res=np.hstack((sobelxy,scharrxy,laplacian))
plt.imshow(res)
7.图像平滑处理
7.1.均值滤波
blur=cv2.blur(img,(3,3))
res = np.hstack((img,blur))
plt.imshow(res)
plt.show()
7.2.方框滤波
box=cv2.boxFilter(img,-1,(3,3),normalize=False)
res = np.hstack((img, box))
plt.imshow(res)
plt.show()
7.3.高斯滤波
aussian=cv2.GaussianBlur(img,(5,5),1)
res = np.hstack((img, aussian))
plt.imshow(res)
plt.show()
7.4.中值滤波
median=cv2.medianBlur(img,5)
res = np.hstack((img, median))
8.图像阈值
- ret,dst=cv2.threshold(src,thresh,maxval,type)
src:输入图,只能输入单通道图像,一般为灰度图
dst:输出图
thresh:阈值
maxval:当像素值超过阈值(或者小于阈值),所赋予的值
type:二值化(大于取一个值,小于取另一个值)操作的类型,包含五种类型
- THRESH_BINARY:超过阈值部分取maxval,否则为0
- THRESH_BINARY_INV: THRESH_BINARY的反转
- THRESH_TRUNC:大于阈值部分设为阈值,否则不变
- THRESH_TOZERO:大于阈值部分不改变,否则设为0
- THRESH_TOZERO_INV: THRESH_TOZERO的反转
ret,thresh1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2=cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3=cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4=cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5=cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
title=['Original','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(title[i])
plt.xticks([]),plt.yticks([])
9.图像灰度化
img = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
10.Canny边缘检测
- 使用高斯滤波,以平滑图像,滤除噪声
- 计算图像中每个像素点的梯度强度和方向
- 应用非极大值(Non-Maximum-Suppression)抑制,以消除边缘检测带来的杂散响应
- 应用双阈值(Double-Threshold)检测来确定真实的和潜在的边缘
- 通过抑制孤立的弱边缘最终完成边缘检测
img = cv2.imread("dog.jpg", cv2.IMREAD_GRAYSCALE)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
v1=cv2.Canny(img,120,250)
v2=cv2.Canny(img,50,100)
res=np.hstack((v1,v2))
plt.imshow(res)
plt.show()