Matlab图像向量绘制与平行y轴直线最短距离计算求助
Hey there! Let's tackle your MATLAB problems step by step—great news, your full request is totally achievable. Here's how to resolve both of your issues and polish the code to meet all your needs:
1. Fixing the Misplaced Vertical Lines
The odd placement of your two vertical lines is likely due to two things:
- The order of
axis equal(applying it before drawing can warp the coordinate system) - MATLAB's default image coordinate system (y-axis increases downward from the top-left corner)
Instead of relying on get(gca, 'ylim') (which can change if you adjust axes later), use the actual image height to define the line's vertical range. This ensures the lines span the entire image consistently.
2. Calculating the Shortest Distance Between Parallel Vertical Lines
Since your lines are parallel to the y-axis (their equations are x = p1(1) and x = p2(1)), their shortest distance is super straightforward—you don't need 4 points at all! The distance is just the absolute difference between their x-coordinates:
distance = abs(p1(1) - p2(1));
This works because parallel vertical lines are separated by a horizontal distance, which is exactly the gap between their x-values.
3. Drawing Arrows Perpendicular to the Ground
Assuming your "ground plane" is the horizontal axis of the image (x-axis), a vector perpendicular to the ground will be vertical (parallel to the y-axis). MATLAB's quiver function is perfect for drawing arrows—way easier than manually creating lines with line.
Full Corrected Code
% Read and display image img = imread('sample.jpg'); fig = figure('Name', 'Image Tool'); imshow(img); hold on; % Get user-clicked points [p1, p2] = getpts(fig); % Optional: Round to nearest pixel for cleaner alignment p1 = round(p1); p2 = round(p2); % Draw vertical lines spanning the full image height img_height = size(img, 1); line([p1(1) p1(1)], [1 img_height], 'Color', 'darkblue', 'LineWidth', 1.5); line([p2(1) p2(1)], [1 img_height], 'Color', 'darkgreen', 'LineWidth', 1.5); % Calculate and display the shortest distance distance = abs(p1(1) - p2(1)); fprintf('Shortest distance between the two lines: %.2f pixels\n', distance); % Draw vertical arrows (perpendicular to horizontal ground) at each point arrow_length = 35; % Adjust this to make arrows longer/shorter % Arrow at p1 (pointing upward) quiver(p1(1), p1(2), 0, arrow_length, 'Color', 'red', 'LineWidth', 1.5, 'MaxHeadSize', 0.5); % Arrow at p2 (pointing upward) quiver(p2(1), p2(2), 0, arrow_length, 'Color', 'black', 'LineWidth', 1.5, 'MaxHeadSize', 0.5); % Optional: To point arrows downward, use -arrow_length instead % quiver(p1(1), p1(2), 0, -arrow_length, 'Color', 'red', 'LineWidth', 1.5, 'MaxHeadSize', 0.5); % Finalize axes (apply equal scaling after all drawing) axis equal; hold off;
Key Improvements Explained
- Line Placement: Using
size(img,1)gives the actual image height, so lines always span from the top to bottom of the image, no matter how axes are adjusted. - Distance Calculation: Leverages the simplicity of parallel vertical lines—no complex geometry needed!
- Arrow Vectors:
quiverhandles arrow heads automatically, and you can easily adjust direction (up/down) and length to fit your needs.
内容的提问来源于stack exchange,提问作者gordon

