Image frames in wrong order when playing back as video

2 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 2 Jul 2013
Edited: Jan on 2 Jul 2013
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.

More Answers (0)

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!