SampleLabeling.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "ConnectedComponents.h"
3 #include "Shared.h"
4 using namespace alvar;
5 using namespace std;
6 
7 int thresh_param1 = 31;
8 
9 void videocallback(IplImage *image)
10 {
11  bool flip_image = (image->origin?true:false);
12  if (flip_image) {
13  cvFlip(image);
14  image->origin = !image->origin;
15  }
16 
17  static LabelingCvSeq* labeling = 0;
18  if(!labeling) {
19  labeling = new LabelingCvSeq();
20  }
21 
22  labeling->SetThreshParams(thresh_param1, 5);
23 
24  const int min_edge_size = 10;
25  CvSeq* edges = labeling->LabelImage(image, min_edge_size);
26 
27  int n_edges = edges->total;
28  for(int i = 0; i < n_edges; ++i)
29  {
30  CvSeq* pixels = (CvSeq*)cvGetSeqElem(edges, i);
31  int n_pixels = pixels->total;
32  for(int j = 0; j < n_pixels; ++j)
33  {
34  CvPoint* pt = (CvPoint*)cvGetSeqElem(pixels, j);
35  cvLine(image, *pt, *pt, CV_RGB(255,0,0));
36  }
37  }
38 
39  // Visualize now also the square corners.
40  labeling->LabelSquares(image, true);
41 
42  if (flip_image) {
43  cvFlip(image);
44  image->origin = !image->origin;
45  }
46 }
47 
48 int keycallback(int key)
49 {
50  if(key == '+')
51  {
52  thresh_param1+=2;
53  std::cout<<"Adaptive threshold block size: "<<thresh_param1<<std::endl;
54  }
55  else if(key == '-')
56  {
57  thresh_param1-=2;
58  if(thresh_param1<3) thresh_param1 = 3;
59  std::cout<<"Adaptive threshold block size: "<<thresh_param1<<std::endl;
60  }
61 
62  else return key;
63 
64  return 0;
65 }
66 
67 int main(int argc, char *argv[])
68 {
69  try {
70  // Output usage message
71  std::string filename(argv[0]);
72  filename = filename.substr(filename.find_last_of('\\') + 1);
73  std::cout << "SampleLabeling" << std::endl;
74  std::cout << "==============" << std::endl;
75  std::cout << std::endl;
76  std::cout << "Description:" << std::endl;
77  std::cout << " This is an example of how to use the 'LabelingCvSeq' class to perform" << std::endl;
78  std::cout << " labeling. Blobs are detected in the image and if the blobs have four" << std::endl;
79  std::cout << " corners, the edges between the corners are visualized." << std::endl;
80  std::cout << std::endl;
81  std::cout << "Usage:" << std::endl;
82  std::cout << " " << filename << " [device]" << std::endl;
83  std::cout << std::endl;
84  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
85  std::cout << " highgui capture devices are prefered" << std::endl;
86  std::cout << std::endl;
87  std::cout << "Keyboard Shortcuts:" << std::endl;
88  std::cout << " +: Increase adaptive threshold block size." << std::endl;
89  std::cout << " -: Decrease adaptive threshold block size." << std::endl;
90  std::cout << " q: quit" << std::endl;
91  std::cout << std::endl;
92 
93  // Initialise CvTestbed
96 
97  // Enumerate possible capture plugins
99  if (plugins.size() < 1) {
100  std::cout << "Could not find any capture plugins." << std::endl;
101  return 0;
102  }
103 
104  // Display capture plugins
105  std::cout << "Available Plugins: ";
106  outputEnumeratedPlugins(plugins);
107  std::cout << std::endl;
108 
109  // Enumerate possible capture devices
111  if (devices.size() < 1) {
112  std::cout << "Could not find any capture devices." << std::endl;
113  return 0;
114  }
115 
116  // Check command line argument for which device to use
117  int selectedDevice = defaultDevice(devices);
118  if (argc > 1) {
119  selectedDevice = atoi(argv[1]);
120  }
121  if (selectedDevice >= (int)devices.size()) {
122  selectedDevice = defaultDevice(devices);
123  }
124 
125  // Display capture devices
126  std::cout << "Enumerated Capture Devices:" << std::endl;
127  outputEnumeratedDevices(devices, selectedDevice);
128  std::cout << std::endl;
129 
130  // Create capture object from camera
131  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
132  std::string uniqueName = devices[selectedDevice].uniqueName();
133 
134  // Handle capture lifecycle and start video capture
135  // Note that loadSettings/saveSettings are not supported by all plugins
136  if (cap) {
137  std::stringstream settingsFilename;
138  settingsFilename << "camera_settings_" << uniqueName << ".xml";
139 
140  cap->start();
141  cap->setResolution(640, 480);
142 
143  if (cap->loadSettings(settingsFilename.str())) {
144  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
145  }
146 
147  std::stringstream title;
148  title << "SampleLabeling (" << cap->captureDevice().captureType() << ")";
149 
150  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
151 
152  if (cap->saveSettings(settingsFilename.str())) {
153  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
154  }
155 
156  cap->stop();
157  delete cap;
158  }
159  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
160  }
161  else {
162  std::cout << "Could not initialize the selected capture backend." << std::endl;
163  }
164 
165  return 0;
166  }
167  catch (const std::exception &e) {
168  std::cout << "Exception: " << e.what() << endl;
169  }
170  catch (...) {
171  std::cout << "Exception: unknown" << std::endl;
172  }
173 }
Main ALVAR namespace.
Definition: Alvar.h:174
int keycallback(int key)
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.
This file implements connected component labeling.
void SetThreshParams(int param1, int param2)
virtual bool start()=0
Starts the camera capture.
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.
void videocallback(IplImage *image)
int main(int argc, char *argv[])
unsigned char * image
Definition: GlutViewer.cpp:155
int thresh_param1
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
void SetKeyCallback(int(*_keycallback)(int key))
Sets the keyboard callback function that will be called when keyboard is pressed. ...
Definition: CvTestbed.cpp:97
CvSeq * LabelImage(IplImage *image, int min_size, bool approx=false)
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
void LabelSquares(IplImage *image, bool visualize=false)
Labels image and filters blobs to obtain square-shaped objects from the scene.
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
Labeling class that uses OpenCV routines to find connected components.
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