一、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.de