”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 在 Python 中使用标准化剪切 (NCut) 进行无监督图像分割的指南

在 Python 中使用标准化剪切 (NCut) 进行无监督图像分割的指南

发布于2024-11-08
浏览:525

A Guide to Unsupervised Image Segmentation using Normalized Cuts (NCut) in Python

介绍

图像分割在理解和分析视觉数据方面起着至关重要的作用,而归一化剪切(NCut)是一种广泛使用的基于图的分割方法。在本文中,我们将探索如何使用 Microsoft Research 的数据集在 Python 中应用 NCut 进行无监督图像分割,重点是使用超像素提高分割质量。
数据集概述
用于此任务的数据集可以从以下链接下载:MSRC 对象类别图像数据库。该数据集包含原始图像及其语义分割为九个对象类(由以“_GT”结尾的图像文件表示)。这些图像被分组为主题子集,其中文件名中的第一个数字指的是类别子集。该数据集非常适合试验分割任务。

问题陈述

我们使用 NCut 算法对数据集中的图像进行图像分割。像素级分割的计算成本很高,而且通常存在噪声。为了克服这个问题,我们使用 SLIC(简单线性迭代聚类)来生成超像素,它将相似的像素分组并减少问题的大小。为了评估分割的准确性,可以使用不同的指标(例如,并集交集、SSIM、兰德指数)。

执行

1。安装所需的库
我们使用 skimage 进行图像处理,使用 numpy 进行数值计算,使用 matplotlib 进行可视化。

pip install numpy matplotlib
pip install scikit-image==0.24.0
**2. Load and Preprocess the Dataset**

下载并提取数据集后,加载图像并进行地面实况分割:

wget http://download.microsoft.com/download/A/1/1/A116CD80-5B79-407E-B5CE-3D5C6ED8B0D5/msrc_objcategimagedatabase_v1.zip -O msrc_objcategimagedatabase_v1.zip
unzip msrc_objcategimagedatabase_v1.zip
rm msrc_objcategimagedatabase_v1.zip

现在我们准备开始编码了。

from skimage import io, segmentation, color, measure
from skimage import graph
import numpy as np
import matplotlib.pyplot as plt

# Load the image and its ground truth
image = io.imread('/content/MSRC_ObjCategImageDatabase_v1/1_16_s.bmp')
ground_truth = io.imread('/content/MSRC_ObjCategImageDatabase_v1/1_16_s_GT.bmp')

# show images side by side
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(image)
ax[0].set_title('Image')
ax[1].imshow(ground_truth)
ax[1].set_title('Ground Truth')
plt.show()

3.使用 SLIC 生成超像素并创建区域邻接图

我们在应用 NCut 之前使用 SLIC 算法来计算超像素。使用生成的超像素,我们基于平均颜色相似度构建区域邻接图(RAG):

from skimage.util import img_as_ubyte, img_as_float, img_as_uint, img_as_float64

compactness=30 
n_segments=100 
labels = segmentation.slic(image, compactness=compactness, n_segments=n_segments, enforce_connectivity=True)
image_with_boundaries = segmentation.mark_boundaries(image, labels, color=(0, 0, 0))
image_with_boundaries = img_as_ubyte(image_with_boundaries)
pixel_labels = color.label2rgb(labels, image_with_boundaries, kind='avg', bg_label=0

紧凑性控制形成超像素时像素的颜色相似度和空间接近度之间的平衡。它决定了对保持超像素紧凑(在空间方面更接近)与确保它们按颜色更均匀分组的重视程度。
较高的值:较高的紧凑度值会导致算法优先创建空间紧凑且大小均匀的超像素,而较少关注颜色相似性。这可能会导致超像素对边缘或颜色渐变不太敏感。
较低的值:较低的紧凑度值允许超像素在空间尺寸上变化更大,以便更准确地考虑颜色差异。这通常会导致超像素更紧密地遵循图像中对象的边界。

n_segments 控制 SLIC 算法尝试在图像中生成的超像素(或段)的数量。本质上,它设置了分割的分辨率。
较高的值:较高的 n_segments 值会创建更多的超像素,这意味着每个超像素会更小,分割会更细粒度。当图像具有复杂纹理或小对象时,这非常有用。
较低的值:较低的 n_segments 值会产生更少、更大的超像素。当您想要对图像进行粗分割,将较大的区域分组为单个超像素时,这非常有用。

4。应用标准化剪切 (NCut) 并可视化结果

# using the labels found with the superpixeled image
# compute the Region Adjacency Graph using mean colors
g = graph.rag_mean_color(image, labels, mode='similarity')

# perform Normalized Graph cut on the Region Adjacency Graph
labels2 = graph.cut_normalized(labels, g)
segmented_image = color.label2rgb(labels2, image, kind='avg')
f, axarr = plt.subplots(nrows=1, ncols=4, figsize=(25, 20))

axarr[0].imshow(image)
axarr[0].set_title("Original")

#plot boundaries
axarr[1].imshow(image_with_boundaries)
axarr[1].set_title("Superpixels Boundaries")

#plot labels
axarr[2].imshow(pixel_labels)
axarr[2].set_title('Superpixel Labels')

#compute segmentation
axarr[3].imshow(segmented_image)
axarr[3].set_title('Segmented image (normalized cut)')

5。评估指标
无监督分割的关键挑战是 NCut 不知道图像中类别的确切数量。 NCut 找到的分段数量可能超过实际的地面实况区域数量。因此,我们需要强大的指标来评估细分质量。

并集交集 (IoU) 是一种广泛使用的评估分割任务的指标,特别是在计算机视觉领域。它测量预测分割区域和地面真实区域之间的重叠。具体来说,IoU 计算预测分割和真实数据之间的重叠面积与其并集面积的比率。

结构相似性指数 (SSIM) 是一种用于通过比较两个图像的亮度、对比度和结构来评估图像感知质量的指标。

要应用这些指标,我们需要预测和地面实况图像具有相同的标签。为了计算标签,我们在地面上计算一个掩模,并在预测时为图像上找到的每种颜色分配一个 ID
然而,使用 NCut 进行分割可能会发现比地面实况更多的区域,这会降低准确性。

def compute_mask(image):
  color_dict = {}

  # Get the shape of the image
  height,width,_ = image.shape

  # Create an empty array for labels
  labels = np.zeros((height,width),dtype=int)
  id=0
  # Loop over each pixel
  for i in range(height):
      for j in range(width):
          # Get the color of the pixel
          color = tuple(image[i,j])
          # Check if it is in the dictionary
          if color in color_dict:
              # Assign the label from the dictionary
              labels[i,j] = color_dict[color]
          else:
              color_dict[color]=id
              labels[i,j] = id
              id =1

  return(labels)
def show_img(prediction, groundtruth):
  f, axarr = plt.subplots(nrows=1, ncols=2, figsize=(15, 10))

  axarr[0].imshow(groundtruth)
  axarr[0].set_title("groundtruth")
  axarr[1].imshow(prediction)
  axarr[1].set_title(f"prediction")
prediction_mask = compute_mask(segmented_image)
groundtruth_mask = compute_mask(ground_truth)

#usign the original image as baseline to convert from labels to color
prediction_img = color.label2rgb(prediction_mask, image, kind='avg', bg_label=0)
groundtruth_img = color.label2rgb(groundtruth_mask, image, kind='avg', bg_label=0)

show_img(prediction_img, groundtruth_img)

现在我们计算准确度分数

from sklearn.metrics import jaccard_score
from skimage.metrics import structural_similarity as ssim

ssim_score = ssim(prediction_img, groundtruth_img, channel_axis=2)
print(f"SSIM SCORE: {ssim_score}")

jac = jaccard_score(y_true=np.asarray(groundtruth_mask).flatten(),
                        y_pred=np.asarray(prediction_mask).flatten(),
                        average = None)

# compute mean IoU score across all classes
mean_iou = np.mean(jac)
print(f"Mean IoU: {mean_iou}")

结论

标准化剪切是一种强大的无监督图像分割方法,但它也面临着过度分割和调整参数等挑战。通过合并超像素并使用适当的指标评估性能,NCut 可以有效地分割复杂图像。 IoU 和 Rand 指数指标为分割质量提供了有意义的见解,尽管需要进一步细化才能有效处理多类场景。
最后,我的笔记本中提供了一个完整的示例。

版本声明 本文转载于:https://dev.to/sopralapanca/a-guide-to-unsupervised-image-segmentation-using-normalized-cuts-ncut-in-python-13pk?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3