Making movie from images

2 ビュー (過去 30 日間)
Ali Kareem
Ali Kareem 2016 年 3 月 12 日
コメント済み: Geoff Hayes 2016 年 3 月 13 日
Hello.
Please,I have a folder with a name (Photos) and it is containing four subfolders with names (Order1,Order2,Order3,Order4). each folder containing 100 images. I am trying to make a movie for each subfolder. I will get 4 movies. I used below code and its read images from each folder automatically and create movies and save it in the same folder. So, I got four movies. The problem that there is jump during movie so picture number 1 is repeated many times during the video at different times. Can anyone help me with this code? or if there are any other code that can accomplish this job.
startpath = pwd;
for k = 1:4
folder = fullfile(startpath, 'Photos', sprintf('Order%d', k));
cd(folder)
Files = dir('*.jpg');
NumFiles= size(Files,1);
Megamind_Images = uint8(zeros([600 1000 3 NumFiles*5]));
VideoObj = VideoWriter('Create_Video.mp4','MPEG-4');
VideoObj.FrameRate = 5;
VideoObj.Quality = 80;
count=1;
for i = 1 : NumFiles
I = imread(Files(i).name);
ResizeImg = imresize(I,[600 1000]);
for j = 1 : 5
Megamind_Images(:,:,:,count)=ResizeImg;
count = count + 1;
end
end
open(VideoObj);
writeVideo(VideoObj, Megamind_Images);
close(VideoObj);
end

採用された回答

Geoff Hayes
Geoff Hayes 2016 年 3 月 12 日
Ali - how do you know that your images are in the correct order? For example, what does
Files = dir('*.jpg');
return? I'm going to guess that your file names have a number in them, but you may not be getting the order that you expect. For example, the following loop creates a 100 (empty) text files
for k=1:100
fid = fopen(sprintf('test%d.txt',k),'wt');
fclose(fid);
end
Now when I do
dir('test*.txt')
I observe that
test18.txt test28.txt test38.txt test48.txt test58.txt test68.txt test78.txt test88.txt test98.txt
test1.txt test19.txt test29.txt test39.txt test49.txt test59.txt test69.txt test79.txt test89.txt test99.txt
test10.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test9.txt
test100.txt test20.txt test30.txt test40.txt test50.txt test60.txt test70.txt test80.txt test90.txt
test11.txt test21.txt test31.txt test41.txt test51.txt test61.txt test71.txt test81.txt test91.txt
test12.txt test22.txt test32.txt test42.txt test52.txt test62.txt test72.txt test82.txt test92.txt
test13.txt test23.txt test33.txt test43.txt test53.txt test63.txt test73.txt test83.txt test93.txt
test14.txt test24.txt test34.txt test44.txt test54.txt test64.txt test74.txt test84.txt test94.txt
test15.txt test25.txt test35.txt test45.txt test55.txt test65.txt test75.txt test85.txt test95.txt
test16.txt test26.txt test36.txt test46.txt test56.txt test66.txt test76.txt test86.txt test96.txt
test17.txt test27.txt test37.txt test47.txt test57.txt test67.txt test77.txt test87.txt test97.txt
Note the ordering of the files: 1,10,100,11-19,2,20-29,3,30-39,etc. with early images being inserted every ten images. If your files are named in a similar fashion then you will need to force the correct order or rename he files. If, in the code above, I had named the files as
for k=1:100
fid = fopen(sprintf('test%03d.txt',k),'wt');
fclose(fid);
end
where each integer is padded with up to three zeros, then I get the correct ordering of the files as
dir('test*.txt')
test001.txt test012.txt test023.txt test034.txt test045.txt test056.txt test067.txt test078.txt test089.txt test100.txt
test002.txt test013.txt test024.txt test035.txt test046.txt test057.txt test068.txt test079.txt test090.txt
test003.txt test014.txt test025.txt test036.txt test047.txt test058.txt test069.txt test080.txt test091.txt
test004.txt test015.txt test026.txt test037.txt test048.txt test059.txt test070.txt test081.txt test092.txt
test005.txt test016.txt test027.txt test038.txt test049.txt test060.txt test071.txt test082.txt test093.txt
test006.txt test017.txt test028.txt test039.txt test050.txt test061.txt test072.txt test083.txt test094.txt
test007.txt test018.txt test029.txt test040.txt test051.txt test062.txt test073.txt test084.txt test095.txt
test008.txt test019.txt test030.txt test041.txt test052.txt test063.txt test074.txt test085.txt test096.txt
test009.txt test020.txt test031.txt test042.txt test053.txt test064.txt test075.txt test086.txt test097.txt
test010.txt test021.txt test032.txt test043.txt test054.txt test065.txt test076.txt test087.txt test098.txt
test011.txt test022.txt test033.txt test044.txt test055.txt test066.txt test077.txt test088.txt test099.txt
  3 件のコメント
Image Analyst
Image Analyst 2016 年 3 月 12 日
What is the value of Files(i).name? Does it have an extension tacked onto the end of it? Otherwise, how will imread() know what kind of image it is?
Geoff Hayes
Geoff Hayes 2016 年 3 月 13 日
Ali - the code that I posted was for illustration only with the idea of demonstrating what perhaps the problem is. It just creates empty text files. When you run this code, it will create empty (zero byte) files with a jpg extension. If your files have been named as
Motion1.jpg
Motion2.jpg
...
Motion10.jpg
Motion11.jpg
...
then you will have to either rename them manually or perhaps do something like
textFiles = dir('Motion*.jpg');
for k=1:length(textFiles)
filename = textFiles(k).name;
newFilename = sprintf('Motion%03d.jpg',str2double(char(regexp(filename,'[0-9]','match'))));
if strcmp(filename,newFilename) ~= 1
movefile(filename,newFilename);
end
end
The above code looks for all files which match the Motion*.jpg wildcard. For each file, we extra the integer component using regexp and then create the new filename where the integer is padded with up to two zeros. For example,
Motion1.jpg
would become
Motion001.jpg
The above code will change the name (or move the files) so be aware of that before you execute it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by