Connect disjoint edges in binary image

后端 未结 2 904
醉酒成梦
醉酒成梦 2021-01-20 02:53

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

2条回答
  •  庸人自扰
    2021-01-20 03:25

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

    Debug images -

    enter image description here

    enter image description here

    enter image description here

    enter image description here

提交回复
热议问题