如何使用 imfreehand 绘制多个二进制区域

How to draw multiple binary regions using imfreehand

我正在尝试使用 imfreehand 创建一个用户控制的二进制掩码。我正在尝试允许用户 select 多个区域而不是单个区域。一旦在图像 tophatImage 上绘制了一个区域,selected 区域的二进制版本就会出现在图像 totalBinary 上。到目前为止一切顺利。

问题是,当需要绘制下一个区域时,tophatImage中先前绘制的区域消失了。这可能是个问题,因为用户无法看到 s/he 绘制了哪些区域,也无法删除绘制的区域。 totalBinary 中先前绘制区域的二进制版本保持不变,因此这不是问题。

我认为问题出在 for-loop 中的 subplot(1,2,1); imshow(tophatImage) 行。每次需要绘制新区域时都会重置图像 tophatImage。正如您在下面的第一张图片中看到的那样,没有像第三张图片那样保留绘制的图像。

原错误代码

subplot(1,2,1); imshow(tophatImage)

hFH = imfreehand();
binaryImage = createMask(hFH);
totalBinary = false(size(histogramEq));

for k = 1:5
    totalBinary = totalBinary | binaryImage;
    subplot(1,2,2); imshow(totalBinary); drawnow

    subplot(1,2,1); imshow(tophatImage)
    hFH = imfreehand();
    binaryImage = createMask(hFH);
end

更正代码

使用更正后的代码,我怎样才能做到当从 tophatImage 中删除一个区域时,它会更新到 totalBinary

for k = 1:5
    totalBinary = totalBinary | binaryImage;
    subplot(1,2,2); imshow(totalBinary); drawnow

    subplot(1,2,1); %imshow(tophatImage) % <-- Remove this
    hFH = imfreehand();
    binaryImage = createMask(hFH);
end

非常简单的错误。在你的循环中,在你调用 subplot(1,2,1); 之后移除对 imshow(tophatImage); 的调用。这是擦除图形的内容并仅显示图像本身。如果你想让你用imfreehand绘制的等高线保留下来,就不要调用imshow。对于 imfreehand,等高线应该保持不变,直到您关闭图形或更改此 subplot 中的内容。

为了绝对确定你知道我在说什么:

for k = 1:5
    totalBinary = totalBinary | binaryImage;
    subplot(1,2,2); imshow(totalBinary); drawnow

    subplot(1,2,1); %imshow(tophatImage) % <-- Remove this
    hFH = imfreehand();
    binaryImage = createMask(hFH);
end