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)來細分多邊形的曲線,該曲線通常在凸包線的內(nèi)部。
函數(shù)格式為:
skimage.measure.subdivide_polygon(coords, degree=2, preserve_ends=False)
coords: 坐標點序列。
degree: B樣條的度數(shù),默認為2
preserve_ends: 如果曲線為非閉合曲線,是否保存開始和結(jié)束點坐標,默認為false
返回細分為的坐標點序列。
approximate_polygon()是基于Douglas-Peucker算法的一種近似曲線模擬。它根據(jù)指定的容忍值來近似一條多邊形曲線鏈,該曲線也在凸包線的內(nèi)部。
函數(shù)格式為:
skimage.measure.approximate_polygon(coords, tolerance)
coords: 坐標點序列
tolerance: 容忍值
返回近似的多邊形曲線坐標序列。
例:
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ù)字圖像處理之邊緣輪廓檢測的詳細內(nèi)容,更多關(guān)于python數(shù)字圖像邊緣輪廓的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python利用Xpath選擇器爬取京東網(wǎng)商品信息
這篇文章主要介紹了Python利用Xpath選擇器爬取京東網(wǎng)商品信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
利用Django-environ如何區(qū)分不同環(huán)境
這篇文章主要給大家介紹了關(guān)于利用Django-environ如何區(qū)分不同環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用django具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08
Python實現(xiàn)的隨機森林算法與簡單總結(jié)
這篇文章主要介紹了Python實現(xiàn)的隨機森林算法,結(jié)合實例形式詳細分析了隨機森林算法的概念、原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2018-01-01
Python實現(xiàn)一個簡單的畢業(yè)生信息管理系統(tǒng)的示例代碼
這篇文章主要介紹了Python實現(xiàn)一個簡單的畢業(yè)生信息管理系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

