Use images to create video, script to read through hundreds of folders to do it

4 views (last 30 days)
Hi,
Task: I have been tasked to come up with a MATLAB script that takes tiff images in a folder and creates a movie from them.
Details: There are hundreds of folders, each containing two subfolders, that contain hundreds of images each. I need the script to... >Go into folder 1 >Go into subfolder A >Create a video using the hundreds of images there and save it in the folder >Go back out to folder 1 >Go into subfolder B >Create video like above >Go back out and into folder 2 and so on until it has gone through all of the folders.
I know... this is a big job
Problems: I created a folder on my desktop and downloaded pictures of numbers 1-10 as samples. I found a script that will go into that folder and create a video from the images. But that's it. I don't know if the video is created or saved anywhere, and all the viewer shows is the single images that are in the folder.
Here's the code I used... found it on here and linked to a video explaining it.
clear all
close all
clc
framesPerSec = Enter #;
movie_name = ('Enter name');
aviobj = avifile(movie_name,'fps',framesPerSec);
dname = ('C/blah/blah);
top_file = [dname '\'];
ls_top_file = ls(top_file);
c = cellstr(ls_top_file);
cc = c(3:length(c));
S = size(cc);
a = 1;
while a <= S(1)
close all
file = char(cellstr([top_file char(cc(a))]));
data_n = char(cc(a))
file_name = char(cc(a));
figure
fileToRead2 = [dname '\' file_name];
imshow((fileToRead2))
axis tight
hold off
h = getframe(gcf);
aviobj = addframe(aviobj,h);
a = a+1;
end
h = getframe(gcf);
aviobj = addframe(aviobj,h);
aviobj = close(aviobj);
Any tips on where to look, as I'm sure there are bits to my question that have been asked on here before.
  1 Comment
Jan
Jan on 26 Aug 2013
The brute clearing header "clear all, close all, clc" reduces the power of the debugger for no real benefit.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Aug 2013
And here's a demo to show you how to recurse into subfolders:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all PNG, JPG, and TIF images in
% that folder and all of its subfolders.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox\images\imdemos');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing image file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
  28 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!