MATLAB Answers

how to draw a line from the centre of the image to its border

In the given image, i plotted 6 points on its border manually. a random point is selected in the image. My objective is to connect all those 6 points with this random point. how to connect those by lines.


1 Answer

Answer by Image Analyst
on 1 Sep 2012
Edited by Image Analyst
on 1 Sep 2012
 Accepted answer

Did you try the line() function? That will plot in the overlay. If you want the line burned into the image use the imline() function's createMask() method.

% Demo to write a line into the overlay of an image,
% and then to burn the overlay into the image.
%----- Initializing steps -----
% Clean up
clear all;
close all;
workspace; % Display the workspace panel.
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.
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 2, 1);
title('Original Image');
subplot(2, 2, 2);
title('Original Image with line in overlay');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off') 
%----- Burn line into image -----
burnedImage = imread('pout.tif');
% Create line mask, h, as an ROI object over the image.
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, 2, 3);
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, 2, 4);
title('New image with line burned into image');


Thanks Image Analyst. It works fine.

i found that the code works for a single line. can we use for drawing multiple lines

Of course. Why don't you just package it into a function where you can pass in the two endpoint coordinates?

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!