forked from snavely/bundler_sfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyMatchFull.cpp
162 lines (124 loc) · 4.27 KB
/
KeyMatchFull.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
/*
* Copyright (c) 2008-2010 Noah Snavely (snavely (at) cs.cornell.edu)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/* KeyMatchFull.cpp */
/* Read in keys, match, write results to a file */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keys2a.h"
int ReadFileList(char* list_in, std::vector<std::string>& key_files) {
FILE* fp;
if ((fp = fopen(list_in, "r")) == NULL) {
printf("Error opening file %s for reading.\n", list_in);
return 1;
}
char buf[512], *start;
while (fgets(buf, 512, fp)) {
// Remove trailing new-line
if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = '\0';
// Find first non-space character
start = buf;
while(isspace(*start)) start++;
// Skip empty lines
if (strlen(start) == 0) continue;
// Append file-name to key_files
key_files.push_back(std::string(buf));
}
// Check we found input files
if (key_files.size() == 0) {
printf("No input files found in %s.\n", list_in);
return 1;
}
return 0;
}
int main(int argc, char **argv) {
char *list_in;
char *file_out;
double ratio;
if (argc != 3 && argc != 4) {
printf("Usage: %s <list.txt> <outfile> [window_radius]\n", argv[0]);
return EXIT_FAILURE;
}
list_in = argv[1];
ratio = 0.6;
file_out = argv[2];
int window_radius = -1;
if (argc == 4) {
window_radius = atoi(argv[3]);
}
clock_t start = clock();
/* Read the list of files */
std::vector<std::string> key_files;
if (ReadFileList(list_in, key_files) != 0) return EXIT_FAILURE;
FILE *f;
if ((f = fopen(file_out, "w")) == NULL) {
printf("Could not open %s for writing.\n", file_out);
return EXIT_FAILURE;
}
int num_images = (int) key_files.size();
std::vector<unsigned char*> keys(num_images);
std::vector<int> num_keys(num_images);
/* Read all keys */
for (int i = 0; i < num_images; i++) {
keys[i] = NULL;
num_keys[i] = ReadKeyFile(key_files[i].c_str(), &keys[i]);
}
clock_t end = clock();
printf("[KeyMatchFull] Reading keys took %0.3fs\n",
(end - start) / ((double) CLOCKS_PER_SEC));
for (int i = 0; i < num_images; i++) {
if (num_keys[i] == 0)
continue;
printf("[KeyMatchFull] Matching to image %d\n", i);
start = clock();
/* Create a tree from the keys */
ANNkd_tree *tree = CreateSearchTree(num_keys[i], keys[i]);
/* Compute the start index */
int start_idx = 0;
if (window_radius > 0)
start_idx = std::max(i - window_radius, 0);
for (int j = start_idx; j < i; j++) {
if (num_keys[j] == 0)
continue;
/* Compute likely matches between two sets of keypoints */
std::vector<KeypointMatch> matches =
MatchKeys(num_keys[j], keys[j], tree, ratio);
int num_matches = (int) matches.size();
if (num_matches >= 16) {
/* Write the pair */
fprintf(f, "%d %d\n", j, i);
/* Write the number of matches */
fprintf(f, "%d\n", (int) matches.size());
for (int i = 0; i < num_matches; i++) {
fprintf(f, "%d %d\n",
matches[i].m_idx1, matches[i].m_idx2);
}
}
}
end = clock();
printf("[KeyMatchFull] Matching took %0.3fs\n",
(end - start) / ((double) CLOCKS_PER_SEC));
fflush(stdout);
delete tree;
}
/* Free keypoints */
for (int i = 0; i < num_images; i++) {
if (keys[i] != NULL)
delete [] keys[i];
}
fclose(f);
return EXIT_SUCCESS;
}