图像处理总目录←点击这里
原图
不建议这么算
sobelxy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
sobelxy = cv2.convertScaleAbs(sobelxy)
cv_show(sobelxy,'sobelxy')
当前位置的像素值等于sobel算子与(当前位置与周边位置8个点)进行对应位置相乘并相加操作,作为当前位置的像素点
G
x
=
(
−
1
0
+
1
−
2
0
+
2
−
1
0
+
1
)
∗
(
p
1
p
2
p
3
p
4
p
5
p
6
p
7
p
8
p
9
)
=
(
p
3
−
p
1
)
+
(
2
p
6
−
2
p
4
)
+
(
p
9
−
p
7
)
G_x=\left( −10+1−20+2−10+1 \right) *\left( p1p2p3p4p5p6p7p8p9 \right) =\left( p_3-p_1 \right) +\left( 2p_6-2p_4 \right) +\left( p_9-p_7 \right)
Gx=⎝⎛−1−2−1000+1+2+1⎠⎞∗⎝⎛p1p4p7p2p5p8p3p6p9⎠⎞=(p3−p1)+(2p6−2p4)+(p9−p7)
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
cv2.convertScaleAbs
解决)Gx (右边减左边)cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
中1,0为取Gx
deep可能有负值,白到黑是正数,黑到白就是负数了,所有的负数会被截断成0,效果如下
img = cv2.imread('./image/pie.png',cv2.IMREAD_GRAYSCALE)
def cv_show(img,name):
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
cv_show(sobelx,'sobelx')
deep的值要取绝对值cv2.convertScaleAbs
,效果如下
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
cv_show(sobelx,'sobelx')
Gy(下面减上面)cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
中为0,1为
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
cv_show(sobely,'sobely')
Gx和Gy求和
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
cv_show(sobelxy,'sobelxy')
原图
img = cv2.imread('./image/lena.jpg',cv2.IMREAD_GRAYSCALE)
cv_show(img,'img')
Sobel算子,轮廓效果
img = cv2.imread('./image/lena.jpg',cv2.IMREAD_GRAYSCALE)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
cv_show(sobelxy,'sobelxy')
cv2.Scharr(src, ddepth, dx, dy, ksize)
cv2.convertScaleAbs
解决)img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
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)
cv_show(scharrxy,'scharrxy')
cv2.Scharr(src, ddepth, ksize)
cv2.convertScaleAbs
解决)laplacian = cv2.Laplacian(img,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
cv_show(laplacian,'laplacian')
#不同算子的差异
img = cv2.imread('./image/lena.jpg',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)
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)
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')