SampleCvTestbed.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "Shared.h"
3 using namespace alvar;
4 using namespace std;
5 
6 void videocallback(IplImage *image)
7 {
8  static IplImage *img_gray=NULL;
9 
10  assert(image);
11  if (img_gray == NULL) {
12  // Following image is toggled visible using key '0'
13  img_gray = CvTestbed::Instance().CreateImageWithProto("Grayscale", image, 0, 1);
14  }
15  if (image->nChannels > 1) {
16  cvCvtColor(image, img_gray, CV_RGB2GRAY);
17  } else {
18  cvCopy(image, img_gray);
19  }
20  // TODO: Do your image operations
21 }
22 
23 int main(int argc, char *argv[])
24 {
25  try {
26  // Output usage message
27  std::string filename(argv[0]);
28  filename = filename.substr(filename.find_last_of('\\') + 1);
29  std::cout << "SampleCvTestbed" << std::endl;
30  std::cout << "===============" << std::endl;
31  std::cout << std::endl;
32  std::cout << "Description:" << std::endl;
33  std::cout << " This is an example of how to use the 'CvTestbed', 'CaptureFactory' and" << std::endl;
34  std::cout << " 'Capture' classes. The 'CaptureFactory' can create 'Capture' objects" << std::endl;
35  std::cout << " from many different backends (see SampleCvTestbed.cpp). You can also" << std::endl;
36  std::cout << " show/hide the 1st ten images created using 'CvTestbed' using the number" << std::endl;
37  std::cout << " keys. In this example you can use key '0' to show/hide a grayscale" << std::endl;
38  std::cout << " version of the captured image." << std::endl;
39  std::cout << std::endl;
40  std::cout << "Usage:" << std::endl;
41  std::cout << " " << filename << " [device]" << std::endl;
42  std::cout << std::endl;
43  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
44  std::cout << " highgui capture devices are prefered" << std::endl;
45  std::cout << std::endl;
46  std::cout << "Keyboard Shortcuts:" << std::endl;
47  std::cout << " 0: show/hide grayscale image" << std::endl;
48  std::cout << " q: quit" << std::endl;
49  std::cout << std::endl;
50 
51  // Initialise CvTestbed
53 
54  // Enumerate possible capture plugins
56  if (plugins.size() < 1) {
57  std::cout << "Could not find any capture plugins." << std::endl;
58  return 0;
59  }
60 
61  // Display capture plugins
62  std::cout << "Available Plugins: ";
63  outputEnumeratedPlugins(plugins);
64  std::cout << std::endl;
65 
66  // Enumerate possible capture devices
68  if (devices.size() < 1) {
69  std::cout << "Could not find any capture devices." << std::endl;
70  return 0;
71  }
72 
73  // Check command line argument for which device to use
74  int selectedDevice = defaultDevice(devices);
75  if (argc > 1) {
76  selectedDevice = atoi(argv[1]);
77  }
78  if (selectedDevice >= (int)devices.size()) {
79  selectedDevice = defaultDevice(devices);
80  }
81 
82  // Display capture devices
83  std::cout << "Enumerated Capture Devices:" << std::endl;
84  outputEnumeratedDevices(devices, selectedDevice);
85  std::cout << std::endl;
86 
87  // Create capture object from camera
88  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
89  std::string uniqueName = devices[selectedDevice].uniqueName();
90 
91  // Handle capture lifecycle and start video capture
92  // Note that loadSettings/saveSettings are not supported by all plugins
93  if (cap) {
94  std::stringstream settingsFilename;
95  settingsFilename << "camera_settings_" << uniqueName << ".xml";
96 
97  cap->start();
98  cap->setResolution(640, 480);
99 
100  if (cap->loadSettings(settingsFilename.str())) {
101  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
102  }
103 
104  std::stringstream title;
105  title << "SampleCvTestbed (" << cap->captureDevice().captureType() << ")";
106 
107  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
108 
109  if (cap->saveSettings(settingsFilename.str())) {
110  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
111  }
112 
113  cap->stop();
114  delete cap;
115  }
116  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
117  }
118  else {
119  std::cout << "Could not initialize the selected capture backend." << std::endl;
120  }
121 
122  return 0;
123  }
124  catch (const std::exception &e) {
125  std::cout << "Exception: " << e.what() << endl;
126  }
127  catch (...) {
128  std::cout << "Exception: unknown" << std::endl;
129  }
130 }
Main ALVAR namespace.
Definition: Alvar.h:174
static CvTestbed & Instance()
The one and only instance of CvTestbed is accessed using CvTestbed::Instance()
Definition: CvTestbed.cpp:88
filename
Capture * createCapture(const CaptureDevice captureDevice)
Create Capture class. Transfers onwership to the caller.
virtual void stop()=0
Stops the camera capture.
virtual bool start()=0
Starts the camera capture.
int main(int argc, char *argv[])
CaptureDevice captureDevice()
The camera information associated to this capture object.
Definition: Capture.h:70
void SetVideoCallback(void(*_videocallback)(IplImage *image))
Set the videocallback function that will be called for every frame.
Definition: CvTestbed.cpp:93
static CaptureFactory * instance()
The singleton instance of CaptureFactory.
IplImage * CreateImageWithProto(const char *title, IplImage *proto, int depth=0, int channels=0)
Creates an image based on the given prototype and stores it with a given &#39;title&#39; (see CvTestbed::SetI...
Definition: CvTestbed.cpp:150
unsigned char * image
Definition: GlutViewer.cpp:155
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
std::string captureType() const
The type of capture backend.
bool StartVideo(Capture *_cap, const char *_wintitle=0)
Start video input from given capture device.
Definition: CvTestbed.cpp:101
std::vector< std::string > CapturePluginVector
Vector of strings.
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
Capture interface that plugins must implement.
Definition: Capture.h:46
virtual bool loadSettings(std::string filename)
Load camera settings from a file.
Definition: Capture.h:145
int defaultDevice(CaptureFactory::CaptureDeviceVector &devices)
Definition: Shared.h:40
virtual void setResolution(const unsigned long xResolution, const unsigned long yResolution)
Set the resolution.
Definition: Capture.h:93
void videocallback(IplImage *image)
virtual bool saveSettings(std::string filename)
Save camera settings to a file.
Definition: Capture.h:124


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04