在 MATLAB 中制作动画时如何修复帧大小错误
How to Fix Frame Size Error When Making Animation in MATLAB
我有这个代码来创建动画:
obj = VideoWriter(pvideo);
open(obj);
pic=dir(strcat(outputs,'/','*.tif'));
num=length(pic);
for i=1:num
im = imread(strcat(outputs,'/',pic(i).name));
writeVideo(obj,im);
end
close(obj)
它给我以下错误:
Error using VideoWriter/writeVideo (line 368)
Frame must be 1768 by 1326
我知道我的视频尺寸不对,但我不确定我的代码的哪一部分需要更改才能解决这个问题。
可能尺寸不匹配
我认为一个可能的问题是合并到视频文件中的图像可能 resolution/dimensional 大小不匹配。下面是一个从内置 MATLAB 图像创建视频文件的示例脚本。这里所有的图像都写成 500 x 500 的大小。如果其中一张图像不匹配,则会抛出错误。它们是处理问题的两种方法使用 imresize()
函数调整图像大小或使用 imcrop()
函数或索引 裁剪图像(这可以在 for 循环中完成)。下面脚本中的 500
维度之一是否会抛出相同的错误,这可能表明存在此问题。
%Creating test images to combine into video file%
Folder_Path = "";
Image = imresize(imread('moon.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_1.tif"));
Image = imresize(imread('circuit.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_2.tif"));
Image = imresize(imread('autumn.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_3.tif"));
%Starting to create video%
Pictures = dir(fullfile(Folder_Path,'*.tif'));
%Creating a video object to save the video structure to%
Video_Object = VideoWriter('Saved_Video.mp4','MPEG-4');
Video_Object.FrameRate = 1;
Video_Object.Quality = 100;
Number_Of_Frames = length(Pictures);
open(Video_Object);
%Scanning the frames into the video structure%
for Frame_Index = 1: Number_Of_Frames
writeVideo(Video_Object,imread(fullfile(Folder_Path,Pictures(Frame_Index).name)));
end
close(Video_Object);
implay('Saved_Video.mp4');
我有这个代码来创建动画:
obj = VideoWriter(pvideo);
open(obj);
pic=dir(strcat(outputs,'/','*.tif'));
num=length(pic);
for i=1:num
im = imread(strcat(outputs,'/',pic(i).name));
writeVideo(obj,im);
end
close(obj)
它给我以下错误:
Error using VideoWriter/writeVideo (line 368)
Frame must be 1768 by 1326
我知道我的视频尺寸不对,但我不确定我的代码的哪一部分需要更改才能解决这个问题。
可能尺寸不匹配
我认为一个可能的问题是合并到视频文件中的图像可能 resolution/dimensional 大小不匹配。下面是一个从内置 MATLAB 图像创建视频文件的示例脚本。这里所有的图像都写成 500 x 500 的大小。如果其中一张图像不匹配,则会抛出错误。它们是处理问题的两种方法使用 imresize()
函数调整图像大小或使用 imcrop()
函数或索引 裁剪图像(这可以在 for 循环中完成)。下面脚本中的 500
维度之一是否会抛出相同的错误,这可能表明存在此问题。
%Creating test images to combine into video file%
Folder_Path = "";
Image = imresize(imread('moon.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_1.tif"));
Image = imresize(imread('circuit.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_2.tif"));
Image = imresize(imread('autumn.tif'),[500 500]);
imwrite(Image,fullfile(Folder_Path,"Image_3.tif"));
%Starting to create video%
Pictures = dir(fullfile(Folder_Path,'*.tif'));
%Creating a video object to save the video structure to%
Video_Object = VideoWriter('Saved_Video.mp4','MPEG-4');
Video_Object.FrameRate = 1;
Video_Object.Quality = 100;
Number_Of_Frames = length(Pictures);
open(Video_Object);
%Scanning the frames into the video structure%
for Frame_Index = 1: Number_Of_Frames
writeVideo(Video_Object,imread(fullfile(Folder_Path,Pictures(Frame_Index).name)));
end
close(Video_Object);
implay('Saved_Video.mp4');