gpt4 book ai didi

Matlab 用白色填充形状

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:03 25 4
gpt4 key购买 nike

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

输入是: enter image description here

我想得到这个输出: 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);

最佳答案

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

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

这是生成的图像:

enter image description here

关于Matlab 用白色填充形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com