Hugin trunk 0.1
Loading...
Searching...
No Matches
KeyPointIO.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Pablo d'Angelo
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23#include <fstream>
24#include <string>
25
26#include "KeyPointIO.h"
27
28namespace lfeat
29{
30// extremly fagile check...
31static bool identifySIFTKeypoints(const std::string& filename)
32{
33 ImageInfo info;
34 std::ifstream in(filename.c_str());
35 if (!in)
36 {
37 return false;
38 }
39
40 int nKeypoints = 0;
41 int dims = 0;
42 in >> nKeypoints >> dims;
43 return (in && nKeypoints > 0 && dims >= 0);
44}
45
46static ImageInfo loadSIFTKeypoints(const std::string& filename, KeyPointVect_t& vec)
47{
48 ImageInfo info;
49 std::ifstream in(filename.c_str());
50 if (!in.good())
51 {
52 return info;
53 }
54
55 int nKeypoints = 0;
56 int dims = 0;
57 in >> nKeypoints >> dims;
58
59 info.dimensions = dims;
60
61 for (int i = 0; i < nKeypoints; i++)
62 {
63 KeyPointPtr k(new lfeat::KeyPoint(0, 0, 0, 0, 0));
64 in >> k->_y >> k->_x >> k->_scale >> k->_ori >> k->_score;
65 if (dims > 0)
66 {
67 k->allocVector(dims);
68 for (int j = 0; j < dims; j++)
69 {
70 in >> k->_vec[j];
71 }
72 }
73 vec.push_back(k);
74 }
75 // finish reading empty line
76 std::getline(in, info.filename);
77 // read line with filename
78 std::getline(in, info.filename);
79 in >> info.width >> info.height;
80 //std::cerr << "*** Loaded keypoints for image " << info.filename << " ("<<info.width<<"x"<<info.height<<")" << std::std::endl;
81
82 return info;
83}
84
85ImageInfo loadKeypoints(const std::string& filename, KeyPointVect_t& vec)
86{
87 if (identifySIFTKeypoints(filename))
88 {
89 return loadSIFTKeypoints(filename, vec);
90 }
91 else
92 {
93 ImageInfo r;
94 return r;
95 }
96}
97
98
100{
102 o << nKeypoints << std::endl;
103 o << dims << std::endl;
104}
105
106
107void SIFTFormatWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
108{
109 o << y << " " << x << " " << scale << " " << orientation << " " << score;
110 for (int i = 0; i < dims; i++)
111 {
112 o << " " << vec[i];
113 }
114 o << std::endl;
115}
116
118{
119 o << _image.filename << std::endl;
120 o << _image.width << " " << _image.height << std::endl;
121}
122
123
125{
127 o << dims << std::endl;
128 o << nKeypoints << std::endl;
129}
130
131
132void DescPerfFormatWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
133{
134 double sc = 2.5 * scale;
135 sc *= sc;
136 o << x << " " << y << " " << 1.0 / sc << " 0 " << 1.0 / sc;
137 for (int i = 0; i < dims; i++)
138 {
139 o << " " << vec[i];
140 }
141 o << std::endl;
142}
143
147
148
150{
151 o << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
152 o << "<KeypointXMLList xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
153 o << " <XDim>" << imageinfo.width << "</XDim>" << std::endl;
154 o << " <YDim>" << imageinfo.height << "</YDim>" << std::endl;
155 o << " <ImageFile>" << imageinfo.filename << "</ImageFile>" << std::endl;
156 o << " <Arr>" << std::endl;
157}
158
159void AutopanoSIFTWriter::writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double* vec)
160{
161 o << " <KeypointN>" << std::endl;
162 o << " <X>" << x << "</X>" << std::endl;
163 o << " <Y>" << y << "</Y>" << std::endl;
164 o << " <Scale>" << scale << "</Scale>" << std::endl;
165 o << " <Orientation>" << orientation << "</Orientation>" << std::endl;
166 if (dims > 0)
167 {
168 o << " <Dim>" << dims << "</Dim>" << std::endl;
169 o << " <Descriptor>" << std::endl;
170
171 for (int i = 0; i < dims; i++)
172 {
173 o << " <int>" << (vec[i] * 256) << "</int>" << std::endl;
174 }
175 o << " </Descriptor>" << std::endl;
176 }
177 o << " </KeypointN>" << std::endl;
178}
179
181{
182 o << " </Arr>" << std::endl;
183 o << "</KeypointXMLList>" << std::endl;
184}
185
186} // namespace lfeat
void writeHeader(const ImageInfo &imageinfo, int nKeypoints, int dims)
void writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double *vec)
void writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double *vec)
void writeHeader(const ImageInfo &imageinfo, int nKeypoints, int dims)
std::ostream & o
Definition KeyPointIO.h:63
void writeHeader(const ImageInfo &imageinfo, int nKeypoints, int dims)
void writeKeypoint(double x, double y, double scale, double orientation, double score, int dims, double *vec)
ImageInfo loadKeypoints(const std::string &filename, KeyPointVect_t &vec)
std::shared_ptr< KeyPoint > KeyPointPtr
Definition KeyPoint.h:110
static bool identifySIFTKeypoints(const std::string &filename)
static ImageInfo loadSIFTKeypoints(const std::string &filename, KeyPointVect_t &vec)
std::vector< KeyPointPtr > KeyPointVect_t
Definition KeyPoint.h:111
std::string filename
Definition KeyPointIO.h:44
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41