35 #include <opencv2/highgui/highgui.hpp> 49 if (m1.at<uchar>(y,x)!=m2.at<uchar>(y,x)) dist++;
57 for (
int i=0;i<in.rows;i++)
59 for (
int j=0;j<in.cols;j++)
61 out.at<uchar>(i,j)=in.at<uchar>(in.cols-j-1,i);
68 int HammDist(
const cv::Mat &m1,
const cv::Mat & m2)
70 cv::Mat mc=m1.clone();
71 int minD=std::numeric_limits<int>::max();
74 if( dist<minD) minD=dist;
87 for (
int x=0;x<5;x++){
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++;
107 cerr<<
"Usage: nofMarkers outbasename size [minimum_entropy(9,25)]"<<endl;
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++){
121 cout<<
"Calculating distance matrix"<<endl;
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]);
129 int nMarkers=atoi(argv[1]);
131 vector<bool> usedMarkers(1024,
false);
135 vector<int> selectedMarkers;
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;
145 for(
size_t i=0;i<ventropy.size();i++)
146 if (ventropy[i]<minimimEntropy) usedMarkers[i]=
true;
148 cout<<
"Max Entroy in ="<<bestEntr<<
" "<<ventropy[bestEntr]<<endl;
150 for (
int i=1;i<nMarkers;i++) {
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);
160 if (minDist>shorterDist){
166 if (bestMarker!=-1 && shorterDist>1 ){
167 selectedMarkers.push_back(bestMarker);
168 usedMarkers[bestMarker]=
true;
171 else {cerr<<
"COUDL NOT ADD ANY MARKER"<<endl;exit(0);}
175 sort(selectedMarkers.begin(),selectedMarkers.end());
176 for(
size_t i=0;i<selectedMarkers.size();i++){
178 sprintf(name,
"%s%d.png",argv[2],selectedMarkers[i]);
180 cout<<selectedMarkers[i]<<
" "<<flush;
182 imwrite(name,markerImage);
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++){
190 if (distances.at<
int> ( selectedMarkers[i], selectedMarkers[j]) < minDist) minDist=distances.at<
int> ( selectedMarkers[i], selectedMarkers[j]);
195 cout<<
"Min Dist="<<minDist<<endl;
198 catch (std::exception &ex)
200 cout<<ex.what()<<endl;
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)
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)