【发布时间】: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