【发布时间】:2018-04-23 02:52:42
【问题描述】:
我目前正在使用skimage.measure.find_contours() 在表面上查找轮廓。现在我已经找到了我需要能够找到包含在其中的区域的轮廓。
当所有顶点都在数据集中时,这很好,因为它具有完全封闭的多边形。 但是,如果轮廓在边缘或拐角处超出表面的边缘,如何确保多边形完全封闭?发生这种情况时,我想使用曲面的边缘作为附加顶点来关闭多边形。例如在下图中,显示了轮廓,您可以看到轮廓在图像的边缘结束,如何关闭它们?同样在棕色轮廓的示例中,它只是一条线,我不认为我想要返回一个区域,我该如何挑选这种情况?
我知道我可以通过检查多边形的最后一个顶点是否与第一个顶点相同来检查封闭的轮廓/多边形。
我有计算多边形内面积的代码,取自here
def find_area(array):
a = 0
ox,oy = array[0]
for x,y in array[1:]:
a += (x*oy-y*ox)
ox,oy = x,y
return -a/2
我只需要帮助关闭多边形。并检查可能发生的不同情况。
谢谢
更新:
应用@soupault 建议的解决方案后,我有以下代码:
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
# Construct some test data
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
# Coordinates of point of interest
pt = [(49,75)]
# Apply thresholding to the surface
threshold = 0.8
blobs = r > threshold
# Make a labelled image based on the thresholding regions
blobs_labels = measure.label(blobs, background = 0)
# Show the thresholded regions
plt.figure()
plt.imshow(blobs_labels, cmap='spectral')
# Apply regionprops to charactersie each of the regions
props = measure.regionprops(blobs_labels, intensity_image = r)
# Loop through each region in regionprops, identify if the point of interest is
# in that region. If so, plot the region and print it's area.
plt.figure()
plt.imshow(r, cmap='Greys')
plt.plot(pt[0][0], pt[0][1],'rx')
for prop in props:
coords = prop.coords
if np.sum(np.all(coords[:,[1,0]] == pt[0], axis=1)):
plt.plot(coords[:,1],coords[:,0],'r.')
print(prop.area)
此解决方案假定每个像素的大小为 1x1。在我的真实数据解决方案中,情况并非如此,因此我还应用了以下函数来对数据应用线性插值。相信你也可以应用类似的功能,让每个像素的面积更小,提高数据的分辨率。
import numpy as np
from scipy import interpolate
def interpolate_patch(x,y,patch):
x_interp = np.arange(np.ceil(x[0]), x[-1], 1)
y_interp = np.arange(np.ceil(y[0]), y[-1], 1)
f = interpolate.interp2d(x, y, patch, kind='linear')
patch_interp = f(x_interp, y_interp)
return x_interp, y_interp, patch_interp
【问题讨论】:
标签: python scikit-image