日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python数字图像处理(17):边缘与轮廓

發布時間:2025/3/21 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数字图像处理(17):边缘与轮廓 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在前面的python數字圖像處理(10):圖像簡單濾波?中,我們已經講解了很多算子用來檢測邊緣,其中用得最多的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')

from:?https://www.cnblogs.com/denny402/p/5160955.html?

總結

以上是生活随笔為你收集整理的python数字图像处理(17):边缘与轮廓的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。