SampleIntegralImage.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "IntegralImage.h"
3 #include "Shared.h"
4 using namespace alvar;
5 using namespace std;
6 
7 void videocallback(IplImage *image)
8 {
9  static IplImage *img_grad = NULL;
10  static IplImage *img_gray = NULL;
11  static IplImage *img_ver = NULL;
12  static IplImage *img_hor = NULL;
13  static IplImage *img_canny = NULL;
14  static IntegralImage integ;
15  static IntegralGradient grad;
16 
17  assert(image);
18  if (img_gray == NULL) {
19  // Following image is toggled visible using key '0'
20  img_grad = CvTestbed::Instance().CreateImageWithProto("Gradient", image);
22  img_gray = CvTestbed::Instance().CreateImageWithProto("Grayscale", image, 0, 1);
23  img_ver = CvTestbed::Instance().CreateImage("Vertical", cvSize(1,image->height), IPL_DEPTH_8U, 1);
24  img_hor = CvTestbed::Instance().CreateImage("Horizontal", cvSize(image->width,1), IPL_DEPTH_8U, 1);
25  img_canny = CvTestbed::Instance().CreateImageWithProto("Canny", image, 0, 1);
26  img_canny->origin = img_ver->origin = img_hor->origin = image->origin;
27  }
28  if (image->nChannels > 1) {
29  cvCvtColor(image, img_gray, CV_RGB2GRAY);
30  } else {
31  cvCopy(image, img_gray);
32  }
33 
34  // Show PerformanceTimer
35  //PerformanceTimer timer;
36  //timer.Start();
37 
38  // Update the integral images
39  integ.Update(img_gray);
40  grad.Update(img_gray);
41 
42  // Whole image projections
43  integ.GetSubimage(cvRect(0,0,image->width,image->height), img_ver);
44  integ.GetSubimage(cvRect(0,0,image->width,image->height), img_hor);
45  for (int y=1; y<image->height; y++) {
46  cvLine(image,
47  cvPoint(int(cvGet2D(img_ver, y-1, 0).val[0]), y-1),
48  cvPoint(int(cvGet2D(img_ver, y, 0).val[0]), y),
49  CV_RGB(255,0,0));
50  }
51  for (int x=1; x<image->width; x++) {
52  cvLine(image,
53  cvPoint(x-1, int(cvGet2D(img_hor, 0, x-1).val[0])),
54  cvPoint(x, int(cvGet2D(img_hor, 0, x).val[0])),
55  CV_RGB(0,255,0));
56  }
57 
58  // Gradients
59  // Mark gradients for 4x4 sub-blocks
60  /*
61  cvZero(img_grad);
62  CvRect r = {0,0,4,4};
63  for (int y=0; y<image->height/4; y++) {
64  r.y = y*4;
65  for (int x=0; x<image->width/4; x++) {
66  r.x = x*4;
67  double dirx, diry;
68  grad.GetAveGradient(r, &dirx, &diry);
69  cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(dirx),r.y+2+int(diry)), CV_RGB(255,0,0));
70  }
71  }
72  */
73 
74  // Gradients on canny
75  cvZero(img_grad);
76  static int t1=64, t2=192;
77  cvCreateTrackbar("t1", "Gradient", &t1, 255, NULL);
78  cvCreateTrackbar("t2", "Gradient", &t2, 255, NULL);
79  cvCanny(img_gray, img_canny, t1, t2);
80  CvRect r = {0,0,4,4};
81  for (r.y=0; r.y<img_canny->height-4; r.y++) {
82  for (r.x=0; r.x<img_canny->width-4; r.x++) {
83  if (img_canny->imageData[r.y*img_canny->widthStep+r.x]) {
84  double dirx, diry;
85  grad.GetAveGradient(r, &dirx, &diry);
86  cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(dirx),r.y+2+int(diry)), CV_RGB(0,0,255));
87  cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(-diry),r.y+2+int(+dirx)), CV_RGB(255,0,0));
88  cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(+diry),r.y+2+int(-dirx)), CV_RGB(255,0,0));
89  }
90  }
91  }
92 
93  // Show PerformanceTimer
94  //cout<<"Processing: "<<1.0 / timer.Stop()<<" fps"<<endl;
95 }
96 
97 int main(int argc, char *argv[])
98 {
99  try {
100  // Output usage message
101  std::string filename(argv[0]);
102  filename = filename.substr(filename.find_last_of('\\') + 1);
103  std::cout << "SampleIntegralImage" << std::endl;
104  std::cout << "===================" << std::endl;
105  std::cout << std::endl;
106  std::cout << "Description:" << std::endl;
107  std::cout << " This is an example of how to use the 'IntegralImage' and" << std::endl;
108  std::cout << " 'IntegralGradient' classes. The vertical (green) and horizontal (red)" << std::endl;
109  std::cout << " whole image projections are computed using 'IntegralImage::GetSubimage'" << std::endl;
110  std::cout << " and shown in the SampleIntegralImage window. The gradients of the" << std::endl;
111  std::cout << " image edges are shown in the Gradient window. The edges are detected" << std::endl;
112  std::cout << " using the Canny edge detector where t1 and t2 are parameters for the" << std::endl;
113  std::cout << " Canny algorithm. The gradients are drawn in red and their local normals" << std::endl;
114  std::cout << " are drawn in blue." << std::endl;
115  std::cout << std::endl;
116  std::cout << "Usage:" << std::endl;
117  std::cout << " " << filename << " [device]" << std::endl;
118  std::cout << std::endl;
119  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
120  std::cout << " highgui capture devices are prefered" << std::endl;
121  std::cout << std::endl;
122  std::cout << "Keyboard Shortcuts:" << std::endl;
123  std::cout << " 0: show/hide gradient image" << std::endl;
124  std::cout << " 1: show/hide grayscale image" << std::endl;
125  std::cout << " 2: show/hide vertical image" << std::endl;
126  std::cout << " 3: show/hide horizontal image" << std::endl;
127  std::cout << " 4: show/hide canny image" << std::endl;
128  std::cout << " q: quit" << std::endl;
129  std::cout << std::endl;
130 
131  // Initialise CvTestbed
133 
134  // Enumerate possible capture plugins
136  if (plugins.size() < 1) {
137  std::cout << "Could not find any capture plugins." << std::endl;
138  return 0;
139  }
140 
141  // Display capture plugins
142  std::cout << "Available Plugins: ";
143  outputEnumeratedPlugins(plugins);
144  std::cout << std::endl;
145 
146  // Enumerate possible capture devices
148  if (devices.size() < 1) {
149  std::cout << "Could not find any capture devices." << std::endl;
150  return 0;
151  }
152 
153  // Check command line argument for which device to use
154  int selectedDevice = defaultDevice(devices);
155  if (argc > 1) {
156  selectedDevice = atoi(argv[1]);
157  }
158  if (selectedDevice >= (int)devices.size()) {
159  selectedDevice = defaultDevice(devices);
160  }
161 
162  // Display capture devices
163  std::cout << "Enumerated Capture Devices:" << std::endl;
164  outputEnumeratedDevices(devices, selectedDevice);
165  std::cout << std::endl;
166 
167  // Create capture object from camera
168  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
169  std::string uniqueName = devices[selectedDevice].uniqueName();
170 
171  // Handle capture lifecycle and start video capture
172  // Note that loadSettings/saveSettings are not supported by all plugins
173  if (cap) {
174  std::stringstream settingsFilename;
175  settingsFilename << "camera_settings_" << uniqueName << ".xml";
176 
177  cap->start();
178  cap->setResolution(640, 480);
179 
180  if (cap->loadSettings(settingsFilename.str())) {
181  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
182  }
183 
184  std::stringstream title;
185  title << "SampleIntegralImage (" << cap->captureDevice().captureType() << ")";
186 
187  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
188 
189  if (cap->saveSettings(settingsFilename.str())) {
190  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
191  }
192 
193  cap->stop();
194  delete cap;
195  }
196  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
197  }
198  else {
199  std::cout << "Could not initialize the selected capture backend." << std::endl;
200  }
201 
202  return 0;
203  }
204  catch (const std::exception &e) {
205  std::cout << "Exception: " << e.what() << endl;
206  }
207  catch (...) {
208  std::cout << "Exception: unknown" << std::endl;
209  }
210 }
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.
void Update(IplImage *gray)
Update integral image for the given image.
This file implements integral image and integral gradient computations.
virtual void stop()=0
Stops the camera capture.
virtual bool start()=0
Starts the camera capture.
IntegralImage is used for calculating rectangular image sums and averages rapidly ...
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 * CreateImage(const char *title, CvSize size, int depth, int channels)
Creates an image with given size, depth and channels and stores it with a given &#39;title&#39; (see CvTestbe...
Definition: CvTestbed.cpp:143
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
TFSIMD_FORCE_INLINE const tfScalar & y() const
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 GetAveGradient(CvRect &rect, double *dirx, double *diry)
Calculate the average gradient for the given rectangular area in the image.
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
void Update(IplImage *gray)
Update intermediate images for calculating the gradients to the given image.
TFSIMD_FORCE_INLINE const tfScalar & x() const
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
int main(int argc, char *argv[])
IntegralGradient is used for calculating rectangular image gradients rapidly
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
bool ToggleImageVisible(size_t index, int flags=1)
Toggle the visibility of the stored image.
Definition: CvTestbed.cpp:180
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
r
virtual bool saveSettings(std::string filename)
Save camera settings to a file.
Definition: Capture.h:124
void GetSubimage(const CvRect &rect, IplImage *sub)
Get a sub-image using integral image representation.
void videocallback(IplImage *image)


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