1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
43 #include "opencv2/photo.hpp"
44 #include "opencv2/imgproc.hpp"
45 #include "hdr_common.hpp"
50 class MergeDebevecImpl CV_FINAL : public MergeDebevec
55 weights(triangleWeights())
59 void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
61 CV_INSTRUMENT_REGION();
63 std::vector<Mat> images;
64 src.getMatVector(images);
65 Mat times = _times.getMat();
67 CV_Assert(images.size() == times.total());
68 checkImageDimensions(images);
69 CV_Assert(images[0].depth() == CV_8U);
71 int channels = images[0].channels();
72 Size size = images[0].size();
73 int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
75 dst.create(images[0].size(), CV_32FCC);
76 Mat result = dst.getMat();
78 Mat response = input_response.getMat();
80 if(response.empty()) {
81 response = linearResponse(channels);
82 response.at<Vec3f>(0) = response.at<Vec3f>(1);
86 log(response, log_response);
87 CV_Assert(log_response.rows == LDR_SIZE && log_response.cols == 1 &&
88 log_response.channels() == channels);
90 Mat exp_values(times.clone());
91 log(exp_values, exp_values);
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);
98 for(size_t i = 0; i < images.size(); i++) {
99 std::vector<Mat> splitted;
100 split(images[i], splitted);
102 Mat w = Mat::zeros(size, CV_32F);
103 for(int c = 0; c < channels; c++) {
104 LUT(splitted[c], weights, splitted[c]);
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));
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);
121 merge(result_split, result);
125 void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
127 CV_INSTRUMENT_REGION();
129 process(src, dst, times, Mat());
137 Ptr<MergeDebevec> createMergeDebevec()
139 return makePtr<MergeDebevecImpl>();
142 class MergeMertensImpl CV_FINAL : public MergeMertens
145 MergeMertensImpl(float _wcon, float _wsat, float _wexp) :
146 name("MergeMertens"),
153 void process(InputArrayOfArrays src, OutputArrayOfArrays dst, InputArray, InputArray) CV_OVERRIDE
155 CV_INSTRUMENT_REGION();
160 void process(InputArrayOfArrays src, OutputArray dst) CV_OVERRIDE
162 CV_INSTRUMENT_REGION();
164 std::vector<Mat> images;
165 src.getMatVector(images);
166 checkImageDimensions(images);
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);
173 std::vector<Mat> weights(images.size());
174 Mat weight_sum = Mat::zeros(size, CV_32F);
175 Mutex weight_sum_mutex;
177 parallel_for_(Range(0, static_cast<int>(images.size())), [&](const Range& range) {
178 for(int i = range.start; i < range.end; i++) {
179 Mat& img = images[i];
180 Mat gray, contrast, saturation, wellexp;
181 std::vector<Mat> splitted(channels);
183 img.convertTo(img, CV_32F, 1.0f/255.0f);
185 cvtColor(img, gray, COLOR_RGB2GRAY);
189 split(img, splitted);
191 Laplacian(gray, contrast, CV_32F);
192 contrast = abs(contrast);
194 Mat mean = Mat::zeros(size, CV_32F);
195 for(int c = 0; c < channels; c++) {
200 saturation = Mat::zeros(size, CV_32F);
201 for(int c = 0; c < channels; c++) {
202 Mat deviation = splitted[c] - mean;
203 pow(deviation, 2.0f, deviation);
204 saturation += deviation;
206 sqrt(saturation, saturation);
208 wellexp = Mat::ones(size, CV_32F);
209 for(int c = 0; c < channels; c++) {
210 Mat expo = splitted[c] - 0.5f;
211 pow(expo, 2.0f, expo);
212 expo = -expo / 0.08f;
214 wellexp = wellexp.mul(expo);
217 pow(contrast, wcon, contrast);
218 pow(saturation, wsat, saturation);
219 pow(wellexp, wexp, wellexp);
221 weights[i] = contrast;
223 weights[i] = weights[i].mul(saturation);
225 weights[i] = weights[i].mul(wellexp) + 1e-12f;
227 AutoLock lock(weight_sum_mutex);
228 weight_sum += weights[i];
232 int maxlevel = static_cast<int>(logf(static_cast<float>(min(size.width, size.height))) / logf(2.0f));
233 std::vector<Mat> res_pyr(maxlevel + 1);
234 std::vector<Mutex> res_pyr_mutexes(maxlevel + 1);
236 parallel_for_(Range(0, static_cast<int>(images.size())), [&](const Range& range) {
237 for(int i = range.start; i < range.end; i++) {
238 weights[i] /= weight_sum;
240 std::vector<Mat> img_pyr, weight_pyr;
241 buildPyramid(images[i], img_pyr, maxlevel);
242 buildPyramid(weights[i], weight_pyr, maxlevel);
244 for(int lvl = 0; lvl < maxlevel; lvl++) {
246 pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size());
249 for(int lvl = 0; lvl <= maxlevel; lvl++) {
250 std::vector<Mat> splitted(channels);
251 split(img_pyr[lvl], splitted);
252 for(int c = 0; c < channels; c++) {
253 splitted[c] = splitted[c].mul(weight_pyr[lvl]);
255 merge(splitted, img_pyr[lvl]);
257 AutoLock lock(res_pyr_mutexes[lvl]);
258 if(res_pyr[lvl].empty()) {
259 res_pyr[lvl] = img_pyr[lvl];
261 res_pyr[lvl] += img_pyr[lvl];
266 for(int lvl = maxlevel; lvl > 0; lvl--) {
268 pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size());
269 res_pyr[lvl - 1] += up;
271 dst.create(size, CV_32FCC);
272 res_pyr[0].copyTo(dst);
275 float getContrastWeight() const CV_OVERRIDE { return wcon; }
276 void setContrastWeight(float val) CV_OVERRIDE { wcon = val; }
278 float getSaturationWeight() const CV_OVERRIDE { return wsat; }
279 void setSaturationWeight(float val) CV_OVERRIDE { wsat = val; }
281 float getExposureWeight() const CV_OVERRIDE { return wexp; }
282 void setExposureWeight(float val) CV_OVERRIDE { wexp = val; }
284 void write(FileStorage& fs) const CV_OVERRIDE
288 << "contrast_weight" << wcon
289 << "saturation_weight" << wsat
290 << "exposure_weight" << wexp;
293 void read(const FileNode& fn) CV_OVERRIDE
295 FileNode n = fn["name"];
296 CV_Assert(n.isString() && String(n) == name);
297 wcon = fn["contrast_weight"];
298 wsat = fn["saturation_weight"];
299 wexp = fn["exposure_weight"];
304 float wcon, wsat, wexp;
307 Ptr<MergeMertens> createMergeMertens(float wcon, float wsat, float wexp)
309 return makePtr<MergeMertensImpl>(wcon, wsat, wexp);
312 class MergeRobertsonImpl CV_FINAL : public MergeRobertson
315 MergeRobertsonImpl() :
316 name("MergeRobertson"),
317 weight(RobertsonWeights())
321 void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
323 CV_INSTRUMENT_REGION();
325 std::vector<Mat> images;
326 src.getMatVector(images);
327 Mat times = _times.getMat();
329 CV_Assert(images.size() == times.total());
330 checkImageDimensions(images);
331 CV_Assert(images[0].depth() == CV_8U);
333 int channels = images[0].channels();
334 int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
336 dst.create(images[0].size(), CV_32FCC);
337 Mat result = dst.getMat();
339 Mat response = input_response.getMat();
340 if(response.empty()) {
341 float middle = LDR_SIZE / 2.0f;
342 response = linearResponse(channels) / middle;
344 CV_Assert(response.rows == LDR_SIZE && response.cols == 1 &&
345 response.channels() == channels);
347 result = Mat::zeros(images[0].size(), CV_32FCC);
348 Mat wsum = Mat::zeros(images[0].size(), CV_32FCC);
349 for(size_t i = 0; i < images.size(); i++) {
351 LUT(images[i], weight, w);
352 LUT(images[i], response, im);
354 result += times.at<float>((int)i) * w.mul(im);
355 wsum += times.at<float>((int)i) * times.at<float>((int)i) * w;
357 result = result.mul(1 / (wsum + Scalar::all(DBL_EPSILON)));
360 void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
362 CV_INSTRUMENT_REGION();
364 process(src, dst, times, Mat());
372 Ptr<MergeRobertson> createMergeRobertson()
374 return makePtr<MergeRobertsonImpl>();