Detecting coordinates of line edges

조회 수: 14 (최근 30일)
Pankaj
Pankaj 2020년 11월 30일
댓글: Pankaj 2020년 12월 1일
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.
  댓글 수: 3
Pankaj
Pankaj 2020년 11월 30일
No, just the ends.
sushanth govinahallisathyanarayana
Yes, Hough might be useful, since you can describe a line with 2 points, however, you need an approximate idea of the scales at which you want to look, and how the lines may be oriented.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 11월 30일
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.
  댓글 수: 6
Image Analyst
Image Analyst 2020년 11월 30일
Yes. But you have to label the binary image so as to know which endpoint belongs to which blob. Keep in mind that is the blob is shaped like an X then it would have 4 endpoints, or 3 if it were shaped like a Y. So you could have different numbers of endpoints for each blob. So you'd do something like
[labeledImage, numRegions] = bwlabel(binaryImage);
endPointsImage = bwmorph(binaryImage, 'endpoints');
[rows, columns] = find(endPointsImage);
endpoints = cell(numRegions, 1; % At least 1 endpoint for each region
endpointsPerLabel = zeros(numRegions, 1);
for k = 1 : length(rows)
blobLabel = labeledImage(rows(k), columns(k));
endpointsPerLabel(blobLabel) = endpointsPerLabel(blobLabel) + 1;
x = columns(k);
y = rows(k);
endpoints{blobLabel, endpointsPerLabel(blobLabel)} = [x, y];
end
If this doesn't work, re-read my last comment, especially the last sentence.
Pankaj
Pankaj 2020년 12월 1일
Thanks!! This is exactly what I wanted. I have also modified my question, slightly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2020년 11월 30일
A= [0 1 0 0;...
0 0 1 0; ...
0 0 0 1; ...
0 0 0 0] ;
idx = find(A) ;
[i,j] = ind2sub(size(A),idx) ;
[i j]
  댓글 수: 3
KSSV
KSSV 2020년 11월 30일
The above ives the locations of 1 in the matrix A.
Image Analyst
Image Analyst 2020년 11월 30일
Pankaj, the code I gave you in my Answer gives the endpoints only. Did you overlook it?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by