aruco_selectoptimalmarkers.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  * Select the n best markers according to their distance. In other words, selects the n farthert markers in terms of hamming distance
30  *
31  *
32  *
33  *****/
34 
35 #include <opencv2/highgui/highgui.hpp>
36 #include <aruco/aruco.h>
37 #include <iostream>
38 #include <limits>
39 #include <aruco/arucofidmarkers.h>
40 using namespace cv;
41 using namespace std;
42 
43 int HammDist_(const cv::Mat &m1,const cv::Mat & m2)
44 {
45 
46  int dist=0;
47  for (int y=0;y<5;y++)
48  for (int x=0;x<5;x++)
49  if (m1.at<uchar>(y,x)!=m2.at<uchar>(y,x)) dist++;
50  return dist;
51 
52 }
53 Mat rotate(Mat in)
54 {
55  Mat out;
56  in.copyTo(out);
57  for (int i=0;i<in.rows;i++)
58  {
59  for (int j=0;j<in.cols;j++)
60  {
61  out.at<uchar>(i,j)=in.at<uchar>(in.cols-j-1,i);
62  }
63  }
64  return out;
65 }
66 
67 
68 int HammDist(const cv::Mat &m1,const cv::Mat & m2)
69 {
70  cv::Mat mc=m1.clone();
71  int minD=std::numeric_limits<int>::max();
72  for(int i=0;i<4;i++){
73  int dist=HammDist_(mc,m2);
74  if( dist<minD) minD=dist;
75  mc= rotate(mc);
76  }
77  return minD;
78 
79 }
80 
81 int entropy(const cv::Mat &marker)
82 {
83 
84  //the entropy is calcualte for each bin as the number of elements different from it in its sourroundings
85  int totalEntropy=0;
86  for (int y=0;y<5;y++)
87  for (int x=0;x<5;x++){
88  int minX=max(x-1,0);
89  int maxX=min(x+1,5);
90  int minY=max(y-1,0);
91  int maxY=min(y+1,5);
92 
93  for(int yy=minY;yy<maxY;yy++)
94  for(int xx=minX;xx<maxX;xx++)
95  if (marker.at<uchar>(y,x)!=marker.at<uchar>(yy,xx)) totalEntropy++;
96  }
97 
98  return totalEntropy;
99 }
100 
101 int main(int argc,char **argv)
102 {
103  try {
104  if (argc<4) {
105 
106  //You can also use ids 2000-2007 but it is not safe since there are a lot of false positives.
107  cerr<<"Usage: nofMarkers outbasename size [minimum_entropy(9,25)]"<<endl;
108  return -1;
109  }
110 
111 
112  //create a vector with all markers
113  int minimimEntropy=0;
114  if(argc>=5) minimimEntropy=atoi(argv[4]);
115  vector<cv::Mat> markers;
116  vector<int> ventropy;
117  for (int i=0;i<1024;i++){
118  markers.push_back(aruco::FiducidalMarkers::getMarkerMat(i) );
119  ventropy.push_back(entropy( aruco::FiducidalMarkers::getMarkerMat(i) ));
120  }
121  cout<<"Calculating distance matrix"<<endl;
122  //create a matrix with all distances
123  cv::Mat distances=cv::Mat::zeros(1024,1024,CV_32SC1);
124  for (int i=0;i<1024;i++)
125  for (int j=i+1;j<1024;j++)
126  distances.at<int>(i,j)=distances.at<int>(j,i)= HammDist ( markers[i],markers[j]);
127  cout<<"done"<<endl;
128  //
129  int nMarkers=atoi(argv[1]);
130  //select the first marker
131  vector<bool> usedMarkers(1024,false);
132 
133 
134 
135  vector<int> selectedMarkers;
136  //select the masker with higher entropy first
137  int bestEntr=0;
138  for(size_t i=0;i<ventropy.size();i++)
139  if (ventropy[i]>ventropy[bestEntr]) bestEntr=i;
140  selectedMarkers.push_back(bestEntr);
141  usedMarkers[bestEntr]=true;
142 
143  //remove these with low entropy. Not very elegnat. Other day I will improve the algorithm
144  //to make it multiobjective
145  for(size_t i=0;i<ventropy.size();i++)
146  if (ventropy[i]<minimimEntropy) usedMarkers[i]=true;
147 
148  cout<<"Max Entroy in ="<<bestEntr<<" "<<ventropy[bestEntr]<<endl;
149  //add new markers according to the distance added to the global
150  for (int i=1;i<nMarkers;i++) {
151  int bestMarker=-1;
152  int shorterDist=0;
153  //select as new marker the one that maximizes mean distance to
154  for (int j=0;j<distances.cols;j++) {
155  if (!usedMarkers[j]) {
156  int minDist=std::numeric_limits< int >::max();
157  for (size_t k=0;k<selectedMarkers.size();k++)
158  if (distances.at<int> ( selectedMarkers[k], j)<minDist) minDist=distances.at<int> ( selectedMarkers[k], j);
159 // cout<<"j="<<j<<" "<<distSum<<"|"<<flush;
160  if (minDist>shorterDist){
161  shorterDist=minDist;
162  bestMarker=j;
163  }
164  }
165  }
166  if (bestMarker!=-1 && shorterDist>1 ){
167  selectedMarkers.push_back(bestMarker);
168  usedMarkers[bestMarker]=true;
169 // cout<<"Best id="<<bestMarker<<" dist="<<farthestDist<< endl;
170  }
171  else {cerr<<"COUDL NOT ADD ANY MARKER"<<endl;exit(0);}
172 // char c;cin>>c;
173  }
174 
175  sort(selectedMarkers.begin(),selectedMarkers.end());
176  for(size_t i=0;i<selectedMarkers.size();i++){
177  char name[1024];
178  sprintf(name,"%s%d.png",argv[2],selectedMarkers[i]);
179 // cout<<"name="<<name<<endl;
180  cout<<selectedMarkers[i]<<" "<<flush;
181  Mat markerImage=aruco::FiducidalMarkers::createMarkerImage(selectedMarkers[i],atoi(argv[3]));
182  imwrite(name,markerImage);
183  }
184  cout<<endl;
185  //print the minimim distance between any two elements
186  int minDist=std::numeric_limits<int>::max();
187  for(size_t i=0;i<selectedMarkers.size()-1;i++)
188  for(size_t j=i+1;j<selectedMarkers.size();j++){
189 // cout<<" d=" << selectedMarkers[i]<<" "<<selectedMarkers[j]<<"->"<<distances.at<int> ( selectedMarkers[i], selectedMarkers[j])<<endl;
190  if (distances.at<int> ( selectedMarkers[i], selectedMarkers[j]) < minDist) minDist=distances.at<int> ( selectedMarkers[i], selectedMarkers[j]);
191 
192  }
193 
194 
195  cout<<"Min Dist="<<minDist<<endl;
196 
197  }
198  catch (std::exception &ex)
199  {
200  cout<<ex.what()<<endl;
201  }
202 
203 }
204 
int main(int argc, char **argv)
static cv::Mat createMarkerImage(int id, int size)
Creates an ar marker with the id specified using a modified version of the hamming code...
int entropy(const cv::Mat &marker)
Mat rotate(Mat in)
static cv::Mat getMarkerMat(int id)
int HammDist(const cv::Mat &m1, const cv::Mat &m2)
int HammDist_(const cv::Mat &m1, const cv::Mat &m2)


aruco
Author(s): Rafael Muñoz Salinas , Bence Magyar
autogenerated on Wed Sep 2 2020 04:02:09