Python+OpenCV 图像边缘检测四种实现方法
程序员文章站
2022-04-18 15:43:01
目录1.sobel算子2.schaar算子(更能体现细节)3.laplacian算子(基于零穿越的,二阶导数的0值点)4.canny边缘检测(被认为是最优的边缘检测算法)总结import cv2 as...
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # 设置兼容中文 plt.rcparams['font.family'] = ['sans-serif'] plt.rcparams['font.sans-serif'] = ['simhei']
d:\anaconda\azwz\lib\site-packages\numpy\_distributor_init.py:30: userwarning: loaded more than 1 dll from .libs: d:\anaconda\azwz\lib\site-packages\numpy\.libs\libopenblas.noijjg62emaszi6nyurl6jbkm4evbgm7.gfortran-win_amd64.dll d:\anaconda\azwz\lib\site-packages\numpy\.libs\libopenblas.wcdjnk7yvmpzq2me2zzhjjrj3jikndb7.gfortran-win_amd64.dll warnings.warn("loaded more than 1 dll from .libs:\n%s" %
horse = cv.imread('img/horse.jpg',0)
plt.imshow(horse,cmap=plt.cm.gray)
plt.imshow(horse,cmap=plt.cm.gray)
1.sobel算子
# 1,0 代表沿x方向做sobel算子 x = cv.sobel(horse,cv.cv_16s,1,0) # 0,1 代表沿y方向做sobel算子 y = cv.sobel(horse,cv.cv_16s,0,1)
# 格式转换 absx = cv.convertscaleabs(x) absy = cv.convertscaleabs(y)
# 边缘检测结果 res = cv.addweighted(absx,0.5,absy,0.5,0)
plt.figure(figsize=(20,20)) plt.subplot(1,2,1) m1 = plt.imshow(horse,cmap=plt.cm.gray) plt.title("原图") plt.subplot(1,2,2) m2 = plt.imshow(res,cmap=plt.cm.gray) plt.title("sobel算子边缘检测")
text(0.5, 1.0, 'sobel算子边缘检测')
2.schaar算子(更能体现细节)
# 1,0 代表沿x方向做sobel算子 x = cv.sobel(horse,cv.cv_16s,1,0,ksize=-1) # 0,1 代表沿y方向做sobel算子 y = cv.sobel(horse,cv.cv_16s,0,1,ksize=-1)
# 格式转换 absx = cv.convertscaleabs(x) absy = cv.convertscaleabs(y)
# 边缘检测结果 res = cv.addweighted(absx,0.5,absy,0.5,0)
plt.figure(figsize=(20,20)) plt.subplot(1,2,1) m1 = plt.imshow(horse,cmap=plt.cm.gray) plt.title("原图") plt.subplot(1,2,2) m2 = plt.imshow(res,cmap=plt.cm.gray) plt.title("schaar算子边缘检测")
text(0.5, 1.0, 'schaar算子边缘检测')
3.laplacian算子(基于零穿越的,二阶导数的0值点)
res = cv.laplacian(horse,cv.cv_16s)
res = cv.convertscaleabs(res)
plt.figure(figsize=(20,20)) plt.subplot(1,2,1) m1 = plt.imshow(horse,cmap=plt.cm.gray) plt.title("原图") plt.subplot(1,2,2) m2 = plt.imshow(res,cmap=plt.cm.gray) plt.title("laplacian算子边缘检测")
text(0.5, 1.0, 'laplacian算子边缘检测')
4.canny边缘检测(被认为是最优的边缘检测算法)
res = cv.canny(horse,0,100)
# res = cv.convertscaleabs(res) canny边缘检测是一种二值检测,不需要转换格式这一个步骤
plt.figure(figsize=(20,20)) plt.subplot(1,2,1) m1 = plt.imshow(horse,cmap=plt.cm.gray) plt.title("原图") plt.subplot(1,2,2) m2 = plt.imshow(res,cmap=plt.cm.gray) plt.title("canny边缘检测")
text(0.5, 1.0, 'canny边缘检测')
总结
以上就是python+opencv 图像边缘检测四种实现方法的详细内容,更多关于python opencv图像边缘检测的资料请关注其它相关文章!