5

如你所见,我有形状和它们的白色边界。我想用白色填充形状。

输入是: 在此处输入图像描述

我想得到这个输出: http://s12.postimage.org/z3txnlb3f/untitled33.png

有人可以帮我解决这个代码吗?它不会将黑色椭圆更改为白色。非常感谢 :]]

I = imread('untitled4.bmp');
Ibw = im2bw(I);
CC = bwconncomp(Ibw); %Ibw is my binary image
stats = regionprops(CC,'pixellist');

% pass all over the stats
for i=1:length(stats),
size = length(stats(i).PixelList);
% check only the relevant stats (the black ellipses)
if size >150 && size < 600 
    % fill the black pixel by white    
    x = round(mean(stats(i).PixelList(:,2)));
    y = round(mean(stats(i).PixelList(:,1)));
    Ibw = imfill(Ibw, [x, y]);
end;
end;

imshow(Ibw);
4

2 回答 2

3

您的代码可以改进和简化如下。首先,求反Ibw并使用BWCONNCOMP查找 4 连通分量将为您提供每个黑色区域的索引。其次,按连接区域中的像素数对连接区域进行排序,然后选择除最大的两个之外的所有区域,将为您提供所有较小圆形区域的索引。最后,可以收集这些较小区域的线性索引并使用白色填充这些区域。这是代码(相当短,不需要任何循环):

I = imread('untitled4.bmp');
Ibw = im2bw(I);

CC = bwconncomp(~Ibw, 4);
[~, sortIndex] = sort(cellfun('prodofsize', CC.PixelIdxList));

Ifilled = Ibw;
Ifilled(vertcat(CC.PixelIdxList{sortIndex(1:end-2)})) = true;
imshow(Ifilled);

这是生成的图像:

在此处输入图像描述

于 2012-04-07T16:26:39.897 回答
1

如果您的图像都是黑白的,并且您拥有图像处理工具包,那么这看起来就像您需要的那样:http: //www.mathworks.co.uk/help/toolbox/images/ref/imfill.html

就像是:

imfill(image, [startX, startY])

其中 startX, startY 是您要填充的区域中的一个像素。

于 2012-04-07T12:35:41.613 回答