Figure to movie frame with set size

조회 수: 46 (최근 30일)
Oscar Frick
Oscar Frick 2018년 9월 30일
편집: Arthur 2023년 8월 3일
I'm generating movie frames from figures using the getframe function. However, I need the movie frame matrix output to be of a certain size (a set dimension in pixel units for each frame). I have tried
set(gca,'Units','pixels','Position',[10 10 sizeX sizeY])
But when I export the movie frame the cdata of the frame never seems to be the same size as sizeX and sizeY. There is change in the size of the frame, but it doesn't end up identical.
I know I could just resize the output frames to the size I want, but under some circumstances it resizes away edges and other effects mess up the final result, so it would be preferable if I could get the correct output size directly from the figure.

채택된 답변

Oscar Frick
Oscar Frick 2018년 10월 1일
Some digging led me to this previously asked question:
https://mathworks.com/matlabcentral/answers/272767-how-to-set-exact-figure-size-in-pixels
And it seems it indeed does have something to do with display scaling. I use a three display setup with two of the displays at 100% scaling (one of which is my designated main display) - but on the third display (the laptop display) I have 125% scaling. I changed the scaling on the laptop display to 100%, and now the example script gives the correct pixel size output. Changing it back, however, the output still stay the same (150 x 100 px). I have tried playing around with the scaling afterwards, but I have not gotten the incorrect scaling to return.

추가 답변 (4개)

Image Analyst
Image Analyst 2018년 9월 30일
What do you mean by resize and export? You mean "on screen" or the actual image size in pixels? And export how? To a disk file or to a movie variable?
See my movie resizing demo, attached.
  댓글 수: 1
Oscar Frick
Oscar Frick 2018년 9월 30일
getframe outputs a struct with field cdata and colormap. I need the cdata (matrix) output from getframe to be a certain size, for example 300 x 450 x 3 (the example being a 300x400 px RGB image).
Export might have been a poor choice of word in the context, my point is that when I set the figure axes to be a certain size in pixel, the image frame still comes out the wrong size.
As an example, the following script:
figure(1)
x = 0:0.01:pi;
y = sin(x);
plot(x,y) %just input to give frame some content
set(gca,'Units','pixels','Position', [20 20 100 150])
movieFrame = getframe;
figure(2)
imshow(movieFrame.cdata)
size(movieFrame.cdata)
Gives a movie frame matrix size of 188x125 px, not 150x100 px. I need a way to reliably control the frame size.
As I stated in my original post, I know about the option to resize after having fetched the frame, but due to certain problems with the results it would be preferable to me to omit that step and have the figure be the correct size from the beginning.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2018년 9월 30일
Try truesize(), or see the attached demo.
  댓글 수: 5
sam l
sam l 2020년 8월 25일
Hi
I have similare issue regarding the figure plot frame size controlling.
I am plotting a figure inside for looop - create a array to store the frame size.
Even I am using the gcf to enter the required frame size, the plot frame size is different and it changes after some steps. The figure plot has 4 subplots - three are in 3D plot..
Could you help if I can get a constant frame size, so I am able to create a vedio animation.
The frame size change - cdaData
Image Analyst
Image Analyst 2020년 8월 25일
You can call imresize() after you get thisFrame to set it to the exact size you want.

댓글을 달려면 로그인하십시오.


Eduardo Vicente Wolf Trentini
Eduardo Vicente Wolf Trentini 2022년 11월 26일
I had the same problem as you and finally managed to solve it.
The problem is the camera's field of view.
In this link I comment about:

Arthur
Arthur 2023년 8월 3일
편집: Arthur 2023년 8월 3일
Yes, display scaling will affect this. In Windows, it's in the settings. I tested that in a remote linux server the size is the one specified using fig.Position, since there's no display. If you want a scaling different from the display scaling, we can do
fig = figure();
frame = getframe(gcf);
img = frame2im(frame);
img = imresize(img, scale);
writeVideo(video, img);

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by