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)
# 格式轉(zhuǎn)換 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)
# 格式轉(zhuǎn)換 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邊緣檢測是一種二值檢測,不需要轉(zhuǎn)換格式這一個步驟
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)方法的詳細內(nèi)容,更多關于Python OpenCV圖像邊緣檢測的資料請關注腳本之家其它相關文章!
相關文章
Python解析excel文件存入sqlite數(shù)據(jù)庫的方法
最近工作中遇到一個需求,需要使用Python解析excel文件并存入sqlite,本文就實現(xiàn)的過程做個總結分享給大家,文中包括數(shù)據(jù)庫設計、建立數(shù)據(jù)庫、Python解析excel文件、Python讀取文件名并解析和將解析的數(shù)據(jù)存儲入庫,有需要的朋友們下面來一起學習學習吧。2016-11-11
Python測試WebService接口的實現(xiàn)示例
webService接口是走soap協(xié)議通過http傳輸,請求報文和返回報文都是xml格式的,本文主要介紹了Python測試WebService接口,具有一定的參考價值,感興趣的可以了解一下2024-03-03

