简体   繁体   English

skimage中的Slic分割

[英]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()

Original image Labeled image 原始图像 标签图像

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.

相关问题 skimage.segmentation.slic() 究竟代表什么? - What exactly does skimage.segmentation.slic() represent? opencv ximgproc.slic和skimage segmentation.slic有什么区别? - What is the difference between opencv ximgproc.slic and skimage segmentation.slic? skimage slic:获得相邻的细分 - skimage slic: getting neighbouring segments TypeError: load() 缺少 1 个必需的位置参数:从 skimage.segmentation 导入 felzenszwalb、slic、quickshift、watershed 时的“Loader” - TypeError: load() missing 1 required positional argument: 'Loader' when importing felzenszwalb, slic, quickshift, watershed from skimage.segmentation Skimage 重新缩放分割蒙版 - Skimage Rescale Segmentation Mask 来自快速分割的Skimage Region Adjacency Graph(RAG) - Skimage Region Adjacency Graph (RAG) from quickshift segmentation 如何使用skimage.segmentation.felzenszwalb获取围绕轮廓的框? - How to get box around contour using skimage.segmentation.felzenszwalb? 在 skimage 中使用简单线性迭代聚类生成的分割边界没有明确定义? - Segmentation boundaries generated using Simple Linear Iterative Clustering in skimage are not well defined? Python 3.8:找出使用 skimage.segmentation.mark_boundaries 绘制的检测边界的属性 - Python 3.8: Find out the properties of detected boundaries drawn using skimage.segmentation.mark_boundaries 切片超像素的平均颜色 - Average colour of slic superpixel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM