【问题标题】:Matlab: Force watershed to segment into a specific number of segmentsMatlab:强制分水岭分割成特定数量的段
【发布时间】:2018-07-19 00:44:50
【问题描述】:

为了避免Matlab中的分水岭算法过度分割,我想强制算法分割成特定数量的段(在这里的例子中,算法自动分割成4,我希望它分割进入2)。有没有一种通用的方法来定义允许的输出段数?

我目前使用的代码:

% Load the image
grayscaleImg = imread('https://i.stack.imgur.com/KyatF.png');
white_in_current_bits = 65535;

% Display the original image
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
hold on;
imshow(grayscaleImg);
title('The origianl image');

% Binarize the image.
binaryImageElement = grayscaleImg < white_in_current_bits;

% Calculate the distance transform
D = -bwdist(~binaryImageElement);

% Find the regional minima of the distance matrix:
mask = imextendedmin(D,2);

%Display the mask on top of the binary image:
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
imshowpair(binaryImageElement,mask,'blend');
title('Blend of binary image and the regional minima mask');

%Impose the regional minima on the distance transform:
D2 = imimposemin(D,mask);

%Watershed the distance transform after imposing the regional minima:
Ld2 = watershed(D2);

%Display the binary image with the watershed segmentation lines:
bw3 = binaryImageElement;
bw3(Ld2 == 0) = 0;
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
imshow(bw3);
title('Binary image after watershedding');

【问题讨论】:

  • 您无法指定流域算法将找到的盆地数量。这直接取决于您的数据。但是,您可以合并生成的段以获得特定数量的段。您可以使用bwconncomp 列出段,然后根据某些规则(例如,按大小)合并它们。

标签: matlab image-processing image-segmentation watershed


【解决方案1】:

没有直接的方法来指定流域将产生的区域数量。流域将始终产生每个局部最小值的一个区域。但是您可以修改图像以减少局部最小值的数量。一种方法是H-minima transform。此函数会移除所有深度小于阈值的局部最小值。

我们的想法是迭代(这可能不会很快......)超过阈值,直到获得所需的区域数量。

% iterate over h, starting at 0
tmp = imhmin(D2,h);
Ld2 = watershed(tmp);
% count regions in Ld2, increase h and repeat

我刚刚注意到您在D2 中施加了最小值。您可以使用imextendedmin 确定这些最小值。这意味着您应用 H 最小值,找到生成的局部最小值,然后再次施加这些最小值。您不妨跳过这一步,直接应用 H 最小值变换。

【讨论】:

  • 谢谢克里斯。分水岭(至少在 Matlab 中)可以为每个局部最小值提供更多区域,至少在我尝试使用 4 的连通性时是这样。如果不是默认使用 8 的连通性的 Ld2 = watershed(D2),您可以尝试 Ld2 = watershed( D2,4) 你会看到即使最小值的数量相同,它也会产生更多的段。有什么想法吗?
  • 最小值的数量取决于连接性。在 4 连通世界中,具有相同值的两个像素可以断开连接,从而形成两个最小值。在一个 8 连接的世界中,同样的两个像素可以连接起来,形成一个最小值。
  • 在测试我的代码后,我意识到 imposemin 函数还需要连接性作为输入变量,所以我得到的过度分割是由于 imposemin 中的 8(默认值)连接性和连接性的混合4 在分水岭。我最终通过使用函数 getpts 并从 bwdist 后跟 imextendedmin 建议的最小值中选择来手动控制段数。这样我可以确保只使用 2 个最小值,因此只生成 2 个段(并且如前所述 imimposemin 和 watershed 必须获得相同的连接值。
猜你喜欢
  • 1970-01-01
  • 2011-11-17
  • 2012-07-11
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2019-08-06
  • 2018-10-26
相关资源
最近更新 更多