How to determine the number screen pixels being used to display an image?

15 vues (au cours des 30 derniers jours)
I need to figure out the screen real-estate (in pixels) being used to display an image within a figure. If the image's parent axes has the same aspect ratio as the image, then the solution simple... the axes' position property (in pixel units) tells you the number of screen pixels.
However, if the aspect ratio of the axes is not the same as the image, then you have some blank area either on the side or top depending on the image aspect ratio and the axes aspect ratio. In this scenario, the position property of the axes (in pixel units) includes that extra screen space even though the image being displayed doesn't cover that space. See attached image to see what I'm talking about.
Any ideas on how to always get the number of screen pixels being used to display an image, independent of the image and axes' respective aspect ratios? Thanks in advance for any help!
Justin

Réponse acceptée

Image Analyst
Image Analyst le 11 Jan 2017
Try getframe(). See this little demo:
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
imshow(grayImage);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Try getframe() - it will give the displayed size on screen.
displayedImage1 = getframe();
[rows, columns, numberOfColorChannels] = size(displayedImage1.cdata)
% Try getimage - it will give the same size as the original
displayedImage2 = getimage();
[rows, columns, numberOfColorChannels] = size(displayedImage2)
  3 commentaires
Image Analyst
Image Analyst le 11 Jan 2017
Why do you incorrectly think that? Just call
imshow(displayedImage1.cdata);
and you'll see it's just the contents of the axes, not the whole figure itself.
Justin Solomon
Justin Solomon le 11 Jan 2017
You're right. My mistake. In the past i've always used getframe() to grab the whole figure (e.g., getframe(gcf)) instead of the axis. Thanks for the solution!

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 11 Jan 2017
Modifié(e) : Jan le 11 Jan 2017
This is not trivial. I do not know a documentation of the behavior for subpixels or when the operating system scales the output. The only really really trustworthy test is displaying a mono-chromatic image with a defined color and taking a screenshot. Then you can search for the rectangle of the defined color.
How is "pixel" exactly defined? Monitor pixels might be interpolated alread for high-resolution screens.
[EDITED] I had set the screen scaling in Windows 7 to 125% to improve the readability of icons and fonts on the desktop. To my surprise afterwards setting the figure 'Position' to an integer pixels position lead to an 'OuterPosition' with a fractional pixel position and vice versa. I expect this is not consistent over different operating systems and Matlab versions.
  1 commentaire
Justin Solomon
Justin Solomon le 11 Jan 2017
Thanks Jan for the response. As you said not a trivial problem and difficult to know how different OS would respond. The solution I came up with (which is probably not exact in all system/monitor configurations but gets me close enough for my application) is to look at the aspect ratio (width/height) of the axis, Aa, (according to its position property in pixel units), and also compute the aspect ratio of the displayed image, Ai. If Aa>Ai that means the empty space happens in the horizontal direction and the image is flush against the axes limits vertically. Thus I can compute the ratio of screen pixel to image pixel (axis height / image height). Similarly, if Aa<Ai, then the extra space happens in the vertical direction and the image is flush against the axes limits horizontally. Thus the ratio of screen pixels to image pixels is give by axis width / image width. Seems to work well enough for what I'm doing.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by