How to set exact figure size in pixels?

153 ビュー (過去 30 日間)
Klaus Förger
Klaus Förger 2016 年 3 月 11 日
編集済み: Serge 2022 年 4 月 23 日
With older versions of Matlab, it was possible to set the exact figure size in pixels with a command such as: figure('Position', [100 100 400 400]); This produced a figure with 400x400 pixels.
After upgrading to R2015b, getting a 400x400 figure can be done on my computer with command: figure('Position', [100 100 400/1.5 400/1.5]);
This works fine with some resolutions, but for example the command: figure('Position', [100 100 401/1.5 401/1.5]); does not produce a size of 401x401 but instead 400x401. Usually an offset of one pixel is not a problem, but when rendering videos it can break things because some codecs work only with certain pixel sizes.
Is there a reliable way set the size of a figure that would not depend of the screen DPI or the used operating system?
  4 件のコメント
Image Analyst
Image Analyst 2016 年 3 月 11 日
What are the figures 'units' property? Is it normalized, characters, or pixels?
ju7ga7
ju7ga7 2016 年 3 月 11 日
We are using MS Windows 7 enterprise 64bit. The figure units property is pixels. The scaling factor comes from the Windows scale settings (100%, 125% or 150%), I guess. In our case it is the scaling factor. We need a figure behaviour independet of the system scaling factor. That's all. On modern high resolution Displays 125% or 150% is essential for ergonomic reasons when working with Windows OS. The groot Object also holds wrong display settings, display resolution and dpi is also scaled by the windows scaling factor. so also figure window positioning does not work as in previous Matlab Releases. That is also bad!

サインインしてコメントする。

回答 (4 件)

Serge
Serge 2021 年 8 月 20 日
編集済み: Serge 2022 年 4 月 22 日
I use this (works for me in R2020a, win10):
rez = [1024 768]; %set desired [horizontal vertical] resolution
set(gcf,'PaperPosition',[0 0 rez/100],'PaperUnits','inches'); %set paper size (does not affect display)
img = print(gcf,'-RGBImage','-r100'); %capture RGB image (a bit slow)
EDIT: Fixed problem with font size and improved switched to inches as per Thomas' sugestion.
  2 件のコメント
Thomas Gilbert
Thomas Gilbert 2022 年 4 月 21 日
This was helpful thank you :)
I found setting 'PaperUnits','inches' allows you to get rid of the 2.54.
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 rez]);
Serge
Serge 2022 年 4 月 22 日
編集済み: Serge 2022 年 4 月 23 日
Great find!
Here it is package as a function with a couple more options I use often.
function figsave(fig,file,rez,txt,bak)
%Save figure as image, with custom resolutions and text scaling.
% figsave(fig,file,rez,txt,bak)
%
%Example:
% clf,text(0.1,0.5,{'This text should be';'50 pixels high';'and the image';'900W x 600H pix'},'FontSize',50)
% figsave(gcf,'Figure.jpg',[900 600])
if nargin<1 || isempty(fig), fig = gcf; end %figure handle
if nargin<2 || isempty(file), file = 'Figure.jpg'; end %output file name
if nargin<3 || isempty(rez), rez = [900 600]; end %resolution [horizontal vertical]
if nargin<4 || isempty(txt), txt = 1; end %text scale factor
if nargin<5 || isempty(bak), bak = 1; end %preserve background colour
set(fig,'PaperPosition',[0 0 rez/(100*txt)],'PaperUnits','inches'); %set paper size (does not affect display)
if bak
set(fig,'InvertHardcopy','off'); %preserve background colour
end
imwrite(print(gcf,'-RGBImage',['-r' num2str(100*txt,'%f')]),file) %print RGB image to file (slow)
end

サインインしてコメントする。


Klaus Förger
Klaus Förger 2016 年 3 月 14 日
The solution that I found was to use trial and error to search for the correct input for the command: figure('Position', [100 100 x(1) x(2)]);
This can be done automatically by saving the function reso_test to a file:
function [ output_args ] = reso_test( resolution ) close all; figure('position', [100 100 max(resolution(1), 1) max(resolution(2),1) ]); frame = getframe(gcf); output_args(1) = size(frame.cdata, 1); output_args(2) = size(frame.cdata, 2); end
And then running the following script:
target_resolution = [401 401]; options = optimset ('Display', 'iter', 'TolFun', 0.1, 'TolX', 0.1); x = fminsearch(@(x)(sum(abs(reso_test(x) - target_resolution))), target_resolution, options);
Finally, the x contains the input to get the target resolution.
My solution is very inefficient, but I could not think of anything else that would be platform independent.

Matthew Tarabulski
Matthew Tarabulski 2017 年 12 月 29 日
If you don't need to do this programatically this will work:
1) Figure Window -> File -> Export Settings -> Size: points
2) set your height and width exactly
3) click "apply to figure".
  1 件のコメント
Manoj Payani
Manoj Payani 2018 年 1 月 5 日
Though we are able to set the pixels, the final output is not as we set. Ex - I set the pixels to 1920*1080 but the output is 1924*994. Can u pls help?

サインインしてコメントする。


Syed Abuzar Shah
Syed Abuzar Shah 2018 年 3 月 15 日
編集済み: Syed Abuzar Shah 2018 年 3 月 15 日
>> I=imread('1.jpg');
>> size(I)
ans =
460 819
this will be your image size in pixels 460*819
Must try and give feedback!

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by