opencv/samples/cpp/starter_imagelist.cpp

88 lines
2.2 KiB
C++
Raw Normal View History

2010-11-25 08:39:43 +08:00
/*
* 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?
*/
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
2010-11-25 08:39:43 +08:00
#include <vector>
using namespace cv;
using namespace std;
//hide the local functions in an unnamed namespace
namespace
{
void help(char** av)
{
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
}
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)
{
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 indefinitely 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);
}