连接二进制图像中的不相交边 [英] Connect disjoint edges in binary image

查看:130
本文介绍了连接二进制图像中的不相交边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对立方体的图像执行了一些操作,我获得了立方体边缘的二进制图像,这些图像在某些地方断开连接。我获得的图像如下所示:

I performed some operations on an image of a cube and I obtained a binary image of the edges of the cube which are disconnected at some places.The image I obtained is shown below:

我想加入双方使其成为一个封闭的数字。我尝试了以下内容:

I want to join the sides to make it a closed figure.I have tried the following:

BW = im2bw(image,0.5);                   
BW = imdilate(BW,strel('square',5));   
figure,imshow(BW);

但这只会使图像变粗。它没有连接边缘。我也试过bwmorph()和其他各种功能,但它不工作。任何人都可以建议任何连接边缘的功能或步骤?谢谢

But this only thickens the image.It does not connect the edges.I have also tried bwmorph() and various other functions, but it is not working.Can anyone please suggest any function or steps to connect the edges? Thank you

推荐答案

这可能是一种方法 -

This could be one approach -

%// Read in the input image
img = im2bw(imread('http://i.imgur.com/Bl7zhcn.jpg'));

%// There seems to be white border, which seems to be non-intended and
%// therefore could be removed
img = img(5:end-4,5:end-4);

%// Thin input binary image, find the endpoints in it and connect them
im1 = bwmorph(img,'thin',Inf);
[x,y] = find(bwmorph(im1,'endpoints'));
for iter = 1:numel(x)-1
    two_pts = [y(iter) x(iter) y(iter+1) x(iter+1)];
    shapeInserter = vision.ShapeInserter('Shape', 'Lines', 'BorderColor', 'White');
    rectangle = int32(two_pts);
    im1 = step(shapeInserter, im1, rectangle);
end
figure,imshow(im1),title('Thinned endpoints connected image')

%// Dilate the output image a bit
se = strel('diamond', 1);
im2 = imdilate(im1,se);
figure,imshow(im2),title('Dilated Thinned endpoints connected image')

%// Get a convex shaped blob from the endpoints connected and dilate image
im3 = bwconvhull(im2,'objects',4);
figure,imshow(im3),title('Convex blob corresponding to previous output')

%// Detect the boundary of the convex shaped blob and 
%// "attach" to the input image to get the final output
im4 = bwboundaries(im3);
idx = im4{:};

im5 = false(size(im3));
im5(sub2ind(size(im5),idx(:,1),idx(:,2))) = 1;

img_out = img;
img_out(im5==1 & img==0)=1;
figure,imshow(img_out),title('Final output')

调试图像 -

这篇关于连接二进制图像中的不相交边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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