-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.cpp
233 lines (214 loc) · 7.31 KB
/
main.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
#include "main.h"
#include "APD.h"
using namespace boost::filesystem;
void GenerateSampleList(const path &dense_folder, std::vector<Problem> &problems)
{
path cluster_list_path = dense_folder / path("pair.txt");
problems.clear();
ifstream file(cluster_list_path);
std::stringstream iss;
std::string line;
int num_images;
iss.clear();
std::getline(file, line);
iss.str(line);
iss >> num_images;
for (int i = 0; i < num_images; ++i) {
Problem problem;
problem.index = i;
problem.src_image_ids.clear();
iss.clear();
std::getline(file, line);
iss.str(line);
iss >> problem.ref_image_id;
problem.dense_folder = dense_folder;
problem.result_folder = dense_folder / path("APD") / path(ToFormatIndex(problem.ref_image_id));
create_directory(problem.result_folder);
int num_src_images;
iss.clear();
std::getline(file, line);
iss.str(line);
iss >> num_src_images;
for (int j = 0; j < num_src_images; ++j) {
int id;
float score;
iss >> id >> score;
if (score <= 0.0f) {
continue;
}
problem.src_image_ids.push_back(id);
}
problems.push_back(problem);
}
}
bool CheckImages(const std::vector<Problem> &problems) {
if (problems.size() == 0) {
return false;
}
path image_path = problems[0].dense_folder / path("images") / path(ToFormatIndex(problems[0].ref_image_id) + ".jpg");
cv::Mat image = cv::imread(image_path.string());
if (image.empty()) {
return false;
}
const int width = image.cols;
const int height = image.rows;
for (size_t i = 1; i < problems.size(); ++i) {
image_path = problems[i].dense_folder / path("images") / path(ToFormatIndex(problems[i].ref_image_id) + ".jpg");
image = cv::imread(image_path.string());
if (image.cols != width || image.rows != height) {
return false;
}
}
return true;
}
int ComputeRoundNum(const std::vector<Problem> &problems) {
if (problems.size() == 0) {
return 0;
}
path image_path = problems[0].dense_folder / path("images") / path(ToFormatIndex(problems[0].ref_image_id) + ".jpg");
cv::Mat image = cv::imread(image_path.string());
if (image.empty()) {
return 0;
}
int max_size = MAX(image.cols, image.rows);
int round_num = 1;
while (max_size > 1000) {
max_size /= 2;
round_num++;
}
return round_num;
}
void ProcessProblem(const Problem &problem) {
std::cout << "Processing image: " << std::setw(8) << std::setfill('0') << problem.ref_image_id << "..." << std::endl;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
APD APD(problem);
APD.InuputInitialization();
APD.CudaSpaceInitialization();
APD.SetDataPassHelperInCuda();
APD.RunPatchMatch();
int width = APD.GetWidth(), height = APD.GetHeight();
cv::Mat depth = cv::Mat(height, width, CV_32FC1);
cv::Mat normal = cv::Mat(height, width, CV_32FC3);
cv::Mat pixel_states = APD.GetPixelStates();
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
float4 plane_hypothesis = APD.GetPlaneHypothesis(r, c);
depth.at<float>(r, c) = plane_hypothesis.w;
if (depth.at<float>(r, c) < APD.GetDepthMin() || depth.at<float>(r, c) > APD.GetDepthMax()) {
depth.at<float>(r, c) = 0;
pixel_states.at<uchar>(r, c) = UNKNOWN;
}
normal.at<cv::Vec3f>(r, c) = cv::Vec3f(plane_hypothesis.x, plane_hypothesis.y, plane_hypothesis.z);
}
}
path depth_path = problem.result_folder / path("depths.dmb");
WriteBinMat(depth_path, depth);
path normal_path = problem.result_folder / path("normals.dmb");
WriteBinMat(normal_path, normal);
path weak_path = problem.result_folder / path("weak.bin");
WriteBinMat(weak_path, pixel_states);
path selected_view_path = problem.result_folder / path("selected_views.bin");
WriteBinMat(selected_view_path, APD.GetSelectedViews());
if (problem.show_medium_result) {
path depth_img_path = problem.result_folder / path("depth_" + std::to_string(problem.iteration) + ".jpg");
path normal_img_path = problem.result_folder / path("normal_" + std::to_string(problem.iteration) + ".jpg");
path weak_img_path = problem.result_folder / path("weak_" + std::to_string(problem.iteration) + ".jpg");
ShowDepthMap(depth_img_path, depth, APD.GetDepthMin(), APD.GetDepthMax());
ShowNormalMap(normal_img_path, normal);
ShowWeakImage(weak_img_path, pixel_states);
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Processing image: " << std::setw(8) << std::setfill('0') << problem.ref_image_id << " done!" << std::endl;
std::cout << "Cost time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "USAGE: APD dense_folder\n";
return EXIT_FAILURE;
}
path dense_folder(argv[1]);
path output_folder = dense_folder / path("APD");
create_directory(output_folder);
// set cuda device for multi-gpu machine
int gpu_index = 0;
if (argc == 3) {
gpu_index = std::atoi(argv[2]);
}
cudaSetDevice(gpu_index);
// generate problems
std::vector<Problem> problems;
GenerateSampleList(dense_folder, problems);
if (!CheckImages(problems)) {
std::cerr << "Images may error, check it!\n";
return EXIT_FAILURE;
}
int num_images = problems.size();
std::cout << "There are " << num_images << " problems needed to be processed!" << std::endl;
int round_num = ComputeRoundNum(problems);
std::cout << "Round nums: " << round_num << std::endl;
int iteration_index = 0;
for (int i = 0; i < round_num; ++i) {
for (auto &problem : problems) {
{
auto ¶ms = problem.params;
if (i == 0) {
params.state = FIRST_INIT;
params.use_APD = false;
}
else {
params.state = REFINE_INIT;
params.use_APD = true;
params.ransac_threshold = 0.01 - i * 0.00125;
params.rotate_time = MIN(static_cast<int>(std::pow(2, i)), 4);
}
params.geom_consistency = false;
params.max_iterations = 3;
params.weak_peak_radius = 6;
}
problem.iteration = iteration_index;
problem.show_medium_result = true;
problem.scale_size = static_cast<int>(std::pow(2, round_num - 1 - i)); // scale
ProcessProblem(problem);
}
iteration_index++;
for (int j = 0; j < 3; ++j) {
for (auto &problem : problems) {
{
auto ¶ms = problem.params;
params.state = REFINE_ITER;
if (i == 0) {
params.use_APD = false;
}
else {
params.use_APD = true;
params.ransac_threshold = 0.01 - i * 0.00125;
params.rotate_time = MIN(static_cast<int>(std::pow(2, i)), 4);
}
params.geom_consistency = true;
params.max_iterations = 3;
params.weak_peak_radius = MAX(4 - 2 * j, 2);
}
problem.iteration = iteration_index;
problem.show_medium_result = true;
problem.scale_size = static_cast<int>(std::pow(2, round_num - 1 - i)); // scale
ProcessProblem(problem);
}
iteration_index++;
}
std::cout << "Round: " << i << " done\n";
}
RunFusion(dense_folder, problems);
{// delete files
for (size_t i = 0; i < problems.size(); ++i) {
const auto &problem = problems[i];
remove(problem.result_folder / path("weak.bin"));
remove(problem.result_folder / path("depths.dmb"));
remove(problem.result_folder / path("normals.dmb"));
remove(problem.result_folder / path("selected_views.bin"));
//remove(problem.result_folder / path("neighbour.bin"));
//remove(problem.result_folder / path("neighbour_map.bin"));
}
}
std::cout << "All done\n";
return EXIT_SUCCESS;
}