Skip to content

Access a Folder Containing Images

Andrés Solís Montero edited this page Oct 18, 2015 · 3 revisions

Access a Folder Containing Images

You can specify the path of a folder containing images by doing the following:

int main(int argc, const char * argv[])
{
    
    auto processFrame = [](size_t frameN, Mat &input, Mat &output){
        GaussianBlur(input, output, Size(7,7), 1.5, 1.5);
        Canny(output, output, 0, 30, 3);
    };
    
    Ptr<Input> input = new ImageListInput("path/to/folder/");
  
    Processor processor;
    processor.setInput(input);
    processor.setProcess(processFrame);
    processor.run();
    
    return 0;
}

The ImageListInput class takes a string as its first argument, filename - path to the folder containing the images (e.g. "path/to/folder/") and list them in alphabetically order. Only images files will be processed from this folder. Alternatively, you could specify a vector of string with path to single files instead of a containing folder.

The setInput method sets the Processor class to use the input instance (i.e. ImageListInput).

processor.setInput(input)

You can force the image size of your input by passing a second argument specifying the desired resolution. If you would like to use the image original resolutions a value of Size(-1,-1) should be passed (i.e. default value Size(-1,-1)).

Ptr<Input> input = new ImageListInput("path/to/folder/", Size(640,480));

A third argument specifies the OpenCV color space conversion code to apply to the images. By default no code conversion is applied to the images generated by the input device (i.e. default value - 1).

Ptr<Input> input = new ImageListInput("path/to/folder/", Size(640,480),CV_RGB2GRAY);

This is useful in the case your image sequence files are in a different color spectrum to the one you would like to use in your algorithm. (e.g. CV_GRAY2RGB, CV_RGB2HLS, ... etc).

NOTE: The same conversion will be applied to every image inside of the folder. This might not be the desired output if your images are in different space colors. You should build your own Input Class managing this scenario.