svmmarkers.cpp
Go to the documentation of this file.
1 /*****************************
2 Copyright 2011 Rafael Muñoz Salinas. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification, are
5 permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of
8  conditions and the following disclaimer.
9 
10  2. Redistributions in binary form must reproduce the above copyright notice, this list
11  of conditions and the following disclaimer in the documentation and/or other materials
12  provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY Rafael Muñoz Salinas ''AS IS'' AND ANY EXPRESS OR IMPLIED
15 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Rafael Muñoz Salinas OR
17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 The views and conclusions contained in the software and documentation are those of the
25 authors and should not be interpreted as representing official policies, either expressed
26 or implied, of Rafael Muñoz Salinas.
27 ********************************/
28 
29 #include "svmmarkers.h"
30 #include <iostream>
31 #include <vector>
32 #include <math.h>
33 #include <string>
34 #include <fstream>
35 #include <opencv2/core/core.hpp>
36 
37 #include <opencv2/imgproc/imgproc.hpp>
38 #include <opencv2/ml/ml.hpp>
39 #include <iostream>
40 
41 using namespace std;
42 
43 namespace aruco {
44 namespace impl{
45 class SVMMarkers {
46  float _minFeatureValue, _maxFeatureValue; // image range for svm classification
47  int _patchSize, _dictSize; // marker pathSize (total features = pathSize^2) and dictionary size (number of markers)
48  bool _rotateMarkers; // interchange rotation 1 and 3 ??
49 #ifdef OPENCV_VERSION_3
50  cv::Ptr<cv::ml::SVM> _model;
51 #else
52  cv::Ptr<CvSVM> _model ;
53 #endif
54 public:
56  // static variables from SVMMarkers. Need to be here to avoid linking errors
57  _minFeatureValue = -1;
58  _maxFeatureValue = 1;
59  _patchSize = 10;
60  _dictSize = -1;
61  _rotateMarkers = true;
62 
63  }
64 
65 
66  int getBestInputSize(){return _patchSize;}
67  bool load(string path)throw (cv::Exception){
68  if (path.empty())return false;
69 
70 
71 #ifdef OPENCV_VERSION_3
72  _model= cv::ml::StatModel::load<cv::ml::SVM>(path);
73  _patchSize=sqrt(_model->getSupportVectors().size().width);
74 
75 #else
76  _model=new CvSVM;
77  _model->load(path.c_str());
78  _patchSize=sqrt(_model->get_var_count());
79 
80 #endif
81  //read in the file the number of classes
82  ifstream svmfile(path.c_str());
83  if (!svmfile)return false;
84  _dictSize=-1;
85  while(!svmfile.eof()){
86  std::string str;
87  std::getline(svmfile,str);
88  if (str.find("class_count:")!=std::string::npos){
89  stringstream sstr;sstr<<str;
90  string cc;
91  int nclasses;
92  sstr>>cc>>nclasses;
93  _dictSize=(nclasses-1)/4;
94  }
95  }
96  if (_dictSize==-1)return false;
97 
98  return true;
99  }
100 
101 
102 
103 
104 
105 
108  bool detect(const cv::Mat &in, int &mid,int &nRotations) {
109 
110 
111  // convert to gray
112  assert(in.rows == in.cols);
113  cv::Mat grey;
114  if (in.type() == CV_8UC1)
115  grey = in;
116  else
117  cv::cvtColor(in, grey, CV_BGR2GRAY);
118 
119  // resize to svm path size
120  cv::Mat greyResized;
121  if(grey.cols != _patchSize)
122  cv::resize(grey, greyResized, cv::Size(_patchSize, _patchSize) );
123  else
124  greyResized = grey;
125 
126  // normalize image range
127  cv::Mat greyResizedNormalized;
128  cv::normalize(greyResized, greyResizedNormalized, _minFeatureValue, _maxFeatureValue, cv::NORM_MINMAX, CV_32FC1);
129 
130  // rearrange data in a row
131  cv::Mat dataRow;
132  dataRow = greyResizedNormalized.reshape(1,1);
133 
134  // predict id with svm
135 #ifdef OPENCV_VERSION_3
136  int predict_id = (int)_model->predict(dataRow);
137 #else
138  int predict_id = (int)_model->predict(dataRow, true);
139 #endif
140 
141  // get rotation of marker
142  nRotations = predict_id%4;
143  // if _rotateMarkers, interchange rotation 1 and 3
144  if(_rotateMarkers)
145  if(nRotations==1 || nRotations==3) nRotations = nRotations==1?3:1;
146 
147  // if dictSize is known (dictSize!=-1), and the marker id is equal to the number of markers * 4
148  // then it is the invalid marker class
149  if(_dictSize!=-1 && predict_id==_dictSize*4) return false;
150  // else, divide by 4 to obtain the final marker id
151  mid= predict_id/4;
152  return true;
153 
154  }
155 
156 };
157 
158 }
159 SVMMarkers::SVMMarkers(){
160  _impl=new impl::SVMMarkers;
161 }
162 
163 bool SVMMarkers::load(string path)throw (cv::Exception){
164  return _impl->load(path);
165 }
166 
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();}
174 
175 }
bool detect(const cv::Mat &in, int &mid, int &nRotations)
Definition: svmmarkers.cpp:108
bool load(string path)
Definition: svmmarkers.cpp:67
cv::Ptr< CvSVM > _model
Definition: svmmarkers.cpp:52
const CwiseUnaryOp< internal::scalar_sqrt_op< Scalar >, const Derived > sqrt() const


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:41:00