Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/calibrate.cpp
16354 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) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "opencv2/photo.hpp"
45
#include "opencv2/imgproc.hpp"
46
#include "hdr_common.hpp"
47
48
namespace cv
49
{
50
51
class CalibrateDebevecImpl CV_FINAL : public CalibrateDebevec
52
{
53
public:
54
CalibrateDebevecImpl(int _samples, float _lambda, bool _random) :
55
name("CalibrateDebevec"),
56
samples(_samples),
57
lambda(_lambda),
58
random(_random),
59
w(triangleWeights())
60
{
61
}
62
63
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times) CV_OVERRIDE
64
{
65
CV_INSTRUMENT_REGION();
66
67
// check inputs
68
std::vector<Mat> images;
69
src.getMatVector(images);
70
Mat times = _times.getMat();
71
72
CV_Assert(images.size() == times.total());
73
checkImageDimensions(images);
74
CV_Assert(images[0].depth() == CV_8U);
75
CV_Assert(times.type() == CV_32FC1);
76
77
// create output
78
int channels = images[0].channels();
79
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
80
int rows = images[0].rows;
81
int cols = images[0].cols;
82
83
dst.create(LDR_SIZE, 1, CV_32FCC);
84
Mat result = dst.getMat();
85
86
// pick pixel locations (either random or in a rectangular grid)
87
std::vector<Point> points;
88
points.reserve(samples);
89
if(random) {
90
for(int i = 0; i < samples; i++) {
91
points.push_back(Point(rand() % cols, rand() % rows));
92
}
93
} else {
94
int x_points = static_cast<int>(sqrt(static_cast<double>(samples) * cols / rows));
95
CV_Assert(0 < x_points && x_points <= cols);
96
int y_points = samples / x_points;
97
CV_Assert(0 < y_points && y_points <= rows);
98
int step_x = cols / x_points;
99
int step_y = rows / y_points;
100
for(int i = 0, x = step_x / 2; i < x_points; i++, x += step_x) {
101
for(int j = 0, y = step_y / 2; j < y_points; j++, y += step_y) {
102
if( 0 <= x && x < cols && 0 <= y && y < rows ) {
103
points.push_back(Point(x, y));
104
}
105
}
106
}
107
// we can have slightly less grid points than specified
108
//samples = static_cast<int>(points.size());
109
}
110
111
// we need enough equations to ensure a sufficiently overdetermined system
112
// (maybe only as a warning)
113
//CV_Assert(points.size() * (images.size() - 1) >= LDR_SIZE);
114
115
// solve for imaging system response function, over each channel separately
116
std::vector<Mat> result_split(channels);
117
for(int ch = 0; ch < channels; ch++) {
118
// initialize system of linear equations
119
Mat A = Mat::zeros((int)points.size() * (int)images.size() + LDR_SIZE + 1,
120
LDR_SIZE + (int)points.size(), CV_32F);
121
Mat B = Mat::zeros(A.rows, 1, CV_32F);
122
123
// include the data-fitting equations
124
int k = 0;
125
for(size_t i = 0; i < points.size(); i++) {
126
for(size_t j = 0; j < images.size(); j++) {
127
// val = images[j].at<Vec3b>(points[i].y, points[i].x)[ch]
128
int val = images[j].ptr()[channels*(points[i].y * cols + points[i].x) + ch];
129
float wij = w.at<float>(val);
130
A.at<float>(k, val) = wij;
131
A.at<float>(k, LDR_SIZE + (int)i) = -wij;
132
B.at<float>(k, 0) = wij * log(times.at<float>((int)j));
133
k++;
134
}
135
}
136
137
// fix the curve by setting its middle value to 0
138
A.at<float>(k, LDR_SIZE / 2) = 1;
139
k++;
140
141
// include the smoothness equations
142
for(int i = 0; i < (LDR_SIZE - 2); i++) {
143
float wi = w.at<float>(i + 1);
144
A.at<float>(k, i) = lambda * wi;
145
A.at<float>(k, i + 1) = -2 * lambda * wi;
146
A.at<float>(k, i + 2) = lambda * wi;
147
k++;
148
}
149
150
// solve the overdetermined system using SVD (least-squares problem)
151
Mat solution;
152
solve(A, B, solution, DECOMP_SVD);
153
solution.rowRange(0, LDR_SIZE).copyTo(result_split[ch]);
154
}
155
156
// combine log-exposures and take its exponent
157
merge(result_split, result);
158
exp(result, result);
159
}
160
161
int getSamples() const CV_OVERRIDE { return samples; }
162
void setSamples(int val) CV_OVERRIDE { samples = val; }
163
164
float getLambda() const CV_OVERRIDE { return lambda; }
165
void setLambda(float val) CV_OVERRIDE { lambda = val; }
166
167
bool getRandom() const CV_OVERRIDE { return random; }
168
void setRandom(bool val) CV_OVERRIDE { random = val; }
169
170
void write(FileStorage& fs) const CV_OVERRIDE
171
{
172
writeFormat(fs);
173
fs << "name" << name
174
<< "samples" << samples
175
<< "lambda" << lambda
176
<< "random" << static_cast<int>(random);
177
}
178
179
void read(const FileNode& fn) CV_OVERRIDE
180
{
181
FileNode n = fn["name"];
182
CV_Assert(n.isString() && String(n) == name);
183
samples = fn["samples"];
184
lambda = fn["lambda"];
185
int random_val = fn["random"];
186
random = (random_val != 0);
187
}
188
189
protected:
190
String name; // calibration algorithm identifier
191
int samples; // number of pixel locations to sample
192
float lambda; // constant that determines the amount of smoothness
193
bool random; // whether to sample locations randomly or in a grid shape
194
Mat w; // weighting function for corresponding pixel values
195
};
196
197
Ptr<CalibrateDebevec> createCalibrateDebevec(int samples, float lambda, bool random)
198
{
199
return makePtr<CalibrateDebevecImpl>(samples, lambda, random);
200
}
201
202
class CalibrateRobertsonImpl CV_FINAL : public CalibrateRobertson
203
{
204
public:
205
CalibrateRobertsonImpl(int _max_iter, float _threshold) :
206
name("CalibrateRobertson"),
207
max_iter(_max_iter),
208
threshold(_threshold),
209
weight(RobertsonWeights())
210
{
211
}
212
213
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times) CV_OVERRIDE
214
{
215
CV_INSTRUMENT_REGION();
216
217
std::vector<Mat> images;
218
src.getMatVector(images);
219
Mat times = _times.getMat();
220
221
CV_Assert(images.size() == times.total());
222
checkImageDimensions(images);
223
CV_Assert(images[0].depth() == CV_8U);
224
225
int channels = images[0].channels();
226
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
227
CV_Assert(channels >= 1 && channels <= 3);
228
229
dst.create(LDR_SIZE, 1, CV_32FCC);
230
Mat response = dst.getMat();
231
response = linearResponse(3) / (LDR_SIZE / 2.0f);
232
233
Mat card = Mat::zeros(LDR_SIZE, 1, CV_32FCC);
234
for(size_t i = 0; i < images.size(); i++) {
235
uchar *ptr = images[i].ptr();
236
for(size_t pos = 0; pos < images[i].total(); pos++) {
237
for(int c = 0; c < channels; c++, ptr++) {
238
card.at<Vec3f>(*ptr)[c] += 1;
239
}
240
}
241
}
242
card = 1.0 / card;
243
244
Ptr<MergeRobertson> merge = createMergeRobertson();
245
for(int iter = 0; iter < max_iter; iter++) {
246
247
radiance = Mat::zeros(images[0].size(), CV_32FCC);
248
merge->process(images, radiance, times, response);
249
250
Mat new_response = Mat::zeros(LDR_SIZE, 1, CV_32FC3);
251
for(size_t i = 0; i < images.size(); i++) {
252
uchar *ptr = images[i].ptr();
253
float* rad_ptr = radiance.ptr<float>();
254
for(size_t pos = 0; pos < images[i].total(); pos++) {
255
for(int c = 0; c < channels; c++, ptr++, rad_ptr++) {
256
new_response.at<Vec3f>(*ptr)[c] += times.at<float>((int)i) * *rad_ptr;
257
}
258
}
259
}
260
new_response = new_response.mul(card);
261
for(int c = 0; c < 3; c++) {
262
float middle = new_response.at<Vec3f>(LDR_SIZE / 2)[c];
263
for(int i = 0; i < LDR_SIZE; i++) {
264
new_response.at<Vec3f>(i)[c] /= middle;
265
}
266
}
267
float diff = static_cast<float>(sum(sum(abs(new_response - response)))[0] / channels);
268
new_response.copyTo(response);
269
if(diff < threshold) {
270
break;
271
}
272
}
273
}
274
275
int getMaxIter() const CV_OVERRIDE { return max_iter; }
276
void setMaxIter(int val) CV_OVERRIDE { max_iter = val; }
277
278
float getThreshold() const CV_OVERRIDE { return threshold; }
279
void setThreshold(float val) CV_OVERRIDE { threshold = val; }
280
281
Mat getRadiance() const CV_OVERRIDE { return radiance; }
282
283
void write(FileStorage& fs) const CV_OVERRIDE
284
{
285
writeFormat(fs);
286
fs << "name" << name
287
<< "max_iter" << max_iter
288
<< "threshold" << threshold;
289
}
290
291
void read(const FileNode& fn) CV_OVERRIDE
292
{
293
FileNode n = fn["name"];
294
CV_Assert(n.isString() && String(n) == name);
295
max_iter = fn["max_iter"];
296
threshold = fn["threshold"];
297
}
298
299
protected:
300
String name;
301
int max_iter;
302
float threshold;
303
Mat weight, radiance;
304
};
305
306
Ptr<CalibrateRobertson> createCalibrateRobertson(int max_iter, float threshold)
307
{
308
return makePtr<CalibrateRobertsonImpl>(max_iter, threshold);
309
}
310
311
}
312
313