highlyreliablemarkers.h
Go to the documentation of this file.
00001 /*****************************
00002 Copyright 2011 Rafael Muñoz Salinas. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without modification, are
00005 permitted provided that the following conditions are met:
00006 
00007    1. Redistributions of source code must retain the above copyright notice, this list of
00008       conditions and the following disclaimer.
00009 
00010    2. Redistributions in binary form must reproduce the above copyright notice, this list
00011       of conditions and the following disclaimer in the documentation and/or other materials
00012       provided with the distribution.
00013 
00014 THIS SOFTWARE IS PROVIDED BY Rafael Muñoz Salinas ''AS IS'' AND ANY EXPRESS OR IMPLIED
00015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00016 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Rafael Muñoz Salinas OR
00017 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00018 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00019 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00020 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00021 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00022 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00023 
00024 The views and conclusions contained in the software and documentation are those of the
00025 authors and should not be interpreted as representing official policies, either expressed
00026 or implied, of Rafael Muñoz Salinas.
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]; // ids in the four rotations
00147   std::vector<bool> _bits[4]; // bit strings in the four rotations
00148   unsigned int _n; // marker dimension
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   // convert to string
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; // dictionary sorted by id,
00240                                                  // first element is the id, 
00241                                                  // second element is the position in original D
00242     std::vector< std::pair<int, int> > _binaryTree; // binary tree itself (as a vector), each element is a node of the tree
00243                                                       // first element indicate the position in _binaryTree of the lower child
00244                                                       // second element is the position in _binaryTree of the higher child
00245                                                       // -1 value indicates no lower or higher child
00246     unsigned int _root; // position in _binaryTree of the root node of the tree
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; // loaded dictionary
00268   static BalancedBinaryTree _binaryTree;
00269   // marker dimension, marker dimension with borders, maximunCorrectionDistance
00270   static unsigned int _n;
00271   static unsigned int _ncellsBorder;
00272   static unsigned int _correctionDistance;
00273   static int _swidth; // cell size in the canonical image
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


ar_sys
Author(s): Hamdi Sahloul , Rafael Muñoz Salinas , Bence Magyar
autogenerated on Sun Oct 5 2014 22:12:49