bazar  1.3.1
multigrab.cpp
Go to the documentation of this file.
1 #include "multigrab.h"
2 
3 int MultiGrab::init(bool cacheTraining)
4 {
5  cams.clear();
6  while (1) {
7  CvCapture *c = cvCaptureFromCAM(cams.size());
8  if (!c) break;
9  cams.push_back(new Cam(c));
10  }
11  if (cams.size()==0) return 0;
12 
13  if (!model.buildCached(cams.size(), cams[0]->cam, cacheTraining, cams[0]->detector)) {
14  cout << "model.buildCached() failed.\n";
15  return 0;
16  }
17  for (int i=1; i<cams.size(); ++i) {
19  cams[i]->detector.load("model.bmp.classifier");
20  }
21 
22  return 1;
23 }
24 
26 {
27  for (vector<Cam *>::iterator it=cams.begin(); it!=cams.end(); ++it)
28  {
29  cvGrabFrame((*it)->cam);
30  }
31 }
32 
34 {
35  for (vector<Cam *>::iterator it=cams.begin(); it!=cams.end(); ++it)
36  (*it)->lc = new LightCollector(model.map.reflc);
37 }
38 
39 void MultiGrab::Cam::setCam(CvCapture *c) {
40  if (cam) cvReleaseCapture(&cam);
41  if (c==0) return;
42  cam = c;
43  // avoid saturating the firewire bus
44  cvSetCaptureProperty(cam, CV_CAP_PROP_FPS, 15);
45  cvSetCaptureProperty(cam, CV_CAP_PROP_FRAME_WIDTH, 1024);
46  cvSetCaptureProperty(cam, CV_CAP_PROP_FRAME_HEIGHT, 768);
47  IplImage *f = myQueryFrame(cam);
48  assert(f != 0);
49 
50  // downsample the video to accelerate computation
51  width = f->width; //cvRound(cvGetCaptureProperty(cam, CV_CAP_PROP_FRAME_WIDTH));
52  height = f->height; //cvRound(cvGetCaptureProperty(cam, CV_CAP_PROP_FRAME_HEIGHT));
53  double fps = cvGetCaptureProperty(cam, CV_CAP_PROP_FPS);
54  cout << "Camera " << width << "x" << height << " at "
55  << fps << " fps, " << f->nChannels << " channels.\n";
56 }
57 
59  if (gray && gray != frame) cvReleaseImage(&gray);
60  if (cam) cvReleaseCapture(&cam);
61  if (lc) delete lc;
62 }
63 
64 // this could be run in a separate thread
66 {
67  IplImage *f = myRetrieveFrame(cam);
68  if (f == 0) return false;
69  if (frame == 0) frame = cvCloneImage(f);
70  else cvCopy(f, frame);
71 
72  // convert it to gray levels, if required
73  if (frame->nChannels >1) {
74  if( !gray )
75  gray = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );
76  cvCvtColor(frame, gray, CV_RGB2GRAY);
77  } else {
78  gray = frame;
79  }
80 
81  // run the detector
82  if (detector.detect(gray)) {
83  if (lc) lc->averageImage(frame, detector.H);
84  return true;
85  }
86  return false;
87 }
88 
90 {
91  static std::vector<CamCalibration::s_struct_points> pts;
92  pts.clear();
93 
94  for (int i=0; i<detector.match_number; ++i) {
95  image_object_point_match * match = detector.matches+i;
96  if (match->inlier) {
97  pts.push_back(CamCalibration::s_struct_points(
98  PyrImage::convCoordf(match->image_point->u, int(match->image_point->scale), 0),
99  PyrImage::convCoordf(match->image_point->v, int(match->image_point->scale), 0),
100  PyrImage::convCoordf((float)match->object_point->M[0], int(match->object_point->scale), 0),
101  PyrImage::convCoordf((float)match->object_point->M[1], int(match->object_point->scale), 0)));
102  }
103  }
104 
105  return calib.AddHomography(n, pts, detector.H);
106 }
107 
109 {
110  static std::vector<CamCalibration::s_struct_points> pts;
111  pts.clear();
112 
113  for (int i=0; i<detector.match_number; ++i) {
114  image_object_point_match * match = detector.matches+i;
115  if (match->inlier) {
116  pts.push_back(CamCalibration::s_struct_points(
117  PyrImage::convCoordf(match->image_point->u, int(match->image_point->scale), 0),
118  PyrImage::convCoordf(match->image_point->v, int(match->image_point->scale), 0),
119  PyrImage::convCoordf(match->object_point->M[0], int(match->object_point->scale), 0),
120  PyrImage::convCoordf(match->object_point->M[1], int(match->object_point->scale), 0)));
121  }
122  }
123 
124  a.AddHomography(pts, detector.H);
125  return true;
126 }