opencv/samples/cpp/imagelist_reader.cpp

81 lines
2.1 KiB
C++
Raw Normal View History

2010-11-25 08:39:43 +08:00
/*
* 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?
*/
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
#include <iostream>
2010-11-25 08:39:43 +08:00
#include <vector>
using namespace cv;
using namespace std;
static void help(char** av)
2010-11-25 08:39:43 +08:00
{
2010-12-04 16:31:02 +08:00
cout << "\nThis program gets you started being able to read images from a list in a file\n"
2012-10-17 15:12:04 +08:00
"Usage:\n./" << av[0] << " image_list.yaml\n"
2010-11-25 08:39:43 +08:00
<< "\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"
2010-12-04 16:31:02 +08:00
<< "\tTry running imagelist_creator to generate a list of images.\n"
2012-10-17 15:12:04 +08:00
"Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
2010-11-25 08:39:43 +08:00
}
static bool readStringList(const string& filename, vector<string>& l)
2010-11-25 08:39:43 +08:00
{
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;
}
static int process(const vector<string>& images)
2010-11-25 08:39:43 +08:00
{
namedWindow("image", WINDOW_KEEPRATIO); //resizable window;
for (size_t i = 0; i < images.size(); i++)
{
Mat image = imread(images[i], IMREAD_GRAYSCALE); // do grayscale processing?
imshow("image",image);
cout << "Press a key to see the next image in the list." << endl;
waitKey(); // wait infinitely for a key to be pressed
}
return 0;
2010-11-25 08:39:43 +08:00
}
int main(int ac, char** av)
{
2015-08-01 23:24:23 +08:00
cv::CommandLineParser parser(ac, av, "{help h||}{@input||}");
if (parser.has("help"))
{
help(av);
return 0;
}
std::string arg = parser.get<std::string>("@input");
if (arg.empty())
2010-11-25 08:39:43 +08:00
{
help(av);
return 1;
}
vector<string> imagelist;
if (!readStringList(arg,imagelist))
2012-10-17 15:12:04 +08:00
{
2010-12-01 09:43:22 +08:00
cerr << "Failed to read image list\n" << endl;
help(av);
2010-11-25 08:39:43 +08:00
return 1;
}
return process(imagelist);
}