How to fix the Frame size error when creating a video.?

38 views (last 30 days)
Hello
Requesting help to fix the error encountered during the video creation.
Trying to generate a video by varying one parameter. Facing with the following error:
Error using VideoWriter/writeVideo (line 368)
Frame must be 977 by 771
Error in Movie (line 115)
writeVideo(vidObj,currFrame);
I did review the solution to a similar problem. But I couldnt use it to fix the mesh plot that I am generating. Below is my code:
%% Plotting the curve to be reflected
clear all
close all
clc
% Constants
I0 = 1; % Initial X-ray beam intensity
rCu = 900; % radius of copper wire, um
rPMMA = 400; % radius of the PMMA, um
lamda = 7.1255e-5; % x-ray wavelength, um, (17400 eV)
k = 2*pi/lamda; % wavenumber, um^-1
beta_Cu = 2.5914E-07; % https://henke.lbl.gov/optical_constants/
beta_PMMA = 5.0314E-10; % https://henke.lbl.gov/optical_constants/
mu_Cu = beta_Cu*2*k; % Absorption coefficient, mu=2*beta*k, um^-1
mu_PMMA = beta_PMMA*2*k; % Absorption coefficient, mu=2*beta*k, um^-1
%% Guass
xstart=-2;
xstop=2;
xpoints=200;
xinc=(xstop-xstart)/(xpoints-1);
ystart=-2;
ystop=2;
ypoints=200;
yinc=(xstop-xstart)/(xpoints-1);
for ii=1:xpoints
x(ii)=xstart+xinc*(ii-1);
for jj=1:ypoints
y(jj)=ystart+yinc*(jj-1);
z=x(ii)*x(ii)+y(jj)*y(jj);
Is(ii,jj)=exp(-z);
end
end
%% Computing intensity versus x-position
%I(x)= I0*exp(−(µCu*LCu(x)+µPMMA*LPMMA(x))]
%Assume wire long axis is along z-axis, with center at x=0.
%Assume x-rays travel in y direction.
rT=rCu+rPMMA; % Total radius of the copper wire ,um
x=(-2:.02:1.98)*rT; %x values (um) (range = +-2*rT)
t_Cu=2*sqrt(max(rCu^2-x.^2,0));
t_total=2*sqrt(max(rT^2-x.^2,0));
t_PMMA=t_total-t_Cu;
% mu_PMMA steps
% steps = (1:10:100);
steps =[1 10 20 30 40 50]
mu_PMMA_inc = (beta_PMMA*2*k)*steps;
for i=1:length(mu_PMMA_inc)
It(i,:)=exp(-mu_Cu*t_Cu-mu_PMMA_inc(i)*t_PMMA);
end
%% Movie
% Opening a figure and create the axes
figure
% Create and open the video object
nframes=6;
Frames=moviein(nframes);
vidObj = VideoWriter('Cu_wire_Xray.avi');
vidObj.FrameRate = 1; % frames per second.
open(vidObj);
% Looping over the data to create the video
ax = gca();
for j=1:nframes
Id = It(j,:).*Is; %Detector intensity
% Plot the data
h=mesh(Id);
xlabel('$x$ (mm)','interpreter','latex')
ylabel('$y$ (mm)','interpreter','latex')
zlabel('$I$','interpreter','latex')
title(['Absorptoion coefficient, mu_PMMA =',num2str(mu_PMMA_inc(j))])
% pause(0.1);
% STEP 2
% Getting the current frame
currFrame = getframe(gca);
size(currFrame.cdata);
% STEP 3
%
% Write the current frame
writeVideo(vidObj,currFrame);
%
delete(h)
end
% Closing (and save) the video object
close(vidObj);

Accepted Answer

Parsa
Parsa on 14 May 2022
Edited: Parsa on 14 May 2022
Hi Anand
I think these modifications are needed :
  • First of all, it's good to create an structure, containing #'nframe' rows with fileds, 'cdata' and 'colormap' , to storing movie frames. Indeed each row is corresponded to each frame.
  • The construction of frames in the loop is correct, but instead of using 'moviein' , which is not recommended in new virsions of Matlab, use 'getframe' inside the loop.
  • Also,using 'gca' does not preserve the labels and title in final movie file. Instead, you can use 'gcf' input for 'getframe' function, to capture whole of the figure.
  • After creating and storing the frames , you can set a path and a name for final video file , create a video object using 'VideoWriter' and specify the properties for the video object .
  • After all these steps and settings, now you could 'open' the video object , using 'writeVideo' function (of course outside of the 'for' loop) and 'close' the video object. Please be noted that the first argument of 'writeVideo' is the video object, and the second one is the structure of frames.
In the link below, there is some good examples of using 'getframe', especially the last one, which exactly belongs to your question, you could find another examples of using 'getframe' in the link below:
Here, I share the modified %% Movie section with you. Hope it will be helpful.
Best
Parsa
%% Movie
figure
nframes=6;
M(nframes) = struct('cdata',[],'colormap',[]);
for j=1:nframes
Id = It(j,:).*Is; %Detector intensity
h=mesh(Id);
xlabel('$x$ (mm)','interpreter','latex')
ylabel('$y$ (mm)','interpreter','latex')
zlabel('$I$','interpreter','latex')
title(['Absorptoion coefficient, mu_PMMA =',num2str(mu_PMMA_inc(j))])
M(j)=getframe(gcf);
end
%%
file_path='D:\New folder\';
file_name='Cu_wire_Xray.avi';
v = VideoWriter([file_path,file_name]);
v.FrameRate=1;
open(v)
writeVideo(v,M(:))
close(v)
  1 Comment
Anand Ra
Anand Ra on 14 May 2022
Thanks @Parsa. Would like to know how to add test to the plot.
Would like to slow down the video and saying. I
I have the plots looping for different mu_PMMA mentioned in the title.
I would like to include "Original mu_PMMA = XX" in red font on the right side of all the plots for reference.
And also have the numerica values displayed only in exponential form such as 8.3E-5,8.4E0-4 etc instead of decimals. Thanks for the help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!