image - 如何从此图像中删除此非红色和非圆形对象?把红色变成白色,其余的变成黑色

标签 image matlab image-processing

enter image description here 我需要红色圆圈为白色,其余为黑色。与二值图像完全相同。我可以只过滤掉红色,然后做一些事情,但如何只包含圆圈? 我尝试了很多东西,但就是行不通。我猜是因为 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)

enter image description here

但如果您使用的是旧版本,请不要担心。 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 的版本中运行代码,请将分配 statskeepMask 的行替换为以下行:

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/

相关文章:

c# - 如何使用 C# 捕获特定 url 的图像?

html - 如何在 html5 中显示 SVG 圆圈内的图像?

performance - Matlab 中的高效分类

matlab - 如何用Matlab进行车牌定位?

r - 在 R 中使用 EBImage 进行图像处理循环

image - 从Hadoop sequenceFile获取原始图像时出错

asp.net - ASP.NET 中图像的 ImgUrl 属性的预处理器指令存在问题

c - simulink 编码器 c 文件到 simulink 模型

r - 为什么 "image_read_svg"在 R 中无法工作?

opencv - 如何使用 OpenCV 绘制 3D 坐标轴进行面部姿态估计?