## Indexing points along curve in matrix in order from one endpoint to the other

on 17 Jul 2012

### Image Analyst (view profile)

I have a logical image with many 1-px wavy curve segments and I am trying to fit a curve to these segments to find the orientation along the curve through derivative information. To do this I am planning on using the indexed points from the curves and something such as cubic splines, but "PixelList" and "find" return the points in order of appearance in the matrix, not order of appearance in the line.
Is there a method in MATLAB to index the points in the matrix in an order starting from one endpoint to the other endpoint.
For example given:
A = [0 1 1 1 0; 1 0 0 0 1; 1 0 0 0 0; 1 0 0 0 1; 0 1 0 1 0; 0 0 1 0 0];
% A =
% 0 1 1 1 0
% 1 0 0 0 1
% 1 0 0 0 0
% 1 0 0 0 1
% 0 1 0 1 0
% 0 0 1 0 0
[rows cols] = find(A==1); %This gives the following [row,col] pairs:
(2,1),(3,1),(4,1),(1,2),(5,2)... etc
I would like the points in this order ([row,col] again):
(2,5),(1,4),(1,3),(1,2),(2,1),(3,1),(4,1)... etc
% Or starting from the other endpoint

### Image Analyst (view profile)

on 17 Jul 2012
Edited by Image Analyst

### Image Analyst (view profile)

on 17 Jul 2012

Is interparc what you're looking for? http://www.mathworks.com/matlabcentral/fileexchange/34874-interparc Or you might try bwboundaries() or bwtraceboundary().

Ryan

### Ryan (view profile)

on 18 Jul 2012
bwboundaries returns what I am looking for, it is just not immediately obvious. For the provided sample matrix A is starts in the middle and traverses to an endpoint, THEN traverses from endpoint to endpoint along the line before traversing from the second endpoint back to the original position.
Sean de Wolski

### Sean de Wolski (view profile)

on 18 Jul 2012
Combine this with the endpoints that I suggested and you can extract exactly what you need!
Ryan

### Ryan (view profile)

on 18 Jul 2012
I may switch to endpoints to see if it's faster, but I initially just wrote an if statement + for loop combo that finds the points that repeat 1 spot from where it first occurs and then clip the array appropriately.
It's worth noting that interparc is a great interpolation function for after bwboundaries gets me the point values!

### Sean de Wolski (view profile)

on 17 Jul 2012

Use the 'endpoints' option in bwmorph to find the end points. Pick an end point and then traverse your 'pixellist' selecting points adjacent to each selected pixel repeating until you reach the other end.

Ryan

### Ryan (view profile)

on 18 Jul 2012
If for some reason bwboundaries doesn't work I'll figure out a way to use bwdist or something similar to do this. Thank you.
Sean de Wolski

### Sean de Wolski (view profile)

on 18 Jul 2012
I don't see how bwdist can be of any assistance to you. Every point on the line will be returned as a zero. I would give my approach with bwmorph and endpoints a try.
Ryan

### Ryan (view profile)

on 18 Jul 2012
I misunderstood what bwdist calculated, I will hopefully get a chance to test these solutions soon, thanks again.