-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (183 loc) · 5.96 KB
/
main.cpp
File metadata and controls
195 lines (183 loc) · 5.96 KB
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
/*
* main.cc
*
* Created on: 24/mar/2015
* Author: nicola
*/
#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <tuple>
#include <algorithm>
#include <utility>
#include "Image.hpp"
#include "Utils.hpp"
#include "DA3D.hpp"
using std::cerr;
using std::endl;
using std::min;
using std::max;
using std::copy;
using std::ostream_iterator;
using std::istream_iterator;
using std::vector;
using std::string;
using std::ofstream;
using std::ifstream;
using std::istringstream;
using std::ostringstream;
using std::getline;
using std::move;
using std::pair;
using std::tie;
using utils::pick_option;
using utils::read_image;
using utils::save_image;
using da3d::Image;
using da3d::DA3D;
double l2(const Image &im1, const Image &im2) {
double ans = 0.;
for (auto it1 = im1.begin(), it2 = im2.begin(); it1 != im1.end(); ++it1, ++it2) {
float v1 = min(max(*it1, 0.f), 255.f);
float v2 = min(max(*it2, 0.f), 255.f);
ans += (v1 - v2) * (v1 - v2);
}
return ans;
}
void save_lut(string filename, vector<float> h, vector<float> l) {
ostringstream lut_h, lut_l;
copy(h.begin(), h.end(), ostream_iterator<float>(lut_h, " "));
copy(l.begin(), l.end(), ostream_iterator<float>(lut_l, " "));
ofstream file(filename);
file << lut_h.str() << endl << lut_l.str() << endl;
file.close();
}
pair<vector<float>, vector<float>> load_lut(string filename) {
string s_h, s_l;
ifstream file(filename);
getline(file, s_h);
getline(file, s_l);
file.close();
istringstream lut_h(s_h), lut_l(s_l);
vector<float> h(istream_iterator<float>(lut_h), {});
vector<float> l(istream_iterator<float>(lut_l), {});
return {move(h), move(l)};
}
int main(int argc, char **argv) {
bool usage = static_cast<bool>(pick_option(&argc, argv, "h", nullptr));
bool only_psnr = static_cast<bool>(pick_option(&argc, argv, "m", nullptr));
const char* lut = pick_option(&argc, argv, "lut", "");
bool optimize = static_cast<bool>(pick_option(&argc, argv, "o", nullptr));
if (usage || argc < 3 || (!optimize && argc < 4) || (optimize && !strlen(lut))) {
cerr << "usage: " << argv[0] << " noisy guide sigma output [-lut file]" <<
endl;
cerr << " " << argv[0] <<
" algorithm sigma files -o -lut file [-m]" << endl;
return EXIT_FAILURE;
}
#ifndef _OPENMP
cerr << "Warning: OpenMP not available. The algorithm will run in a single" <<
" thread." << endl;
#endif
vector<float> K_high, K_low;
if (strlen(lut)) tie(K_high, K_low) = load_lut(lut);
if (optimize) {
float sigma = static_cast<float>(atof(argv[2]));
vector<Image> input;
vector<Image> guide;
vector<Image> reference;
for (int i = 3; i < argc; ++i) {
input.push_back(read_image(
string("noisy/") + argv[2] + "/" + argv[i] + ".tiff"));
guide.push_back(read_image(
string(argv[1]) + "/" + argv[2] + "/" + argv[i] + ".tiff"));
reference.push_back(read_image(string("originals/") + argv[i] + ".png"));
}
if (only_psnr) {
double m = 0.;
for (unsigned k = 0; k < input.size(); ++k)
m += l2(reference[k], DA3D(input[k], guide[k], sigma, K_high, K_low));
cerr << "Error " << m << endl;
} else {
for (int j = 0; j < 2; ++j) {
for (float &v : K_high) {
float a = 0.f, b = 1.f, c;
v = a;
double msea = 0.;
for (unsigned k = 0; k < input.size(); ++k)
msea += l2(reference[k], DA3D(input[k], guide[k], sigma, K_high,
K_low));
v = b;
double mseb = 0.;
for (unsigned k = 0; k < input.size(); ++k)
mseb += l2(reference[k], DA3D(input[k], guide[k], sigma, K_high,
K_low));
double msec;
for (int i = 0; i < 5 + j; ++i) {
c = 0.5f * (a + b);
v = c;
msec = 0.;
for (unsigned k = 0; k < input.size(); ++k)
msec += l2(reference[k], DA3D(input[k], guide[k], sigma, K_high,
K_low));
if (msea < mseb) {
b = c;
mseb = msec;
} else {
a = c;
msea = msec;
}
}
if (msea < msec) v = a;
if (mseb <= msec) v = b;
cerr << "High: ";
copy(K_high.begin(), K_high.end(), ostream_iterator<float>(cerr, " "));
cerr << " Error " << min({msea, mseb, msec}) << endl;
}
save_lut(lut, K_high, K_low);
for (float &v : K_low) {
float a = 0.f, b = 1.f, c;
v = a;
double msea = 0.;
for (unsigned k = 0; k < input.size(); ++k)
msea +=
l2(reference[k], DA3D(input[k], guide[k], sigma, K_high, K_low));
v = b;
double mseb = 0.;
for (unsigned k = 0; k < input.size(); ++k)
mseb +=
l2(reference[k], DA3D(input[k], guide[k], sigma, K_high, K_low));
double msec;
for (int i = 0; i < 10; ++i) {
c = 0.5f * (a + b);
v = c;
msec = 0.;
for (unsigned k = 0; k < input.size(); ++k)
msec += l2(reference[k],
DA3D(input[k], guide[k], sigma, K_high, K_low));
if (msea < mseb) {
b = c;
mseb = msec;
} else {
a = c;
msea = msec;
}
}
if (msea < msec) v = a;
if (mseb <= msec) v = b;
cerr << "Low: ";
copy(K_low.begin(), K_low.end(), ostream_iterator<float>(cerr, " "));
cerr << " Error " << min({msea, mseb, msec}) << endl;
}
save_lut(lut, K_high, K_low);
}
}
} else {
Image input = read_image(argv[1]);
Image guide = read_image(argv[2]);
float sigma = atof(argv[3]);
Image output = DA3D(input, guide, sigma, K_high, K_low, strlen(lut));
save_image(output, argc > 4 ? argv[4] : "-");
}
}