python數(shù)字圖像處理之邊緣輪廓檢測
引言
在前面的python數(shù)字圖像處理簡單濾波 中,我們已經(jīng)講解了很多算子用來檢測邊緣,其中用得最多的canny算子邊緣檢測。
本篇我們講解一些其它方法來檢測輪廓。
1、查找輪廓(find_contours)
measure模塊中的find_contours()函數(shù),可用來檢測二值圖像的邊緣輪廓。
函數(shù)原型為:
skimage.measure.find_contours(array, level)
array: 一個二值數(shù)組圖像
level: 在圖像中查找輪廓的級別值
返回輪廓列表集合,可用for循環(huán)取出每一條輪廓。
例1:
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure,draw
#生成二值測試圖像
img=np.zeros([100,100])
img[20:40,60:80]=1 #矩形
rr,cc=draw.circle(60,60,10) #小圓
rr1,cc1=draw.circle(20,30,15) #大圓
img[rr,cc]=1
img[rr1,cc1]=1
#檢測所有圖形的輪廓
contours = measure.find_contours(img, 0.5)
#繪制輪廓
fig, (ax0,ax1) = plt.subplots(1,2,figsize=(8,8))
ax0.imshow(img,plt.cm.gray)
ax1.imshow(img,plt.cm.gray)
for n, contour in enumerate(contours):
ax1.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax1.axis('image')
ax1.set_xticks([])
ax1.set_yticks([])
plt.show()結(jié)果如下:不同的輪廓用不同的顏色顯示

例2:
import matplotlib.pyplot as plt
from skimage import measure,data,color
#生成二值測試圖像
img=color.rgb2gray(data.horse())
#檢測所有圖形的輪廓
contours = measure.find_contours(img, 0.5)
#繪制輪廓
fig, axes = plt.subplots(1,2,figsize=(8,8))
ax0, ax1= axes.ravel()
ax0.imshow(img,plt.cm.gray)
ax0.set_title('original image')
rows,cols=img.shape
ax1.axis([0,rows,cols,0])
for n, contour in enumerate(contours):
ax1.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax1.axis('image')
ax1.set_title('contours')
plt.show()
2、逼近多邊形曲線
逼近多邊形曲線有兩個函數(shù):subdivide_polygon()和 approximate_polygon()
subdivide_polygon()采用B樣條(B-Splines)來細(xì)分多邊形的曲線,該曲線通常在凸包線的內(nèi)部。
函數(shù)格式為:
skimage.measure.subdivide_polygon(coords, degree=2, preserve_ends=False)
coords: 坐標(biāo)點序列。
degree: B樣條的度數(shù),默認(rèn)為2
preserve_ends: 如果曲線為非閉合曲線,是否保存開始和結(jié)束點坐標(biāo),默認(rèn)為false
返回細(xì)分為的坐標(biāo)點序列。
approximate_polygon()是基于Douglas-Peucker算法的一種近似曲線模擬。它根據(jù)指定的容忍值來近似一條多邊形曲線鏈,該曲線也在凸包線的內(nèi)部。
函數(shù)格式為:
skimage.measure.approximate_polygon(coords, tolerance)
coords: 坐標(biāo)點序列
tolerance: 容忍值
返回近似的多邊形曲線坐標(biāo)序列。
例:
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure,data,color
#生成二值測試圖像
hand = np.array([[1.64516129, 1.16145833],
[1.64516129, 1.59375],
[1.35080645, 1.921875],
[1.375, 2.18229167],
[1.68548387, 1.9375],
[1.60887097, 2.55208333],
[1.68548387, 2.69791667],
[1.76209677, 2.56770833],
[1.83064516, 1.97395833],
[1.89516129, 2.75],
[1.9516129, 2.84895833],
[2.01209677, 2.76041667],
[1.99193548, 1.99479167],
[2.11290323, 2.63020833],
[2.2016129, 2.734375],
[2.25403226, 2.60416667],
[2.14919355, 1.953125],
[2.30645161, 2.36979167],
[2.39112903, 2.36979167],
[2.41532258, 2.1875],
[2.1733871, 1.703125],
[2.07782258, 1.16666667]])
#檢測所有圖形的輪廓
new_hand = hand.copy()
for _ in range(5):
new_hand =measure.subdivide_polygon(new_hand, degree=2)
# approximate subdivided polygon with Douglas-Peucker algorithm
appr_hand =measure.approximate_polygon(new_hand, tolerance=0.02)
print("Number of coordinates:", len(hand), len(new_hand), len(appr_hand))
fig, axes= plt.subplots(2,2, figsize=(9, 8))
ax0,ax1,ax2,ax3=axes.ravel()
ax0.plot(hand[:, 0], hand[:, 1],'r')
ax0.set_title('original hand')
ax1.plot(new_hand[:, 0], new_hand[:, 1],'g')
ax1.set_title('subdivide_polygon')
ax2.plot(appr_hand[:, 0], appr_hand[:, 1],'b')
ax2.set_title('approximate_polygon')
ax3.plot(hand[:, 0], hand[:, 1],'r')
ax3.plot(new_hand[:, 0], new_hand[:, 1],'g')
ax3.plot(appr_hand[:, 0], appr_hand[:, 1],'b')
ax3.set_title('all')
以上就是python數(shù)字圖像處理之邊緣輪廓檢測的詳細(xì)內(nèi)容,更多關(guān)于python數(shù)字圖像邊緣輪廓的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python使用文件鎖實現(xiàn)進(jìn)程間同步功能【基于fcntl模塊】
這篇文章主要介紹了Python使用文件鎖實現(xiàn)進(jìn)程間同步功能,結(jié)合實例形式分析了Python基于fcntl模塊文件鎖功能實現(xiàn)進(jìn)程間同步的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
pycharm-professional-2020.1下載與激活的教程
這篇文章主要介紹了pycharm-professional-2020.1下載與激活的教程,本文分為安裝和永久激活兩部分內(nèi)容,需要的朋友可以參考下2020-09-09
python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說明
這篇文章主要介紹了python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
PHP實現(xiàn)發(fā)送和接收J(rèn)SON請求
本篇文章給大家分享了PHP實現(xiàn)發(fā)送和接收J(rèn)SON請求的詳細(xì)方法和實例代碼,有興趣的朋友可以參考學(xué)習(xí)下。2018-06-06

