Image processing for determining endpoints on a line

8 次查看(过去 30 天)
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.

采纳的回答

DGM
DGM 2024-2-12
编辑:DGM 2024-2-12
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 个评论
Elliott Read
Elliott Read 2024-2-13
@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.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by