Error by creating video from images

2 views (last 30 days)
Serhii Tetora
Serhii Tetora on 8 Sep 2020
Commented: Serhii Tetora on 13 Sep 2020
I'm creating images from a data in parfor-loop. Then i want to create a video from this images, but have some error. I don't understand, why the frame sizes are not equal. In first case (line 110) is all rigth, but in the second case some images have different sizes. Thanks in advance for your help!
clear; close all;clc
bilder.p_c = 1; % Druck
Limschalter = 1;
MakeVideo = 1;
% Bildereinstellungen
im.width=9; % Bildbreite [cm]
im.height=6; % Bildhöhe [cm]
im.cmapclass = 60; % Zahl von Farben in colorbar % früher 10
im.cmap = 'jet';
im.res = 600; % dpi
TeilungCB=5;
% globale Schriftart
font.name = 'Arial';font.size = 9;
%% FB-Einstellungen - wichtig!!!
maxLS = 20; % Anzahl der Lastschritten
start = 4; % Anfangsschritt
endstep = maxLS+4; % Endschritt
%% Voraussetzung der Limits
% Limits für Innenring
X_IR_limits = nan(1,2); Y_IR_limits = nan(1,2);
pc_lim_IR = nan(1,2);
% Limits für Aussenring
X_AR_limits = nan(1,2); Y_AR_limits = nan(1,2);
pc_lim_AR = nan(1,2);
%%
folder = '.\Sim_80';
im.folder = '\export_bilder_farbe';
load('data.mat')
%% Temporary limits
% Limits für Innenring
X_IR_lim_tmp = roundn([min(xData_IR(:,:,start:end),[],'all') max(xData_IR(:))],-2);
Y_IR_lim_tmp = roundn([min(yData_IR(:,:,start:end),[],'all') max(yData_IR(:))],-2);
pc_lim_IR_tmp = [0 max(p_c_IR,[],'all')];
% Limits für Aussenring
X_AR_lim_tmp = roundn([min(xData_AR(:,:,start:end),[],'all') max(xData_AR(:))],-2);
Y_AR_lim_tmp = roundn([min(yData_AR(:,:,start:end),[],'all') max(yData_AR(:))],-2);
pc_lim_AR_tmp = [0 max(p_c_AR,[],'all')];
%% Global limits
% Limits für Innenring
Y_IR_limits = [min(Y_IR_lim_tmp(1),Y_IR_limits(1)) max(Y_IR_lim_tmp(2),Y_IR_limits(2))];
X_IR_limits = [8.8 11];
pc_lim_IR = [0 max(pc_lim_IR_tmp(2),pc_lim_IR(2))];
% Limits für Aussenring
Y_AR_limits = [min(Y_AR_lim_tmp(1),Y_AR_limits(1)) max(Y_AR_lim_tmp(2),Y_AR_limits(2))];
X_AR_limits = [4.3 5.9];
pc_lim_AR = [0 max(pc_lim_AR_tmp(2),pc_lim_AR(2))];
%% Clearing von Variablen
clear var xData_IR yData_IR p_c_IR xData_AR yData_AR p_c_AR
%% LimIR = LimAR
if Limschalter
pc_lim_IR = [0 max(pc_lim_IR(2),pc_lim_AR(2))];
pc_lim_AR = pc_lim_IR;
end
%% Zeichenbereiche erzeugen
% Create figures
hfig_IR = figure('PaperUnits','centimeters','PaperSize',[im.width im.height],...
'PaperPosition',[0 0 im.width im.height],'Units','centimeters',...
'Position',[2 15 im.width im.height],'NumberTitle','off',...
'name','Innenring','NextPlot','replacechildren',...
'color', [1 1 1],'menubar','none');
hfig_AR = figure('PaperUnits','centimeters','PaperSize',[im.width im.height],...
'PaperPosition',[0 0 im.width im.height],'Units','centimeters',...
'Position',[2 2 im.width im.height],'NumberTitle','off',...
'name','Aussenring','NextPlot','replacechildren',...
'color', [1 1 1],'menubar','none');
% Set outer bounds
set(hfig_IR, 'OuterPosition',[2 15 im.width im.height])
set(hfig_AR, 'OuterPosition',[2 2 im.width im.height])
% Create axes
hax_AR = axes('Fontname',font.name,'parent',hfig_AR,...
'Units', 'normalized', 'OuterPosition', [.01 .01 .99 .99],...
'NextPlot','replacechildren','Box','on');
hax_IR = axes('Fontname',font.name,'parent',hfig_IR,...
'Units', 'normalized', 'OuterPosition', [.01 .01 .99 .99],...
'NextPlot','replacechildren','Box','on');
pathV = [folder,'\export_video'];
load('data.mat')
%% Löschen von unbenutzten Dateien
clear var X_IR_limits_tmp Y_IR_limits_tmp pc_lim_IR_tmp...
X_AR_limits_tmp Y_AR_limits_tmp pc_lim_AR_tmp
%% p_c - Kontaktdrück
if bilder.p_c
CBarTickIR = linspace(pc_lim_IR(1),pc_lim_IR(2),TeilungCB);
CBarTickAR = linspace(pc_lim_AR(1),pc_lim_AR(2),TeilungCB);
cla(hax_IR);cla(hax_AR)
parfor k = start:endstep
MakePic_pc('p_c',1,xData_IR,yData_IR,p_c_IR,pc_lim_IR,CBarTickIR,X_IR_limits,Y_IR_limits,LS_IR,SS_IR,k,font,folder,im,hfig_IR,hax_IR)
MakePic_pc('p_c',2,xData_AR,yData_AR,p_c_AR,pc_lim_AR,CBarTickAR,X_AR_limits,Y_AR_limits,LS_AR,SS_AR,k,font,folder,im,hfig_AR,hax_AR)
end
if MakeVideo
appendix='\p_c';
pathB = [folder im.folder appendix];
create_video(pathB,pathV,appendix,'AR');
create_video(pathB,pathV,appendix,'IR');
end
end
function MakePic_pc(Valuename,flagR,xData,yData,value,value_lim,CBarTick,X_limits,Y_limits,LS,SS,k,font,folder,im,hfig,hax)
if flagR == 1
Ring = 'Innenring';
ring = 'IR';
elseif flagR == 2
Ring = 'Aussenring';
ring = 'AR';
end
fprintf('[%s %s] LS_%s SS_%s\n',Valuename,Ring,LS{k},SS{k});
figure(hfig);
contourf(xData(:,:,k),yData(:,:,k),value(:,:,k),im.cmapclass,'EdgeColor','none','Parent',hax);
colormap(jet);
caxis(value_lim);
haxes_cb=colorbar(hax,'Ylim',value_lim,'Location','northoutside');
haxes_cb.Label.String = 'Kontaktpressung p_c [MPa]';
haxes_cb.Label.FontName = font.name;
haxes_cb.Label.FontSize = font.size;
set(haxes_cb,'Ytick',CBarTick,'YTickLabel',round(CBarTick));
set(hax,'XLim',X_limits,'YLim',roundn(Y_limits,-2));
hax.XTick = roundn(linspace(X_limits(1),X_limits(2),6),-1);
hax.YTick = roundn([linspace(Y_limits(1),0,3) linspace(Y_limits(2)/2,Y_limits(2),2)],-2);
text(0.99,0.01,sprintf('p_{c,max}=%dMPa',round(max(value,[],'all'))),'Units','normalized',...
'Fontname',font.name,'Fontsize',font.size-1,'HorizontalAlignment','right',...
'VerticalAlignment','bottom','BackgroundColor','w','Margin',0.01,'Parent',hax)
set(hax,'color', [1 1 1],'Fontname',font.name,'Fontsize',font.size,...
'PlotBoxAspectRatio',[1 1 1],'DataAspectRatio',[1 1 1]);
im_folderB=sprintf('%s/%s',im.folder,Valuename);
if(~isfolder(strcat(folder,im_folderB)))
mkdir(strcat(folder,im_folderB));
end
exportgraphics(hfig,sprintf('%s/%s/%s__%03i_%s__LS%s_SS%s.png',folder,im_folderB,ring,k,Valuename,LS{k},SS{k}),'Resolution',im.res)
end
function create_video(pathB,pathV,appendix,ring)
if(~isfolder([pathV,appendix]))
mkdir([pathV,appendix]);
end
images = dir(fullfile(pathB,[ring,'*.png']));
video = VideoWriter(fullfile(pathV,appendix,[appendix,'_',ring]),'Motion JPEG AVI');
video.Quality = 100;
video.FrameRate = 5;
open(video);
for ii=1:length(images)
I = imread(fullfile(pathB,images(ii).name));
writeVideo(video,I);
end
close(video);
end
Error using VideoWriter/writeVideo (line 368)
Frame must be 1829 by 1125
Error in code>create_video (line 169)
writeVideo(video,I);
Error in code (line 111)
create_video(pathB,pathV,appendix,'IR');
  1 Comment
Serhii Tetora
Serhii Tetora on 11 Sep 2020
I made this code shorter
clear; close all;clc
load('data_1.mat')
pathV = [folder,'\export_video'];
%% Create figures
hfig_IR = figure('PaperUnits','centimeters','PaperSize',[im.width im.height],...
'PaperPosition',[0 0 im.width im.height],'Units','centimeters',...
'Position',[2 15 im.width im.height],'NumberTitle','off',...
'name','Innenring','NextPlot','replacechildren',...
'color', [1 1 1],'menubar','none');
hfig_AR = figure('PaperUnits','centimeters','PaperSize',[im.width im.height],...
'PaperPosition',[0 0 im.width im.height],'Units','centimeters',...
'Position',[2 2 im.width im.height],'NumberTitle','off',...
'name','Aussenring','NextPlot','replacechildren',...
'color', [1 1 1],'menubar','none');
% Set outer bounds
set(hfig_IR, 'OuterPosition',[2 15 im.width im.height])
set(hfig_AR, 'OuterPosition',[2 2 im.width im.height])
% Create axes
hax_AR = axes('Fontname',font.name,'parent',hfig_AR,...
'Units', 'normalized', 'OuterPosition', [.01 .01 .99 .99],...
'NextPlot','replacechildren','Box','on');
hax_IR = axes('Fontname',font.name,'parent',hfig_IR,...
'Units', 'normalized', 'OuterPosition', [.01 .01 .99 .99],...
'NextPlot','replacechildren','Box','on');
%% p_c - Kontaktdrück
if bilder.p_c
CBarTickIR = linspace(pc_lim_IR(1),pc_lim_IR(2),TeilungCB);
CBarTickAR = linspace(pc_lim_AR(1),pc_lim_AR(2),TeilungCB);
cla(hax_IR);cla(hax_AR)
parfor k = start:endstep
MakePic_pc('p_c',1,xData_IR,yData_IR,p_c_IR,pc_lim_IR,CBarTickIR,X_IR_limits,Y_IR_limits,LS_IR,SS_IR,k,font,folder,im,hfig_IR,hax_IR)
MakePic_pc('p_c',2,xData_AR,yData_AR,p_c_AR,pc_lim_AR,CBarTickAR,X_AR_limits,Y_AR_limits,LS_AR,SS_AR,k,font,folder,im,hfig_AR,hax_AR)
end
if MakeVideo
appendix='\p_c';
pathB = [folder im.folder appendix];
create_video(pathB,pathV,appendix,'AR');
create_video(pathB,pathV,appendix,'IR');
end
end
function MakePic_pc(Valuename,flagR,xData,yData,value,value_lim,CBarTick,X_limits,Y_limits,LS,SS,k,font,folder,im,hfig,hax)
if flagR == 1
Ring = 'Innenring';
ring = 'IR';
elseif flagR == 2
Ring = 'Aussenring';
ring = 'AR';
end
fprintf('[%s %s] LS_%s SS_%s\n',Valuename,Ring,LS{k},SS{k});
figure(hfig);
contourf(xData(:,:,k),yData(:,:,k),value(:,:,k),im.cmapclass,'EdgeColor','none','Parent',hax);
colormap(jet);
caxis(value_lim);
haxes_cb=colorbar(hax,'Ylim',value_lim,'Location','northoutside');
haxes_cb.Label.String = 'Kontaktpressung p_c [MPa]';
haxes_cb.Label.FontName = font.name;
haxes_cb.Label.FontSize = font.size;
set(haxes_cb,'Ytick',CBarTick,'YTickLabel',round(CBarTick));
set(hax,'XLim',X_limits,'YLim',roundn(Y_limits,-2));
hax.XTick = roundn(linspace(X_limits(1),X_limits(2),6),-1);
hax.YTick = roundn([linspace(Y_limits(1),0,3) linspace(Y_limits(2)/2,Y_limits(2),2)],-2);
text(0.99,0.01,sprintf('p_{c,max}=%dMPa',round(max(value,[],'all'))),'Units','normalized',...
'Fontname',font.name,'Fontsize',font.size-1,'HorizontalAlignment','right',...
'VerticalAlignment','bottom','BackgroundColor','w','Margin',0.01,'Parent',hax)
set(hax,'color', [1 1 1],'Fontname',font.name,'Fontsize',font.size,...
'PlotBoxAspectRatio',[1 1 1],'DataAspectRatio',[1 1 1]);
im_folderB=sprintf('%s/%s',im.folder,Valuename);
if(~isfolder(strcat(folder,im_folderB)))
mkdir(strcat(folder,im_folderB));
end
exportgraphics(hfig,sprintf('%s/%s/%s__%03i_%s__LS%s_SS%s.png',folder,im_folderB,ring,k,Valuename,LS{k},SS{k}),'Resolution',im.res)
end
function create_video(pathB,pathV,appendix,ring)
if(~isfolder([pathV,appendix]))
mkdir([pathV,appendix]);
end
images = dir(fullfile(pathB,[ring,'*.png']));
video = VideoWriter(fullfile(pathV,appendix,[appendix,'_',ring]),'Motion JPEG AVI');
video.Quality = 100;
video.FrameRate = 5;
open(video);
for ii=1:length(images)
I = imread(fullfile(pathB,images(ii).name));
writeVideo(video,I);
end
close(video);
end
Error using VideoWriter/writeVideo (line 368)
Frame must be 1829 by 1125
Error in code>create_video (line 103)
writeVideo(video,I);
Error in code (line 44)
create_video(pathB,pathV,appendix,'IR');

Sign in to comment.

Answers (2)

ahcen aliouat
ahcen aliouat on 8 Sep 2020
Hello, there is difficulties in reading you code because of the used language in the comments, however, have you tried to redefine the path , as below for exemple:
pathB = [folder im.folder appendix];
create_video(pathB,pathV,appendix,'AR');
pathC = [folder im.folder appendix];
create_video(pathC,pathV,appendix,'IR');
  2 Comments

Sign in to comment.


Image Analyst
Image Analyst on 11 Sep 2020
I think your axes must be changing size or shape so your image is not the same size. Try not specifying the 'Resolution' option. Or else, call imresize() when you add each image to make sure it's the same size as the first one.
  1 Comment
Serhii Tetora
Serhii Tetora on 13 Sep 2020
But why it is changing size? I tried to plot for all steps in the loop on the same axes with same limits and same labels, title etc.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!