MATLAB Answers

Jean
0

Image frames in wrong order when playing back as video

Asked by Jean
on 2 Jul 2013
Accepted Answer by Jan
Hello, I am doing a 3D plot based on intensity from a series of video frames I am extracting from a file. I have implemented a sort for the video frames by name, but I can't figure out how to tweak this code such that I can play the 3D animation in the right frame order. Here is the code:
%Create working directory and name
workingDir=tempname;
mkdir(workingDir);
mkdir(workingDir, 'images');
vids1=VideoReader('trimmedshort1_4.mov');
%select frames, extract, convert to intensity
for ii=1100:1200
img1=read(vids1,ii);
[img1index, map]=rgb2ind(img1, 256);
J=ind2gray(img1index,map);
I_I2=imcomplement(J);
%colormap(map);
%adjust intensity
J1 = imadjust(I_I2,[],[],5);
imshow(J1)
colormap(jet)
imwrite(J1,jet,fullfile(workingDir,'images',sprintf('img%d.jpg',ii)))
colormap(jet)
mesh(double(J1))
end
%sort into proper order
imageNames = dir(fullfile(workingDir,'images','*.jpg'));
disp(imageNames)
imageNames = {imageNames.name}';
imageStrings = regexp([imageNames{:}],'(\d*)','match');
imageNumbers = str2double(imageStrings);[~,sortedIndices] = sort(imageNumbers);
sortedImageNames = imageNames(sortedIndices);
disp(sortedImageNames)
%write video file
outputVideo = VideoWriter('vidout.avi');
outputVideo.FrameRate = vids1.FrameRate;
open(outputVideo);
for iii = 1:length(sortedImageNames)
img = imread(fullfile(workingDir,'images',sortedImageNames{iii}));
writeVideo(outputVideo,J1);
end
I am working on a Mac and can't read the output .avi file, also. thanks, I promise once I get better at this I will also try to be of some help to others.

  0 Comments

Sign in to comment.

1 Answer

Answer by Jan
on 2 Jul 2013
Edited by Jan
on 2 Jul 2013
 Accepted Answer

What about writing the index in the file names with leading zeros:
imwrite(J1, jet, fullfile(workingDir, 'images', sprintf('img%.5d.jpg', ii)));
Then the alphabetical order equals the numerical order.

  1 Comment

Awesome! This worked perfectly. Didn't think of that. Thanks, Jan!

Sign in to comment.