在MATLAB中填充两个连接组件之间的区域

use*_*792 7 matlab image-processing fill

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

图片描述

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

在此输入图像描述

我发现的唯一的东西就是imfill函数,但由于我丢失了内部数据(例如9的内圈),因此这并不是很有用.

Amr*_*mro 6

另一种可能性是使用BWBOUNDARIES函数,它具有:

跟踪对象的外部边界以及这些对象内部的孔的边界

该信息包含在第四个输出中A,这是一个表示父子孔依赖关系的邻接矩阵.

%# read binary image
bw = imread('SUvif.png');

%# find all boundaries
[B,L,N,A] = bwboundaries(bw, 8, 'holes');

%# exclude inner holes
[r,~] = find(A(:,N+1:end));        %# find inner boundaries that enclose stuff
[rr,~] = find(A(:,r));                      %# stuff they enclose
idx = setdiff(1:numel(B), [r(:);rr(:)]);    %# exclude both
bw2 = ismember(L,idx);                      %# filled image

%# compare results
subplot(311), imshow(bw), title('original')
subplot(312), imshow( imfill(bw,'holes') ), title('imfill')
subplot(313), imshow(bw2), title('bwboundaries')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


And*_*ein 5

问题是如何区分孔和数字。一种可能的临时解决方案是通过内部像素的面积过滤它们。

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
Run Code Online (Sandbox Code Playgroud)

步骤(1)之后

在此处输入图片说明

步骤(2)之后

在此处输入图片说明