Image processing for determining endpoints on a line

9 views (last 30 days)
Hello,
I am looking to calculate the distance between endpoints of several skeletonized images. However, at the moment, the bwmorph function identifies endpoints in the skeletonized image that I am not interested in. My code for identifying the endpoints is below, and three examples of BW files are attached in .mat format.
imshow(BW)
BW2 = bwpropfilt(BW, 'perimeter', 1);
skel = bwskel(BW2);
imshow(skel)
[I,J]=find(bwmorph(skel,'endpoints'));
hold on
scatter(J, I)
I want to refine this code so that only the endpoints that I am interested in are identified. The endpoints that I am interested in are the ones at the true ends of the line. An image of the endpoints that are identified for BW_3 with my current code is shown below. As can be seen, far too many endpoints are found.
I have attempted to pass a minimum branch length into the bwskel function but this does not work for all of my images.
Any help with this would be appreciated.

Accepted Answer

DGM
DGM on 12 Feb 2024
Edited: DGM on 12 Feb 2024
bwskel() will almost always leave branches on the skeleton. You have to get rid of them. While you can use the minbranchlength option in the call to bwskel(), the result will usually still have small unpruned branches and spurs, depending on the shape of the skeleton near the corresponding branchpoint. There are other ways to get rid of them, but this is one. It's probably not the most efficient, but it's an example.
load BW_2.mat
BW2 = bwpropfilt(BW, 'perimeter', 1);
BW2 = imfill(BW2,'holes');
skel = bwskel(BW2,'minbranchlength',100); % get rid of most small branches
% remove remaining branches and spurs by pruning only the endpoints close to a branchpoint
skel2 = skel;
ep = bwmorph(skel2,'endpoints');
nendpoints = nnz(ep);
while nendpoints > 2
bp = bwmorph(skel2,'branchpoints');
Db = bwdist(bp); % distance from branchpoints
Deb = Db.*ep; % endpoint distances from closest branchpoint
skel2(Deb == min(nonzeros(Deb))) = false; % remove endpoint that's closest to a branchpoint
ep = bwmorph(skel2,'endpoints');
nendpoints = nnz(ep);
end
% now the skeleton only has two endpoints
[I,J]=find(ep);
imshow(BW,'border','tight')
hold on
scatter(J,I,50,'filled')
There's no guarantee that the pruned skeleton reaches the apparent end of the blob. That statement applies both to the skeleton returned directly from bwskel() and to the skeleton after post-processing.
  1 Comment
Elliott Read
Elliott Read on 13 Feb 2024
@DGM thanks a lot for your answer. The code you have suggested is just the type of thing that I was looking for; I recognised that the spurious end points were all close to branch points, but wasn't sure where to start for removing them. So thanks again for your suggestion and useful explanation too.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!