在图像上绘制多个区域 - imfreehand

时间:2014-05-05 01:21:58

标签: matlab roi

我想在图像上手动绘制多个区域,以创建绘制区域的二元蒙版(基础事实)。

我附加代码使用imfreehand为一个区域执行任务,但是一旦释放鼠标按钮,就会显示该单个区域的二进制掩码。有没有办法绘制多个区域,然后显示二进制掩码? (多次调用imfreehand可能无效,因为每个图像的区域数量会有所不同。)

h= imfreehand();

h = imfreehand(gca);
setColor(h,'red');

position = wait(h); 
BW = createMask(h);
figure,imshow(BW);
axis on;

感谢。

2 个答案:

答案 0 :(得分:2)

你可以循环直到你得到一个空面具 - 这将表明用户完成了所有面具的绘制 让sz成为输出掩码的所需大小,然后

totMask = false( sz ); % accumulate all single object masks to this one
h = imfreehand( gca ); setColor(h,'red');
position = wait( h );
BW = createMask( h );
while sum(BW(:)) > 10 % less than 10 pixels is considered empty mask
      totMask = totMask | BW; % add mask to global mask
      % you might want to consider removing the old imfreehand object:
      delete( h ); % try the effect of this line if it helps you or not.

      % ask user for another mask
      h = imfreehand( gca ); setColor(h,'red');
      position = wait( h );
      BW = createMask( h );
end
% show the resulting mask
figure; imshow( totMask ); title('multi-object mask');

答案 1 :(得分:0)

作为@Shai给出的答案的补充,你也可以使用Matlab中的input()函数来提示用户所需的掩码总数,或者只是在while循环中作为是/否的问题。 也就是说:

imshow(img,[]) %display image to determine number of masks needed
n = input('How many masks are needed? ')  %Ask user for number of masks needed
for i=1:n
    create multiple masks...
end

n=1;
while(n==1)
     create mask ...
     n = input('Need more masks? [0=No, 1=Yes] ')
end