draw lines on top of image displayed in matlab gui
4 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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.
6 Comments
Walter Roberson
on 7 Feb 2013
Edited: Walter Roberson
on 12 Jun 2020
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.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!