My Project
Marker.h
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #ifndef MARKER_H
25 #define MARKER_H
26 
34 #include "Alvar.h"
35 #include "Bitset.h"
36 #include "Camera.h"
37 #include "Pose.h"
38 #include "Util.h"
39 
40 #include <algorithm>
41 #include <iostream>
42 #include <opencv2/imgproc.hpp>
43 #include <vector>
44 
45 namespace alvar {
46 
52 class ALVAR_EXPORT Marker
53 {
54 protected:
55  void VisualizeMarkerPose(cv::Mat & image,
56  Camera * cam,
57  double visualize2d_points[12][2],
58  const cv::Scalar color = CV_RGB(255, 0, 0)) const;
59  virtual void VisualizeMarkerContent(cv::Mat &image,
60  Camera * cam,
61  double datatext_point[2],
62  double content_point[2]) const;
63  virtual void VisualizeMarkerError(cv::Mat &image, Camera *cam, double errortext_point[2]) const;
64  bool UpdateContentBasic(std::vector<PointDouble> &_marker_corners_img,
65  cv::Mat & gray,
66  Camera * cam,
67  int frame_no = 0);
68 
69 public:
76  void CompareCorners(std::vector<Point<cv::Point2d>> &_marker_corners_img,
77  int * orientation,
78  double * error);
81  void CompareContent(std::vector<PointDouble> &_marker_corners_img,
82  cv::Mat & gray,
83  Camera * cam,
84  int * orientation) const;
87  virtual bool UpdateContent(std::vector<Point<cv::Point2d>> &_marker_corners_img,
88  cv::Mat & gray,
89  Camera * cam,
90  int frame_no = 0);
93  void UpdatePose(std::vector<Point<cv::Point2d>> &_marker_corners_img,
94  Camera * cam,
95  int orientation,
96  int frame_no = 0,
97  bool update_pose = true);
101  virtual bool DecodeContent(int *orientation);
102 
105  cv::Mat
106  GetContent() const
107  {
108  return marker_content;
109  }
112  void SaveMarkerImage(const char *filename, int save_res = 0) const;
115  void ScaleMarkerToImage(cv::Mat &image) const;
118  void Visualize(cv::Mat &image, Camera *cam, const cv::Scalar color = CV_RGB(255, 0, 0)) const;
120  void SetMarkerSize(double _edge_length = 0, int _res = 0, double _margin = 0);
122  double
124  {
125  return edge_length;
126  }
134  Marker(double _edge_length = 0, int _res = 0, double _margin = 0);
136  Marker(const Marker &m);
142  virtual unsigned long
143  GetId() const
144  {
145  return 0;
146  }
147  virtual void SetId(unsigned long _id){};
148 
154  int
155  GetRes() const
156  {
157  return res;
158  }
159 
164  double
165  GetMargin() const
166  {
167  return margin;
168  }
169 
180  double
181  GetError(int errors = (MARGIN_ERROR | DECODE_ERROR)) const
182  {
183  int count = 0;
184  double error = 0;
185  if (errors & MARGIN_ERROR) {
186  error += margin_error;
187  count++;
188  }
189  if (errors & DECODE_ERROR) {
190  error += decode_error;
191  count++;
192  }
193  if (errors & TRACK_ERROR) {
194  error += track_error;
195  count++;
196  }
197  return error / count;
198  }
200  void
201  SetError(int error_type, double value)
202  {
203  if (error_type == MARGIN_ERROR)
204  margin_error = value;
205  else if (error_type == DECODE_ERROR)
206  decode_error = value;
207  else if (error_type == TRACK_ERROR)
208  track_error = value;
209  }
210  static const int MARGIN_ERROR = 1;
211  static const int DECODE_ERROR = 2;
212  static const int TRACK_ERROR = 4;
213 
214 protected:
215  double margin_error;
216  double decode_error;
217  double track_error;
218  double edge_length;
219  int res;
220  double margin;
221  cv::Mat marker_content;
222 
223 public:
225  std::vector<PointDouble> marker_points;
227  std::vector<PointDouble> marker_corners;
229  std::vector<PointDouble> marker_corners_img;
231  std::vector<PointDouble> marker_margin_w;
233  std::vector<PointDouble> marker_margin_b;
234 #ifdef VISUALIZE_MARKER_POINTS
235  std::vector<PointDouble> marker_allpoints_img;
236 #endif
237 };
238 
242 class ALVAR_EXPORT MarkerArtoolkit : public Marker
243 {
244 protected:
245  int
246  default_res()
247  {
248  std::cout << "MarkerArtoolkit::default_res" << std::endl;
249  return 3;
250  }
251  double
252  default_margin()
253  {
254  return 1.5;
255  }
256 
257 public:
259  unsigned long id;
261  MarkerArtoolkit(double _edge_length = 0, int _res = 0, double _margin = 0)
262  : Marker(_edge_length, (_res ? _res : 3), (_margin ? _margin : 1.5))
263  {
264  }
266  unsigned long
267  GetId() const
268  {
269  return id;
270  }
271  void
272  SetId(unsigned long _id)
273  {
274  id = _id;
275  }
277  bool DecodeContent(int *orientation);
279  void SetContent(unsigned long _id);
280 };
281 
285 class ALVAR_EXPORT MarkerData : public Marker
286 {
287 protected:
288  virtual void VisualizeMarkerContent(cv::Mat &image,
289  Camera * cam,
290  double datatext_point[2],
291  double content_point[2]) const;
292  void DecodeOrientation(int *error, int *total, int *orientation);
293  int DecodeCode(int orientation,
294  BitsetExt * bs,
295  int * erroneous,
296  int * total,
297  unsigned char *content_type);
298  void Read6bitStr(BitsetExt *bs, char *s, size_t s_max_len);
299  void Add6bitStr(BitsetExt *bs, char *s);
300  int UsableDataBits(int marker_res, int hamming);
301  bool DetectResolution(std::vector<Point<cv::Point2d>> &_marker_corners_img,
302  cv::Mat & gray,
303  Camera * cam);
304 
305 public:
306  static const int MAX_MARKER_STRING_LEN = 2048;
307  enum MarkerContentType {
308  MARKER_CONTENT_TYPE_NUMBER,
309  MARKER_CONTENT_TYPE_STRING,
310  MARKER_CONTENT_TYPE_FILE,
311  MARKER_CONTENT_TYPE_HTTP
312  };
313  unsigned char content_type;
314 
316  union {
317  unsigned long id;
318  char str[MAX_MARKER_STRING_LEN];
319  } data;
320 
326  MarkerData(double _edge_length = 0, int _res = 0, double _margin = 0)
327  : Marker(_edge_length, _res, (_margin ? _margin : 2))
328  {
329  }
331  unsigned long
332  GetId() const
333  {
334  return data.id;
335  }
337  void
338  SetId(unsigned long _id)
339  {
340  data.id = _id;
341  }
346  virtual bool UpdateContent(std::vector<Point<cv::Point2d>> &_marker_corners_img,
347  cv::Mat & gray,
348  Camera * cam,
349  int frame_no = 0);
352  bool DecodeContent(int *orientation);
355  void SetContent(MarkerContentType content_type,
356  unsigned long id,
357  const char * str,
358  bool force_strong_hamming = false,
359  bool verbose = false);
360 };
361 
364 class ALVAR_EXPORT MarkerIterator : public std::iterator<std::forward_iterator_tag, Marker *>
365 {
366 public:
367  virtual bool operator==(const MarkerIterator &other) = 0;
368  virtual bool operator!=(const MarkerIterator &other) = 0;
369  virtual MarkerIterator &operator++() = 0;
370  virtual const Marker * operator*() = 0;
371  virtual const Marker * operator->() = 0;
372  virtual MarkerIterator &reset() = 0;
373 
374  void *_data;
375 };
376 
381 template <typename T>
382 class ALVAR_EXPORT MarkerIteratorImpl : public MarkerIterator
383 {
384 public:
385  MarkerIteratorImpl(typename std::vector<T>::const_iterator i) : _begin(i), _i(i)
386  {
387  _data = this;
388  }
389 
391  {
392  }
393 
394  // The assignment and relational operators are straightforward
396  operator=(const MarkerIteratorImpl &other)
397  {
398  _i = other._i;
399  return (*this);
400  }
401 
402  bool
403  operator==(const MarkerIterator &other)
404  {
405  MarkerIteratorImpl *pother = (MarkerIteratorImpl *)other._data;
406  return (_i == pother->_i);
407  }
408 
409  bool
410  operator!=(const MarkerIterator &other)
411  {
412  MarkerIteratorImpl *pother = (MarkerIteratorImpl *)other._data;
413  return (_i != pother->_i);
414  }
415 
417  operator++()
418  {
419  _i++;
420  return (*this);
421  }
422 
423  const Marker *
424  operator*()
425  {
426  return &(*_i);
427  }
428 
429  const Marker *
430  operator->()
431  {
432  return &(*_i);
433  }
434 
436  reset()
437  {
438  _i = _begin;
439  return (*this);
440  }
441 
442 private:
443  typename std::vector<T>::const_iterator _begin;
444  typename std::vector<T>::const_iterator _i;
445 };
446 
447 } // namespace alvar
448 
449 #endif
This file defines library export definitions, version numbers and build information.
This file implements bit set handling.
This file implements a camera used for projecting points and computing homographies.
This file implements a pose.
This file implements generic utility functions and a serialization interface.
An extended Bitset ( BitsetExt ) for handling e.g. Hamming encoding/decoding.
Definition: Bitset.h:139
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
MarkerArtoolkit for using matrix markers similar with the ones used in ARToolkit.
Definition: Marker.h:243
bool DecodeContent(int *orientation)
DecodeContent should be called after UpdateContent to fill content_type, decode_error and data
void SetContent(unsigned long _id)
Updates the marker_content by "encoding" the given parameters.
unsigned long GetId() const
Get ID for recognizing this marker.
Definition: Marker.h:267
MarkerArtoolkit(double _edge_length=0, int _res=0, double _margin=0)
Constructor.
Definition: Marker.h:261
unsigned long id
MarkerArtoolkit supports only 'id' as data type
Definition: Marker.h:259
MarkerData contains matrix of Hamming encoded data.
Definition: Marker.h:286
MarkerData(double _edge_length=0, int _res=0, double _margin=0)
Default constructor.
Definition: Marker.h:326
void SetId(unsigned long _id)
Set the ID.
Definition: Marker.h:338
bool DecodeContent(int *orientation)
DecodeContent should be called after UpdateContent to fill content_type, decode_error and data
virtual bool UpdateContent(std::vector< Point< cv::Point2d >> &_marker_corners_img, cv::Mat &gray, Camera *cam, int frame_no=0)
Updates the marker_content from the image using Homography Compared to the basic implementation in Ma...
unsigned long GetId() const
Get ID for recognizing this marker.
Definition: Marker.h:332
void SetContent(MarkerContentType content_type, unsigned long id, const char *str, bool force_strong_hamming=false, bool verbose=false)
Updates the marker_content by "encoding" the given parameters.
Basic 2D Marker functionality.
Definition: Marker.h:53
std::vector< PointDouble > marker_corners
Marker corners in marker coordinates.
Definition: Marker.h:227
Marker(double _edge_length=0, int _res=0, double _margin=0)
Default constructor.
int GetRes() const
Definition: Marker.h:155
virtual unsigned long GetId() const
Get id for this marker This is used e.g. in MarkerDetector to associate a marker id with an appropria...
Definition: Marker.h:143
void ScaleMarkerToImage(cv::Mat &image) const
Draw the marker filling the ROI in the given image.
virtual bool UpdateContent(std::vector< Point< cv::Point2d >> &_marker_corners_img, cv::Mat &gray, Camera *cam, int frame_no=0)
Updates the marker_content from the image using Homography.
void SaveMarkerImage(const char *filename, int save_res=0) const
Saves the marker as an image.
void SetMarkerSize(double _edge_length=0, int _res=0, double _margin=0)
Method for resizing the marker dimensions
std::vector< PointDouble > marker_margin_b
Samples to be used in figuring out min/max for thresholding.
Definition: Marker.h:233
cv::Mat GetContent() const
Returns the content as a matrix.
Definition: Marker.h:106
std::vector< PointDouble > marker_corners_img
Marker corners in image coordinates.
Definition: Marker.h:229
Marker(const Marker &m)
Copy constructor
~Marker()
Destructor
Pose pose
The current marker Pose.
Definition: Marker.h:172
void UpdatePose(std::vector< Point< cv::Point2d >> &_marker_corners_img, Camera *cam, int orientation, int frame_no=0, bool update_pose=true)
Updates the markers pose estimation.
double GetMargin() const
Definition: Marker.h:165
void SetError(int error_type, double value)
Set the marker error estimate.
Definition: Marker.h:201
void Visualize(cv::Mat &image, Camera *cam, const cv::Scalar color=CV_RGB(255, 0, 0)) const
Visualize the marker.
std::vector< PointDouble > marker_margin_w
Samples to be used in figuring out min/max for thresholding.
Definition: Marker.h:231
void CompareCorners(std::vector< Point< cv::Point2d >> &_marker_corners_img, int *orientation, double *error)
Compares the marker corners with the previous match.
double GetMarkerEdgeLength() const
Get edge length (to support different size markers.
Definition: Marker.h:123
double GetError(int errors=(MARGIN_ERROR|DECODE_ERROR)) const
Get marker detection error estimate.
Definition: Marker.h:181
void CompareContent(std::vector< PointDouble > &_marker_corners_img, cv::Mat &gray, Camera *cam, int *orientation) const
Compares the marker corners with the previous match.
std::vector< PointDouble > marker_points
Marker color points in marker coordinates.
Definition: Marker.h:225
virtual bool DecodeContent(int *orientation)
Decodes the marker content. Please call UpdateContent before this. This virtual method is meant to be...
Iterator type for traversing templated Marker vector without the template.
Definition: Marker.h:365
Iterator implementation for traversing templated Marker vector without the template.
Definition: Marker.h:383
Pose representation derived from the Rotation class
Definition: Pose.h:53
Main ALVAR namespace.
Definition: Alvar.h:174