imagelist_reader.cpp 2.1 KB

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