**
图像梯度处理
**
soble算子
dest=cv2.soble(src,ddepth,dx,dy,ksize)
ddepth:图像的深度(通常指定-1)
dx和dy分别表示水平和竖直方向
ksize是Sobel算子的大小
import cv2
import numpy as np
img=cv2.imread('D:/opencv/tx/pie.png',cv2.IMREAD_GRAYSCALE)
def cv_show(img,name):
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
cv_show(sobelx,'sobelx')
(右边减去左边)白到黑是正数,黑到白是负数,所有的负数会被截断成0 ,所以要取绝对值
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
cv_show(sobelx,'sobelx')
计算y
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
cv_show(sobely,'sobely')
求和
sobelxy = cv2.addWeighted(soblex,0.5,sobley,0.5,0)
cv_show (sobelxy,'sobelxy ' )
不建议直接计算
sobelxy = cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
cv_show (sobelxy,'sobelxy ' )
其他算子
对比
import cv2
import numpy as np
img=cv2.imread('D:/opencv/tx/lena.jpg',cv2.IMREAD_GRAYSCALE)
def cv_show(img,name):
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
#cv_show(sobely,'sobely')
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
#cv_show(sobelx,'sobelx')2.Sobel
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)
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)
res = np.hstack((sobelxy ,scharrxy,laplacian))
cv_show (res,'res' )