Convert multiple images to Avi using Matlab

Source: Internet
Author: User

Some time ago, when I did nothing, I wrote a program to convert multiple images into AVI, because the MATLAB language was used for the convenience of graphs!

In fact, the entire program and process are very simple, that is, to read the images in a folder in sequence, and store their data in the AVI container!

 

The program contains two subprograms: getallfile. m ----- to find all the images contained in the folder

Pic2avi. m ----- display and save the image as an AVI file

 

The following code is directly used:

 

% Function: The program converts multiple images to the AVI format. Description: 1. you can choose whether to pre-process the image. If the image is of the same size and the image format, % You can skip this step directly. Otherwise, the image is preprocessed. % 2. step: % Step1: Set the parameter % step2: traverse all the images in the target folder, save the path information of the image as a struct % Step3: Read the image according to the set parameters, perform preprocessing % step4: Get the image information, convert it to Avi, and save % Step1: Set the parameter clear clcdisp ('step 1, settings of basic parameters ................... ') % folder where the image to be processed is stored. The subfolders can exist. dirname = 'd: \ MATLAB \ work \ study \ Data'; % directory where the processed folder is stored, the program automatically generates desname = 'd: \ MATLAB \ work \ study \ pdata '; % Uniform size thesizex = 500; thesizey = 500; thesizez = 3; ischangeszie = 0; % use RGB or use gray image imagetype = 'Gray 'directly '; % set the common format of the image to FMt = '.bmp '; % step2: traverse all the images in the target folder and save the path information of the image as the structure disp ('step 2, retrieve all image files in the folder ................... ') % Note: Do not place other types of files in this folder; otherwise, an error will be reported. % get all files in the folder filelist = getallfiles (dirname); % Step3: start to pre-process the image disp ('step 3, start to process each image ................... ') % number of images img_num = length (filelist); If img_num = 0; error ('set text There are no images in the clip. Please check again... ') endfor II = 1: img_num % start traversing imgname = filelist {II}; I _img = imread (imgname); % convert to grayscale image [sizex, sizey, sizez] = size (I _img); If sizez = 3 & strcmp (imagetype, 'Gray ') I _img = rgb2gray (I _img); thesizez = 1; end if II = 1 disp (strcat ('current size range: ', num2str (sizex),' * ', num2str (sizey),'; \ n ')); elseif (II = 1) & (ischangeszie = 1) disp (strcat ('convert all images to: ', num2str (thesizex ),'*', num2str (thesizey ))); Ischange = input ('whether to change the size of the transform: Yes (1) No (0 )'); if ischange = 1 thesizex = input ('Enter the height of the normalized image: '); thesizey = input ('Enter the width of the normalized image :'); end elseif (II = 1) & (ischangeszie = 0) disp ('Do not change the image size '); end % normalization operation if ischangeszie = 1 I _img = imresize (I _img, [thesizex, thesizey], 'bilinear '); end % Save the processed image in a uniform name if II = 1 if not (exist (desname, 'dir') mkdir (desname); end imwrite (I _img, strcat (desname, '\', num2st R (II), FMT), FMT (2: End); endclear II I _img sizex sizey sizez filelist % step4: Get the image information, convert it to Avi, and save % to get all the files in the folder filelist = getallfiles (desname); % number of images img_num_out = length (filelist); If img_num_out = 0 | img_num_out ~ = Img_num error ('the number of images in the specified folder is incorrect. Please check again... ') end % creates an AVI container to save image data information aviobj = avifile ('mymovie. avi ', 'fps', 1); % mdata = zeros (thesizex, thesizey, thesizez, img_num_out, 'uint8'); for I = 1: img_num_out % mdata (:, :,:, I) = imread (filelist {I}); mdata = imread (filelist {I}); imshow (mdata) frame = getframe (GCA ); aviobj = addframe (aviobj, frame); endaviobj = close (aviobj );

 

Function filelist = getallfiles (dirname) % function: Specify the file path and obtain all the files contained in the path. % because of nested operations, therefore, you can obtain the file dirdata = Dir (dirname); % # Get the data for the current directorydirindex = [dirdata. isdir]; % # Find the index for directoriesfilelist = {dirdata (~ Dirindex). name} '; %' # Get a list of the filesif ~ Isempty (filelist) filelist = cellfun (@ (x) fullfile (dirname, x ),... % # prepend path to files filelist, 'uniformoutput', false); endsubdirs = {dirdata (dirindex ). name };%# get a list of the subdirectoriesvalidindex = ~ Ismember (subdirs ,{'. ','.. '}); % # Find index of subdirectories % # That are not '. 'or '.. 'For Idir = find (validindex) % # loop over valid subdirectories nextdir = fullfile (dirname, subdirs {Idir}); % # Get the subdirectory path filelist = [filelist; getallfiles (nextdir)]; % # recursively call getallfilesendend

Corresponding test programs and test images include instructions for use. Download them from my resources!

Http://download.csdn.net/detail/me_sky_2012/4345849

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.