draw lines on top of image displayed in matlab gui

Hi,
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.

Answers (1)

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.

6 Comments

No I did not specify coordinate positions for the image. I simple read in the slice as a 2D matrix and used imagesc() to draw the same on the axes. So i assume my axis data coordinates are set to rows and columns. How can I reset it to x and y coordinates?
Also I didn't understand the second part of your answer 'axis image'? Where should I use that?
Lisa
Lisa on 6 Feb 2013
Edited: Lisa on 6 Feb 2013
I've been trying it out different ways and finally I've concluded that my problem is exactly as you stated it - my axis coordinates have been set to the rows and columns of the image.
As for using angles that's been easily solved. I use the equation
t = x*cos(theta)-y*sin(theta)
s = y*cos(theta)+x*sin(theta)
and then use 2 such points to draw the line using line( ) command.
But about the coordinates, how do I set it back to the original axis data coordinates?
What "original axis data coordinates"? Did you already create an axis and establish a coordinate system for it either implicitly by plotting on the axis, or explicitly by using xlim() and ylim() ? If you did not do that then there are no "original axis data coordinates" for the axis.
There might have been a coordinate system in place at the time the image was created by the DICOM-compliant instrument, but if that is what you want to use then you need to extract the relevant parameters from the DICOM header. Use dicominfo to extract the information. I would need to see the list of returned information in order to tell you what was relevant to recreate the axis used by the instrument.
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.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Asked:

on 5 Feb 2013

Edited:

on 12 Jun 2020

Community Treasure Hunt

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

Start Hunting!