Search code examples
imageidl-programming-language

Reading multiple images in IDL


I am writing a program in IDL that requires reading n images (each of m pixels) from a directory, convert them to grayscale, concatenate each image as a single vector, and then form a an m * n matrix from the data.

So far I have managed to read and convert a single image to a grayscale vector, but I can't figure out how to extend this to reading multiple image files.

Can anyone advise on how I could adapt my code in order to do this? (The image files will all be of the same size, and stored in the same directory with convenient filenames - i.e. testpicture1, testpicture2, etc)

Thanks

    pro readimage

    image = READ_IMAGE('Z:\My Documents\testpicture.jpg')

    redChannel = REFORM(image[0, *, *])
    greenChannel = REFORM(image[1, * , *])
    blueChannel = REFORM(image[2, *, *])

    grayscaleImage  = BYTE(0.299*FLOAT(redChannel) + $
        0.587*FLOAT(greenChannel) + 0.114*FLOAT(blueChannel))

    imageVec = grayscaleImage[*]

    end

Solution

  • Use FILE_SEARCH to find the names and number of the images of the given name:

    filenames = FILE_SEARCH('Z:\My Documents\testpicture*.jpg', count=nfiles)
    

    You will probably also want to declare an array to hold your results:

    imageVec = bytarr(m, nfiles)
    

    Then loop over the files with a FOR loop doing what you are doing already:

    for f = 0L, nfiles - 1L do begin
      ; stuff you are already doing
      imageVec[*, f] = grayscaleImage[*]
    endfor