我需要红色圆圈为白色,其余为黑色。与二值图像完全相同。我可以只过滤掉红色,然后做一些事情,但如何只包含圆圈? 我尝试了很多东西,但就是行不通。我猜是因为 MATLAB 已弃用的函数。因为其中许多主题已经有至少 10 年历史了。我尝试过这些:
https://nl.mathworks.com/help/images/ref/bwareaopen.html
https://nl.mathworks.com/matlabcentral/answers/19507-remove-non-circle-objects-from-image
我对这段代码的了解非常接近,但它仍然留下了正方形和六边形。我想让它们消失,但不知道怎么做。我尝试增加 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)
最佳答案
从 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)
但如果您使用的是旧版本,请不要担心。 Docs实际上描述了它的计算方式:
'Circularity'
Roundness of objects, returned as a struct with field Circularity. The struct contains the circularity value for each object in the input image. The circularity value is computed as(4*Area*pi)/(Perimeter^2)
. For a perfect circle, the circularity value is 1. The input must be a label matrix or binary image with contiguous regions. If the image contains discontiguous regions, regionprops returns unexpected results.Note
Circularity is not recommended for very small objects such as a 3-by-3 square. For such cases, the results might exceed the circularity value for a perfect circle.
因此,如果您在早于 R2019a 的版本中运行代码,请将分配 stats
和 keepMask
的行替换为以下行:
stats = regionprops(L,'Area', 'Perimeter');
%Find the ones that are like a circle
Circularity = (4*[stats(:).Area]*pi)./([stats(:).Perimeter].^2);
keepMask = Circularity>.99;
关于image - 如何从此图像中删除此非红色和非圆形对象?把红色变成白色,其余的变成黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65448965/