MATLAB: Detecting coordinates of line edges

edgeimage processingImage Processing Toolboxline

I have a matrix containing 1s and 0s. The elements of this matrix are pixels of an image, where 1&0 represents black and white. The 1s form non-intersecting line segmennts. I want to know pixel coordinates of line edges. Following is an example matrix-
binaryImage= [0 1 0 0;...
0 0 1 0; ...
0 0 0 1; ...
0 0 0 0]
The desired output is-
[1, 2], [3, 4]
Note that there could be multiple line segments. Is Hough transform useful here?
Thanks
——————–
Update: I am also attaching another example matrix and corrosponding `imagesc` for better visualization of problem.

Best Answer

Use bwmorph() to get an image of just the endpoints, then use find() to get the rows and columns:
endPointsImage = bwmorph(binaryImage, 'endpoints');
[rows, columns] = find(endPointsImage);
rows and columns will be a list of the rows and columns of each endpoints. They are synced up so for index N, rows(N) has the row of the Nth endpoint and columns(N) has the column of the Nth endpoint.