how to normalize the image axis

hi, i need to plot a arrow in my image. I know that there is an inbuilt function in matlab called "annotation" which does the job, but it needs to normalize the axis in the range of [0 1]. Can any one help me in understanding how to normalize the image axis?

Risposte (1)

Jonathan Epperl
Jonathan Epperl il 6 Dic 2012
Modificato: Jonathan Epperl il 6 Dic 2012
So it appears, that annontaion('arrow',... is kind of annoying to work with, since it is based on a coordinate system of the figure window, and not the axis. I think there are two ways to get what you want:
Either, go to the FEX and get one of the many scripts that create arrows for you.
Or, you can do some math with the coordinates. This code here is assuming that your y-axis is reversed, as the image command does it, and that both axes start at 0.
load mandrill
image(X);
axpos = get(gca,'Position'); % normalized position of the axis in the figure
xyl = [get(gca,'Xlim'); get(gca,'YLim')]*[-1 1]' % length of your axes
arrpos = [200 300; 150 250]; % the coords of the arrow in the units of you axes
% now figure out how your axis-unit coordinates translate to figure coordinates
arrposnorm = diag(axpos(3:4))*[arrpos(1,:)/xyl(1); (1-arrpos(2,:)/xyl(2))];
annotation('arrow',arrposnorm(1,:)+axpos(1),arrposnorm(2,:)+axpos(2),'Color','w')
I hope this is what you asked for, otherwise please give some more detail.

4 Commenti

Niraj
Niraj il 7 Dic 2012
Modificato: Niraj il 7 Dic 2012
Thanks for the answer. I figured out how to normalize and it works on my test image, but when i use on my actual image(on a subplot), it does not works. There is some issue, of getting position from
get(gca,'Position')
I get the arrow at same coordinates as specified by x axis but not with y axis.Its a small way off.
It happened with the same, when i used your piece of code as specified above. i have attached the image for your reference. http://postimage.org/image/insyu1o0x/
I am trying to draw the arrow from same coordinates as shown by you i.e. [200 300; 150 250]
Maybe there's some issue with the "stuff" around the axes like tick marks, labels, titles, padding, etc. Try it with "axis off" no labels or titles, and see if it's the same problem. Also, there are other position parameters that you can try to retrieve like innerposition and outerposition. Try those to see if they make your arrow ends land spot on.
If all of that fails, it may be due to some kind of aspect ratio thing. I know that if I use GUIDE and draw out an axes control on the GUI, then the image that is displayed has a different size and shape from the "blank" axes I drew. It seems to zoom/scale the image to do its best job of fitting that image into the axes you drew. But what I'm not sure of is if the position parameters get updated for the new size and shape of the axes or do they still have the values for the initial, larger axes. You could just get those properties before and after putting an image into the axes to check to see if the values updated.
If all that fails, I'd call the Mathworks. I agree it is a pain and they should have a version of annotation that works just on the main part of an axes within the bounding box (i.e. not including tick labels, title, etc.). This would, in my opinion, actually be far more useful than the current annotation that works only with figure coordinates.
Thanks for the reply. I tried using the outerposition parameters, but it does not help. The strange thing is the same code works when i used to make arrow in a test image. My test image is a blank image as shown on the left side of the above posted image. Also when i read the position of my current axis, then it gives me :(Units is in centimeters)
xpos =
19.3009 1.9892 11.3252 14.7383
Since, this is the position of my sublot (1 2 2) and 19.3009 representing the left corner of my subplot, the number 1.9892 seems to be out of proportion.
To me, it seems that 1.9892 is very small.When i see the image, the bottom value should be around 2.5 -3 cm.
I am trying to make a directional graph, where every interaction of a blob marks an arrow between the blobs. So, in that case, any help/suggestion? Thanks
I can't tell from the picture, maybe your y-axis doesn't start at 0?
Zooming messes stuff up, too, since the arrow won't move relative to the figure at all.
Also, whatever you do to your axes, like titles, labels, legends, might move the axes relative to the figure, which would then result in a changed 'Position'. It thus might solve your problem, if you did everything you're doing with the axis, and only then run my code. If I run it on my machine, there is no problem:
load mandrill
s=subplot(2,2,2)
image(X)
axpos = get(s,'Position'); % normalized position of the axis in the figure
xyl = [get(s,'Xlim'); get(s,'YLim')]*[-1 1]' % length of your axes
arrpos = [200 300; 150 250]; % the coords of the arrow in the units of you axes
arrposnorm = diag(axpos(3:4))*[arrpos(1,:)/xyl(1); (1-arrpos(2,:)/xyl(2))];
annotation('arrow',arrposnorm(1,:)+axpos(1),arrposnorm(2,:)+axpos(2),'Color','w')
grid minor
If you're using subplots you can get their handles during creation
hs = subplot(2,1,2)
or so, that is more robust than using gca.
I agree with Image Analyst though, it's pretty pathetic that annotation relative to the axis coordinates is so hard. For what you want to do I would recommend a FEX function: http://www.mathworks.com/matlabcentral/fileexchange/278-arrow-m That is what I (along with probably most other people) am using for 2-D arrows.
If you're going to use arrow.m also note Kent Leung's comment, that might be relevant to you:
>>> 11 Jun 2012 Kent Leung
@Matthias, I had this problem too and just stumbled on a solution. (In fact, the problem for me was that the xlabel was disappearing.) To fix this I did:
ax1=subplot(2,1,1); [...] axes(ax1); arrow([x1 y1],[x2 y2]); arrow fixlimits;
I always do "fixlimits" just in case. The reason I tried this was because in the help file: "You may want to execute AXIS(AXIS) before calling arrow so it doesn't change the axes on you; arrow determines the sizes of arrow components BEFORE the arrow is plotted, so if arrow changes axis limits, arrows may be malformed."
It's not obvious that this fixed the subplot resizing problem, but it worked! <<<

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Richiesto:

il 6 Dic 2012

Community Treasure Hunt

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

Start Hunting!

Translated by