KeyPointIO.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Pablo d'Angelo
00003  *
00004  * This file is part of Panomatic.
00005  *
00006  * Panomatic is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * Panomatic is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with Panomatic; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 
00022 #include <iostream>
00023 #include <fstream>
00024 #include <string>
00025 
00026 #include "KeyPointIO.h"
00027 
00028 using namespace lfeat;
00029 using namespace std;
00030 
00031 
00032 // extremly fagile check...
00033 static bool identifySIFTKeypoints( const std::string& filename)
00034 {
00035     ImageInfo info;
00036     ifstream in(filename.c_str());
00037     if (!in)
00038     {
00039         return false;
00040     }
00041 
00042     int nKeypoints = 0;
00043     int dims = 0;
00044     in >> nKeypoints >> dims;
00045     return (cin && nKeypoints > 0 && dims >= 0);
00046 }
00047 
00048 static ImageInfo loadSIFTKeypoints( const std::string& filename, KeyPointVect_t& vec)
00049 {
00050     ImageInfo info;
00051     ifstream in(filename.c_str());
00052     if (!in.good())
00053     {
00054         return info;
00055     }
00056 
00057     int nKeypoints = 0;
00058     int dims = 0;
00059     in >> nKeypoints >> dims;
00060 
00061     info.dimensions = dims;
00062 
00063     for (int i = 0; i < nKeypoints; i++)
00064     {
00065         KeyPointPtr k(new lfeat::KeyPoint(0,0,0,0,0));
00066         in >> k->_y >> k->_x >> k->_scale >> k->_ori >> k->_score;
00067         if (dims > 0)
00068         {
00069             k->allocVector(dims);
00070             for (int j=0; j < dims; j++)
00071             {
00072                 in >> k->_vec[j];
00073             }
00074         }
00075         vec.push_back(k);
00076     }
00077     // finish reading empty line
00078     std::getline(in, info.filename);
00079     // read line with filename
00080     std::getline(in, info.filename);
00081     in >> info.width >> info.height;
00082     //std::cerr << "*** Loaded keypoints for image " << info.filename  << " ("<<info.width<<"x"<<info.height<<")"  << std::endl;
00083 
00084     return info;
00085 }
00086 
00087 ImageInfo lfeat::loadKeypoints( const std::string& filename, KeyPointVect_t& vec)
00088 {
00089     if (identifySIFTKeypoints(filename))
00090     {
00091         return loadSIFTKeypoints( filename, vec );
00092     }
00093     else
00094     {
00095         ImageInfo r;
00096         return r;
00097     }
00098 }
00099 
00100 
00101 void SIFTFormatWriter::writeHeader ( const ImageInfo& imageinfo, int nKeypoints, int dims )
00102 {
00103     _image = imageinfo;
00104     o << nKeypoints << endl;
00105     o << dims << endl;
00106 }
00107 
00108 
00109 void SIFTFormatWriter::writeKeypoint ( double x, double y, double scale, double orientation, double score, int dims, double* vec )
00110 {
00111     o << y << " " << x << " " << scale << " " << orientation << " " << score;
00112     for ( int i=0; i < dims; i++ )
00113     {
00114         o << " " <<  vec[i];
00115     }
00116     o << endl;
00117 }
00118 
00119 void SIFTFormatWriter::writeFooter()
00120 {
00121     o << _image.filename << endl;
00122     o << _image.width << " " << _image.height << endl;
00123 }
00124 
00125 
00126 void DescPerfFormatWriter::writeHeader ( const ImageInfo& imageinfo, int nKeypoints, int dims )
00127 {
00128     _image = imageinfo;
00129     o << dims << endl;
00130     o << nKeypoints << endl;
00131 }
00132 
00133 
00134 void DescPerfFormatWriter::writeKeypoint ( double x, double y, double scale, double orientation, double score, int dims, double* vec )
00135 {
00136     double sc = 2.5 * scale;
00137     sc*=sc;
00138     o << x << " " << y << " " << 1.0/sc << " 0 " << 1.0/sc;
00139     for ( int i=0; i < dims; i++ )
00140     {
00141         o << " " <<  vec[i];
00142     }
00143     o << endl;
00144 }
00145 
00146 void DescPerfFormatWriter::writeFooter()
00147 {
00148 }
00149 
00150 
00151 void AutopanoSIFTWriter::writeHeader ( const ImageInfo& imageinfo, int nKeypoints, int dims )
00152 {
00153     o << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"<< endl;
00154     o << "<KeypointXMLList xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
00155     o << "  <XDim>"<< imageinfo.width <<"</XDim>"<< endl;
00156     o << "  <YDim>"<< imageinfo.height <<"</YDim>"<< endl;
00157     o << "  <ImageFile>"<< imageinfo.filename <<"</ImageFile>"<< endl;
00158     o << "  <Arr>"<< endl;
00159 }
00160 
00161 void AutopanoSIFTWriter::writeKeypoint ( double x, double y, double scale, double orientation, double score, int dims, double* vec )
00162 {
00163     o << "    <KeypointN>"<< endl;
00164     o << "      <X>"<< x <<"</X>"<<endl;
00165     o << "      <Y>"<< y <<"</Y>"<<endl;
00166     o << "      <Scale>"<< scale <<"</Scale>"<<endl;
00167     o << "      <Orientation>"<< orientation <<"</Orientation>"<<endl;
00168     if ( dims > 0 )
00169     {
00170         o << "      <Dim>"<< dims <<"</Dim>"<<endl;
00171         o << "      <Descriptor>"<<endl;
00172 
00173         for ( int i=0; i < dims; i++ )
00174         {
00175             o << "        <int>"<< ( vec[i]*256 ) << "</int>" << endl;
00176         }
00177         o << "      </Descriptor>"<<endl;
00178     }
00179     o << "    </KeypointN>"<< endl;
00180 }
00181 
00182 void AutopanoSIFTWriter::writeFooter()
00183 {
00184     o << "  </Arr>"<< endl;
00185     o << "</KeypointXMLList>"<< endl;
00186 }
00187 
00188 

Generated on 5 Dec 2014 for Hugintrunk by  doxygen 1.4.7