251 views (last 30 days)

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)...

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.

K M Ibrahim Khalilullah
on 20 Jun 2016

Image Analyst
on 26 Jun 2016

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');

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

Start Hunting!
## 8 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37842

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37842

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37844

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37844

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37847

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37847

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37850

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37850

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37853

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37853

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37855

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37855

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37856

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37856

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37923

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/17086-draw-a-straight-line-in-image-given-an-angle#comment_37923

Sign in to comment.