📄 sift-driver.cpp
字号:
if(outputFilename.size() == 0) { // case 2) above outputFilename = name ; // if we specify an output directory, then extract // the basename if(outputFilenamePrefix.size() != 0) { outputFilename = outputFilenamePrefix + std::string(basename(outputFilename.c_str())) ; } // remove .pgm extension, add .key outputFilename = removeExtension(outputFilename, ".pgm") ; outputFilename += ".key" ; } // remove .key extension, add .desc descriptorsFilename = removeExtension(outputFilename, ".key") ; descriptorsFilename += ".desc" ; // --------------------------------------------------------------- // Load PGM image // --------------------------------------------------------------- verbose && cout<<"Lodaing PGM image '"<<name<<"' ..."<<flush; try { ifstream in(name.c_str()) ; if(! in.good()) VL_THROW("Could not open '"<<name<<"'.") ; extractPgm(in, buffer) ; } catch(VL::Exception const& e) { throw VL::Exception("PGM read error: "+e.msg) ; } verbose && cout << " done (" << buffer.width<<" x "<<buffer.height<<")."<<endl ; // --------------------------------------------------------------- // Gaussian scale space // --------------------------------------------------------------- if( verbose ) cout << "Computing Gaussian scale space ... " << flush ; int O = octaves ; int const S = levels ; int const omin = first ; float const sigman = .5 ; float const sigma0 = 1.6 * powf(2.0f, 1.0f / S) ; // optionally autoselect the number number of octaves // we downsample up to 8x8 patches if(O < 1) { O = std::max (int (std::floor (log2 (std::min(buffer.width,buffer.height))) - omin -3), 1) ; } // initialize scalespace VL::Sift sift(buffer.data, buffer.width, buffer.height, sigman, sigma0, O, S, omin, -1, S+1) ; verbose && cout << "("<<O<<" octaves from "<<omin<<", " <<S<< " levels per octave) done."<<endl ; // --------------------------------------------------------------- // Save Gaussian scale space // --------------------------------------------------------------- if(savegss) { verbose && cout<<"Saving Gaussian scale space."<<endl ; string imageBasename = removeExtension(outputFilename, ".key") ; for(int o = omin ; o < omin + O ; ++o) { for(int s = 0 ; s < S ; ++s) { ostringstream suffix ; suffix<<'.'<<o<<'.'<<s<<".pgm" ; string imageFilename = imageBasename + suffix.str() ; verbose && cout << "Saving octave " <<o << " level "<<s << " to '"<<imageFilename<<"' ..."<<flush ; ofstream fout(imageFilename.c_str()) ; if(!fout.good()) VL_THROW("Could not open '"<<imageFilename<<'\'') ; VL::insertPgm(fout, sift.getLevel(o,s), sift.getOctaveWidth(o), sift.getOctaveHeight(o)) ; fout.close() ; verbose && cout<<" done."<<endl ; } } } // --------------------------------------------------------------- // SIFT detector // --------------------------------------------------------------- if( ! haveKeypoints ) { verbose && cout << "Running SIFT detector (threshold:"<<threshold << ", edge-threshold: "<<edgeThreshold << ") ..."<<flush ; sift.detectKeypoints(threshold, edgeThreshold) ; verbose && cout << " done (" << sift.keypointsEnd() - sift.keypointsBegin() << " keypoints)." << endl ; } // --------------------------------------------------------------- // SIFT orientations and descriptors // --------------------------------------------------------------- if( verbose ) { if( ! noorient && nodescr) cout << "Computing keypoint orientations" ; if( noorient && ! nodescr) cout << "Computing keypoint descriptors" ; if( ! noorient && ! nodescr) cout << "Computing orientations and descriptors" ; if( noorient && nodescr) cout << "Finalizing" ; cout<<flush ; } { auto_ptr<ifstream> keypointsIn_pt ; auto_ptr<ofstream> descriptorsOut_pt ; // open output file ofstream out(outputFilename.c_str()) ; if(!out.good()) VL_THROW("Could not open output file '"<<outputFilename<<"'.") ; verbose && cout<<" (writing to '"<<outputFilename<<"')"<<flush ; out.flags(ios::fixed) ; // optionally open keypoints file if( haveKeypoints ) { keypointsIn_pt = auto_ptr<ifstream>(new ifstream(keypointsFilename.c_str())) ; if(!keypointsIn_pt->good()) VL_THROW("Could not open keypoints file '"<<keypointsFilename<<"'.") ; if(verbose) cout<<" (reading from '"<<keypointsFilename<<"')"<<flush ; } // optionally open descriptors file if( binary ) { descriptorsOut_pt = auto_ptr<ofstream> (new ofstream(descriptorsFilename.c_str(), ios::binary)) ; if(!descriptorsOut_pt->good()) VL_THROW("Could not open descriptors file '"<<descriptorsFilename<<"'.") ; if(verbose) cout << " (writing '"<<descriptorsFilename<<"')"<<flush; } verbose && cout<<" ..."<<flush ; if( haveKeypoints ) { // ------------------------------------------------------------- // Reads keypoint from file, compute descriptors // ------------------------------------------------------------- Keypoints keypoints ; // readoff keypoints while( !keypointsIn_pt->eof() ) { VL::float_t x,y,sigma,th ; // extract next keypoint from file (*keypointsIn_pt) >> x>>y>>sigma>>th ; (*keypointsIn_pt).ignore(numeric_limits<streamsize>::max(),'\n') ; if(keypointsIn_pt->eof()) break ; if(!keypointsIn_pt->good()) VL_THROW("Error reading keypoints file") ; // compute integer components VL::Sift::Keypoint key = sift.getKeypoint(x,y,sigma) ; Keypoints::value_type entry ; entry.first = key ; entry.second = th ; keypoints.push_back(entry) ; } // sort keypoints if(! stableorder) sort(keypoints.begin(), keypoints.end(), cmpKeypoints) ; // process in batch for(Keypoints::const_iterator iter = keypoints.begin() ; iter != keypoints.end() ; ++iter) { VL::Sift::Keypoint const& key = iter->first ; VL::float_t th = iter->second ; // write keypoint out << setprecision(2) << key.x << " " << setprecision(2) << key.y << " " << setprecision(2) << key.sigma << " " << setprecision(3) << th ; // compute descriptor VL::float_t descr [128] ; sift.computeKeypointDescriptor(descr, key, th) ; VL::uint8_t intDescr [128] ; for(int i = 0 ; i < 128 ; ++i) { intDescr[i] = VL::uint8_t(VL::float_t(512) * descr[i]) ; } if(descriptorsOut_pt.get()) { descriptorsOut_pt->write(reinterpret_cast<char*>(intDescr), 128) ; } else { for(int i = 0 ; i < 128 ; ++i) { out << ' '<< uint32_t(intDescr[i]) ; } } out << endl ; } // next keypoint } else { // ------------------------------------------------------------- // Run detector, compute orientations and descriptors // ------------------------------------------------------------- for( VL::Sift::KeypointsConstIter iter = sift.keypointsBegin() ; iter != sift.keypointsEnd() ; ++iter ) { // detect orientations VL::float_t angles [4] ; int nangles ; if( ! noorient ) { nangles = sift.computeKeypointOrientations(angles, *iter) ; } else { nangles = 1; angles[0] = VL::float_t(0) ; } // compute descriptors for(int a = 0 ; a < nangles ; ++a) { out << setprecision(2) << iter->x << ' ' << setprecision(2) << iter->y << ' ' << setprecision(2) << iter->sigma << ' ' << setprecision(3) << angles[a] ; if( ! nodescr ) { VL::float_t descr [128] ; sift.computeKeypointDescriptor(descr, *iter, angles[a]) ; VL::uint8_t intDescr [128] ; for(int i = 0 ; i < 128 ; ++i) { intDescr[i] = VL::uint8_t(VL::float_t(512) * descr[i]) ; } if(descriptorsOut_pt.get()) { descriptorsOut_pt->write( reinterpret_cast<char*>(intDescr), 128 ) ; } else { for(int i = 0 ; i < 128 ; ++i) { out << ' '<< uint32_t(intDescr[i]) ; } } } out << endl ; } // next angle } // next keypoint } out.close() ; if(descriptorsOut_pt.get()) descriptorsOut_pt->close(); if(keypointsIn_pt.get()) keypointsIn_pt->close(); verbose && cout << " done."<<endl ; } argc-- ; argv++ ; outputFilename = string("") ; } catch(VL::Exception &e) { cerr<<endl<<"Error processing '"<<name<<"': "<<e.msg<<endl ; return 1 ; } } // next image return 0 ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -