0

I was trying to use imfreehand function in Matlab to create multiply ROI. After the users choose enough ROI they need, they can stop it by hit the ESC key. Here is my code but it has an error on it.

Error: Expected one output from a curly brace or dot indexing expression, but there were 0 results.

Can someone help me and point out the problem? The code is modified from here

Draw multiple regions on an image- imfreehand

Thanks

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');
SimaGuanxing
  • 673
  • 2
  • 10
  • 29

1 Answers1

1

The problem is that when you hit Esc, the imfreehand tool quits and returns an empty h. Therefore your setColor(h,'green'); fails. Also, you should add a totMask = totMask | BW; AFTER you have defined BW within the loop, otherwise you will lose the last ROI.

Try this:

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');

Also note that at the end I am using imagesc instead of imshow: this will scale the output image colors between 0 and 1, properly showing your ROIs.

Zep
  • 1,541
  • 13
  • 21