在MATLAB中填充两个连接组件之间的区域 [英] Fill area between two connected components in MATLAB

查看:237
本文介绍了在MATLAB中填充两个连接组件之间的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表MATLAB中数字的二进制图像:

I have a binary image that represents a number in MATLAB:

我想填写所有数字。期望的结果是:

I'd like to fill all the digits. The desired result is:

我发现的唯一一件事就是 imfill 函数,但是因为我丢失了,所以这并不是很有用我的内部数据(例如9的内圈)。

The only thing I found was the imfill function, but that wasn't really helpfull since I've lost my inner data (the 9's inner circle for example).

推荐答案

问题是如何区分孔和数字。一个可能的临时解决方案是按里面的像素区域过滤它们。

The problem is how to distinguish the holes from the digits. A possible ad hoc solution is filtering them by the area of the pixels inside.

function SolveSoProblem()

    I = imread('http://i.stack.imgur.com/SUvif.png');

    %Fill all the holes 
    F = imfill(I,'holes');

    %Find all the small ones,and mark their edges in the image
    bw = bwlabel(I);
    rp = regionprops(bw,'FilledArea','PixelIdxList');
    indexesOfHoles = [rp.FilledArea]<150;   
    pixelsNotToFill = vertcat(rp(indexesOfHoles).PixelIdxList); 
    F(pixelsNotToFill) = 0;
    figure;imshow(F);

    %Remove the inner area
    bw1 = bwlabel(F,4);
    rp = regionprops(bw1,'FilledArea','PixelIdxList');
    indexesOfHoles1 = [rp.FilledArea]<150;
    pixelListToRemove = vertcat(rp(indexesOfHoles1).PixelIdxList);
    F(pixelListToRemove) = 0;

    figure;imshow(F);
end

步骤(1)后

步骤(2)之后:

这篇关于在MATLAB中填充两个连接组件之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆