-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcolmap_import_features_batch.cpp
288 lines (216 loc) · 11 KB
/
colmap_import_features_batch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <colmap.h>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <boost/filesystem/operations.hpp>
using namespace colmap;
static std::vector<std::string> list_dir(const char* path)
{
std::vector<std::string> aRes;
struct dirent *entry;
DIR *dir = opendir(path);
if (dir==NULL)
{
return aRes;
}
while ((entry = readdir(dir)) != NULL)
{
aRes.push_back(entry->d_name);
}
return aRes;
}
int main(int argc, char** argv)
{
std::cout << "colmap_import_features_batch" << "\n";
colmap::InitializeGlog(argv);
std::string aImagePath;
std::vector<std::string> aImageList;
std::string aHomName;
double aFocal=0;
double aPPx=0, aPPy=0;
double aSzX=0, aSzY=0;
bool Do2ViewGeom=false;
bool OnlyCreateImsInDB = false;
std::string CamModel = "SIMPLE_RADIAL";
colmap::OptionManager options;
options.AddRequiredOption("homol", &aHomName);
options.AddRequiredOption("image_path", &aImagePath);
options.AddDefaultOption("focal", &aFocal);
options.AddDefaultOption("ppx", &aPPx);
options.AddDefaultOption("ppy", &aPPy);
options.AddDefaultOption("szx", &aSzX);
options.AddDefaultOption("szy", &aSzY);
options.AddDefaultOption("2ViewGeom", &Do2ViewGeom);
options.AddDefaultOption("OnlyCreateIms", &OnlyCreateImsInDB);
options.AddDefaultOption("CamModel", &CamModel);
options.AddDatabaseOptions();
options.Parse(argc, argv);
aImageList = GetFileList(aImagePath);
//read the database
Database database(*options.database_path);
if (OnlyCreateImsInDB)
{
/* Create a camera in the database */
Camera camera;
camera.InitializeWithName(CamModel, aFocal, aSzX, aSzY);
camera.SetPrincipalPointX(aPPx);
camera.SetPrincipalPointY(aPPy);
camera.SetFocalLength(aFocal);
camera.SetCameraId(database.WriteCamera(camera));
/* Create images in the database */
for (auto aIm1 : aImageList)
{
if ( (aIm1.find("png") != std::string::npos) || (aIm1.find("tif") != std::string::npos) || (aIm1.find("jpg") != std::string::npos))
{
aIm1 = aIm1.substr(2);
auto aIm1Base = boost::filesystem::basename(aIm1) + boost::filesystem::extension(aIm1);
Image aImCol;
if (! database.ExistsImageWithName(aIm1Base))
{
aImCol.SetName(aIm1Base);
aImCol.SetCameraId(camera.CameraId());
//aImCol.SetQvecPrior(Eigen::Vector4d(0.1, 0.2, 0.3, 0.4));
//aImCol.SetTvecPrior(Eigen::Vector3d(0.1, 0.2, 0.3));
aImCol.SetImageId(database.WriteImage(aImCol));
std::cout << aImCol.ImageId() << " " << aIm1Base << "\n";
}
}
}
}
if (!OnlyCreateImsInDB)
{
/* Keypoints / matches for the entire dataset */
std::map<std::string,FeatureKeypoints*> aFKPtsMap;
std::vector<FeatureMatches> aFMatchesVec;
std::vector<std::pair<int,int>> aFMatchesImId;
/* For each image in the list */
int aNb=0;
for (auto aIm1 : aImageList)
{
if ( (aIm1.find("png") != std::string::npos) || (aIm1.find("tif") != std::string::npos) || (aIm1.find("jpg") != std::string::npos))
{
std::cout << aIm1 << "\n";
aIm1 = aIm1.substr(2);
auto aIm1Base = boost::filesystem::basename(aIm1) + boost::filesystem::extension(aIm1);
//std::cout << boost::filesystem::basename(aIm1) << " ewelinaaa" << "\n";
std::string aPastisDir = aHomName + "/Pastis" + aIm1Base;
std::vector<std::string> aMatchedFiles = GetFileList(aPastisDir);
//initialise the map for Im1 if necesary
if (aFKPtsMap[aIm1Base] == NULL)
aFKPtsMap[aIm1Base] = new FeatureKeypoints;
//read existing featurepoints (whether empty or not)
FeatureKeypoints* aFKPts1 = aFKPtsMap[aIm1Base];
/* For a pair of images */
for (auto aMatchFile : aMatchedFiles)
{
std::size_t aPosIm2 = aMatchFile.find_last_of("/\\");
std::string aIm2 = aMatchFile.substr(aPosIm2+1);
aIm2 = aIm2.substr(0,aIm2.size()-4);//remove txt or dat
//if this match is part of image_path pattern
if (std::find(aImageList.begin(), aImageList.end(), aImagePath+aIm2) != aImageList.end())
{
std::cout << aIm1Base << " " << aIm2 << "\n";
//to be able to read the image ids of the current pair
Image aIm1Col = database.ReadImageWithName(aIm1Base);
Image aIm2Col = database.ReadImageWithName(aIm2);
//leave if symmetric features already exist
std::vector<std::pair<int,int>>::iterator aFMatchesImIdIter;
aFMatchesImIdIter = std::find(aFMatchesImId.begin(),aFMatchesImId.end(),std::pair<int,int>(aIm2Col.ImageId(),aIm1Col.ImageId()));
//if symmetric does not exist
if (aFMatchesImIdIter == aFMatchesImId.end())
{
//initialise the map for Im2 if necesary
if (aFKPtsMap[aIm2] == NULL)
aFKPtsMap[aIm2] = new FeatureKeypoints;
//read existing featurepoints (whether empty or not)
FeatureKeypoints* aFKPts2 = aFKPtsMap[aIm2];
//temp var to save matches
FeatureMatches aFMatches;
//single keypoints
FeatureKeypoint aKP1;
FeatureKeypoint aKP2;
double Pds;
//iterate the file lines
std::ifstream aFIn(aMatchFile.c_str(),std::ifstream::in);
while (aFIn.good())
{
aFIn >> aKP1.x >> aKP1.y >> aKP2.x >> aKP2.y >> Pds ;
//update the global features for the current pair
aFKPts1->push_back(aKP1);
aFKPts2->push_back(aKP2);
//save the global matches
aFMatches.push_back( FeatureMatch(aFKPts1->size()-1,aFKPts2->size()-1) );
//std::cout << aKP1.x << " " << aKP1.y << " " << aKP2.x << " " << aKP2.y << " " << aFKPts1->size()-1 << " " << aFKPts2->size()-1 << "\n";
}
//save the id's of the current pair
aFMatchesImId.push_back(std::pair<int,int>(aIm1Col.ImageId(),aIm2Col.ImageId()));
//save the matches for the current pair
aFMatchesVec.push_back(aFMatches);
aFIn.close();
/* Calculate two-view geometry */
if (Do2ViewGeom)
{
TwoViewGeometry two_view_geometry;
TwoViewGeometry::Options two_view_geometry_options;
camera_t camerat1 = aIm1Col.CameraId();
camera_t camerat2 = aIm2Col.CameraId();
Camera camera1 = database.ReadCamera(camerat1);
Camera camera2 = database.ReadCamera(camerat2);
std::cout << " 2ViewGeom=> " << camera1.CameraId() << " F=" << camera1.FocalLength() << " " << camera2.CameraId() << " F=" << camera2.FocalLength() << "\n";
//getchar();
two_view_geometry_options.min_num_inliers = 10;
two_view_geometry_options.ransac_options.max_error = 4;
two_view_geometry_options.ransac_options.confidence = 0.999;
two_view_geometry_options.ransac_options.max_num_trials = 10000;
two_view_geometry_options.ransac_options.min_inlier_ratio = 0.25;
two_view_geometry.config = TwoViewGeometry::CALIBRATED;
//two_view_geometry.inlier_matches = aFMatches;
//two_view_geometry.Estimate(
// camera1, FeatureKeypointsToPointsVector(*aFKPts1), camera2,
// FeatureKeypointsToPointsVector(*aFKPts2), aFMatches,
// two_view_geometry_options);
two_view_geometry.EstimateCalibrated(camera1,
FeatureKeypointsToPointsVector(*aFKPts1),
camera2,
FeatureKeypointsToPointsVector(*aFKPts2),
aFMatches,
two_view_geometry_options);
std::cout << "E " << two_view_geometry.E << ", inliers=" << two_view_geometry.inlier_matches.size() << "\n";
//if (two_view_geometry.inlier_matches.size() > 0)
{
database.WriteTwoViewGeometry(aIm1Col.ImageId(), aIm2Col.ImageId(),
two_view_geometry);
}
}
}
}
}
}
}
//clean database
/* Update the databse with keypoints */
for (auto aFKs : aFKPtsMap)
{
if (database.ExistsImageWithName(aFKs.first))
{
Image aImCol = database.ReadImageWithName(aFKs.first);
std::cout << "image exists " << aImCol.ImageId() << " saving keypoints \n";
database.WriteKeypoints(aImCol.ImageId(), *(aFKs.second));
}
else
{
std::cout << "Image " << aFKs.first << " does not exist in the database." << "\n";
}
}
/* Update the databse with matches */
std::cout << aFMatchesImId.size() << " " << aFMatchesVec.size() << "\n";
if (aFMatchesImId.size() == aFMatchesVec.size())
for (int aK=0; aK<int(aFMatchesVec.size()); aK++)
{
std::cout << "match " << aFMatchesImId.at(aK).first << " " << aFMatchesImId.at(aK).second << " saving matches \n";
database.WriteMatches(aFMatchesImId.at(aK).first,aFMatchesImId.at(aK).second,aFMatchesVec.at(aK));
}
}
database.Close();
return EXIT_SUCCESS;
}