MATLAB Answers


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

Asked by John
on 20 Aug 2013
Latest activity Commented on by John
on 26 Nov 2013


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
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));
      fileToRead2 = [dname '\' file_name];
      axis tight
      hold off
      h = getframe(gcf);     
      aviobj = addframe(aviobj,h);    
      a = a+1;
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 Simon
on 26 Aug 2013

The brute clearing header "clear all, close all, clc" reduces the power of the debugger for no real benefit.

1 Answer

Answer by Image Analyst
on 20 Aug 2013
 Accepted answer

Try this code:

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
% 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)
	listOfFolderNames = [listOfFolderNames singleSubFolder];
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);
		fprintf('     Folder %s has no image files in it.\n', thisFolder);


on 26 Nov 2013

I sent you a private message through MathWorks needing help on something else, but you always replied quickly when posting to a topic (maybe my message didn't go through).

It was regarding how to measure the radius of curvature of a crater or dimple on a surface (rounded surface with an indention in it, need to measure the indention's radius of curvature).

Never saw a message. I don't get or use email for this account. Why don't you start a new discussion and post some images and your code?

on 26 Nov 2013

Sorry, didn't know. I posted the new question here

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!