簡體   English   中英

找到分割圖像的兩個區域的界面

[英]Finding the Interface of two regions of a segmented image

我有一個共享一個邊界的兩個區域的分段(按分水嶺)圖像。 我如何輕松找到界面上像素的position? 我嘗試使用此答案中的提示,但無法正常工作。 這是我的示例代碼:

import matplotlib.pyplot as plt
from scipy import ndimage as ndi

from skimage.segmentation import watershed
from skimage.feature import peak_local_max

from skimage import future
from skimage.measure import label, regionprops, regionprops_table


# Generate an initial image with two overlapping circles
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2
mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2
image = np.logical_or(mask_circle1, mask_circle2)

# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
coords = peak_local_max(distance, footprint=np.ones((3, 3)), labels=image)
mask = np.zeros(distance.shape, dtype=bool)
mask[tuple(coords.T)] = True
markers, _ = ndi.label(mask)
labels = watershed(-distance, markers, mask=image)

fig, axes = plt.subplots(ncols=3, figsize=(9, 3), sharex=True, sharey=True)
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Overlapping objects')
ax[1].imshow(-distance, cmap=plt.cm.gray)
ax[1].set_title('Distances')
ax[2].imshow(labels, cmap=plt.cm.nipy_spectral)
ax[2].set_title('Separated objects')

for a in ax:
    a.set_axis_off()

fig.tight_layout()
plt.show()

#---------------- find the interface pixels (either of the two interfaces) of these two objects -----------

rag = future.graph.RAG(labels) 
rag.remove_node(0)
for region in regionprops(labels):
    nlist=list(rag.neighbors(region.label))
print(nlist)

nlist 似乎只是一個包含一個元素 1: [1]的列表。 我期待 position 像素。 我在使用圖形和 RAG 方面沒有太多經驗。 似乎 rag 創建了區域的 graph.network 並具有哪個區域與哪個區域相鄰的信息,但我無法以界面像素的形式提取該信息。 謝謝你的幫助。

目前 RAG object 不跟蹤所有區域和邊界,但我們希望將來支持它。 您找到的只是相鄰區域的列表。

現在,如果您只有兩個區域,手動執行此操作並不會太昂貴:

from skimage.morphology import dilation

label1 = labels == 1
label2 = labels == 2

boundary = dilation(label1) & dilation(label2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM