tags:

views:

41

answers:

1

I have to run an image processing algorithm on numerous images in a directory.

An image is saved as name_typeX.tif, so there are X different type of images for a given name.

The image processing algorithm takes an input image and outputs an image result.

I need to save this result as name_typeX_number.tif, where 'number' is also an output from the algorithm for a given image.

Now..

How do I tell MATLAB to open a specific 'typeX' file? Also note that there are other non-tif files in the same directory.

How to save the result as name_typeX_number.tif?

The results have to be saved in the same directory where the input images are present. How do I tell MATLAB NOT to treat the results that have been saved as an input images?

I have to run this as background code on a server... so no user inputs allowed

Thanks

+3  A: 

It sounds like you are wanting to get all files in a directory whose names match a certain format, then process them all automatically. You can do this using the function DIR to get a list of file names in the current directory, then using the function REGEXP to find file names that match a certain pattern. Here's an example:

fileData = dir();             %# Get a structure of data for the files in the
                              %#   current directory
fileNames = {fileData.name};  %# Put the file names in a cell array
index = regexp(fileNames,...                 %# Match a file name if it begins
               '^[A-Za-z]+_type\d+\.tif$');  %#   with at least one letter,
                                             %#   followed by `_type`, followed
                                             %#   by at least one number, and
                                             %#   ending with '.tif'
inFiles = fileNames(~cellfun(@isempty,index));  %# Get the names of the matching
                                                %#   files in a cell array

Once you have a cell array of files in inFiles that matches the naming pattern you want, you can simply loop over the files and perform your processing. For example, your code might look like this:

nFiles = numel(inFiles);    %# Get the number of input files
for iFile = 1:nFiles        %# Loop over the input files
  inFile = inFiles{iFile};  %# Get the current input file
  inImg = imread(inFile);   %# Load the image data
  [outImg,someNumber] = process_your_image(inImg);  %# Process the image data
  outFile = [strtok(inFile,'.') ...   %# Remove the '.tif' from the input file,
             '_' ...                  %#   append an underscore,
             num2str(someNumber) ...  %#   append the number as a string, and
             '.tif'];                 %#   add the `.tif` again
  imwrite(outImg,outFile);  %# Write the new image data to a file
end

The above example uses the functions NUMEL, STRTOK, NUM2STR, IMREAD, and IMWRITE.

gnovice
Thanks gnovice! On line 3index = regexp(fileNames,'[A-Za-z]+_type\d+\.tif');what if I want the number to be 1, 2 and 5 ( instead of all the other numbers available). I haven't ran the your code yet.. busy googling all the functions you've used :P but is the "+" part of the code?
its-me
@its-me: If you want to match *only one* of the numbers 1, 2, or 5 after the word `type`, you can remove the `\d+` and replace it with `[125]`. The `+` is a quantifier indicating that the preceding character(s) be matched 1 or more times. I also added some extra links for the function documentation to my answer.
gnovice
Perfect answer!
its-me

related questions