How to determine the number screen pixels being used to display an image?
16 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2017-1-11
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 个评论
Image Analyst
2017-1-11
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.
更多回答(1 个)
Jan
2017-1-11
编辑:Jan
2017-1-11
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!