Image 如何从此图像中删除此非红色和非圆形对象?把红色变成白色,剩下的是黑色

Image 如何从此图像中删除此非红色和非圆形对象?把红色变成白色,剩下的是黑色,image,matlab,image-processing,Image,Matlab,Image Processing,我需要红色的圆圈,白色的,其余的都是黑色的。就像二值图像一样。我可以只过滤红色,然后做一些事情,但如何只包括圆圈? 我试过很多方法,但都不管用。我猜是因为MATLAB的函数不受欢迎。因为这些话题很多都已经有10年的历史了。我试过这些: 我只差这么一点点就完成了这个代码,但它仍然会留下正方形和六边形。我想让他们走,但不知道怎么走。我试着增加或减少minExtend,但没有成功 I=imread('circlesColored.png'); % Get the image as a b&

我需要红色的圆圈,白色的,其余的都是黑色的。就像二值图像一样。我可以只过滤红色,然后做一些事情,但如何只包括圆圈? 我试过很多方法,但都不管用。我猜是因为MATLAB的函数不受欢迎。因为这些话题很多都已经有10年的历史了。我试过这些:

我只差这么一点点就完成了这个代码,但它仍然会留下正方形和六边形。我想让他们走,但不知道怎么走。我试着增加或减少minExtend,但没有成功

I=imread('circlesColored.png');
% Get the image as a b&w indexed (non-rgb) image
R=I(:,:,1);
R=(R>35&R<255);
imshow(R);
bwareaopen(I, 50);
 BW = gray2ind(R, 2);
%Calculate its connected regions
L = bwlabel(BW); % Not using bwconncomps() for older version users
stats = regionprops(L,'Extent','Area');
%Find the ones that are like a circle
minExtent = 0.75;
keepMask = [stats.Extent]>minExtent;
%Extract the image of circles only and display
BWcircles = ismember(L, find(keepMask));
BWnonCircles = BW & ~BWcircles;
%Show the circles
figure, imshow(BWcircles)
I=imread('circlescared.png');
%将图像作为黑白索引(非rgb)图像获取(&w)
R=I(:,:,1);
R=(R>35&40);
%仅提取圆的图像并显示
BWcircles=ismember(L,find(keepMask));
BWnonCircles=BW&~BW圆;
%显示圆圈
图,imshow(圆形)

这里有一个稍微不同的方法:

% remove non-red
thr=100;
I_only_red= I(:,:,1)>thr & I(:,:,2)<thr & I(:,:,3)<thr ;

%remove non circles: we first find the circles in the image
[centers, radii, metric] = imfindcircles(I_only_red,[40 120]);

% and make a mask according to their centers and radii:
[x y]=meshgrid(1:size(I,2),1:size(I,1));
mask=zeros(size(I_only_red));

for n=1:numel(radii)
    mask = mask | (x-centers(n,1)).^2+(y-centers(n,2)).^2<=radii(n).^2;
end

自R2019a起,
'Circularity'
属性添加到
regionprops
。我修改了您的代码以获得:

close all; clc; clear variables;
I=imread('2JxCA.png');
% Get the image as a b&w indexed (non-rgb) image
R=I(:,:,1);
R=(R>35&R<255);
imshow(R);
%Calculate its connected regions
L = bwlabel(R); % Not using bwconncomps() for older version users
stats = regionprops(L,'Circularity'); 
%Find the ones that are like a circle
keepMask = [stats.Circularity]>.99;
%Extract the image of circles only and display
BWcircles = ismember(L, find(keepMask));
BWnonCircles = R & ~BWcircles;
%Show the circles
figure, imshow(BWcircles)

很抱歉,我忘记添加了,我必须将红色圆圈设置为白色,其余部分设置为黑色。就像二值图像一样,只有黑色和白色。如果使用
imshow
而不是
imagesc
,您可能会看到黑色和白色。否则,只需将绘图末尾的颜色贴图更改为
颜色贴图(灰色)
这正是我所需要的,谢谢。顺便说一句,我不明白“更改你的代码”部分。我可以像第一个一样做,但我也要为旧版本做,所以我应该把代码放在哪里?@Stayheuh我很高兴它能帮上忙。很抱歉,这部分我不够清楚。我编辑了它,希望现在它清楚了。
close all; clc; clear variables;
I=imread('2JxCA.png');
% Get the image as a b&w indexed (non-rgb) image
R=I(:,:,1);
R=(R>35&R<255);
imshow(R);
%Calculate its connected regions
L = bwlabel(R); % Not using bwconncomps() for older version users
stats = regionprops(L,'Circularity'); 
%Find the ones that are like a circle
keepMask = [stats.Circularity]>.99;
%Extract the image of circles only and display
BWcircles = ismember(L, find(keepMask));
BWnonCircles = R & ~BWcircles;
%Show the circles
figure, imshow(BWcircles)
stats = regionprops(L,'Area', 'Perimeter'); 
%Find the ones that are like a circle
Circularity = (4*[stats(:).Area]*pi)./([stats(:).Perimeter].^2);
keepMask = Circularity>.99;