Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 #ifndef HIGHLYRELIABLEMARKERS_H
00030 #define HIGHLYRELIABLEMARKERS_H
00031 
00032 
00033 #include <vector>
00034 #include <math.h>
00035 #include <string>
00036 #include <opencv2/core/core.hpp>
00037 #include <opencv2/imgproc/imgproc.hpp>
00038 #include "exports.h"
00039 
00040 #include <iostream>
00041 
00042 namespace aruco {
00043 
00049 class ARUCO_EXPORTS MarkerCode {
00050 public:
00051   
00055   MarkerCode(unsigned int n=0);
00056   
00060   MarkerCode(const MarkerCode &MC);
00061   
00065   unsigned int getId(unsigned int rot=0) { return _ids[rot]; };
00066   
00071   bool get(unsigned int pos, unsigned int rot=0) { return _bits[rot][pos]; }
00072   
00076   std::vector<bool> getRotation(unsigned int rot) { return _bits[rot]; };
00077   
00086   void set(unsigned int pos, bool val);
00087   
00091   unsigned int size() {return n()*n(); };
00092   
00096   unsigned int n() {return _n; };
00097   
00102   unsigned int selfDistance(unsigned int &minRot);
00103   
00108   unsigned int selfDistance() {
00109     unsigned int minRot;
00110     return selfDistance(minRot);
00111   };  
00112   
00117   unsigned int distance(MarkerCode m, unsigned int &minRot);
00118   
00123   unsigned int distance(MarkerCode m) {
00124     unsigned int minRot;
00125     return distance(m, minRot);  
00126   };  
00127   
00131   void fromString(std::string s);
00132 
00136   std::string toString();
00137   
00138   
00143   cv::Mat getImg(unsigned int pixSize);
00144   
00145 private:
00146   unsigned int _ids[4]; 
00147   std::vector<bool> _bits[4]; 
00148   unsigned int _n; 
00149   
00153   unsigned int hammingDistance(std::vector<bool> m1, std::vector<bool> m2);
00154   
00155 };
00156 
00157 
00163 class ARUCO_EXPORTS Dictionary : public std::vector<MarkerCode> {
00164 public: 
00165   
00169   bool fromFile(std::string filename);
00170   
00174   bool toFile(std::string filename);
00175   
00181   unsigned int distance(MarkerCode m, unsigned int &minMarker, unsigned int &minRot);
00182   
00187   unsigned int distance(MarkerCode m) {
00188     unsigned int minMarker, minRot;
00189     return distance(m,minMarker,minRot);
00190   }  
00191   
00195   unsigned int minimunDistance();
00196   
00197 private:
00198   
00199   
00200   template <class T> static std::string toStr(T num) {
00201     std::stringstream ss;
00202     ss << num;
00203     return ss.str();
00204   }
00205   
00206   
00207 };
00208 
00209 
00215 class ARUCO_EXPORTS HighlyReliableMarkers
00216 {
00217 public:
00218   
00223   class BalancedBinaryTree {
00224     
00225   public:
00226        
00230     void loadDictionary(Dictionary *D);
00231     
00235     bool findId(unsigned int id, unsigned int &orgPos);
00236    
00237   private:
00238 
00239     std::vector< std::pair<unsigned int,unsigned int> > _orderD; 
00240                                                  
00241                                                  
00242     std::vector< std::pair<int, int> > _binaryTree; 
00243                                                       
00244                                                       
00245                                                       
00246     unsigned int _root; 
00247     
00248   };
00249   
00253   static bool loadDictionary(Dictionary D);
00254   static bool loadDictionary(std::string filename);
00255   static Dictionary& getDictionary() { return _D; }
00256     
00257   
00263   static int detect(const cv::Mat& in, int& nRotations);
00264   
00265   
00266 private:
00267   static Dictionary _D; 
00268   static BalancedBinaryTree _binaryTree;
00269   
00270   static unsigned int _n;
00271   static unsigned int _ncellsBorder;
00272   static unsigned int _correctionDistance;
00273   static int _swidth; 
00274   
00275   
00279   static bool checkBorders(cv::Mat grey);
00280   
00284   static MarkerCode getMarkerCode(cv::Mat grey);
00285    
00286   
00287 };
00288 
00289 };
00290 
00291 
00292 #endif // HIGHLYRELIABLEMARKERS_H