ilabLoweSiftComp.C

Go to the documentation of this file.
00001 /*! @file SIFT/ilabLoweSiftComp.C compare the SIFT alg to lowe's sift program */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SIFT/ilabLoweSiftComp.C $
00035 // $Id: ilabLoweSiftComp.C 10746 2009-02-03 07:09:00Z itti $
00036 //
00037 
00038 
00039 #include "Image/Image.H"
00040 #include "Image/Pixels.H"
00041 #include "SIFT/ScaleSpace.H"
00042 #include "SIFT/VisualObject.H"
00043 #include "SIFT/Keypoint.H"
00044 #include "Util/MathFunctions.H"
00045 #include "Raster/Raster.H"
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #define USECOLOR true
00049 #define TMPKEYFILE "/tmp/loweSift.key"
00050 
00051 /* Read keypoints from the given file pointer and return the list of
00052    keypoints.  The file format starts with 2 integers giving the total
00053    number of keypoints and the size of descriptor vector for each
00054    keypoint (currently assumed to be 128). Then each keypoint is
00055    specified by 4 floating point numbers giving subpixel row and
00056    column location, scale, and orientation (in radians from -PI to
00057    PI).  Then the descriptor vector for each keypoint is given as a
00058    list of integers in range [0,255].
00059 
00060    changed to support Ilab format
00061 */
00062 
00063 std::vector< rutz::shared_ptr<Keypoint> >& ReadKeys(const char *filename)
00064 {
00065 
00066    int i, j, num, len, val;
00067    FILE *fp = fopen(filename, "r");
00068    if (!fp)
00069       LFATAL("Can not open %s", filename);
00070 
00071    std::vector< rutz::shared_ptr<Keypoint> > *keypoints = new std::vector< rutz::shared_ptr<Keypoint> >();
00072 
00073    if (fscanf(fp, "%d %d", &num, &len) != 2)
00074       LFATAL("Invalid keypoint file beginning.");
00075 
00076     if (len != 128)
00077        LFATAL("Keypoint descriptor length invalid (should be 128).");
00078 
00079     for (i = 0; i < num; i++) {
00080 
00081       float x, y, s, o;
00082       if (fscanf(fp, "%f %f %f %f", &y, &x, &s, &o) != 4)
00083          LFATAL("Invalid keypoint file format.");
00084 
00085       std::vector<byte> fv;
00086 
00087       for (j = 0; j < len; j++) {
00088          if (fscanf(fp, "%d", &val) != 1 || val < 0 || val > 255)
00089             LFATAL("Invalid keypoint file value.");
00090          fv.push_back((byte)val);
00091       }
00092 
00093       // create a keypoint:
00094       rutz::shared_ptr<Keypoint> newkey(new Keypoint(fv, x, y, s, o, 0));
00095       keypoints->push_back(newkey);
00096     }
00097 
00098     return *keypoints;
00099 }
00100 
00101 
00102 
00103 int main(const int argc, const char **argv)
00104 {
00105 
00106    if (argc < 3)
00107       LFATAL("Usage: ilabSift <lowe's sift code> <image filename> ");
00108 
00109 
00110    //get the keypoints from Lowe's output file
00111    char cmd[255];
00112    sprintf(cmd, "%s < %s > %s", argv[1], argv[2], TMPKEYFILE);
00113    if (system(cmd) == -1)
00114       LFATAL("Can not run %s", cmd);
00115 
00116    std::vector< rutz::shared_ptr<Keypoint> > loweKeypoints = ReadKeys(TMPKEYFILE);
00117    LINFO("Lowe SIFT found: %"ZU" keypoints", loweKeypoints.size());
00118 
00119 
00120    //get the keypoints from the Ilab SIFT code
00121    Image< PixRGB<byte> > input = Raster::ReadRGB(argv[2]);
00122    rutz::shared_ptr<VisualObject>
00123      vo(new VisualObject("Test", "test", input,
00124                          Point2D<int>(-1,-1),
00125                          std::vector<float>(),
00126                          std::vector< rutz::shared_ptr<Keypoint> >(),
00127                          USECOLOR));
00128    std::vector< rutz::shared_ptr<Keypoint> > ilabKeypoints = vo->getKeypoints();
00129 
00130    LINFO("Ilab SIFT found: %"ZU" keypoints", ilabKeypoints.size());
00131 
00132 
00133 
00134    //find keypoint matches
00135 
00136    int numKeyDiff = 0;
00137    int numFvDiff = 0;
00138    for(unsigned int i=0; i<loweKeypoints.size(); i++){
00139       float lowe_x = loweKeypoints[i]->getX();
00140       float lowe_y = loweKeypoints[i]->getY();
00141       float lowe_s = loweKeypoints[i]->getS();
00142       float lowe_o = loweKeypoints[i]->getO() * -1; //lowe has this from -PI to PI
00143 
00144       //look for the closest keypoint
00145       double minDist = 9999;
00146       int minMatch = -1;
00147       for (unsigned int j=0; j<ilabKeypoints.size(); j++){
00148          float ilab_x = ilabKeypoints[j]->getX();
00149          float ilab_y = ilabKeypoints[j]->getY();
00150          float ilab_s = ilabKeypoints[j]->getS();
00151          float ilab_o = ilabKeypoints[j]->getO();
00152 
00153          double dist = sqrt(squareOf(ilab_x - lowe_x) +
00154                             squareOf(ilab_y - lowe_y) +
00155                             squareOf(ilab_s - lowe_s) +
00156                             squareOf(ilab_o - lowe_o) );
00157          if (dist < minDist){
00158             minDist = dist;
00159             minMatch = j;
00160          }
00161       }
00162 
00163       float fvDist = sqrt(loweKeypoints[i]->distSquared(ilabKeypoints[minMatch]));
00164 
00165       if (minDist > 1) numKeyDiff++; //Threshold for matches
00166       if (fvDist > 500) numFvDiff++; //Threshold for matches
00167 
00168      /* LINFO("keypoint match lowe(%0.2f,%0.2f,%0.2f,%0.2f) ilab(%0.2f,%0.2f,%0.2f,%0.2f) dist %0.2f fvDist = %f",
00169             lowe_x, lowe_y, lowe_s, lowe_o,
00170             ilabKeypoints[minMatch]->getX(), ilabKeypoints[minMatch]->getY(),
00171             ilabKeypoints[minMatch]->getS(), ilabKeypoints[minMatch]->getO(),
00172             minDist, fvDist);*/
00173    }
00174    LINFO("Number of diffrent keypoints %i", numKeyDiff);
00175    LINFO("Number of diffrent Feature Vectors %i", numFvDiff);
00176 
00177 
00178 }
00179 
Generated on Sun May 8 08:06:49 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3