フィルターのクリア

Draw line on image with known slope

3 ビュー (過去 30 日間)
Kamu
Kamu 2018 年 12 月 3 日
コメント済み: Adam Danz 2018 年 12 月 4 日
Given Point A and B and a line equation y = mx + c. I calculated the intercept and slope.
Now I want to draw a line (linspace function) from point A to the image boundary. It worked for lines with small gradients but failed for a nearly 'vertical line'.
I used the following code:
imshow(I)
xlims = xlim(gca);
ylims = ylim(gca);
The problem with vertical lines is shown in the screenshot. What I want is that the blue line stops at the image boundary because there are only 5 magenta 'plus' coordinates within the image, which is too less.
Screenshot 2018-12-03 at 15.18.26.png

採用された回答

Adam Danz
Adam Danz 2018 年 12 月 3 日
編集済み: Adam Danz 2018 年 12 月 3 日
If you have the staring point (A,B), the slope (m) the y-intercept (yint), and the axis limits XL, YL (where XL and YL are (min,max) pairs), you can calculate the end point of the line where it crosses the border of your axes. Then use the start coordinate and end coordinate to draw a line via plot().
% record your axis limits
XL = xlim(gca);
YL = ylim(gca);
hold(gca, 'on')
% calculate the two possible end points
% 1) line crosses upper axis limit. X = ?, Y = YL(2)
xEnd = (YL(2)-yint)/m;
% 2) line crosses rightward axis limit. X = XL(2), y = ?
yEnd = (m * XL(2)) + yint ;
% Now determine which end point to use (ie, which axis it crosses)
if xEnd <= XL(2)
% The line crosses the upper y axis border
yEnd = YL(2);
else
% The line crosses the rightward x axis border
xEnd = XL(2);
end
%Draw the line
plot([A, xEnd], [B, yEnd], 'b-', 'linewidth', 3)
%circle end point for confirmation
plot(xEnd, yEnd, 'mo', 'linewidth', 3)
  4 件のコメント
Kamu
Kamu 2018 年 12 月 4 日
You are right, that's the solution. Thanks!
Adam Danz
Adam Danz 2018 年 12 月 4 日
Nice, glad it worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by