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
00022 #define USE_VIGRA
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 #include <ctime>
00033 #include <algorithm>
00034 
00035 #include <string>
00036 #include "APImage.h"
00037 #include "HessianDetector.h"
00038 #include "Descriptor.h"
00039 #include <limits>
00040 
00041 using namespace std;
00042 
00043 
00044 
00045 double getDistance(vector<double>* d1,vector<double>* d2) {
00046 double dist=0;
00047 for(int i=0; i<DESCRIPTOR_SIZE;i++) {
00048     dist+=pow((*d1)[i]-(*d2)[i],2);
00049 }
00050 return dist;
00051 }
00052 
00053 int main(int argc, char *argv[])
00054 {
00055 
00056     clock_t start,finish;
00057     double time;
00058 
00059     int nrPoints=250;
00060     if(argc!=3) {
00061         cout << "Usage: ./main img1 img2 " << "\n";
00062         return 0;
00063     }
00064     //APImage im1("trees/img1.ppm");
00065     APImage im1(argv[1]);
00066     if(!im1.open()) {
00067         cout<< "Error! APImage can not be opened"<<"\n";
00068         return 0;
00069     }
00070 
00071     APImage im2(argv[2]);
00072     if(!im2.open()) {
00073         cout<< "Error! APImage can not be opened"<<"\n";
00074         return 0;
00075     }
00076 
00077     //need to integrate the image before the detection process(only if using box filter approximation)
00078     im1.integrate();
00079     im2.integrate();
00080 
00081     start = clock();
00082 
00083     HessianDetector hd1(&im1,nrPoints, HD_BOX_FILTERS,1);
00084     if(!hd1.detect()) {
00085     cout << "Detection of points failed!";
00086     return 1;
00087     }
00088     //print points and display image with the detected points(debugging only)
00089     //hd1.printPoints(cout);
00090 
00091     finish = clock();
00092 
00093     time = (double(finish)-double(start))/CLOCKS_PER_SEC;
00094     cout << "Measured time:"<<time<<"\n";
00095 
00096 
00097     vector<vector<int> >* interestPoints1=hd1.getPoints();
00098 
00099     Descriptor d1(&im1,&hd1);
00100     d1.setPoints(interestPoints1);
00101     //d.orientate();
00102     d1.createDescriptors();
00103     //d1.generateAutopanoXML("test3.xml");
00104     /*d1.printDescriptors(cout);
00105     return 0;*/
00106 
00107     HessianDetector hd2(&im2,nrPoints, HD_BOX_FILTERS,1);
00108     if(!hd2.detect()) {
00109     cout << "Detection of points failed!";
00110     return 1;
00111     }
00112     //print points and display image with the detected points(debugging only)
00113     //hd2.printPoints();
00114 
00115 
00116     vector<vector<int> >* interestPoints2=hd2.getPoints();
00117 
00118     Descriptor d2(&im2,&hd2);
00119     d2.setPoints(interestPoints2);
00120     //d.orientate();
00121     d2.createDescriptors();
00122     /*d2.generateAutopanoXML("test4.xml");
00123     return 0;*/
00124 
00125     vector<vector<double> >* descriptors1=d1.getDescriptors();
00126     vector<vector<double> >::iterator iter1 = (*descriptors1).begin();
00127 
00128     vector<vector<double> >* descriptors2=d2.getDescriptors();
00129 
00130     int pointCount=0;
00131     int max;
00132     int maxId;  //position int the vector of the nearest neighbour
00133 
00134      while( iter1 != (*descriptors1).end()) {
00135         vector<double> current1 = *iter1;
00136 
00137         vector<vector<double> >::iterator iter2 =(*descriptors2).begin();
00138 
00139         max=1;
00140         maxId=0;
00141 
00142         vector<double> current2 = *iter2;
00143 
00144         double distance=getDistance(&current1,&current2);
00145         iter2++;
00146         double diff=distance;   //nearest neighbour
00147         double diff2=std::numeric_limits<double>::max();    //second nearest neighbour
00148 
00149         while( iter2 != (*descriptors2).end()) {
00150             current2 = *iter2;
00151             distance=getDistance(&current1,&current2);
00152 
00153             if(distance<diff) {
00154                 diff2=diff;
00155                 diff=distance;
00156                 maxId=max;
00157             } else if(distance<diff2) {
00158                 //set the new second nearest neighbour
00159                 diff2=distance;
00160             }
00161             max++;
00162             iter2++;
00163         }
00164         if(diff<(0.5*diff2)) {
00165             im1.drawCircle((*interestPoints1)[pointCount][1],(*interestPoints1)[pointCount][0],10);
00166             im2.drawCircle((*interestPoints2)[maxId][1],(*interestPoints2)[maxId][0],10);
00167         }
00168 
00169         iter1++;
00170         pointCount++;
00171      }
00172      im1.show();
00173      im2.show();
00174     return EXIT_SUCCESS;
00175 }

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