MATLAB Answers


draw lines on top of image displayed in matlab gui

Asked by Lisa
on 5 Feb 2013


My GUI is used to display DICOM images. I want to draw lines on top of the image displayed in the GUI. For instance my GUI allows the user to input an angle theta. I want to draw a line on top of the figure which makes an angle theta with the x axis. How do I go about doing that?

I know I can draw lines using the line( ) command but somehow the line that gets drawn appears random not at the angle I specified. I would greatly appreciate some guidance.


Log in to comment.


1 Answer

Answer by Walter Roberson
on 5 Feb 2013

You can use line() or plot()

Remember that line() and plot() use data coordinates. Images drawn with imshow() or image() or imagesc() also use data coordinates, but keep in mind that if the image was the first thing you drew in the axis and you did not specify coordinate positions for the image, then the axis data coordinates will be set as 1 to rows(Image) and 1 to columns(Image).

line() and plot() do not accept angles as input. If you want to draw by angle, use pol2cart([0 angle], [0 linelength]) and add the x/y coordinates of the origin of the line to the result.

Also make sure you use

axis image

so that you get square pixels for the image; otherwise the output angles would not appear random but they would be stretched horizontally or vertically.


No I do not want the coordinate system used during image creation.

I had established xlim() and ylim() explicitly. I want that. However when the image is displayed on the axis, the xlim and ylim are no more accessible. Even the origin changes. The origin becomes the upper left corner instead of the lower left.

Somebody told me that my problem might possibly be worked out by using multiple axis overlapping one another but they had no idea exactly how to go about doing that. Is that possible? To have my image on one axis and to draw lines on another axis which overlaps the first one? I've experimented that too. Doesn't seem to work the way I'm doing it.

However, if it is possible could you point me in the right direction?

Have a look at this syntax for image (and imagesc)

image(x,y,C), where x and y are two-element vectors, specifies the range of the x- and y-axis labels, but produces the same image as image(C). This can be useful, for example, if you want the axis tick labels to correspond to real physical dimensions represented by the image. If x(1) > x(2) or y(1) > y(2), the image is flipped left-right or up-down, respectively. It can also be useful when you want to place the image within a set of axes already created. In this case, use hold on with the current figure and enter x and y values corresponding to the corners of the desired image location. The image is stretched and oriented as applicable.

Watch out when you use this: the pairs you specify should refer to the data coordinates for the center of the lower left and upper right pixels. The x and y values correspond to the XData and YData properties of the image object.

Log in to comment.

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!