Search code examples
.netopencvvisual-c++c++-cli

How to use loop to Process all images present in folder in visual C++


Goal : Perform operation on all images one by one present in folder

till now : got name of first file using objOpenFileDialog->ShowDialog(); and then
System::String^ imgName = objFileDialog->FileName;

problem :
1. blank image.data by Mat image = imread("imgName", CV_LOAD_IMAGE_GRAYSCALE);
2. How to use loop to reach up to last image (image names are in numbers with special characters)?


Solution

  • I had the same problem as you and got a way to fix it but do not know if it is the right way to do it.

    char * filepath = new char[100];
    for(int j=0; j<n_images ; j++) // for all images
    {
        sprintf(filepath,"/home/datasets/img-%i.png",j); // changing the path
        Mat image = imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
    
    your code here (.....)
    
    }
    

    My images are named "img-0.png", "img-1.png", "img-2.png", etc. You can adapt the code to the name you want.

    Run time number of images version:

    Mat image = imread("/home/datasets/img-0.png", CV_LOAD_IMAGE_GRAYSCALE );
    int j=0;
    while(image.data)
    {
        sprintf(filename,"/home/datasets/img-%i.png",j);
        image = imread(filename, CV_LOAD_IMAGE_GRAYSCALE );
        if(!image.data )
        {
            // no more images
            break;
        }
    
        ( your code... )
    
        j++;
    }