Trouble in making video from frames, can anyone help?
Show older comments
Hi,
I have around 1000 pictures which is like 30th frame from each second of 30 fps. I am trying to make a video with 1 fps hence I am using the code below but the problem is this code is not compiling the video in a sequence manner like frame30.jpg, frame60. jpg instead frames are messed up in video like starting from middle then finish then add the starting frames at the end. what i want is a it reads a frames from first to last hence made a video. I am not sure where I am making a mistake.
Code:
imgFile =dir('*.jpg');
N = length(imgFile);
% create the video writer with 1 FPS
writerObj = VideoWriter('compilingFrames.avi');
writeObj.FrameRate = 1;
%Open the video writer
open(writerObj);
%write the frame to the video
for i=1:N
img = imgFile(i).name;
I = imread(img);
writeVideo(writerObj,I);
end
% close the writer object
close(writerObj);
Accepted Answer
More Answers (1)
If your image file names are well behaved, you can do a simple sorting of the file names:
imgFile =dir('*.jpg');
imgFile = sort({imgFile.name}'); % sort the file name
N = length(imgFile);
% create the video writer with 1 FPS
writerObj = VideoWriter('compilingFrames.avi');
writeObj.FrameRate = 1;
%Open the video writer
open(writerObj);
%write the frame to the video
for i=1:N
img = imgFile{i}; %.name;
I = imreadimg);
writeVideo(writerObj,I);
end
% close the writer object
close(writerObj);
4 Comments
muhammad choudhry
on 31 Aug 2021
Chunru
on 31 Aug 2021
Change to the following line
%img = imgFile(i);
img = imgFile{i};
muhammad choudhry
on 31 Aug 2021
Chunru
on 31 Aug 2021
Yes. You can define the frame sequence with the order you want:
imgFile ={'filename1.jpg', 'filename2.jp', ....}
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!