Python+OpenCV 圖像邊緣檢測四種實現(xiàn)方法
更新時間:2021年11月26日 09:07:12 投稿:newname
本文主要介紹了通過OpenCV中Sobel算子、Schaar算子、Laplacian算子以及Canny分別實現(xiàn)圖像邊緣檢測并總結了四者的優(yōu)缺點,感興趣的同學可以參考一下
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算子(更能體現(xiàn)細節(jié))
# 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算子(基于零穿越的,二階導數(shù)的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邊緣檢測(被認為是最優(yōu)的邊緣檢測算法)
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 圖像邊緣檢測四種實現(xiàn)方法的詳細內容,更多關于Python OpenCV圖像邊緣檢測的資料請關注腳本之家其它相關文章!
相關文章
python目錄操作之python遍歷文件夾后將結果存儲為xml
需求是獲取服務器某個目錄下的某些類型的文件,考慮到服務器即有Linux、又有Windows,所以寫了一個Python小程序來完成這項工作,大家參考使用吧2014-01-01深入理解Python虛擬機中整型(int)的實現(xiàn)原理及源碼剖析
在本篇文章當中主要給大家介紹在 cpython 內部是如何實現(xiàn)整型數(shù)據(jù) int 的,主要是分析 int 類型的表示方式,分析 int 類型的巧妙設計2023-03-03Python 用三行代碼提取PDF表格數(shù)據(jù)
這篇文章主要介紹了Python 用三行代碼提取PDF表格數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10