skimage.segmentation.slic如何实现非二进制mask下的分割?
How does skimage.segmentation.slic achieve segmentation under non-binary masks?
Slic可以实现二值化mask下的分割,如下图
from https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_mask_slic.html
但是如果我需要划分不同相邻区域的超像素,我该怎么办?
Each color represents an area, each region requires independent superpixel segmentation
目前没有任何方法可以在一次调用中处理具有多个区域的掩码。对于您的用例,您必须将每个区域拆分为一个单独的掩码,然后每个掩码调用一次 slic
。您可以通过适当增加标签将多个分段合并为一个。
下面粘贴的是两个单独的屏蔽区域的具体示例(改编自您引用的 existing example):
import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage import color
from skimage import morphology
from skimage import segmentation
# Input data
img = data.immunohistochemistry()
# Compute a mask
lum = color.rgb2gray(img)
mask = morphology.remove_small_holes(
morphology.remove_small_objects(
lum < 0.7, 500),
500)
mask1 = morphology.opening(mask, morphology.disk(3))
# create a second mask as the inverse of the first
mask2 = ~mask1
segmented = np.zeros(img.shape[:-1], dtype=np.int64)
max_label = 0
# replace [mask2, mask1] with a list of any number of binary masks
for mask in [mask2, mask1]:
# maskSLIC result
m_slic = segmentation.slic(img, n_segments=100, mask=mask, start_label=1)
if max_label > 0:
# offset the labels by the current maximum label
m_slic += max_label
# add the label into the current combined segmentation
segmented += m_slic
# increment max label
max_label += m_slic.max()
# Display result
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
ax1, ax2, ax3, ax4 = ax_arr.ravel()
ax1.imshow(img)
ax1.set_title('Original image')
ax2.imshow(mask, cmap='gray')
ax2.set_title('Mask')
ax3.imshow(segmentation.mark_boundaries(img, m_slic))
ax3.contour(mask, colors='red', linewidths=1)
ax3.set_title('maskSLIC (mask1 only)')
ax4.imshow(segmentation.mark_boundaries(img, segmented))
ax4.contour(mask, colors='red', linewidths=1)
ax4.set_title('maskSLIC (both masks)')
for ax in ax_arr.ravel():
ax.set_axis_off()
plt.tight_layout()
plt.show()
我建议的基本方法是在上面的 for 循环中。大多数其他代码只是生成数据和绘图。
Slic可以实现二值化mask下的分割,如下图
from https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_mask_slic.html
但是如果我需要划分不同相邻区域的超像素,我该怎么办?
Each color represents an area, each region requires independent superpixel segmentation
目前没有任何方法可以在一次调用中处理具有多个区域的掩码。对于您的用例,您必须将每个区域拆分为一个单独的掩码,然后每个掩码调用一次 slic
。您可以通过适当增加标签将多个分段合并为一个。
下面粘贴的是两个单独的屏蔽区域的具体示例(改编自您引用的 existing example):
import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage import color
from skimage import morphology
from skimage import segmentation
# Input data
img = data.immunohistochemistry()
# Compute a mask
lum = color.rgb2gray(img)
mask = morphology.remove_small_holes(
morphology.remove_small_objects(
lum < 0.7, 500),
500)
mask1 = morphology.opening(mask, morphology.disk(3))
# create a second mask as the inverse of the first
mask2 = ~mask1
segmented = np.zeros(img.shape[:-1], dtype=np.int64)
max_label = 0
# replace [mask2, mask1] with a list of any number of binary masks
for mask in [mask2, mask1]:
# maskSLIC result
m_slic = segmentation.slic(img, n_segments=100, mask=mask, start_label=1)
if max_label > 0:
# offset the labels by the current maximum label
m_slic += max_label
# add the label into the current combined segmentation
segmented += m_slic
# increment max label
max_label += m_slic.max()
# Display result
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
ax1, ax2, ax3, ax4 = ax_arr.ravel()
ax1.imshow(img)
ax1.set_title('Original image')
ax2.imshow(mask, cmap='gray')
ax2.set_title('Mask')
ax3.imshow(segmentation.mark_boundaries(img, m_slic))
ax3.contour(mask, colors='red', linewidths=1)
ax3.set_title('maskSLIC (mask1 only)')
ax4.imshow(segmentation.mark_boundaries(img, segmented))
ax4.contour(mask, colors='red', linewidths=1)
ax4.set_title('maskSLIC (both masks)')
for ax in ax_arr.ravel():
ax.set_axis_off()
plt.tight_layout()
plt.show()
我建议的基本方法是在上面的 for 循环中。大多数其他代码只是生成数据和绘图。