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

 Akzeptierte Antwort

Adam Danz
Adam Danz am 3 Dez. 2018
Bearbeitet: Adam Danz am 3 Dez. 2018

1 Stimme

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 Kommentare

Kamu
Kamu am 4 Dez. 2018
Bearbeitet: Kamu am 4 Dez. 2018
Thanks for help, but your solution will just find one intercept between line and image border (lower part) but not the intercept with the upper border. I have attached a screenshot. Ideally, I want to the know the coordinates when the line (here in red) hits the image border.
This is a toy example. I changed YL(2) to YL(1) to get the upper part from the red circle to the upper image border:
img = uint8(zeros(382, 382));
pointA = [181.4594, 129.7092];
pointB = [185.3411, 251.6005];
imshow(img)
hold on
plot(pointA(1), pointA(2), 'ro')
plot(pointB(1), pointB(2), 'bo')
hold off
% From y = mx + x
slope = 31.4015;
intercept = -5.5684e+03;
imshow(img)
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(1)-intercept)/slope;
% 2) line crosses rightward axis limit. X = XL(2), y = ?
yEnd = (slope * XL(1)) + intercept ;
% Now determine which end point to use (ie, which axis it crosses)
if xEnd <= XL(1)
% The line crosses the upper y axis border
yEnd = YL(1);
else
% The line crosses the rightward x axis border
xEnd = XL(1);
end
%Draw the line
plot([pointA(1), xEnd], [pointA(2), yEnd], 'r-', 'linewidth', 3)
plot([pointB(1), xEnd], [pointB(2), yEnd], 'g-', 'linewidth', 3)
X_values = linspace(pointB(1), xEnd);
Y_values = linspace(pointB(2), yEnd);
% Check how many coordinates are within image frame
for zz=1:length(X_values)
hold on
plot(X_values(zz),Y_values(zz), 'm+');
end
That's because you're using imshow() which flips the y axis. In the image below, note that the max(ylim) is at the bottom of the plot instead of the top.
figure
I = imread('cameraman.tif');
imshow(I)
axis on
181204 062943-Figure 1.jpg
To adjust my code in my answer so that the line searches for the intersection with the min(ylim) axis, change the two YL(2) to YL(1) and my solution will work with flipped y axes.
Kamu
Kamu am 4 Dez. 2018
You are right, that's the solution. Thanks!
Adam Danz
Adam Danz am 4 Dez. 2018
Nice, glad it worked!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

am 3 Dez. 2018

Kommentiert:

am 4 Dez. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by