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 CHROMATICMASK_H
00030 #define CHROMATICMASK_H
00031 
00032 #include <opencv2/core/core.hpp>
00033 #include <opencv2/ml/ml.hpp>
00034 #include <opencv2/calib3d/calib3d.hpp>
00035 #include <opencv2/highgui/highgui.hpp>
00036 #include <opencv2/imgproc/imgproc.hpp>
00037 #include "exports.h"
00038 #include "board.h"
00039 #include "boarddetector.h"
00040 #include "cvdrawingutils.h"
00041 
00042 
00043 class ARUCO_EXPORTS EMClassifier {
00044 public:
00045   EMClassifier(unsigned int nelements=200);
00046   void addSample(uchar s) { _samples.push_back(s); };
00047   void clearSamples() { _samples.clear();  } ;
00048   void train();
00049   bool classify(uchar s) { return _inside[s]; };
00050   double getProb(uchar s) { return _prob[s]; };
00051   unsigned int numsamples() {return _samples.size();};
00052   void setProb(double p) { _threshProb = p; }
00053   
00054 
00055   
00056 private:
00057   cv::EM _classifier;
00058   vector<uchar> _samples;
00059   bool _inside[256];
00060   double _prob[256];
00061   double _histogram[256];
00062   unsigned int _nelem;
00063   double _threshProb;
00064 
00065 };
00066 
00067 
00068 class ARUCO_EXPORTS ChromaticMask
00069 {
00070 public:
00071   
00072   ChromaticMask() : _cellSize(20) { _isValid=false; };
00073   
00074   void setParams(unsigned int mc, unsigned int nc, double threshProb, aruco::CameraParameters CP, aruco::BoardConfiguration BC, vector<cv::Point3f> corners);
00075   void setParams(unsigned int mc, unsigned int nc, double threshProb, aruco::CameraParameters CP, aruco::BoardConfiguration BC, float markersize=-1.);
00076   
00077   void calculateGridImage(const aruco::Board &board);
00078   
00079   cv::Mat getCellMap() { return _cellMap; };
00080   cv::Mat getMask() { return _mask; };
00081   
00082   void train(const cv::Mat& in, const aruco::Board &board);
00083   void classify(const cv::Mat& in, const aruco::Board &board);
00084   void classify2(const cv::Mat& in, const aruco::Board &board);
00085   void update(const cv::Mat& in);
00086   
00087   bool isValid() {return _isValid;};
00088   void resetMask();
00089   
00090 private:
00091   
00092   double getDistance(cv::Point2d pixel, unsigned int classifier) {
00093     cv::Vec2b canPos = _canonicalPos.at<cv::Vec2b>(pixel.y, pixel.x)[0];
00094     return norm(_cellCenters[classifier] - cv::Point2f(canPos[0], canPos[1]) );
00095   }
00096   
00097   vector<cv::Point2f> _imgCornerPoints;  
00098   vector<cv::Point3f> _objCornerPoints;
00099   cv::Mat _perpTrans;
00100   vector<EMClassifier> _classifiers;
00101   vector<cv::Point2f> _centers;
00102   vector<cv::Point2f> _pixelsVector;
00103   vector<cv::Point2f> _cellCenters;
00104   vector<vector<size_t> > _cell_neighbours;
00105   const float _cellSize;
00106 
00107   
00108   unsigned int _mc, _nc;
00109   aruco::BoardDetector _BD;
00110   aruco::CameraParameters _CP;
00111   cv::Mat _canonicalPos, _cellMap, _mask,_maskAux;
00112   bool _isValid;
00113   double _threshProb;
00114 
00115 
00116   
00117 };
00118 
00119 #endif // CHROMATICMASK_H