0
点赞
收藏
分享

微信扫一扫

超详图解 Apache HTTP Server(httpd)安装与验证

一、sobel算子

算子矩阵:2a74a11188824a4a87b5f3594da9e2ae.jpeg

# sobel算子 算梯度
# Gx = 右边-左边;Gy = 下边 - 上边
import cv2

def cv_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img_people = cv2.imread('people.png', cv2.IMREAD_GRAYSCALE)
cv_show('img_people', img_people)
原图结果:

# dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
# ddepth:图像的深度,ksiz是sobel算子的大小,cv2.CV_64F 是一种浮点数类型,表示为64位的浮点数
# 1, 0 表示的就是在x方向上做,而y方向上不做
sobelx = cv2.Sobel(img_people, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
Gx的结果:

sobely = cv2.Sobel(img_people, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
Gy的结果:

cv_show('Gx', sobelx)
cv_show('Gy', sobely)
# G = Gx+Gy
# 分别计算x与y,再求和
sobelxy = cv2.addWeighted(sobelx,0.5, sobely, 0.5, 0)
cv_show('G', sobelxy)
G的结果:
# 不建议直接计算
# 计算效果提取出来的边缘不明显
sobelxy = cv2.Sobel(img_people, cv2.CV_64F, 1, 1, ksize=3)
# 有正有负,使用convertScaleAbs
sobelxy = cv2.convertScaleAbs(sobelxy)
cv_show('直接计算G', sobelxy)

G直接计算的结果:

3c735158c63d4ad9a88f8149877411a1.png

二、Scharr算子与laplacian算子

计算矩阵中的数值变得更大,代码执行的结果更为丰富。laplacian算子,对噪声更敏感。

Scharr算子:30968224554e4ffcbbbd3fff4c236254.jpeg

laplacian算子:a319aaca069640438ac09ff19b5a1a48.jpeg

 

# 不同算子的差异
sobelx = cv2.Sobel(img_people, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img_people, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5, sobely, 0.5, 0)

Scharr算子:
scharrx = cv2.Scharr(img_people, cv2.CV_64F, 1, 0)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.Scharr(img_people, cv2.CV_64F, 0, 1)
scharry = cv2.convertScaleAbs(sobely)
scharrxy = cv2.addWeighted(sobelx,0.5, scharry, 0.5, 0)

laplacian算子:(没有x与y方向)
laplacian = cv2.Laplacian(img_people, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
res = np.hstack((sobelxy, scharrxy, laplacian))
cv_show('res', res)

6546e9d367004abfb71d9b707364e838.png

 

举报

相关推荐

0 条评论