Join disconnected terminal points of components in a binary image

조회 수: 6 (최근 30일)
Ankit Sahay
Ankit Sahay 2021년 1월 15일
댓글: Ankit Sahay 2021년 2월 25일
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:
Thereafter I used the answer given on this page to remove the cells having 2 neighbours:
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.
  댓글 수: 1
Ankit Sahay
Ankit Sahay 2021년 1월 16일
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
Image Analyst 2021년 1월 16일
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);
  댓글 수: 3
Image Analyst
Image Analyst 2021년 1월 20일
The issue is if you haved a blob that is only touching your main blob at one pixel where that one pixel is touching only at the diagonal tip. This is called "8-connected". The other situation is where the pixel touches along one of the 4 main sides of the square pixel and that is called "4-connected. At the diagonal touching point, the boundaries come to a point so essentially the width of the boundary there is zero and you have to decide if you want to include those 8-connected regions or not. It sounds like you don't want them. So to get rid of them you can use bwareafilt() to get rid of them based on either size, or number. So you could take the largest 4-connected blob:
binaryImage = bwareafilt(binaryImage, 1, 4);
Ankit Sahay
Ankit Sahay 2021년 2월 25일
I apologize for the lalte reply. This worked well. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by