-4

I have a folder that include some images.I dont have image file's names. I am using openCV with C++. I want to read any .jpeg image in the folder and take this image's file name as a string when i read the image. Is it possible? Can i use imread method for this?

I appreciate your helps

Inputvector
  • 960
  • 8
  • 21
  • 3
    Yes and yes. Also I hate to say that but if you google "imread opencv", the very first link is an example on how to open images, and the very second link is "imread" documentation. – SingerOfTheFall Feb 01 '17 at 08:27
  • Possible duplicate of [Can not read image in opencv](http://stackoverflow.com/questions/25904628/can-not-read-image-in-opencv) – Jonas Feb 01 '17 at 08:27
  • Any image in **folder**? You know, they are different things – Miki Feb 01 '17 at 08:34
  • @CodeJam, `i want to get the name as a string when i read` when you read what? Once you have the filename of the image file (you can get it any way you like and it's not related to opencv), you just pass that filename to `imread`. Do you want to open all images in a _folder_? If so, you should have stated that in your question because right now it's impossible to guess that. – SingerOfTheFall Feb 01 '17 at 08:35
  • @CodeJam, there is no "folder" word in your question, how do you want us to read your mind? – SingerOfTheFall Feb 01 '17 at 08:42
  • Also, is it possible for you to use boost, or do you want to do it without it? – SingerOfTheFall Feb 01 '17 at 08:42

1 Answers1

1

If you have access to boost:

boost::filesystem::directory_iterator itr(boost::filesystem::path("your_folder_path"));

for(itr; itr != boost::filesystem::directory_iterator(); ++itr)
{
    if(itr->path().extension().string() == ".jpg")
    {
        std::string imageFilename = itr->path().filename().string();
        imread(imageFilename);
    }
}

If you don't have/don't want to use boost, you can use cv::glob to get the list of files in a directory:

vector<String> filenames;

// Get all jpg in the folder
cv::glob("your/folder/name/*.jpg", filenames);

for (size_t i=0; i<filenames.size(); i++)
{
    Mat im = imread(filenames[i]);
}
Miki
  • 39,217
  • 13
  • 114
  • 193
SingerOfTheFall
  • 28,216
  • 8
  • 63
  • 102