mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03:15 +08:00
adding two simple templates
This commit is contained in:
parent
d3aa808bd1
commit
b22f68091d
78
samples/cpp/starter_imagelist.cpp
Normal file
78
samples/cpp/starter_imagelist.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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?
|
||||
*/
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
//hide the local functions in an unnamed namespace
|
||||
namespace
|
||||
{
|
||||
void help(char** av)
|
||||
{
|
||||
cout << "usage:" << 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." << 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
|
||||
if (ac != 2)
|
||||
{
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
std::string arg = av[1];
|
||||
vector<string> imagelist;
|
||||
|
||||
if (!readStringList(arg,imagelist))
|
||||
{
|
||||
cerr << "Failed to read image list" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return process(imagelist);
|
||||
}
|
67
samples/cpp/starter_video.cpp
Normal file
67
samples/cpp/starter_video.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* starter_video.cpp
|
||||
*
|
||||
* Created on: Nov 23, 2010
|
||||
* Author: Ethan Rublee
|
||||
*
|
||||
* A starter sample for using opencv, get a video stream and display the images
|
||||
* easy as CV_PI right?
|
||||
*/
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
//hide the local functions in an anon namespace
|
||||
namespace {
|
||||
void help(char** av) {
|
||||
cout << "usage:" << av[0] << " <video device number>\n"
|
||||
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
|
||||
<< "\tThe program captures frames from a camera connected to your computer.\n"
|
||||
<< "\tTo find the video device number, try ls /dev/video* \n"
|
||||
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
int process(VideoCapture& capture) {
|
||||
string window_name = "video | q or esc to quit";
|
||||
cout << "press q or esc to quit" << endl;
|
||||
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
|
||||
Mat frame;
|
||||
for (;;) {
|
||||
capture >> frame;
|
||||
if (frame.empty())
|
||||
continue;
|
||||
imshow(window_name, frame);
|
||||
char key = waitKey(5); //delay N millis, usually long enough to display and capture input
|
||||
switch (key) {
|
||||
case 'q':
|
||||
case 'Q':
|
||||
case 27: //escape key
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int ac, char** av) {
|
||||
|
||||
if (ac != 2) {
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
std::string arg = av[1];
|
||||
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
|
||||
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
|
||||
capture.open(atoi(arg.c_str()));
|
||||
if (!capture.isOpened()) {
|
||||
cerr << "Failed to open a video device or video file!" << endl;
|
||||
return 1;
|
||||
}
|
||||
return process(capture);
|
||||
}
|
Loading…
Reference in New Issue
Block a user