網站首頁 編程語言 正文
引言
在前面的python數字圖像處理簡單濾波?中,我們已經講解了很多算子用來檢測邊緣,其中用得最多的canny算子邊緣檢測。
本篇我們講解一些其它方法來檢測輪廓。
1、查找輪廓(find_contours)
measure模塊中的find_contours()函數,可用來檢測二值圖像的邊緣輪廓。
函數原型為:
skimage.measure.find_contours(array,?level)
array: 一個二值數組圖像
level: 在圖像中查找輪廓的級別值
返回輪廓列表集合,可用for循環取出每一條輪廓。
例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()
結果如下:不同的輪廓用不同的顏色顯示
例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、逼近多邊形曲線
逼近多邊形曲線有兩個函數:subdivide_polygon()和?approximate_polygon()
subdivide_polygon()采用B樣條(B-Splines)來細分多邊形的曲線,該曲線通常在凸包線的內部。
函數格式為:
skimage.measure.subdivide_polygon(coords,?degree=2,?preserve_ends=False)
coords: 坐標點序列。
degree: B樣條的度數,默認為2
preserve_ends: 如果曲線為非閉合曲線,是否保存開始和結束點坐標,默認為false
返回細分為的坐標點序列。
approximate_polygon()是基于Douglas-Peucker算法的一種近似曲線模擬。它根據指定的容忍值來近似一條多邊形曲線鏈,該曲線也在凸包線的內部。
函數格式為:
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')
原文鏈接:https://www.cnblogs.com/denny402/p/5160955.html
相關推薦
- 2022-06-12 Redis高并發場景下秒殺超賣解決方案(秒殺場景)_Redis
- 2023-03-22 React使用高階組件與Hooks實現權限攔截教程詳細分析_React
- 2022-04-19 python 讀寫yaml
- 2023-03-26 WPF實現頁面的切換的示例代碼_C#教程
- 2022-11-22 Python實例方法與類方法和靜態方法介紹與區別分析_python
- 2022-07-25 pandas實現數據讀取&清洗&分析的項目實踐_python
- 2022-05-19 Python合并重疊矩形框_python
- 2022-12-07 C++中的自定義函數返回類型_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支