【问题标题】:How to remove this NON RED AND NON CIRCLE objects from this image? And make reds white and rest is black如何从此图像中删除此非红色和非圆形对象?使红色变为白色,其余为黑色
【发布时间】:2021-04-03 12:48:49
【问题描述】:

我需要红色圆圈白色,其余为黑色。完全像二进制图像。我可以只过滤掉红色,然后做一些事情,但如何只包括圆圈? 我尝试了很多东西,但它不起作用。我猜是因为 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)

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:

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

    % 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
    

    让我们开始吧:

    subplot(3,1,1); imagesc(I);
    subplot(3,1,2); imagesc(I_only_red);
                    viscircles(centers , radii ,'EdgeColor','b');
    
    subplot(3,1,3); imagesc(I_only_red.*mask);
    

    【讨论】:

    • 对不起,我忘了补充,我必须将红色圆圈设为白色,其余的设为黑色。像二进制图像一样只有黑白。
    • 如果您使用imshow 而不是imagesc,您可能会看到黑白。否则,只需将绘图末尾的颜色图更改为 colormap(gray)
    【解决方案2】:

    从 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实际上描述了它是如何计算的:

    '循环性'
    对象的圆度,作为带字段的结构返回 圆。该结构包含每个对象的循环值 在输入图像中。圆度值计算为 (4*Area*pi)/(Perimeter^2)。对于一个完美的圆,圆度值 为 1. 输入必须是标签矩阵或具有连续性的二值图像 地区。如果图像包含不连续的区域,regionprops 返回意外结果。

    注意

    不建议将圆形用于非常小的对象,例如 3×3 正方形。对于这种情况,结果可能会超过循环值 一个完美的圆圈。

    因此,如果您在 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;
    

    【讨论】:

    • 这正是我所需要的,谢谢。顺便说一句,不明白你的代码部分的改变。我可以像第一个那样做,但我也会为旧版本做,所以我把那个代码放在哪里?
    • @Stayheuh 我很高兴它有所帮助。对不起那部分,我不够清楚。我编辑了它,希望现在清楚了。
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多