连接二进制图像中的不相交边

时间:2015-02-04 12:23:46

标签: image matlab image-processing

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

enter image description here

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

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

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

2 个答案:

答案 0 :(得分:8)

这可能是一种方法 -

%// 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')

调试图像 -

enter image description here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:2)

我使用上面的代码编写了下面的代码。我没有在很多图像上测试它,它可能没有上面那样高效,但它执行速度比较快。所以我想我会把它作为解决方案发布

I = imread('image.jpg');  % your original image
I=im2bw(I);
figure,imshow(I)
I= I(5:end-4,5:end-4);
im1 = bwmorph(I,'thin',Inf);
[x,y] = find(bwmorph(im1,'endpoints'));
for iter = 1:numel(x)-1
im1=linept(im1, x(iter), y(iter), x(iter+1), y(iter+1));
end
im2=imfill(im1,'holes');
figure,imshow(im2);
BW = edge(im2);
figure,imshow(BW);
se = strel('diamond', 1);
im3 = imdilate(BW,se);
figure,imshow(im3);

最终结果如下: cube

我从这里获得了“linept”功能:http://in.mathworks.com/matlabcentral/fileexchange/4177-connect-two-pixels