How to find the endpoint?

Hi, i have an image and wanted to find the location of endpoints as shown in red circle.
I am using the
bw=bwmorph(bw,'endpoints')
however, it returns "0" instead of the location of endpoints.
How can I do with this? Thank you.

2 Comments

From what I recall, you need to skeletonize your image first.
Thanks Rik.

Sign in to comment.

 Accepted Answer

DGM
DGM on 8 Jun 2022
Edited: DGM on 8 Jun 2022
% image recovered from screenshot
A = imread('vessel.png');
A = A>128; % binarize
% skeletonize
B = bwskel(A,'minbranchlength',50);
% fill loops
B = imfill(B,'holes');
% skeletonize to get rid of filled loops
B = bwskel(B,'minbranchlength',50);
imshow(B) % display interpolation makes fine lines look spotty
% get endpoints as a logical image
endpts = bwmorph(B,'endpoints');
imshow(endpts) % again, display interpolation hides most of the dots
That result can be used for logical indexing if needed. If for some reason, you can't use it directly and need indices instead, you can use find().
% if you really truly need indices instead
endptidx = find(endpts)
endptidx = 21×1
807 124117 134946 246304 371291 453988 479473 492705 497657 526278

6 Comments

Hi, may I know what does the logical indexing mean?
How can I find the location of the endpoints? Thanks
Logical indexing is true or false. Linear indexing is actual row and column numbers. So endpts is the logical matrix and can be used for indexing, like if you want to set some gray level image zero where the end points are, you could do
grayImage(endpts) = 0;
On the other hand if you wanted to get the rows and columns, you could do
[rows, columns] = find(endpts);
for k = 1 : length(rows)
grayimage(rows(k), columns(k)) = 0;
end
He told you the location of the end points. It's the matrix endpts. And if you want the actual (row, column) location of the end points, you use find() like we showed you.
Maybe if you told us what you are going to do with the endpoints we could suggest to you the best approach after getting them.
Hi Image Analyst, thank you for your reply.
Actually I want to find the location of each endpoint and calculate the distanece between each endpoint with centroid (says [500 1000]). Then, the endpoint which is the most distant to the centroid is determined.
Here is my code but it does not work well:
x=500;
y=1000;
endpts=bwmorph(bw,'endpoints'); %bw is a black and white image
[endpt_Y endpt_X]=find(endpts); %[row col]
length(endpt_X);
for n=1: length(endpt_X)
distances=sqrt((x-endpt_X(n)).^2 + (y-endpt_Y(n).^2));
distances=round(distances)
n=n+1;
end
[row col] = find(max(distances))
The output of row and col is 1.
I'm not sure if your x,y for the reference center are flipped. it's outside the image region.
% image recovered from screenshot
A = imread('vessel.png');
A = A>128; % binarize
% skeletonize
B = bwskel(A,'minbranchlength',50);
% fill loops
B = imfill(B,'holes');
% skeletonize to get rid of filled loops
B = bwskel(B,'minbranchlength',50);
% get endpoints as a logical image
endpts = bwmorph(B,'endpoints');
% these might be flipped
x=500;
y=1000;
[endpt_Y endpt_X] = find(endpts); %[row col]
% this is the square of the euclidean distance
% if all you need is to optimize distance, there's no need for sqrt()
% if you actually need the distance for something else, you can add it.
Dsquared = sum(([endpt_Y endpt_X] - [y x]).^2,2);
% find most distal endpoint
[~,mxidx] = max(Dsquared);
mostdistal = [endpt_X(mxidx) endpt_Y(mxidx)]
mostdistal = 1×2
818 19
% plot some stuff
imshow(B); hold on
plot(x,y,'x','markersize',20) % specified centroid
plot(mostdistal(1),mostdistal(2),'o','markersize',20)
ylim([0 1050]) % adjust axes to show marker
I got it. Thanks all for helping, really appreciate it!

Sign in to comment.

More Answers (0)

Categories

Find more on Rubik's Cube in Help Center and File Exchange

Products

Release

R2021b

Asked:

ZWY
on 8 Jun 2022

Commented:

ZWY
on 10 Jun 2022

Community Treasure Hunt

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

Start Hunting!