35 #include <opencv2/core/core.hpp> 37 #include <opencv2/imgproc/imgproc.hpp> 38 #include <opencv2/ml/ml.hpp> 49 #ifdef OPENCV_VERSION_3 50 cv::Ptr<cv::ml::SVM> _model;
57 _minFeatureValue = -1;
61 _rotateMarkers =
true;
67 bool load(
string path)
throw (cv::Exception){
68 if (path.empty())
return false;
71 #ifdef OPENCV_VERSION_3 72 _model= cv::ml::StatModel::load<cv::ml::SVM>(path);
73 _patchSize=
sqrt(_model->getSupportVectors().size().width);
77 _model->load(path.c_str());
78 _patchSize=
sqrt(_model->get_var_count());
82 ifstream svmfile(path.c_str());
83 if (!svmfile)
return false;
85 while(!svmfile.eof()){
87 std::getline(svmfile,str);
88 if (str.find(
"class_count:")!=std::string::npos){
89 stringstream sstr;sstr<<str;
93 _dictSize=(nclasses-1)/4;
96 if (_dictSize==-1)
return false;
108 bool detect(
const cv::Mat &in,
int &mid,
int &nRotations) {
112 assert(in.rows == in.cols);
114 if (in.type() == CV_8UC1)
117 cv::cvtColor(in, grey, CV_BGR2GRAY);
121 if(grey.cols != _patchSize)
122 cv::resize(grey, greyResized, cv::Size(_patchSize, _patchSize) );
127 cv::Mat greyResizedNormalized;
128 cv::normalize(greyResized, greyResizedNormalized, _minFeatureValue, _maxFeatureValue, cv::NORM_MINMAX, CV_32FC1);
132 dataRow = greyResizedNormalized.reshape(1,1);
135 #ifdef OPENCV_VERSION_3 136 int predict_id = (int)_model->predict(dataRow);
138 int predict_id = (int)_model->predict(dataRow,
true);
142 nRotations = predict_id%4;
145 if(nRotations==1 || nRotations==3) nRotations = nRotations==1?3:1;
149 if(_dictSize!=-1 && predict_id==_dictSize*4)
return false;
159 SVMMarkers::SVMMarkers(){
163 bool SVMMarkers::load(
string path)
throw (cv::Exception){
164 return _impl->
load(path);
172 bool SVMMarkers::detect(
const cv::Mat &in,
int &marker_id,
int &nRotations) {
return _impl->detect(in,marker_id,nRotations);}
173 int SVMMarkers::getBestInputSize(){
return _impl->getBestInputSize();}
bool detect(const cv::Mat &in, int &mid, int &nRotations)
const CwiseUnaryOp< internal::scalar_sqrt_op< Scalar >, const Derived > sqrt() const