Draw a straight line in image, given an angle
33 views (last 30 days)
Show older comments
Is there a way to draw a straight line in an image from a starting (x,y) coordinate, such that the line is equivalent to a specified angle?
For example, if I start at pixel (50,50), how can I draw a line to be 30 degrees of this pixel? I would like to draw 10 pixels in this direction (obviously, the resulting line will be zig-zagged)...
8 Comments
Walter Roberson
on 29 Sep 2011
Calculate the total angle relative to the forward Y axis (i.e., angle 0 on a polar plot.) Choose a large radius, R, such as 1000, for an imaginary line segment; don't worry if it is out of the image. Now, R*sin(theta) and R*cos(theta) give you the total delta x and total delta y if you were drawing the whole line segment. Round to integers, and add those rounded deltas to the starting point to get the imaginary end point. Now apply Bresenham's line algorithm to the imaginary line between the starting point and the identified imaginary end point, except stop when you have generated as many total pixels as you desire. The result will be a line segment drawn from the starting point at your desired angle, for the desired number of pixels.
Answers (2)
Image Analyst
on 29 Sep 2011
How about this:
lineLength = 10;
angle = 30;
x(1) = 50;
y(1) = 50;
x(2) = x(1) + lineLength * cosd(angle);
y(2) = y(1) + lineLength * sind(angle);
hold on; % Don't blow away the image.
plot(x, y);
xlim([0 70]);
ylim([0 70]);
grid on;
If you want the line to be "burned into" your image, then you'll have to use imline() - search answers for my demo on that - or use the Bresenham line algorithm like Walter suggested if you want to do it yourself.
9 Comments
Image Analyst
on 21 Jul 2021
I don't see the problem. The code is doing exactly what you told it to. Nor do I know what you want. Perhaps a diagram would help. Have it show what you are getting, and what you want instead.
Walter Roberson
on 21 Jul 2021
Michelle De Luna:
No. It is possible to generate two lines of identical length only if they happen to have a particular Pythagorean triple relationship such as listed at http://oeis.org/A084646 or related sequences. For example if the distance is 25 then there are two different angles that can generate that identical distance.
Any case in which the angle and distance does not happen to correspond to one of the triples, you can never get out identical distances.
Image Analyst
on 30 Sep 2011
% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.
%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
binaryImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary mask of the ellipse');
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% textMask = hText.createMask();
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');
%----- Burn line into image -----
burnedImage = imread('pout.tif');
% Create line mask, h, as an ROI object over the second image in the bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
% Create a binary image ("mask") from the ROI object.
binaryImage2 = hLine.createMask();
% Display the line mask.
subplot(2, 4, 7);
imshow(binaryImage2);
title('Binary mask of the line');
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!