Matlab使用imfreehand绘制多个ROI,并使用ESC键退出

Matlab使用imfreehand绘制多个ROI,并使用ESC键退出,matlab,Matlab,我试图使用Matlab中的imfreehand函数来创建多个ROI。在用户选择了他们需要的足够的ROI后,他们可以通过按ESC键来停止。这是我的代码,但上面有一个错误 Error: Expected one output from a curly brace or dot indexing expression, but there were 0 results. 有人能帮我指出问题吗?代码从此处修改 谢谢 I = imread('pout.tif'); totMask = zeros(s

我试图使用Matlab中的imfreehand函数来创建多个ROI。在用户选择了他们需要的足够的ROI后,他们可以通过按ESC键来停止。这是我的代码,但上面有一个错误

Error: Expected one output from a curly brace or dot indexing expression, but there were 0 results.
有人能帮我指出问题吗?代码从此处修改

谢谢

I = imread('pout.tif');

totMask = zeros(size(I)); % accumulate all single object masks to this one
f = figure('CurrentCharacter','a');
imshow(I)

h = imfreehand( gca ); setColor(h,'green');
position = wait( h );
BW = createMask( h );
while double(get(f,'CurrentCharacter'))~=27 
      totMask = totMask | BW; % add mask to global mask  
      % ask user for another mask
      h = imfreehand( gca ); setColor(h,'green');
      position = wait( h );
      BW = createMask( h );
      pause(.1)
end
% show the resulting mask
figure; imshow( totMask ); title('multi-object mask');

问题是,当您按下Esc键时,
imfreehand
工具退出并返回一个空的h。因此你的
setColor(h,'green')失败。
此外,还应添加一个
totMask=totMask | BW在循环中定义BW后,否则将丢失最后一个ROI

试试这个:

totMask = zeros(size(I)); % accumulate all single object masks to this one
f = figure('CurrentCharacter','a');
imshow(I)

h = imfreehand( gca ); setColor(h,'green');
position = wait( h );
BW = createMask( h );
totMask = totMask | BW; % add mask to global mask  
while double(get(f,'CurrentCharacter'))~=27 
    % ask user for another mask
    h = imfreehand( gca ); 
    if isempty(h)
      % User pressed ESC, or something else went wrong
      continue
    end
    setColor(h,'green');
    position = wait( h );
    BW = createMask( h );
    totMask = totMask | BW; % add mask to global mask  
    pause(.1)
end
% show the resulting mask
figure; imshow( totMask ); title('multi-object mask');
还请注意,最后我使用的是
imagesc
而不是
imshow
:这将在0和1之间缩放输出图像颜色,正确显示您的ROI