[英]Slic segmentation in skimage
I'm using skimage library to define graph nodes and edges, which will describe certain image. 我正在使用skimage库定义图节点和边,这些图将描述某些图像。 After applying algorithm and plotting segmented regions I have realized that one of regions was not labeled. 应用算法并绘制分段区域后,我意识到区域之一未标记。 My goal is label all regions and find out all neighbors for each of them, but I've stuck in attempts to answer this question. 我的目标是标记所有区域并找出每个区域的所有邻居,但是我一直试图回答这个问题。 I would be really grateful for any helpful information. 如有任何有用的信息,我将不胜感激。
import imageio
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from skimage.measure import regionprops
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.2126, 0.7152, 0.0722])
image = imageio.imread(img_file_path)
segments_slic = slic(image, n_segments=250, compactness=100)
regions = regionprops(segments_slic, intensity_image=rgb2gray(image))
for props in regions:
cy, cx = props.centroid
plt.plot(cx, cy, 'ro')
plt.imshow(mark_boundaries(image, segments_slic))
plt.show()
This is an unfortunate historical accident: SLIC returns segments starting from 0, but regionprops (and most other functions) treat 0 as the background. 这是一次不幸的历史事故:SLIC返回从0开始的段,但是regionprops(和大多数其他函数)将0作为背景。 To fix your code, add 1 to the output of SLIC: 要修复您的代码,请在SLIC的输出中添加1:
segments_slic = 1 + slic(image, n_segments=250, compactness=100)
Then you get the output you expect, with the top-left segment (formerly 0, now 1) properly detected: 然后,您将获得期望的输出,并正确检测到左上段(以前为0,现在为1):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.