我有一个相当简单的问题。我正在尝试使用 MATLAB 分割图像。我试过这个imageSegmenter
应用程序,一个带有 GUI 的工具箱。该工具似乎运行良好,尤其是当我使用具有几乎所有容差参数的“Flood Fill”选项时。
是否有洪水填充的功能(不是工具)形式?如果是,函数的名称是什么?该文档似乎不包括此信息。
该函数的grayconnected(I,row,column,tolerance)
作用,即 imageSegmeter-Toolbox 中的 Flood-Fill-Tool 的作用:用一个点[x,y]
(图像中的列/行索引)初始化,并从该点开始“填充”指定灰度值范围内的周围像素该tolerance
参数(左上在洪水填充GUI)。
实际上你只需要那一行(如果你有你的灰度值img
,一个初始化点row
,column
并选择了一个容差,例如12
):
%>>> this is where the magic happens <<<%
segmentation = grayconnected(img, row, column, 12);
为方便起见,请参阅下面的可视化代码片段,您可以在其中选择初始化。输入是一个彩色图像(如果它已经是灰色的,跳过rgb2gray
)。每个点对应的输出(一个分割掩码)i
在 中segmentations(:,:,i)
。您可以将这些单个分割掩码合并为一个或将它们分配给不同的对象。
请注意,这确实是一种非常基本的分割方法,如果您没有清晰的对比度(其中单个阈值操作可能已经在没有初始化的情况下为您提供良好的结果),则容易产生噪音并且很糟糕。您可以使用此初始分割进行细化,例如使用活动轮廓。
[img] = imread('test.jpg');
img = rgb2gray(img);
tolerance = 12; % default setting in imageSegmenter
%% >>>>>>>>>> GET INITIALIZATION POINTS <<<<<<<<<< %%
str = 'Click to select initialization points. Press ENTER to confirm.';
fig_sel = figure(); imshow(img);
title(str,'Color','b','FontSize',10);
fprintf(['\nNote: ' str '\n'...
'Pressing ENTER ends the selection without adding a final point.\n' ...
'Pressing BACKSPACE/DELETE removes the previously selected point.\n'] );
% select points in figure and close afterwards
[x, y] = getpts(fig_sel);
close(fig_sel);
%% >>>>>>>>>> PROCESS INITIALIZATION POINTS <<<<<<<<<< %%
if length(x) == 0
fprintf('\nError: No points specified. An initialization point is needed!');
else
segmentations = zeros([size(img) length(x)]);
fig_result = figure(); hold on;
for i = 1:length(x)
% confusing: y corresponds to row, x to column in img
column = ceil(x(i));
row = ceil(y(i));
%>>> this is where the magic happens <<<%
segmentations(:,:,i) = grayconnected(img,row,column,tolerance);
% show corresponding initialization point
subplot(1,2,1); imshow(img); hold on;
title('Active point (red)');
plot(x(i),y(i),'r.','MarkerSize',10); % active in red
plot(x(1:end ~= i),y(1:end ~= i),'b.','MarkerSize',5); % ... others in blue
hold off;
% ... with segmentation result
title('Segmentation result');
subplot(1,2,2); imshow(segmentations(:,:,i));
% click through results
waitforbuttonpress
end
close(fig_result);
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句