Matlab:强制分水岭划分为特定数量的分段

时间:2018-02-08 12:32:17

标签: matlab image-processing image-segmentation watershed

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

enter image description here enter image description here

我目前使用的代码:

% 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 binnary 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 segemtation lines:
bw3 = binaryImageElement;
bw3(Ld2 == 0) = 0;
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
imshow(bw3);
title('Binary image after watershedding');

1 个答案:

答案 0 :(得分:0)

没有直接的方法来指定流域将产生的区域数量。流域将始终按地方最小值生成一个区域。但您可以修改图像以减少局部最小值。一种方法是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 minima,找到生成的局部最小值,然后再次施加。您也可以跳过此步骤,直接应用H minima变换。