SampleMarkerHide.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "MarkerDetector.h"
3 #include "GlutViewer.h"
4 #include "Shared.h"
5 using namespace alvar;
6 using namespace std;
7 
8 #define GLUT_DISABLE_ATEXIT_HACK // Needed to compile with Mingw?
9 #include <GL/gl.h>
10 
11 const double margin = 1.0;
12 std::stringstream calibrationFilename;
13 
14 // Own drawable for showing hide-texture in OpenGL
15 struct OwnDrawable : public Drawable {
16  unsigned char hidingtex[64*64*4];
17  virtual void Draw() {
18  glPushMatrix();
19  glMultMatrixd(gl_mat);
20 
21  glPushAttrib(GL_ALL_ATTRIB_BITS);
22  glEnable(GL_TEXTURE_2D);
23  int tex=0;
24  glBindTexture(GL_TEXTURE_2D, tex);
25  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
26  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
27  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
28  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
29  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
30  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,64,0,GL_RGBA,GL_UNSIGNED_BYTE,hidingtex);
31  glDisable(GL_CULL_FACE);
32  glDisable(GL_LIGHTING);
33  glDisable(GL_DEPTH_TEST);
34  glEnable(GL_ALPHA_TEST);
35  glEnable(GL_BLEND);
36  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
37  glBegin(GL_QUADS);
38  glTexCoord2d(0.0,0.0);
39  glVertex3d(-margin,-margin,0);
40  glTexCoord2d(0.0,1.0);
41  glVertex3d(-margin,margin,0);
42  glTexCoord2d(1.0,1.0);
43  glVertex3d(margin,margin,0);
44  glTexCoord2d(1.0,0.0);
45  glVertex3d(margin,-margin,0);
46  glEnd();
47  glPopAttrib();
48  glPopMatrix();
49  }
50 };
51 
52 void videocallback(IplImage *image)
53 {
54  static bool init=true;
55  static const int marker_size=15;
56  static Camera cam;
57  static OwnDrawable d[32];
58  static IplImage *hide_texture;
59 
60  bool flip_image = (image->origin?true:false);
61  if (flip_image) {
62  cvFlip(image);
63  image->origin = !image->origin;
64  }
65 
66  static IplImage* bg_image = 0;
67  if(!bg_image) bg_image = cvCreateImage(cvSize(512, 512), 8, 3);
68  if(image->nChannels == 3)
69  {
70  bg_image->origin = 0;
71  cvResize(image, bg_image);
72  GlutViewer::SetVideo(bg_image);
73  }
74 
75  if (init) {
76  init = false;
77  cout<<"Loading calibration: "<<calibrationFilename.str();
78  if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height)) {
79  cout<<" [Ok]"<<endl;
80  } else {
81  cam.SetRes(image->width, image->height);
82  cout<<" [Fail]"<<endl;
83  }
84  double p[16];
85  cam.GetOpenglProjectionMatrix(p,image->width,image->height);
87  hide_texture = CvTestbed::Instance().CreateImage("hide_texture", cvSize(64, 64), 8, 4);
88  }
90  marker_detector.Detect(image, &cam, false, false);
91 
93  for (size_t i=0; i<marker_detector.markers->size(); i++) {
94  if (i >= 32) break;
95  GlutViewer::DrawableAdd(&(d[i]));
96  }
97  for (size_t i=0; i<marker_detector.markers->size(); i++) {
98  if (i >= 32) break;
99 
100  // Note that we need to mirror both the y- and z-axis because:
101  // - In OpenCV we have coordinates: x-right, y-down, z-ahead
102  // - In OpenGL we have coordinates: x-right, y-up, z-backwards
103  // TODO: Better option might be to use OpenGL projection matrix that matches our OpenCV-approach
104  Pose p = (*(marker_detector.markers))[i].pose;
105  BuildHideTexture(image, hide_texture, &cam, d[i].gl_mat, PointDouble(-margin, -margin), PointDouble(margin, margin));
106  //DrawTexture(image, hide_texture, &cam, d[i].gl_mat, PointDouble(-0.7, -0.7), PointDouble(0.7, 0.7));
107 
108  p.GetMatrixGL(d[i].gl_mat);
109  for (int ii=0; ii<64*64; ii++) {
110  d[i].hidingtex[ii*4+0] = hide_texture->imageData[ii*4+2];
111  d[i].hidingtex[ii*4+1] = hide_texture->imageData[ii*4+1];
112  d[i].hidingtex[ii*4+2] = hide_texture->imageData[ii*4+0];
113  d[i].hidingtex[ii*4+3] = hide_texture->imageData[ii*4+3];
114  }
115  }
116  if (flip_image) {
117  cvFlip(image);
118  image->origin = !image->origin;
119  }
120 }
121 
122 int main(int argc, char *argv[])
123 {
124  try {
125  // Output usage message
126  std::string filename(argv[0]);
127  filename = filename.substr(filename.find_last_of('\\') + 1);
128  std::cout << "SampleMarkerHide" << std::endl;
129  std::cout << "================" << std::endl;
130  std::cout << std::endl;
131  std::cout << "Description:" << std::endl;
132  std::cout << " This is an example of how to detect 'MarkerData' markers, similarly" << std::endl;
133  std::cout << " to 'SampleMarkerDetector', and hide them using the 'BuildHideTexture'" << std::endl;
134  std::cout << " and 'DrawTexture' classes." << std::endl;
135  std::cout << std::endl;
136  std::cout << "Usage:" << std::endl;
137  std::cout << " " << filename << " [device]" << std::endl;
138  std::cout << std::endl;
139  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
140  std::cout << " highgui capture devices are prefered" << std::endl;
141  std::cout << std::endl;
142  std::cout << "Keyboard Shortcuts:" << std::endl;
143  std::cout << " q: quit" << std::endl;
144  std::cout << std::endl;
145 
146  // Initialise GlutViewer and CvTestbed
147  GlutViewer::Start(argc, argv, 640, 480, 15);
149 
150  // Enumerate possible capture plugins
152  if (plugins.size() < 1) {
153  std::cout << "Could not find any capture plugins." << std::endl;
154  return 0;
155  }
156 
157  // Display capture plugins
158  std::cout << "Available Plugins: ";
159  outputEnumeratedPlugins(plugins);
160  std::cout << std::endl;
161 
162  // Enumerate possible capture devices
164  if (devices.size() < 1) {
165  std::cout << "Could not find any capture devices." << std::endl;
166  return 0;
167  }
168 
169  // Check command line argument for which device to use
170  int selectedDevice = defaultDevice(devices);
171  if (argc > 1) {
172  selectedDevice = atoi(argv[1]);
173  }
174  if (selectedDevice >= (int)devices.size()) {
175  selectedDevice = defaultDevice(devices);
176  }
177 
178  // Display capture devices
179  std::cout << "Enumerated Capture Devices:" << std::endl;
180  outputEnumeratedDevices(devices, selectedDevice);
181  std::cout << std::endl;
182 
183  // Create capture object from camera
184  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
185  std::string uniqueName = devices[selectedDevice].uniqueName();
186 
187  // Handle capture lifecycle and start video capture
188  // Note that loadSettings/saveSettings are not supported by all plugins
189  if (cap) {
190  std::stringstream settingsFilename;
191  settingsFilename << "camera_settings_" << uniqueName << ".xml";
192  calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
193 
194  cap->start();
195  cap->setResolution(640, 480);
196 
197  if (cap->loadSettings(settingsFilename.str())) {
198  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
199  }
200 
201  std::stringstream title;
202  title << "SampleMarkerHide (" << cap->captureDevice().captureType() << ")";
203 
204  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
205 
206  if (cap->saveSettings(settingsFilename.str())) {
207  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
208  }
209 
210  cap->stop();
211  delete cap;
212  }
213  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
214  }
215  else {
216  std::cout << "Could not initialize the selected capture backend." << std::endl;
217  }
218 
219  return 0;
220  }
221  catch (const std::exception &e) {
222  std::cout << "Exception: " << e.what() << endl;
223  }
224  catch (...) {
225  std::cout << "Exception: unknown" << std::endl;
226  }
227 }
Main ALVAR namespace.
Definition: Alvar.h:174
void GetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height, const float far_clip=1000.0f, const float near_clip=0.1f)
Get OpenGL matrix Generates the OpenGL projection matrix based on OpenCV intrinsic camera matrix K...
Definition: Camera.cpp:394
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.
void GetMatrixGL(double gl[16], bool mirror=true)
Get the transformation matrix representation of the Pose using OpenGL&#39;s transposed format...
Definition: Pose.cpp:104
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.
const double margin
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
This file implements a generic marker detector.
void SetRes(int _x_res, int _y_res)
If we have no calibration file we can still adjust the default calibration to current resolution...
Definition: Camera.cpp:374
unsigned char * image
Definition: GlutViewer.cpp:155
double marker_size
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
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
bool SetCalib(const char *calibfile, int _x_res, int _y_res, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Set the calibration file and the current resolution for which the calibration is adjusted to...
Definition: Camera.cpp:270
std::vector< M, Eigen::aligned_allocator< M > > * markers
std::vector< std::string > CapturePluginVector
Vector of strings.
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
MarkerDetector for detecting markers of type M
void SetGlProjectionMatrix(double p[16])
Definition: GlutViewer.cpp:388
virtual void Draw()
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
void Start(int argc, char **argv, int w, int h, float r=300.0)
Definition: GlutViewer.cpp:227
void DrawableClear()
Definition: GlutViewer.cpp:416
Pose representation derived from the Rotation class
Definition: Pose.h:50
void DrawableAdd(Drawable *item)
Definition: GlutViewer.cpp:421
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
Drawable d[32]
MarkerDetector< MarkerData > marker_detector
bool init
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
std::stringstream calibrationFilename
void videocallback(IplImage *image)
void SetVideo(const IplImage *_image)
Definition: GlutViewer.cpp:428
ALVAR_EXPORT Point< CvPoint2D64f > PointDouble
The default double point type.
Definition: Util.h:108
unsigned char hidingtex[64 *64 *4]
void ALVAR_EXPORT BuildHideTexture(IplImage *image, IplImage *hide_texture, Camera *cam, double gl_modelview[16], PointDouble topleft, PointDouble botright)
This function is used to construct a texture image which is needed to hide a marker from the original...
Definition: Draw.cpp:95
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
Camera * cam
virtual void setResolution(const unsigned long xResolution, const unsigned long yResolution)
Set the resolution.
Definition: Capture.h:93
int main(int argc, char *argv[])
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