在二进制图像中连接不相邻的边缘

3

我对一个立方体的图片执行了一些操作,得到了一个由立方体边缘组成的二进制图像,其中某些地方是不连通的。下面是我得到的图像:

enter image description here

我想连接这些边以使其成为一个封闭的形状。我尝试了以下方法:

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

但这只会使图像变得更加粗糙,而不会连接边缘。我也尝试过bwmorph()和其他各种函数,但都没有起作用。请问有人能建议任何可以连接边缘的函数或步骤吗?谢谢。

2个回答

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

调试图片 -

输入图像描述

输入图像描述

输入图像描述

输入图像描述


1
@Matt 嗯,这段代码应该适用于图像中的凸形单个斑点,并且通过修改可以扩展到单个图像中的多个斑点(我认为)。这回答了你的问题吗? - Divakar
我在问题中发布的图像是在我对其执行Sobel操作并将其转换为二进制后获得的。该图像下面有一个阴影,因此在将其转换为二进制图像之前,我进行了阈值处理。但是每个图像的阈值都不同。是否有一种方法可以设置适用于一般情况的恒定阈值? - Matte
@Matt 如果这个解决方案对你有帮助,请不要忘记通过点击旁边的空心复选标记来接受它!谢谢! - Divakar
这对我帮助很大。我已经连续一周,每天24小时都在努力连接这些边界!谢谢 :) - Matte
@Matt 哇,那真是很长的时间啊!祝你在新职位上好运! - Divakar
显示剩余3条评论

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

非常酷!做得很好。 - Divakar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接