Image frames in wrong order when playing back as video

2 ビュー (過去 30 日間)
Jean
Jean 2013 年 7 月 2 日
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.

採用された回答

Jan
Jan 2013 年 7 月 2 日
編集済み: Jan 2013 年 7 月 2 日
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 件のコメント
Jean
Jean 2013 年 7 月 2 日
Awesome! This worked perfectly. Didn't think of that. Thanks, Jan!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by