Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slimgsetprep
Home > sltoolbox > fileio > slimgsetprep.m

slimgsetprep

PURPOSE ^

SLIMGSETPREP organizes the images in a MATLAB friendly way

SYNOPSIS ^

function slimgsetprep(srcfolder, dstpath, matsize, maxsec)

DESCRIPTION ^

SLIMGSETPREP organizes the images in a MATLAB friendly way

 $ Syntax $
   - slimgsetprep(srcfolder, dstpath, matsize, maxsec) 

 $ Argument $
   - srcfolder:        the source file folder
   - dstpath:          the destination file (without extension name)
   - matsize:          the matrix size (row vector): [nrows, ncols]
   - maxsec:           the maximum section size

 $ Description $
   - slimgsetprep(srcfolder, dstpath, imgsiz, maxsec) prepares the matlab 
     files in destination for a set of images. The requirement is
       1) In source folder: there are all the images and a DSDML 
          description file named dataset.xml
       2) For destination, say abc, then there would be a core file named
          abc.xml. And if the actual data is separately stored, there are
          a series of files named abc.arr.<begin index>-<last index>. For
          example, abc.arr.0001-2000, means that the 1st t0 2000th samples
          are stored in the array file. 
       3) maxsec is the number of samples in each section. If maxsec is
          specified, the images are stored in separate array files. If 
          maxsec is not specified or empty, the images are stored in core
          file.
       4) The core file is a MAT file with following variables:
           'desc':         the DSDML data object descriptor
           'sections':     the starting and ending indices of all sections
           'matsize':      the row vector describing the size of images
           'data':         the image array
                           if imgs is numeric, it is the actual image
                           array.
                           if imgs is a cell array, it is the set of
                           array filenames.
   
 $ History $
   - Created by Dahua Lin, on Jul 26th, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • dataset DATASET Constructs a dataset object (conform to DSDML)
  • slwritearray SLWRITEARRAY Writes an array to an array file
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slimg2mat SLIMG2MAT Converts an image array to a double matrix
  • slpartition SLPARTITION Partition a range into blocks in a specified manner
This function is called by:

SOURCE CODE ^

0001 function slimgsetprep(srcfolder, dstpath, matsize, maxsec)
0002 %SLIMGSETPREP organizes the images in a MATLAB friendly way
0003 %
0004 % $ Syntax $
0005 %   - slimgsetprep(srcfolder, dstpath, matsize, maxsec)
0006 %
0007 % $ Argument $
0008 %   - srcfolder:        the source file folder
0009 %   - dstpath:          the destination file (without extension name)
0010 %   - matsize:          the matrix size (row vector): [nrows, ncols]
0011 %   - maxsec:           the maximum section size
0012 %
0013 % $ Description $
0014 %   - slimgsetprep(srcfolder, dstpath, imgsiz, maxsec) prepares the matlab
0015 %     files in destination for a set of images. The requirement is
0016 %       1) In source folder: there are all the images and a DSDML
0017 %          description file named dataset.xml
0018 %       2) For destination, say abc, then there would be a core file named
0019 %          abc.xml. And if the actual data is separately stored, there are
0020 %          a series of files named abc.arr.<begin index>-<last index>. For
0021 %          example, abc.arr.0001-2000, means that the 1st t0 2000th samples
0022 %          are stored in the array file.
0023 %       3) maxsec is the number of samples in each section. If maxsec is
0024 %          specified, the images are stored in separate array files. If
0025 %          maxsec is not specified or empty, the images are stored in core
0026 %          file.
0027 %       4) The core file is a MAT file with following variables:
0028 %           'desc':         the DSDML data object descriptor
0029 %           'sections':     the starting and ending indices of all sections
0030 %           'matsize':      the row vector describing the size of images
0031 %           'data':         the image array
0032 %                           if imgs is numeric, it is the actual image
0033 %                           array.
0034 %                           if imgs is a cell array, it is the set of
0035 %                           array filenames.
0036 %
0037 % $ History $
0038 %   - Created by Dahua Lin, on Jul 26th, 2006
0039 %
0040 
0041 %% Parse and verify input arguments
0042 
0043 if nargin < 3
0044     raise_lackinput('slimgsetprep', 3);
0045 end
0046 
0047 isseparate = false;
0048 if nargin >= 4
0049     isseparate = ~isempty(maxsec);
0050 end
0051 
0052 matsize = matsize(:)';
0053 if length(matsize) ~= 2
0054     error('sltoolbox:invalidarg', ...
0055         'imgsize should be a 2-element vector');
0056 end
0057 
0058 
0059 %% Read the dataset
0060 
0061 descrfile = [srcfolder, '\dataset.xml'];
0062 desc = dataset(descrfile);
0063 
0064 N = desc.numsamples;
0065 fns = desc.filenames;
0066 
0067 %% Process data
0068 
0069 if ~isseparate
0070     
0071     datasiz = [matsize, N];
0072     data = zeros(datasiz);
0073     
0074     for i = 1 : N
0075         img = imread([srcfolder, '\', fns{i}]);
0076         img = slimg2mat(img);
0077         data(:,:,i) = img;
0078     end
0079         
0080 else
0081     
0082     sections = slpartition(N, 'maxblksize', maxsec);    
0083     nsecs = length(sections.sinds);
0084     
0085     data = cell(nsecs, 1);    
0086     
0087     % process filenames
0088     dstdir = fileparts(dstpath);
0089     numlen = length(num2str(N));    
0090     if isempty(dstdir)  % local dst
0091         dstfnpat = [dstpath, sprintf('.arr.%%0%dd-%%0%dd', numlen, numlen)];
0092         dstfppre = [];
0093     else
0094         dstfnpat = [dstpath(length(dstdir)+2:end), sprintf('.arr.%%0%dd-%%0%dd', numlen, numlen)];
0095         dstfppre = [dstdir, '\'];
0096     end
0097     
0098     for i = 1 : nsecs        
0099         si = sections.sinds(i);
0100         ei = sections.einds(i);
0101         
0102         curfn = sprintf(dstfnpat, si, ei);
0103         curfp = [dstfppre, curfn];
0104         data{i} = curfn;
0105         
0106         curN = ei - si + 1;
0107         arr = zeros([matsize, curN]);
0108         
0109         for j = 1 : curN
0110             curidx = si + j - 1;
0111             img = imread([srcfolder, '\', fns{curidx}]);
0112             img = slimg2mat(img);        
0113             arr(:,:,j) = img;                                                
0114         end        
0115         
0116         slwritearray(arr, curfp);        
0117     end
0118         
0119 end
0120 
0121 
0122 %% Output core
0123 
0124 corefile = [dstpath, '.mat'];
0125 save(corefile, 'desc', 'sections', 'matsize', 'data', '-v6'); 
0126 
0127 
0128 
0129

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com