Home | History | Annotate | Download | only in cpp
      1 /*
      2  * starter_imagelist.cpp
      3  *
      4  *  Created on: Nov 23, 2010
      5  *      Author: Ethan Rublee
      6  *
      7  * A starter sample for using opencv, load up an imagelist
      8  * that was generated with imagelist_creator.cpp
      9  * easy as CV_PI right?
     10  */
     11 #include "opencv2/imgcodecs.hpp"
     12 #include "opencv2/highgui/highgui.hpp"
     13 #include <iostream>
     14 #include <vector>
     15 
     16 using namespace cv;
     17 using namespace std;
     18 
     19 //hide the local functions in an unnamed namespace
     20 namespace
     21 {
     22 void help(char** av)
     23 {
     24   cout << "\nThis program gets you started being able to read images from a list in a file\n"
     25           "Usage:\n./" << av[0] << " image_list.yaml\n"
     26        << "\tThis is a starter sample, to get you up and going in a copy pasta fashion.\n"
     27        << "\tThe program reads in an list of images from a yaml or xml file and displays\n"
     28        << "one at a time\n"
     29        << "\tTry running imagelist_creator to generate a list of images.\n"
     30         "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
     31 }
     32 
     33 bool readStringList(const string& filename, vector<string>& l)
     34 {
     35   l.resize(0);
     36   FileStorage fs(filename, FileStorage::READ);
     37   if (!fs.isOpened())
     38     return false;
     39   FileNode n = fs.getFirstTopLevelNode();
     40   if (n.type() != FileNode::SEQ)
     41     return false;
     42   FileNodeIterator it = n.begin(), it_end = n.end();
     43   for (; it != it_end; ++it)
     44     l.push_back((string)*it);
     45   return true;
     46 }
     47 
     48 int process(vector<string> images)
     49 {
     50     namedWindow("image", WINDOW_KEEPRATIO); //resizable window;
     51     for (size_t i = 0; i < images.size(); i++)
     52     {
     53         Mat image = imread(images[i], IMREAD_GRAYSCALE); // do grayscale processing?
     54         imshow("image",image);
     55         cout << "Press a key to see the next image in the list." << endl;
     56         waitKey(); // wait indefinitely for a key to be pressed
     57     }
     58     return 0;
     59 }
     60 
     61 }
     62 
     63 int main(int ac, char** av)
     64 {
     65 
     66   if (ac != 2)
     67   {
     68     help(av);
     69     return 1;
     70   }
     71   std::string arg = av[1];
     72   vector<string> imagelist;
     73 
     74   if (!readStringList(arg,imagelist))
     75   {
     76     cerr << "Failed to read image list\n" << endl;
     77     help(av);
     78     return 1;
     79   }
     80 
     81   return process(imagelist);
     82 }
     83