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:
00054     MarkerCode(unsigned int n = 0);
00055 
00059     MarkerCode(const MarkerCode &MC);
00060 
00064     unsigned int getId(unsigned int rot = 0) const { return _ids[rot]; };
00065 
00070     bool get(unsigned int pos, unsigned int rot = 0) const { return _bits[rot][pos]; }
00071 
00075     const std::vector< bool > &getRotation(unsigned int rot) const { return _bits[rot]; };
00076 
00085     void set(unsigned int pos, bool val, bool updateIds=true);
00086 
00090     unsigned int size() const { return n() * n(); };
00091 
00095     unsigned int n() const { return _n; };
00096 
00101     unsigned int selfDistance(unsigned int &minRot) const;
00102 
00107     unsigned int selfDistance() const {
00108         unsigned int minRot;
00109         return selfDistance(minRot);
00110     };
00111 
00116     unsigned int distance(const MarkerCode &m, unsigned int &minRot) const;
00117 
00122     unsigned int distance(const MarkerCode &m) const {
00123         unsigned int minRot;
00124         return distance(m, minRot);
00125     };
00126 
00130     void fromString(std::string s);
00131 
00135     std::string toString() const;
00136 
00137 
00142     cv::Mat getImg(unsigned int pixSize) const;
00143 
00144   private:
00145     unsigned int _ids[4];         // ids in the four rotations
00146     std::vector< bool > _bits[4]; // bit strings in the four rotations
00147     unsigned int _n;              // marker dimension
00148 
00152     unsigned int hammingDistance(const std::vector< bool > &m1, const std::vector< bool > &m2) const;
00153 };
00154 
00155 
00161 class ARUCO_EXPORTS Dictionary : public std::vector< MarkerCode > {
00162   public:
00166     bool fromFile(std::string filename);
00167 
00171     bool toFile(std::string filename);
00172 
00178     unsigned int distance(const MarkerCode &m, unsigned int &minMarker, unsigned int &minRot);
00179 
00184     unsigned int distance(const MarkerCode &m) {
00185         unsigned int minMarker, minRot;
00186         return distance(m, minMarker, minRot);
00187     }
00188 
00192     unsigned int minimunDistance();
00193 
00194     int tau0;
00195 
00196   private:
00197     // convert to string
00198     template < class T > static std::string toStr(T num) {
00199         std::stringstream ss;
00200         ss << num;
00201         return ss.str();
00202     }
00203 };
00204 
00205 
00211 class ARUCO_EXPORTS HighlyReliableMarkers {
00212   public:
00217     class BalancedBinaryTree {
00218 
00219       public:
00223         void loadDictionary(Dictionary *D);
00224 
00228         bool findId(unsigned int id, unsigned int &orgPos);
00229 
00230       private:
00231         std::vector< std::pair< unsigned int, unsigned int > > _orderD; // dictionary sorted by id,
00232                                                                         // first element is the id,
00233                                                                         // second element is the position in original D
00234         std::vector< std::pair< int, int > > _binaryTree;               // binary tree itself (as a vector), each element is a node of the tree
00235                                                                         // first element indicate the position in _binaryTree of the lower child
00236                                                                         // second element is the position in _binaryTree of the higher child
00237                                                                         // -1 value indicates no lower or higher child
00238         unsigned int _root;                                             // position in _binaryTree of the root node of the tree
00239     };
00240 
00244     // correctionDistance [0,1] 0: totalmente restrictivo, 1 mas flexible
00245     static bool loadDictionary(Dictionary D, float correctionDistance = 1);
00246     static bool loadDictionary(std::string filename, float correctionDistance = 1);
00247     static Dictionary &getDictionary() { return _D; }
00248 
00249 
00255     static int detect(const cv::Mat &in, int &nRotations);
00256 
00257 
00258   private:
00259     static Dictionary _D; // loaded dictionary
00260     static BalancedBinaryTree _binaryTree;
00261     // marker dimension, marker dimension with borders, maximunCorrectionDistance
00262     static unsigned int _n;
00263     static unsigned int _ncellsBorder;
00264     static unsigned int _correctionDistance;
00265     static int _swidth; // cell size in the canonical image
00266 
00267 
00271     static bool checkBorders(cv::Mat grey);
00272 
00276     static MarkerCode getMarkerCode(const cv::Mat &grey);
00277 };
00278 
00279 }
00280 
00281 
00282 #endif // HIGHLYRELIABLEMARKERS_H


asr_aruco_marker_recognition
Author(s): Allgeyer Tobias, Meißner Pascal, Qattan Mohamad
autogenerated on Thu Jun 6 2019 21:14:12