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  * <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 
00022 #include <iostream>
00023 #include <fstream>
00024 #include <string>
00025 
00026 #include "KeyPointIO.h"
00027 
00028 namespace lfeat
00029 {
00030 // extremly fagile check...
00031 static bool identifySIFTKeypoints(const std::string& filename)
00032 {
00033     ImageInfo info;
00034     std::ifstream in(filename.c_str());
00035     if (!in)
00036     {
00037         return false;
00038     }
00039 
00040     int nKeypoints = 0;
00041     int dims = 0;
00042     in >> nKeypoints >> dims;
00043     return (in && nKeypoints > 0 && dims >= 0);
00044 }
00045 
00046 static ImageInfo loadSIFTKeypoints(const std::string& filename, KeyPointVect_t& vec)
00047 {
00048     ImageInfo info;
00049     std::ifstream in(filename.c_str());
00050     if (!in.good())
00051     {
00052         return info;
00053     }
00054 
00055     int nKeypoints = 0;
00056     int dims = 0;
00057     in >> nKeypoints >> dims;
00058 
00059     info.dimensions = dims;
00060 
00061     for (int i = 0; i < nKeypoints; i++)
00062     {
00063         KeyPointPtr k(new lfeat::KeyPoint(0, 0, 0, 0, 0));
00064         in >> k->_y >> k->_x >> k->_scale >> k->_ori >> k->_score;
00065         if (dims > 0)
00066         {
00067             k->allocVector(dims);
00068             for (int j = 0; j < dims; j++)
00069             {
00070                 in >> k->_vec[j];
00071             }
00072         }
00073         vec.push_back(k);
00074     }
00075     // finish reading empty line
00076     std::getline(in, info.filename);
00077     // read line with filename
00078     std::getline(in, info.filename);
00079     in >> info.width >> info.height;
00080     //std::cerr << "*** Loaded keypoints for image " << info.filename  << " ("<<info.width<<"x"<<info.height<<")"  << std::std::endl;
00081 
00082     return info;
00083 }
00084 
00085 ImageInfo loadKeypoints(const std::string& filename, KeyPointVect_t& vec)
00086 {
00087     if (identifySIFTKeypoints(filename))
00088     {
00089         return loadSIFTKeypoints(filename, vec);
00090     }
00091     else
00092     {
00093         ImageInfo r;
00094         return r;
00095     }
00096 }
00097 
00098 
00099 void SIFTFormatWriter::writeHeader(const ImageInfo& imageinfo, int nKeypoints, int dims)
00100 {
00101     _image = imageinfo;
00102     o << nKeypoints << std::endl;
00103     o << dims << std::endl;
00104 }
00105 
00106 
00107 void SIFTFormatWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
00108 {
00109     o << y << " " << x << " " << scale << " " << orientation << " " << score;
00110     for (int i = 0; i < dims; i++)
00111     {
00112         o << " " << vec[i];
00113     }
00114     o << std::endl;
00115 }
00116 
00117 void SIFTFormatWriter::writeFooter()
00118 {
00119     o << _image.filename << std::endl;
00120     o << _image.width << " " << _image.height << std::endl;
00121 }
00122 
00123 
00124 void DescPerfFormatWriter::writeHeader(const ImageInfo& imageinfo, int nKeypoints, int dims)
00125 {
00126     _image = imageinfo;
00127     o << dims << std::endl;
00128     o << nKeypoints << std::endl;
00129 }
00130 
00131 
00132 void DescPerfFormatWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
00133 {
00134     double sc = 2.5 * scale;
00135     sc *= sc;
00136     o << x << " " << y << " " << 1.0 / sc << " 0 " << 1.0 / sc;
00137     for (int i = 0; i < dims; i++)
00138     {
00139         o << " " << vec[i];
00140     }
00141     o << std::endl;
00142 }
00143 
00144 void DescPerfFormatWriter::writeFooter()
00145 {
00146 }
00147 
00148 
00149 void AutopanoSIFTWriter::writeHeader(const ImageInfo& imageinfo, int nKeypoints, int dims)
00150 {
00151     o << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
00152     o << "<KeypointXMLList xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
00153     o << "  <XDim>" << imageinfo.width << "</XDim>" << std::endl;
00154     o << "  <YDim>" << imageinfo.height << "</YDim>" << std::endl;
00155     o << "  <ImageFile>" << imageinfo.filename << "</ImageFile>" << std::endl;
00156     o << "  <Arr>" << std::endl;
00157 }
00158 
00159 void AutopanoSIFTWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
00160 {
00161     o << "    <KeypointN>" << std::endl;
00162     o << "      <X>" << x << "</X>" << std::endl;
00163     o << "      <Y>" << y << "</Y>" << std::endl;
00164     o << "      <Scale>" << scale << "</Scale>" << std::endl;
00165     o << "      <Orientation>" << orientation << "</Orientation>" << std::endl;
00166     if (dims > 0)
00167     {
00168         o << "      <Dim>" << dims << "</Dim>" << std::endl;
00169         o << "      <Descriptor>" << std::endl;
00170 
00171         for (int i = 0; i < dims; i++)
00172         {
00173             o << "        <int>" << (vec[i] * 256) << "</int>" << std::endl;
00174         }
00175         o << "      </Descriptor>" << std::endl;
00176     }
00177     o << "    </KeypointN>" << std::endl;
00178 }
00179 
00180 void AutopanoSIFTWriter::writeFooter()
00181 {
00182     o << "  </Arr>" << std::endl;
00183     o << "</KeypointXMLList>" << std::endl;
00184 }
00185 
00186 } // namespace lfeat

Generated on 10 Feb 2016 for Hugintrunk by  doxygen 1.4.7