【发布时间】:2017-04-10 20:19:52
【问题描述】:
我有一个相当简单的问题。我正在尝试使用 MATLAB 分割图像。我尝试了imageSegmenter 应用程序,这是一个带有 GUI 的工具箱。该工具似乎运行良好,尤其是当我使用带有几乎任何公差参数的“Flood Fill”选项时。
是否有洪水填充的功能(不是工具)形式?如果有,该功能的名称是什么?文档似乎没有包含这些信息。
【问题讨论】:
我有一个相当简单的问题。我正在尝试使用 MATLAB 分割图像。我尝试了imageSegmenter 应用程序,这是一个带有 GUI 的工具箱。该工具似乎运行良好,尤其是当我使用带有几乎任何公差参数的“Flood Fill”选项时。
是否有洪水填充的功能(不是工具)形式?如果有,该功能的名称是什么?文档似乎没有包含这些信息。
【问题讨论】:
函数grayconnected(I,row,column,tolerance) 的作用,imageSegmeter-Toolbox 中的 Flood-Fill-Tool 的作用:使用点 [x,y](图像中的列/行索引)初始化并从那里开始“洪水”周围由tolerance 参数指定的给定灰度值范围内的像素(Flood Fill GUI 中的左上角)。
实际上你只需要那一行(如果你有你的灰度值img,一个初始化点row,column 并选择了一个容差,例如12):
%>>> this is where the magic happens <<<%
segmentation = grayconnected(img, row, column, 12);
为方便起见,请参阅下面带有可视化的代码 sn-p,您可以在其中选择初始化。输入是彩色图像(如果它已经是灰色的,请跳过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
【讨论】: