00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "GUI/XWindow.H"
00040 #include "Image/Image.H"
00041 #include "Image/ColorOps.H"
00042 #include "Image/CutPaste.H"
00043 #include "Image/Rectangle.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/MatrixOps.H"
00046 #include "Image/Transforms.H"
00047 #include "Image/Convolutions.H"
00048 #include "Media/FrameSeries.H"
00049 #include "nub/ref.h"
00050 #include "Raster/GenericFrame.H"
00051 #include "Raster/Raster.H"
00052 #include "Util/Types.H"
00053 #include "Util/log.H"
00054 #include "SIFT/ScaleSpace.H"
00055 #include "SIFT/VisualObject.H"
00056 #include "SIFT/Keypoint.H"
00057 #include "SIFT/VisualObjectDB.H"
00058
00059 #include "CUDASIFT/CUDAVisualObject.H"
00060
00061 #include "CUDASIFT/tpimageutil.h"
00062 #include "CUDASIFT/tpimage.h"
00063 #include "CUDASIFT/cudaImage.h"
00064 #include "CUDASIFT/cudaSift.h"
00065 #include "CUDASIFT/cudaSiftH.h"
00066
00067 #include "Util/log.H"
00068
00069 #include <fstream>
00070 #include <iostream>
00071 #include <iomanip>
00072 #include <string>
00073 #include <unistd.h>
00074 #include <cstdio>
00075 #include <dirent.h>
00076
00077
00078 std::vector<std::string> readDir(std::string inName)
00079 {
00080 DIR *dp = opendir(inName.c_str());
00081 if(dp == NULL)
00082 {
00083 LFATAL("Directory does not exist %s",inName.c_str());
00084 }
00085 dirent *dirp;
00086 std::vector<std::string> fList;
00087 while ((dirp = readdir(dp)) != NULL ) {
00088 if (dirp->d_name[0] != '.')
00089 fList.push_back(inName + '/' + std::string(dirp->d_name));
00090 }
00091 LINFO("%"ZU" files in the directory\n", fList.size());
00092 LINFO("file list : \n");
00093 for (unsigned int i=0; i<fList.size(); i++)
00094 LINFO("\t%s", fList[i].c_str());
00095 std::sort(fList.begin(),fList.end());
00096 return fList;
00097 }
00098
00099
00100 std::vector<std::string> readList(std::string inName)
00101 {
00102 std::ifstream inFile;
00103 inFile.open(inName.c_str(),std::ios::in);
00104 if(!inFile){
00105 LFATAL("Unable to open image path list file: %s",inName.c_str());
00106 }
00107 std::string sLine;
00108 std::vector<std::string> fList;
00109 while (std::getline(inFile, sLine)) {
00110 std::cout << sLine << std::endl;
00111 fList.push_back(sLine);
00112 }
00113 LINFO("%"ZU" paths in the file\n", fList.size());
00114 LINFO("file list : \n");
00115 for (unsigned int i=0; i<fList.size(); i++)
00116 LINFO("\t%s", fList[i].c_str());
00117 inFile.close();
00118 return fList;
00119 }
00120
00121 std::string matchObject(Image<float> &ima, VisualObjectDB& vdb, float &score)
00122 {
00123 std::vector< rutz::shared_ptr<VisualObjectMatch> > matches;
00124 #ifdef GPUSIFT
00125 rutz::shared_ptr<CUDAVisualObject>
00126 vo(new CUDAVisualObject("PIC", "PIC", ima,
00127 Point2D<int>(-1,-1),
00128 std::vector<float>(),
00129 std::vector< rutz::shared_ptr<Keypoint> >(),
00130 false,true));
00131 #else
00132 rutz::shared_ptr<VisualObject>
00133 vo(new VisualObject("PIC", "PIC", ima,
00134 Point2D<int>(-1,-1),
00135 std::vector<float>(),
00136 std::vector< rutz::shared_ptr<Keypoint> >(),
00137 false,true));
00138 #endif
00139 const uint nmatches = vdb.getObjectMatches(vo, matches, VOMA_SIMPLE,
00140 100U,
00141 0.5F,
00142 0.5F,
00143 1.0F,
00144 3U,
00145 100U,
00146 false
00147 );
00148 score = 0;
00149 float avgScore = 0, affineAvgDist = 0;
00150 int nkeyp = 0;
00151 int objId = -1;
00152 if (nmatches > 0)
00153 {
00154 rutz::shared_ptr<VisualObject> obj;
00155 rutz::shared_ptr<VisualObjectMatch> vom;
00156 for (unsigned int i = 0; i < 1; ++i)
00157 {
00158 vom = matches[i];
00159 obj = vom->getVoTest();
00160 score = vom->getScore();
00161 nkeyp = vom->size();
00162 avgScore = vom->getKeypointAvgDist();
00163 affineAvgDist = vom->getAffineAvgDist();
00164
00165 objId = atoi(obj->getName().c_str()+3);
00166
00167 return obj->getName();
00168 LINFO("### Object match with '%s' score=%f ID:%i",
00169 obj->getName().c_str(), vom->getScore(), objId);
00170
00171
00172
00173
00174 double dist = 0;
00175 for (int keyp=0; keyp<nkeyp; keyp++)
00176 {
00177 const KeypointMatch kpm = vom->getKeypointMatch(keyp);
00178
00179 float refX = kpm.refkp->getX();
00180 float refY = kpm.refkp->getY();
00181
00182 float tstX = kpm.tstkp->getX();
00183 float tstY = kpm.tstkp->getY();
00184 dist += (refX-tstX) * (refX-tstX);
00185 dist += (refY-tstY) * (refY-tstY);
00186 }
00187
00188
00189
00190
00191
00192 }
00193
00194 }
00195
00196 return std::string("nomatch");
00197 }
00198
00199 int main(const int argc, const char **argv)
00200 {
00201
00202 MYLOGVERB = LOG_INFO;
00203
00204 ModelManager *mgr = new ModelManager("Test SIFT with Feature Learning");
00205
00206
00207 mgr->exportOptions(MC_RECURSE);
00208
00209
00210
00211
00212 if (mgr->parseCommandLine( (const int)argc, (const char**)argv,
00213 "<cudadev> <dir|list:images> <vdbfile>", 3, 3) == false)
00214 return 1;
00215
00216 std::string images,devArg;
00217 std::string vdbFileName;
00218
00219 std::string testPosName;
00220
00221 devArg = mgr->getExtraArg(0);
00222 images = mgr->getExtraArg(1);
00223 vdbFileName = mgr->getExtraArg(2);
00224
00225
00226 int dev = strtol(devArg.c_str(),NULL,0);
00227 std::cout << "device = " << dev << std::endl;
00228 cudaSetDevice(dev);
00229
00230 LINFO("Loading db from %s\n", vdbFileName.c_str());
00231 VisualObjectDB vdb;
00232 vdb.loadFrom(vdbFileName,false);
00233
00234
00235 std::string::size_type dirArg=images.find("dir:",0);
00236 std::string::size_type listArg=images.find("list:",0);
00237 std::string imagename;
00238 std::string::size_type spos,dpos;
00239
00240 if((dirArg == std::string::npos &&
00241 listArg == std::string::npos) ||
00242 (dirArg != 0 && listArg != 0)){
00243 LFATAL("images argument is in one of the following formats - dir:<DIRNAME> or list:<LISTOFIMAGEPATHSFILE>");
00244 return EXIT_FAILURE;
00245 }
00246 if(dirArg == 0)
00247 images = images.substr(4);
00248 else
00249 images = images.substr(5);
00250
00251
00252 mgr->start();
00253
00254 std::vector<std::string> imageFileNames;
00255 if(dirArg == 0)
00256 imageFileNames = readDir(images);
00257 else
00258 imageFileNames = readList(images);
00259
00260 for(unsigned int imgInd=0;imgInd<imageFileNames.size();imgInd++){
00261 Image<float> inputf = Raster::ReadGray(imageFileNames[imgInd]);
00262
00263
00264 spos = imageFileNames[imgInd].find_last_of('/');
00265 dpos = imageFileNames[imgInd].find_last_of('.');
00266 imagename = imageFileNames[imgInd].substr(spos+1,dpos-spos-1);
00267
00268 std::cout << "imageFileNames[" << imgInd << "] = " << imageFileNames[imgInd] << std::endl;
00269 std::cout << "spos = " << spos << " ,dpos = " << dpos << std::endl;
00270 std::cout << "imagename = " << imagename << std::endl;
00271
00272 float score = 0.0;
00273 std::string objName = matchObject(inputf, vdb, score);
00274 printf("objName=%s,score=%f\n",objName.c_str(),score);
00275 }
00276
00277 return 0;
00278 }
00279
00280
00281
00282
00283
00284