How do I get or calculate actual pixel dimensions of an image in an axes in a fully normalized figure?

11 views (last 30 days)
I can not for the life of me seem to get the (screen) pixel values for height and length of a currently displayed image in an axes. The reason for this is the normal methods do not work, this is because everything in the GUI environment is set to 'normalized' for units.
So, the figure, the root, the axes, ....everything is set to normalized and so for example if I try to get the get(0,'screensize'); for my x and y (width and height) to make calculations about the pixels ...I'm just going to get [0 0 1 1] as an answer which doesn't tell me anything useful.
I'm trying to play a movie that goes back and forth between 2 images over and over (bar and bar2) and then ends by displaying bar.png in the axes (handles.flex a flexibly sized axes) at the very end. The problem however is that it isn't the right size. I tried to use the XLim and YLim, among other ways to get the right pixel values for x and y and ...it always ends up with my movie not being correctly scaled and stretched/shrunk to the axes length and height.
Example:
bar2 = imread('GUI\base\bar2.png');
bar = imread('GUI\base\bar.png');
bar2 = imresize(bar2,[handles.flex.XLim(2) handles.flex.YLim(2)]);
bar = imresize(bar,[handles.flex.XLim(2) handles.flex.YLim(2)]);
loops = 40;
frame(loops) = struct('cdata',[],'colormap',[]);
for j = 1:loops
if mod(j,2) == 0
frame(j) = im2frame(bar);
else
frame(j) = im2frame(bar2);
end
end
movie(handles.flex,frame,1);
imshow(imread('GUI\base\bar.png'),'InitialMagnification', 'fit', 'parent',handles.flex);

Answers (1)

Image Analyst
Image Analyst on 17 Apr 2017
Until you load something into the flex axes control, it will have an xlim and ylim of 0 to 1. After you load an image into it xlim and ylim will have units of image pixels, not screen pixels. In other words, your image could be 3000 by 4000 pixels and displayed in a region on your screen of 600 by 800. xlim would be 1 to 4000, not 1 to 800.
I'm not really sure what you want to do, but perhaps the attached demo (that the Mathworks sent me and I modified) will help.
  3 Comments
Image Analyst
Image Analyst on 17 Apr 2017
Try getting the api for the display container. See documentation for iptgetapi(). It has tons of methods.
See attached example for how to use it. I don't know what all of those things are but one of them might help you.
gametjd
gametjd on 20 Apr 2017
This gentleman was able to somewhat help me get the pixel values I wanted in a slightly simpler way. <http://www.mathworks.com/matlabcentral/newsreader/view_thread/348233#953287>
However, it seems that the problem isn't going to ever be 100% fixed because of a problem with the DPI of a figure that has yet to be addressed by Matlab developers. http://undocumentedmatlab.com/blog/graphic-sizing-in-matlab-r2015b

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!