Plotting lines in an efficient way.
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I'm detecting lines on an image by the Hough transform, when I use the function houghlines, it return me a struct that I called lines, containing the start and the end point of the lines detected, I'm plotting it by a foor loop:
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
end
In this manner I obtain this result:

But plotting all the lines with the foor loop takes a lots of time.
In order to speed up, the process I have written the following code:
xy = [];
for k = 1:length(lines)
point1 = lines(k).point1;
point2 = lines(k).point2;
xy = [xy; lines(k).point1; lines(k).point2];
end
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
But the result is:

How I have to modify the script in order to obtain, the same result of the first image?
댓글 수: 0
채택된 답변
Jan
2017년 3월 8일
편집: Jan
2017년 3월 8일
It works without loops:
% Abbreviation of: [lines(:).point1; lines(:).point2] :
data = [lines.point1; lines.point2];
x = data(:, 1:2:end);
y = data(:, 2:2:end);
plot(x, y, 'LineWidth', 2, 'Color', 'r');
댓글 수: 3
Jan
2017년 3월 8일
편집: Jan
2017년 3월 8일
@KSSV: Thanks. Writing "+1" usually means, that one has voted for an answer. Currently my answer has 0 votes, so I assume, it is a general compliment. Thanks. I really appreciate the atmosphere of mutual cooperation in this forum.
I had tried 8 different versions, some contained reshape, transpose and cat, and NaNs also. Actually it would be interesting to see my worse approaches also as a tutorial, but this might look confusing in the forum.
추가 답변 (1개)
KSSV
2017년 3월 8일
xy = [];
for k = 1:length(lines)
point1 = [lines(k).point1 NaN]; % if throws error try point1 = [lines(k).point1 ; NaN];
point2 = [lines(k).point2 NaN]; % if throws error try point1 = [lines(k).point2 ; NaN];
xy = [xy; point1; point2];
end
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!