0

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)?

user3042916
  • 87
  • 3
  • 12
  • but you do know, that you can't imread() with a System::String^ ? – berak Oct 26 '14 at 13:55
  • I had doubt over '^', but unable to resolve.Then how can i get "image.jpg" ? – user3042916 Oct 26 '14 at 15:01
  • you will have to use a c++ std::string, const char* or cv::String. opencv won't support managed System::String types. do yourself a favour, and have a clear watershed between your managed/forms code, and the opencv code. – berak Oct 26 '14 at 15:11
  • But, objFileDIalog->FileName return type is String^. – user3042916 Oct 26 '14 at 18:25
  • http://stackoverflow.com/questions/26535662/how-to-read-files-in-sequence-from-a-directory-in-opencv/26536198#26536198 – berak Oct 26 '14 at 18:30

2 Answers2

0

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++;
}
zedv
  • 1,459
  • 12
  • 23
  • what if you don't know number of images present in folder ? int n_images should be set at runtime... but don't know how. – user3042916 Oct 26 '14 at 15:05
0

This solution works for me

String folderpath = "images/*.jpg" //images is the folder where the images are stored
vector<String> filenames;
cv::glob(folderpath, filenames);

for (size_t i=0; i<filenames.size(); i++)
{
    Mat im = imread(filenames[i]);
    ... your processing here
}

From : https://answers.opencv.org/question/88141/how-to-loop-over-all-the-images-using-opencv-300-and-c/