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

问题描述 投票:1回答:1

为了避免在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');
matlab image-processing image-segmentation watershed
1个回答
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变换。

© www.soinside.com 2019 - 2024. All rights reserved.