Opencv进行Sobel、Scharr、Laplacian边缘检测操作

mac2024-04-04  39

一、cv2.Sobel(img,cv2.CV_64F, dx, dy, ksize)图像边缘处理

说明:img为原图像,cv2.CV_64F表示64位浮点数即float64,得出的结果可以是负值,可使用cv2.convertScalerAbs(src) 将像素点进行绝对值计算。dx和dy分别是X与Y方向的导数,对于图像来说就是差分,1表示对其求偏导,0表示不求偏导。(对其求导就是检测其方向上是否有边缘)。ksize表示核大小。

内核为3*3的Sobel算子:

import cv2 img = cv2.imread('sobel4.bmp',cv2.IMREAD_GRAYSCALE) resultx = cv2.Sobel(img,cv2.CV_64F,1,0) resultx = cv2.convertScaleAbs(resultx) #转回uint8 cv2.imshow("img",img) cv2.imshow("resultx",resultx) cv2.waitKey() cv2.destroyAllWindows()

X方向上的边缘检测效果图:

 

import cv2 img = cv2.imread('sobel4.bmp',cv2.IMREAD_GRAYSCALE) resulty = cv2.Sobel(img,cv2.CV_64F,0,1) resulty = cv2.convertScaleAbs(resulty) cv2.imshow("img",img) cv2.imshow("resulty",resulty) cv2.waitKey() cv2.destroyAllWindows()

Y方向上的边缘检测效果图:

import cv2 img = cv2.imread('sobel4.bmp',cv2.IMREAD_GRAYSCALE) resultxy=cv2.Sobel(img,cv2.CV_64F,1,1) resultxy=cv2.convertScaleAbs(resultxy) cv2.imshow("img",img) cv2.imshow("resultxy",resultxy) cv2.waitKey() cv2.destroyAllWindows()

同时在X和Y的边缘检测效果图:

import cv2 img = cv2.imread('sobel4.bmp',cv2.IMREAD_GRAYSCALE) resultx = cv2.Sobel(img,cv2.CV_64F,1,0) resulty = cv2.Sobel(img,cv2.CV_64F,0,1) resultx = cv2.convertScaleAbs(resultx) # 转回uint8 resulty = cv2.convertScaleAbs(resulty) resultxy = cv2.addWeighted(resultx,0.5,resulty,0.5,0) cv2.imshow("img",img) cv2.imshow("resultxy",resultxy) cv2.waitKey() cv2.destroyAllWindows()

X和Y方向上的边缘检测融合效果图:

 

二、cv2.Scharr(img,cv2.CV_64F, dx, dy, ksize)  scharr算子

内核为3*3的Scharr算子:

import cv2 img = cv2.imread('scharr.bmp',cv2.IMREAD_GRAYSCALE) resultx = cv2.Scharr(img,cv2.CV_64F,1,0) resultx = cv2.convertScaleAbs(resultx) # 转回uint8 resulty = cv2.Scharr(img,cv2.CV_64F,0,1) resulty = cv2.convertScaleAbs(resulty) resultxy = cv2.addWeighted(resultx,0.5,resulty,0.5,0) cv2.imshow("img",img) cv2.imshow("resultx",resultx) cv2.imshow("resulty",resulty) cv2.imshow("resultxy",resultxy) cv2.waitKey() cv2.destroyAllWindows()

import cv2 img = cv2.imread('lena.bmp',cv2.IMREAD_GRAYSCALE) sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) sobelx = cv2.convertScaleAbs(sobelx) # 转回uint8 sobely = cv2.convertScaleAbs(sobely) sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0) scharrx = cv2.Scharr(img,cv2.CV_64F,1,0) scharry = cv2.Scharr(img,cv2.CV_64F,0,1) scharrx = cv2.convertScaleAbs(scharrx) # 转回uint8 scharry = cv2.convertScaleAbs(scharry) scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0) cv2.imshow("img",img) cv2.imshow("sobelxy",sobelxy) cv2.imshow("scharrxy",scharrxy) cv2.waitKey() cv2.destroyAllWindows()

Sobel与Scharr的区别效果图:

三、cv2.Laplacian(img,cv2.CV_64F) 拉普拉斯算子

说明:Laplace其实利用Sobel算子的运算,它通过Sobel算子运算出图像在x方向和y方向的导数,得出拉普拉斯变换结果。它就像Sobel算子的升级版。

内核为3*3的Laplacian算子:

import cv2 img = cv2.imread('Laplacian.bmp',cv2.IMREAD_GRAYSCALE) result = cv2.Laplacian(img,cv2.CV_64F) result = cv2.convertScaleAbs(result) cv2.imshow("img",img) cv2.imshow("result",result) cv2.waitKey() cv2.destroyAllWindows()

四、cv2.Canny(img, threshold1, threshold2) Canny边缘检测

说明:img为原图像,threshold1为阈值1,threshold2为阈值2,寻找最优边缘检测算法,即能够尽可能地标识出图像中的实际边缘。

import cv2 img=cv2.imread("lena.bmp",cv2.IMREAD_GRAYSCALE) result1=cv2.Canny(img,128,200) result2=cv2.Canny(img,32,128) cv2.imshow("img",img) cv2.imshow("result1",result1) cv2.imshow("result2",result2) cv2.waitKey() cv2.destroyAllWindows()

效果图:

最新回复(0)