main.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Zoran Mesec   *
00003  *   zoran.mesec@gmail.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #define USE_OPENCV
00021 //#define USE_QT 1
00022 //#define USE_VIGRA 1
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <stdio.h>
00029 #include <iostream>
00030 #include <stdlib.h>
00031 #include <math.h>
00032 
00033 #include <string>
00034 #include "Image.h"
00035 #include "HessianDetector.h"
00036 #include "ANN/ANN.h"
00037 
00038 using namespace std;
00039 
00040 int main(int argc, char *argv[])
00041 {
00042     int kernelArray[]={9,11,15,17,21,27};
00043 
00044     double nrPoints=1000;
00045 
00046     string mystr;
00047     if(argc!=1) {
00048         cout << "Usage: ./main path_to_image" << "\n";
00049         return 0;
00050     }
00051 
00052     mystr="graffiti/img1.ppm";
00053     Image im2(mystr);
00054     if(im2.open()) {
00055        // cout << "Image opened!!!" << "\n";
00056     } else {
00057         cout<< "Error!!!"<<"\n";
00058         return 0;
00059     }
00060     im2.integrate();
00061 
00062     mystr="graffiti/img6.ppm";
00063     Image im(mystr);
00064     if(im.open()) {
00065        //cout << "Image opened!!!" << "\n";
00066     } else {
00067         cout<< "Error!!!"<<"\n";
00068         return 0;
00069     }
00070     im.integrate();
00071 
00072     double homography[3][3]= {
00073     1.0427236e+00, 1.2359858e-02,-1.6974167e+01
00074     ,-4.2238744e-03, 1.0353397e+00,-4.5312478e+01
00075     , 1.2020516e-05, 8.2950327e-06, 1.0000000e+00
00076     };
00077 
00078     int nrNeighbours = 1;   //number of nearest neighbours
00079 
00080         ANNpoint                        queryPt;                                // query point
00081         ANNpoint                        closestPt;                              // closest point to the query point
00082         ANNidxArray                     nnIdx;                                  // near neighbor indices
00083         ANNdistArray            dists;                                  // near neighbor distances
00084 
00085         ANNpointArray           dataPts;                                // data points
00086 
00087     ANNkd_tree* kdTree;
00088 
00089     for(int i=0;i<6;i++) {  //over all possible kernels
00090 
00091     int pointsInRegion=0;
00092 
00093     //detector for the first image
00094     HessianDetector hd2(&im2,nrPoints);
00095     if(!hd2.detect(kernelArray[i])) {
00096     cout << "Detection of points failed!";
00097     return 1;
00098     }
00099     //hd2.printPoints();
00100 
00101     //get detected points
00102     vector<vector<int> > points1 = hd2.getPoints();
00103 
00104     HessianDetector hd(&im,nrPoints,HD_BOX_FILTERS);
00105     if(!hd.detect(kernelArray[i])) {
00106     cout << "Detection of points failed!";
00107     return 1;
00108     }
00109     //hd.printPoints();
00110 
00111     vector<vector<int> > points2 = hd.getPoints();
00112 
00113     vector<int > point;
00114 
00115     dataPts = annAllocPts(nrPoints, 2);                 // allocate data points
00116 
00117     int pointCount=0;
00118 
00119     //fill the points array with the resulting points from the detector
00120     vector<vector<int> >::iterator iter = points2.begin();
00121      while( iter != points2.end()) {
00122         point=*iter;
00123         ANNpoint pTmp= annAllocPt(2);
00124         pTmp[0]=point[0];
00125         pTmp[1]=point[1];
00126         dataPts[pointCount]= pTmp;
00127         pointCount++;
00128         //cout << "("<<pTmp[0]<<","<<pTmp[1]<< ","<< ")\n";
00129        iter++;
00130      }
00131 
00132     //create a tree of 2 dimension from the points
00133     kdTree = new ANNkd_tree(dataPts,nrPoints,2);
00134 
00135     vector<double> result;
00136     double tmp=0;
00137     double score=0;
00138 
00139     vector<vector<int> >::iterator iter1 = points1.begin();
00140      while( iter1 != points1.end()) {
00141          vector<int > point=*iter1;
00142          if(point[0]>200 && point[0]<400 && point[1]>200 && point[1]<400) {
00143          //cout << "("<<point[0]<<","<<point[1]<< ","<< point[2] <<")->\n";
00144             pointsInRegion++;
00145             result.clear();
00146 
00147             //multiply the pixel with the homography
00148             for(int i=0;i<3;i++) {
00149                 tmp=0;
00150                 tmp+=point[0]*homography[i][1];
00151                 tmp+=point[1]*homography[i][0];
00152                 tmp+=homography[i][2];
00153                 result.push_back(tmp);
00154             }
00155             //cout << "("<<result[1]/result[2]<< ","<<result[0]/result[2]<<","<< result[2]/result[2]<<")\n";
00156 
00157             queryPt = annAllocPt(2);                                    // allocate query point
00158             queryPt[1]=result[0]/result[2];
00159             queryPt[0]=result[1]/result[2];
00160 
00161             //cout << "("<<queryPt[0]<<","<<queryPt[1]<< ","<< ")\n";
00162 
00163                         nnIdx = new ANNidx[nrNeighbours];                                               // allocate near neigh indices
00164                         dists = new ANNdist[nrNeighbours];                                              // allocate near neighbor dists
00165 
00166             //search for closest point on the second image
00167                         kdTree->annkSearch(                                             // search
00168                                         queryPt,                                                // query point
00169                                         nrNeighbours,                                   // number of near neighbors
00170                                         nnIdx,                                                  // nearest neighbors (returned)
00171                                         dists,                                                  // distance (returned)
00172                                         0);                                                         // error bound
00173 
00174                         for (int l = 0; l < nrNeighbours; l++) {                        // print summary
00175                                 dists[l] = sqrt(dists[l]);                      // unsquare distance
00176                 closestPt = dataPts[nnIdx[l]];
00177                 //cout << "\t" << "\t(" << closestPt[0] << ","<< closestPt[1]<< ") " << nnIdx[l] << "\t" << dists[l] << "\n";
00178                 //if pixel is closer than 1.5 we have correspondence
00179                 if(dists[l]<1.5) {
00180                     score++;
00181                 }
00182                         }
00183          }
00184          iter1++;
00185      }
00186     double repeatability= score/pointsInRegion;
00187     cout << repeatability <<" ";
00188     }
00189     //}
00190     return EXIT_SUCCESS;
00191 }

Generated on Mon Sep 9 01:25:41 2013 for Hugintrunk by  doxygen 1.3.9.1