Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/test/test_hdr.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
void loadImage(string path, Mat &img)
47
{
48
img = imread(path, -1);
49
ASSERT_FALSE(img.empty()) << "Could not load input image " << path;
50
}
51
52
void checkEqual(Mat img0, Mat img1, double threshold, const string& name)
53
{
54
double max = 1.0;
55
minMaxLoc(abs(img0 - img1), NULL, &max);
56
ASSERT_FALSE(max > threshold) << "max=" << max << " threshold=" << threshold << " method=" << name;
57
}
58
59
static vector<float> DEFAULT_VECTOR;
60
void loadExposureSeq(String path, vector<Mat>& images, vector<float>& times = DEFAULT_VECTOR)
61
{
62
std::ifstream list_file((path + "list.txt").c_str());
63
ASSERT_TRUE(list_file.is_open());
64
string name;
65
float val;
66
while(list_file >> name >> val) {
67
Mat img = imread(path + name);
68
ASSERT_FALSE(img.empty()) << "Could not load input image " << path + name;
69
images.push_back(img);
70
times.push_back(1 / val);
71
}
72
list_file.close();
73
}
74
75
void loadResponseCSV(String path, Mat& response)
76
{
77
response = Mat(256, 1, CV_32FC3);
78
std::ifstream resp_file(path.c_str());
79
for(int i = 0; i < 256; i++) {
80
for(int c = 0; c < 3; c++) {
81
resp_file >> response.at<Vec3f>(i)[c];
82
resp_file.ignore(1);
83
}
84
}
85
resp_file.close();
86
}
87
88
TEST(Photo_Tonemap, regression)
89
{
90
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/tonemap/";
91
92
Mat img, expected, result;
93
loadImage(test_path + "image.hdr", img);
94
float gamma = 2.2f;
95
96
Ptr<Tonemap> linear = createTonemap(gamma);
97
linear->process(img, result);
98
loadImage(test_path + "linear.png", expected);
99
result.convertTo(result, CV_8UC3, 255);
100
checkEqual(result, expected, 3, "Simple");
101
102
Ptr<TonemapDrago> drago = createTonemapDrago(gamma);
103
drago->process(img, result);
104
loadImage(test_path + "drago.png", expected);
105
result.convertTo(result, CV_8UC3, 255);
106
checkEqual(result, expected, 3, "Drago");
107
108
Ptr<TonemapDurand> durand = createTonemapDurand(gamma);
109
durand->process(img, result);
110
loadImage(test_path + "durand.png", expected);
111
result.convertTo(result, CV_8UC3, 255);
112
checkEqual(result, expected, 3, "Durand");
113
114
Ptr<TonemapReinhard> reinhard = createTonemapReinhard(gamma);
115
reinhard->process(img, result);
116
loadImage(test_path + "reinhard.png", expected);
117
result.convertTo(result, CV_8UC3, 255);
118
checkEqual(result, expected, 3, "Reinhard");
119
120
Ptr<TonemapMantiuk> mantiuk = createTonemapMantiuk(gamma);
121
mantiuk->process(img, result);
122
loadImage(test_path + "mantiuk.png", expected);
123
result.convertTo(result, CV_8UC3, 255);
124
checkEqual(result, expected, 3, "Mantiuk");
125
}
126
127
TEST(Photo_AlignMTB, regression)
128
{
129
const int TESTS_COUNT = 100;
130
string folder = string(cvtest::TS::ptr()->get_data_path()) + "shared/";
131
132
string file_name = folder + "lena.png";
133
Mat img;
134
loadImage(file_name, img);
135
cvtColor(img, img, COLOR_RGB2GRAY);
136
137
int max_bits = 5;
138
int max_shift = 32;
139
srand(static_cast<unsigned>(time(0)));
140
int errors = 0;
141
142
Ptr<AlignMTB> align = createAlignMTB(max_bits);
143
RNG rng = theRNG();
144
145
for(int i = 0; i < TESTS_COUNT; i++) {
146
Point shift(rng.uniform(0, max_shift), rng.uniform(0, max_shift));
147
Mat res;
148
align->shiftMat(img, res, shift);
149
Point calc = align->calculateShift(img, res);
150
errors += (calc != -shift);
151
}
152
ASSERT_TRUE(errors < 5) << errors << " errors";
153
}
154
155
TEST(Photo_MergeMertens, regression)
156
{
157
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
158
159
vector<Mat> images;
160
loadExposureSeq((test_path + "exposures/").c_str() , images);
161
162
Ptr<MergeMertens> merge = createMergeMertens();
163
164
Mat result, expected;
165
loadImage(test_path + "merge/mertens.png", expected);
166
merge->process(images, result);
167
result.convertTo(result, CV_8UC3, 255);
168
checkEqual(expected, result, 3, "Mertens");
169
170
Mat uniform(100, 100, CV_8UC3);
171
uniform = Scalar(0, 255, 0);
172
173
images.clear();
174
images.push_back(uniform);
175
176
merge->process(images, result);
177
result.convertTo(result, CV_8UC3, 255);
178
checkEqual(uniform, result, 1e-2f, "Mertens");
179
}
180
181
TEST(Photo_MergeDebevec, regression)
182
{
183
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
184
185
vector<Mat> images;
186
vector<float> times;
187
Mat response;
188
loadExposureSeq(test_path + "exposures/", images, times);
189
loadResponseCSV(test_path + "exposures/response.csv", response);
190
191
Ptr<MergeDebevec> merge = createMergeDebevec();
192
193
Mat result, expected;
194
loadImage(test_path + "merge/debevec.hdr", expected);
195
merge->process(images, result, times, response);
196
197
Ptr<Tonemap> map = createTonemap();
198
map->process(result, result);
199
map->process(expected, expected);
200
201
checkEqual(expected, result, 1e-2f, "Debevec");
202
}
203
204
TEST(Photo_MergeRobertson, regression)
205
{
206
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
207
208
vector<Mat> images;
209
vector<float> times;
210
loadExposureSeq(test_path + "exposures/", images, times);
211
Ptr<MergeRobertson> merge = createMergeRobertson();
212
Mat result, expected;
213
loadImage(test_path + "merge/robertson.hdr", expected);
214
merge->process(images, result, times);
215
216
const float eps = 6.f;
217
checkEqual(expected, result, eps, "MergeRobertson");
218
}
219
220
TEST(Photo_CalibrateDebevec, regression)
221
{
222
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
223
224
vector<Mat> images;
225
vector<float> times;
226
Mat response, expected;
227
loadExposureSeq(test_path + "exposures/", images, times);
228
loadResponseCSV(test_path + "calibrate/debevec.csv", expected);
229
Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
230
231
calibrate->process(images, response, times);
232
Mat diff = abs(response - expected);
233
diff = diff.mul(1.0f / response);
234
double max;
235
minMaxLoc(diff, NULL, &max);
236
ASSERT_FALSE(max > 0.1);
237
}
238
239
TEST(Photo_CalibrateRobertson, regression)
240
{
241
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
242
243
vector<Mat> images;
244
vector<float> times;
245
Mat response, expected;
246
loadExposureSeq(test_path + "exposures/", images, times);
247
loadResponseCSV(test_path + "calibrate/robertson.csv", expected);
248
249
Ptr<CalibrateRobertson> calibrate = createCalibrateRobertson();
250
calibrate->process(images, response, times);
251
checkEqual(expected, response, 1e-1f, "CalibrateRobertson");
252
}
253
254
}} // namespace
255
256