Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/merge.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) 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 "precomp.hpp"
43
#include "opencv2/photo.hpp"
44
#include "opencv2/imgproc.hpp"
45
#include "hdr_common.hpp"
46
47
namespace cv
48
{
49
50
class MergeDebevecImpl CV_FINAL : public MergeDebevec
51
{
52
public:
53
MergeDebevecImpl() :
54
name("MergeDebevec"),
55
weights(triangleWeights())
56
{
57
}
58
59
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
60
{
61
CV_INSTRUMENT_REGION();
62
63
std::vector<Mat> images;
64
src.getMatVector(images);
65
Mat times = _times.getMat();
66
67
CV_Assert(images.size() == times.total());
68
checkImageDimensions(images);
69
CV_Assert(images[0].depth() == CV_8U);
70
71
int channels = images[0].channels();
72
Size size = images[0].size();
73
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
74
75
dst.create(images[0].size(), CV_32FCC);
76
Mat result = dst.getMat();
77
78
Mat response = input_response.getMat();
79
80
if(response.empty()) {
81
response = linearResponse(channels);
82
response.at<Vec3f>(0) = response.at<Vec3f>(1);
83
}
84
85
Mat log_response;
86
log(response, log_response);
87
CV_Assert(log_response.rows == LDR_SIZE && log_response.cols == 1 &&
88
log_response.channels() == channels);
89
90
Mat exp_values(times.clone());
91
log(exp_values, exp_values);
92
93
result = Mat::zeros(size, CV_32FCC);
94
std::vector<Mat> result_split;
95
split(result, result_split);
96
Mat weight_sum = Mat::zeros(size, CV_32F);
97
98
for(size_t i = 0; i < images.size(); i++) {
99
std::vector<Mat> splitted;
100
split(images[i], splitted);
101
102
Mat w = Mat::zeros(size, CV_32F);
103
for(int c = 0; c < channels; c++) {
104
LUT(splitted[c], weights, splitted[c]);
105
w += splitted[c];
106
}
107
w /= channels;
108
109
Mat response_img;
110
LUT(images[i], log_response, response_img);
111
split(response_img, splitted);
112
for(int c = 0; c < channels; c++) {
113
result_split[c] += w.mul(splitted[c] - exp_values.at<float>((int)i));
114
}
115
weight_sum += w;
116
}
117
weight_sum = 1.0f / weight_sum;
118
for(int c = 0; c < channels; c++) {
119
result_split[c] = result_split[c].mul(weight_sum);
120
}
121
merge(result_split, result);
122
exp(result, result);
123
}
124
125
void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
126
{
127
CV_INSTRUMENT_REGION();
128
129
process(src, dst, times, Mat());
130
}
131
132
protected:
133
String name;
134
Mat weights;
135
};
136
137
Ptr<MergeDebevec> createMergeDebevec()
138
{
139
return makePtr<MergeDebevecImpl>();
140
}
141
142
class MergeMertensImpl CV_FINAL : public MergeMertens
143
{
144
public:
145
MergeMertensImpl(float _wcon, float _wsat, float _wexp) :
146
name("MergeMertens"),
147
wcon(_wcon),
148
wsat(_wsat),
149
wexp(_wexp)
150
{
151
}
152
153
void process(InputArrayOfArrays src, OutputArrayOfArrays dst, InputArray, InputArray) CV_OVERRIDE
154
{
155
CV_INSTRUMENT_REGION();
156
157
process(src, dst);
158
}
159
160
void process(InputArrayOfArrays src, OutputArray dst) CV_OVERRIDE
161
{
162
CV_INSTRUMENT_REGION();
163
164
std::vector<Mat> images;
165
src.getMatVector(images);
166
checkImageDimensions(images);
167
168
int channels = images[0].channels();
169
CV_Assert(channels == 1 || channels == 3);
170
Size size = images[0].size();
171
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
172
173
std::vector<Mat> weights(images.size());
174
Mat weight_sum = Mat::zeros(size, CV_32F);
175
176
for(size_t i = 0; i < images.size(); i++) {
177
Mat img, gray, contrast, saturation, wellexp;
178
std::vector<Mat> splitted(channels);
179
180
images[i].convertTo(img, CV_32F, 1.0f/255.0f);
181
if(channels == 3) {
182
cvtColor(img, gray, COLOR_RGB2GRAY);
183
} else {
184
img.copyTo(gray);
185
}
186
split(img, splitted);
187
188
Laplacian(gray, contrast, CV_32F);
189
contrast = abs(contrast);
190
191
Mat mean = Mat::zeros(size, CV_32F);
192
for(int c = 0; c < channels; c++) {
193
mean += splitted[c];
194
}
195
mean /= channels;
196
197
saturation = Mat::zeros(size, CV_32F);
198
for(int c = 0; c < channels; c++) {
199
Mat deviation = splitted[c] - mean;
200
pow(deviation, 2.0f, deviation);
201
saturation += deviation;
202
}
203
sqrt(saturation, saturation);
204
205
wellexp = Mat::ones(size, CV_32F);
206
for(int c = 0; c < channels; c++) {
207
Mat expo = splitted[c] - 0.5f;
208
pow(expo, 2.0f, expo);
209
expo = -expo / 0.08f;
210
exp(expo, expo);
211
wellexp = wellexp.mul(expo);
212
}
213
214
pow(contrast, wcon, contrast);
215
pow(saturation, wsat, saturation);
216
pow(wellexp, wexp, wellexp);
217
218
weights[i] = contrast;
219
if(channels == 3) {
220
weights[i] = weights[i].mul(saturation);
221
}
222
weights[i] = weights[i].mul(wellexp) + 1e-12f;
223
weight_sum += weights[i];
224
}
225
int maxlevel = static_cast<int>(logf(static_cast<float>(min(size.width, size.height))) / logf(2.0f));
226
std::vector<Mat> res_pyr(maxlevel + 1);
227
228
for(size_t i = 0; i < images.size(); i++) {
229
weights[i] /= weight_sum;
230
Mat img;
231
images[i].convertTo(img, CV_32F, 1.0f/255.0f);
232
233
std::vector<Mat> img_pyr, weight_pyr;
234
buildPyramid(img, img_pyr, maxlevel);
235
buildPyramid(weights[i], weight_pyr, maxlevel);
236
237
for(int lvl = 0; lvl < maxlevel; lvl++) {
238
Mat up;
239
pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size());
240
img_pyr[lvl] -= up;
241
}
242
for(int lvl = 0; lvl <= maxlevel; lvl++) {
243
std::vector<Mat> splitted(channels);
244
split(img_pyr[lvl], splitted);
245
for(int c = 0; c < channels; c++) {
246
splitted[c] = splitted[c].mul(weight_pyr[lvl]);
247
}
248
merge(splitted, img_pyr[lvl]);
249
if(res_pyr[lvl].empty()) {
250
res_pyr[lvl] = img_pyr[lvl];
251
} else {
252
res_pyr[lvl] += img_pyr[lvl];
253
}
254
}
255
}
256
for(int lvl = maxlevel; lvl > 0; lvl--) {
257
Mat up;
258
pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size());
259
res_pyr[lvl - 1] += up;
260
}
261
dst.create(size, CV_32FCC);
262
res_pyr[0].copyTo(dst);
263
}
264
265
float getContrastWeight() const CV_OVERRIDE { return wcon; }
266
void setContrastWeight(float val) CV_OVERRIDE { wcon = val; }
267
268
float getSaturationWeight() const CV_OVERRIDE { return wsat; }
269
void setSaturationWeight(float val) CV_OVERRIDE { wsat = val; }
270
271
float getExposureWeight() const CV_OVERRIDE { return wexp; }
272
void setExposureWeight(float val) CV_OVERRIDE { wexp = val; }
273
274
void write(FileStorage& fs) const CV_OVERRIDE
275
{
276
writeFormat(fs);
277
fs << "name" << name
278
<< "contrast_weight" << wcon
279
<< "saturation_weight" << wsat
280
<< "exposure_weight" << wexp;
281
}
282
283
void read(const FileNode& fn) CV_OVERRIDE
284
{
285
FileNode n = fn["name"];
286
CV_Assert(n.isString() && String(n) == name);
287
wcon = fn["contrast_weight"];
288
wsat = fn["saturation_weight"];
289
wexp = fn["exposure_weight"];
290
}
291
292
protected:
293
String name;
294
float wcon, wsat, wexp;
295
};
296
297
Ptr<MergeMertens> createMergeMertens(float wcon, float wsat, float wexp)
298
{
299
return makePtr<MergeMertensImpl>(wcon, wsat, wexp);
300
}
301
302
class MergeRobertsonImpl CV_FINAL : public MergeRobertson
303
{
304
public:
305
MergeRobertsonImpl() :
306
name("MergeRobertson"),
307
weight(RobertsonWeights())
308
{
309
}
310
311
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
312
{
313
CV_INSTRUMENT_REGION();
314
315
std::vector<Mat> images;
316
src.getMatVector(images);
317
Mat times = _times.getMat();
318
319
CV_Assert(images.size() == times.total());
320
checkImageDimensions(images);
321
CV_Assert(images[0].depth() == CV_8U);
322
323
int channels = images[0].channels();
324
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
325
326
dst.create(images[0].size(), CV_32FCC);
327
Mat result = dst.getMat();
328
329
Mat response = input_response.getMat();
330
if(response.empty()) {
331
float middle = LDR_SIZE / 2.0f;
332
response = linearResponse(channels) / middle;
333
}
334
CV_Assert(response.rows == LDR_SIZE && response.cols == 1 &&
335
response.channels() == channels);
336
337
result = Mat::zeros(images[0].size(), CV_32FCC);
338
Mat wsum = Mat::zeros(images[0].size(), CV_32FCC);
339
for(size_t i = 0; i < images.size(); i++) {
340
Mat im, w;
341
LUT(images[i], weight, w);
342
LUT(images[i], response, im);
343
344
result += times.at<float>((int)i) * w.mul(im);
345
wsum += times.at<float>((int)i) * times.at<float>((int)i) * w;
346
}
347
result = result.mul(1 / wsum);
348
}
349
350
void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
351
{
352
CV_INSTRUMENT_REGION();
353
354
process(src, dst, times, Mat());
355
}
356
357
protected:
358
String name;
359
Mat weight;
360
};
361
362
Ptr<MergeRobertson> createMergeRobertson()
363
{
364
return makePtr<MergeRobertsonImpl>();
365
}
366
367
}
368
369