starter_imagelist.cpp 2 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10
/*
 * starter_imagelist.cpp
 *
 *  Created on: Nov 23, 2010
 *      Author: Ethan Rublee
 *
 * A starter sample for using opencv, load up an imagelist
 * that was generated with imagelist_creator.cpp
 * easy as CV_PI right?
 */
a  
Kai Westerkamp committed
11
#include "opencv2/highgui/highgui.hpp"
wester committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

//hide the local functions in an unnamed namespace
namespace
{
void help(char** av)
{
  cout << "\nThis program gets you started being able to read images from a list in a file\n"
          "Usage:\n./" << av[0] << " image_list.yaml\n"
       << "\tThis is a starter sample, to get you up and going in a copy pasta fashion.\n"
       << "\tThe program reads in an list of images from a yaml or xml file and displays\n"
       << "one at a time\n"
       << "\tTry running imagelist_creator to generate a list of images.\n"
        "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
}

bool readStringList(const string& filename, vector<string>& l)
{
  l.resize(0);
  FileStorage fs(filename, FileStorage::READ);
  if (!fs.isOpened())
    return false;
  FileNode n = fs.getFirstTopLevelNode();
  if (n.type() != FileNode::SEQ)
    return false;
  FileNodeIterator it = n.begin(), it_end = n.end();
  for (; it != it_end; ++it)
    l.push_back((string)*it);
  return true;
}

int process(vector<string> images)
{
wester committed
49 50 51 52 53 54 55 56 57
  namedWindow("image",CV_WINDOW_KEEPRATIO); //resizable window;
  for (size_t i = 0; i < images.size(); i++)
  {
    Mat image = imread(images[i], CV_LOAD_IMAGE_GRAYSCALE); // do grayscale processing?
    imshow("image",image);
    cout << "Press a key to see the next image in the list." << endl;
    waitKey(); // wait indefinitely for a key to be pressed
  }
  return 0;
wester committed
58 59 60 61 62 63
}

}

int main(int ac, char** av)
{
wester committed
64 65

  if (ac != 2)
wester committed
66 67 68 69
  {
    help(av);
    return 1;
  }
wester committed
70
  std::string arg = av[1];
wester committed
71 72 73 74 75 76 77 78 79 80 81
  vector<string> imagelist;

  if (!readStringList(arg,imagelist))
  {
    cerr << "Failed to read image list\n" << endl;
    help(av);
    return 1;
  }

  return process(imagelist);
}