# Join disconnected terminal points of components in a binary image

33 views (last 30 days)
Ankit Sahay on 15 Jan 2021
Commented: Ankit Sahay on 25 Feb 2021
HI everybody!
I am working on flame edge detection. What I need is two single loops to depict two different flame edges. I have attached the binary image of my flame edge below: (Please open the image in a different window to view it properly)
Let's talk about the left half of the image to keep things simple. If you look at the left half of the image, you will see that there isn't a single loop, as the edge touches itself at different points. A magnified image is attached below: My primary objective is to remove the appropriate cells so that I can have one closed loop. I might have to remove some parts of the origibal edge, but I think that's okay. To accomplish this, I complemented the original binary image, and used the following code to specify the objects that have 4-connectivity (Please correct me here if I am wrong):
complement = imcomplement(fb2); % fb2 is the origibal binary image matrix
BW2 = bwareafilt(complement,3,4);
BW3 = imcomplement(BW2);
imshow(BW3);
fb2 is the binary image matrix file, attached with the question. One of the problematic areas (from the left half of the above image) is shown below: numberNeighboringPixels = 2; % Can also be 1 or 3
% Using Lookup Table
lut = makelut(@(x)sum(x(:))==(numberNeighboringPixels+1),3);
BW3_LUT = bwlookup(BW3,lut) & BW3;
%%Plot and show the results are the same figure
% subplot(1,2,1);
% imshow(BW3); title('Original Image');
% subplot(1,2,2);
imshow(BW3_LUT);
BW3_LUT gives me the following image: Unfortunately, the above code causes discontinuities in the edge. Magnifying the discontinuity inn the left loop of the above figure: This means that instead of 2 disconnected components (corrsponding to two closed loops) that I want in my image, I have n>2 disconnected components; in this case: 8. I have color code all the components for better visualization: A magnified version of the color coded discontinuity: I then thought that if I am able to find the terminal points of the components, I might be able to connect them. I use the following code to find the endpoints (taken from this answer):
BW3_LUT = bwareaopen(BW3_LUT,2); % to remove isolated cells
[L, Num] = bwlabel(BW3_LUT);
for K = 1 : Num
[epr{K}, epc{K}] = find( bwmorph(L == K, 'endpoints') );
end
Although I now have the end points, the components are not labeled according to the sequence I want to join them. Also, I don't know how to join the end points. I can use the Bresenham's Line Algorithm, but I can use that only after I can sequence the components properly (through code), which means "n-th component connects to n+1-th component. I can separate the two halves of the original binary image in order to do this.
In the end, my main objective is to have one single loop for one flame edge. I might be using a wrong approach, but this is what I can think of right now. I am losing some of the "wrinkled" features of the edges, but that is okay as long as I don't lose all of them. Please help me with this issue. Let me know if there are any more clarifications I need to make.
Ankit Sahay on 16 Jan 2021
I can add a rule that says "I'd like to connect any endpoints that are within a separation of 3 pixels with a line connecting them".

Image Analyst on 16 Jan 2021
What I would try is to draw a white line along the bottom edge only then fill the shapes, and then call bwboundaries()
lastLine = binaryImage(end, :); % Save original last line.
binaryImage(end, :) = true;
binaryImage = imfill(binaryImage, 'holes');
% Restore last line
binaryImage(end, :) = lastLine;
boundaries = bwboundaries(binaryImage);
Ankit Sahay on 25 Feb 2021
I apologize for the lalte reply. This worked well. Thank you!

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!