2013-06-28 20:25:12 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
2013-08-03 05:45:20 +08:00
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
2013-06-28 20:25:12 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-07-19 05:08:42 +08:00
|
|
|
#include "precomp.hpp"
|
2013-06-28 20:25:12 +08:00
|
|
|
#include "opencv2/photo.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2013-08-05 23:22:42 +08:00
|
|
|
#include "hdr_common.hpp"
|
2013-06-28 20:25:12 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
2013-07-05 20:14:08 +08:00
|
|
|
|
2015-05-15 00:32:25 +08:00
|
|
|
inline void log_(const Mat& src, Mat& dst)
|
|
|
|
{
|
|
|
|
max(src, Scalar::all(1e-4), dst);
|
|
|
|
log(dst, dst);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
class TonemapImpl CV_FINAL : public Tonemap
|
2013-07-05 20:14:08 +08:00
|
|
|
{
|
2013-07-31 20:05:31 +08:00
|
|
|
public:
|
2013-09-24 03:40:06 +08:00
|
|
|
TonemapImpl(float _gamma) : name("Tonemap"), gamma(_gamma)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void process(InputArray _src, OutputArray _dst) CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert(!src.empty());
|
2019-02-19 00:15:05 +08:00
|
|
|
CV_Assert(_src.dims() == 2 && _src.type() == CV_32FC3);
|
2013-08-05 23:22:42 +08:00
|
|
|
_dst.create(src.size(), CV_32FC3);
|
|
|
|
Mat dst = _dst.getMat();
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
double min, max;
|
|
|
|
minMaxLoc(src, &min, &max);
|
|
|
|
if(max - min > DBL_EPSILON) {
|
|
|
|
dst = (src - min) / (max - min);
|
|
|
|
} else {
|
|
|
|
src.copyTo(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pow(dst, 1.0f / gamma, dst);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getGamma() const CV_OVERRIDE { return gamma; }
|
|
|
|
void setGamma(float val) CV_OVERRIDE { gamma = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void write(FileStorage& fs) const CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
2016-03-23 06:19:42 +08:00
|
|
|
writeFormat(fs);
|
2013-07-31 20:05:31 +08:00
|
|
|
fs << "name" << name
|
|
|
|
<< "gamma" << gamma;
|
2013-07-30 01:35:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void read(const FileNode& fn) CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
|
|
|
FileNode n = fn["name"];
|
|
|
|
CV_Assert(n.isString() && String(n) == name);
|
|
|
|
gamma = fn["gamma"];
|
|
|
|
}
|
2013-07-30 01:35:10 +08:00
|
|
|
|
2013-07-31 20:05:31 +08:00
|
|
|
protected:
|
2013-08-05 23:22:42 +08:00
|
|
|
String name;
|
|
|
|
float gamma;
|
2013-07-31 20:05:31 +08:00
|
|
|
};
|
2013-07-30 01:35:10 +08:00
|
|
|
|
2013-08-05 23:31:10 +08:00
|
|
|
Ptr<Tonemap> createTonemap(float gamma)
|
2013-07-30 01:35:10 +08:00
|
|
|
{
|
2013-09-24 03:40:06 +08:00
|
|
|
return makePtr<TonemapImpl>(gamma);
|
2013-07-30 01:35:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
class TonemapDragoImpl CV_FINAL : public TonemapDrago
|
2013-07-30 01:35:10 +08:00
|
|
|
{
|
2013-07-31 20:05:31 +08:00
|
|
|
public:
|
2013-09-24 03:40:06 +08:00
|
|
|
TonemapDragoImpl(float _gamma, float _saturation, float _bias) :
|
|
|
|
name("TonemapDrago"),
|
|
|
|
gamma(_gamma),
|
|
|
|
saturation(_saturation),
|
|
|
|
bias(_bias)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void process(InputArray _src, OutputArray _dst) CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert(!src.empty());
|
|
|
|
_dst.create(src.size(), CV_32FC3);
|
|
|
|
Mat img = _dst.getMat();
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2013-08-05 23:31:10 +08:00
|
|
|
Ptr<Tonemap> linear = createTonemap(1.0f);
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->process(src, img);
|
|
|
|
|
|
|
|
Mat gray_img;
|
|
|
|
cvtColor(img, gray_img, COLOR_RGB2GRAY);
|
|
|
|
Mat log_img;
|
2015-05-15 00:32:25 +08:00
|
|
|
log_(gray_img, log_img);
|
2013-08-05 23:22:42 +08:00
|
|
|
float mean = expf(static_cast<float>(sum(log_img)[0]) / log_img.total());
|
|
|
|
gray_img /= mean;
|
|
|
|
log_img.release();
|
|
|
|
|
|
|
|
double max;
|
|
|
|
minMaxLoc(gray_img, NULL, &max);
|
2018-07-17 21:14:54 +08:00
|
|
|
CV_Assert(max > 0);
|
2013-08-05 23:22:42 +08:00
|
|
|
|
|
|
|
Mat map;
|
|
|
|
log(gray_img + 1.0f, map);
|
|
|
|
Mat div;
|
|
|
|
pow(gray_img / static_cast<float>(max), logf(bias) / logf(0.5f), div);
|
|
|
|
log(2.0f + 8.0f * div, div);
|
|
|
|
map = map.mul(1.0f / div);
|
|
|
|
div.release();
|
|
|
|
|
|
|
|
mapLuminance(img, img, gray_img, map, saturation);
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->setGamma(gamma);
|
|
|
|
linear->process(img, img);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getGamma() const CV_OVERRIDE { return gamma; }
|
|
|
|
void setGamma(float val) CV_OVERRIDE { gamma = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getSaturation() const CV_OVERRIDE { return saturation; }
|
|
|
|
void setSaturation(float val) CV_OVERRIDE { saturation = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getBias() const CV_OVERRIDE { return bias; }
|
|
|
|
void setBias(float val) CV_OVERRIDE { bias = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void write(FileStorage& fs) const CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
2016-03-23 06:19:42 +08:00
|
|
|
writeFormat(fs);
|
2013-07-31 20:05:31 +08:00
|
|
|
fs << "name" << name
|
|
|
|
<< "gamma" << gamma
|
2013-08-05 23:22:42 +08:00
|
|
|
<< "bias" << bias
|
|
|
|
<< "saturation" << saturation;
|
2013-07-31 20:05:31 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void read(const FileNode& fn) CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
|
|
|
FileNode n = fn["name"];
|
|
|
|
CV_Assert(n.isString() && String(n) == name);
|
|
|
|
gamma = fn["gamma"];
|
2013-08-05 23:22:42 +08:00
|
|
|
bias = fn["bias"];
|
|
|
|
saturation = fn["saturation"];
|
2013-07-05 20:14:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-31 20:05:31 +08:00
|
|
|
protected:
|
2013-08-05 23:22:42 +08:00
|
|
|
String name;
|
|
|
|
float gamma, saturation, bias;
|
2013-07-31 20:05:31 +08:00
|
|
|
};
|
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Ptr<TonemapDrago> createTonemapDrago(float gamma, float saturation, float bias)
|
2013-07-05 20:14:08 +08:00
|
|
|
{
|
2013-09-24 03:40:06 +08:00
|
|
|
return makePtr<TonemapDragoImpl>(gamma, saturation, bias);
|
2013-07-30 01:35:10 +08:00
|
|
|
}
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
class TonemapReinhardImpl CV_FINAL : public TonemapReinhard
|
2013-07-05 20:14:08 +08:00
|
|
|
{
|
2013-07-31 20:05:31 +08:00
|
|
|
public:
|
2013-09-24 03:40:06 +08:00
|
|
|
TonemapReinhardImpl(float _gamma, float _intensity, float _light_adapt, float _color_adapt) :
|
|
|
|
name("TonemapReinhard"),
|
|
|
|
gamma(_gamma),
|
|
|
|
intensity(_intensity),
|
|
|
|
light_adapt(_light_adapt),
|
|
|
|
color_adapt(_color_adapt)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void process(InputArray _src, OutputArray _dst) CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert(!src.empty());
|
|
|
|
_dst.create(src.size(), CV_32FC3);
|
|
|
|
Mat img = _dst.getMat();
|
2013-08-05 23:31:10 +08:00
|
|
|
Ptr<Tonemap> linear = createTonemap(1.0f);
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->process(src, img);
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Mat gray_img;
|
|
|
|
cvtColor(img, gray_img, COLOR_RGB2GRAY);
|
|
|
|
Mat log_img;
|
2015-05-15 00:32:25 +08:00
|
|
|
log_(gray_img, log_img);
|
2013-08-05 23:22:42 +08:00
|
|
|
|
|
|
|
float log_mean = static_cast<float>(sum(log_img)[0] / log_img.total());
|
|
|
|
double log_min, log_max;
|
|
|
|
minMaxLoc(log_img, &log_min, &log_max);
|
|
|
|
log_img.release();
|
|
|
|
|
|
|
|
double key = static_cast<float>((log_max - log_mean) / (log_max - log_min));
|
|
|
|
float map_key = 0.3f + 0.7f * pow(static_cast<float>(key), 1.4f);
|
|
|
|
intensity = exp(-intensity);
|
|
|
|
Scalar chan_mean = mean(img);
|
|
|
|
float gray_mean = static_cast<float>(mean(gray_img)[0]);
|
|
|
|
|
|
|
|
std::vector<Mat> channels(3);
|
|
|
|
split(img, channels);
|
|
|
|
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
float global = color_adapt * static_cast<float>(chan_mean[i]) + (1.0f - color_adapt) * gray_mean;
|
|
|
|
Mat adapt = color_adapt * channels[i] + (1.0f - color_adapt) * gray_img;
|
|
|
|
adapt = light_adapt * adapt + (1.0f - light_adapt) * global;
|
|
|
|
pow(intensity * adapt, map_key, adapt);
|
2013-09-24 03:40:06 +08:00
|
|
|
channels[i] = channels[i].mul(1.0f / (adapt + channels[i]));
|
2013-08-05 23:22:42 +08:00
|
|
|
}
|
|
|
|
gray_img.release();
|
|
|
|
merge(channels, img);
|
2013-09-24 03:40:06 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->setGamma(gamma);
|
|
|
|
linear->process(img, img);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getGamma() const CV_OVERRIDE { return gamma; }
|
|
|
|
void setGamma(float val) CV_OVERRIDE { gamma = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getIntensity() const CV_OVERRIDE { return intensity; }
|
|
|
|
void setIntensity(float val) CV_OVERRIDE { intensity = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getLightAdaptation() const CV_OVERRIDE { return light_adapt; }
|
|
|
|
void setLightAdaptation(float val) CV_OVERRIDE { light_adapt = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getColorAdaptation() const CV_OVERRIDE { return color_adapt; }
|
|
|
|
void setColorAdaptation(float val) CV_OVERRIDE { color_adapt = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void write(FileStorage& fs) const CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
2016-03-23 06:19:42 +08:00
|
|
|
writeFormat(fs);
|
2013-07-31 20:05:31 +08:00
|
|
|
fs << "name" << name
|
|
|
|
<< "gamma" << gamma
|
2013-09-24 03:40:06 +08:00
|
|
|
<< "intensity" << intensity
|
|
|
|
<< "light_adapt" << light_adapt
|
2013-08-05 23:22:42 +08:00
|
|
|
<< "color_adapt" << color_adapt;
|
2013-07-31 20:05:31 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void read(const FileNode& fn) CV_OVERRIDE
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
|
|
|
FileNode n = fn["name"];
|
|
|
|
CV_Assert(n.isString() && String(n) == name);
|
|
|
|
gamma = fn["gamma"];
|
2013-08-05 23:22:42 +08:00
|
|
|
intensity = fn["intensity"];
|
|
|
|
light_adapt = fn["light_adapt"];
|
|
|
|
color_adapt = fn["color_adapt"];
|
2013-07-31 20:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2013-08-05 23:22:42 +08:00
|
|
|
String name;
|
|
|
|
float gamma, intensity, light_adapt, color_adapt;
|
2013-07-31 20:05:31 +08:00
|
|
|
};
|
|
|
|
|
2013-09-24 03:40:06 +08:00
|
|
|
Ptr<TonemapReinhard> createTonemapReinhard(float gamma, float contrast, float sigma_color, float sigma_space)
|
2013-07-31 20:05:31 +08:00
|
|
|
{
|
2013-09-24 03:40:06 +08:00
|
|
|
return makePtr<TonemapReinhardImpl>(gamma, contrast, sigma_color, sigma_space);
|
2013-08-05 23:22:42 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
class TonemapMantiukImpl CV_FINAL : public TonemapMantiuk
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-24 03:40:06 +08:00
|
|
|
TonemapMantiukImpl(float _gamma, float _scale, float _saturation) :
|
|
|
|
name("TonemapMantiuk"),
|
|
|
|
gamma(_gamma),
|
|
|
|
scale(_scale),
|
|
|
|
saturation(_saturation)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void process(InputArray _src, OutputArray _dst) CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-08-05 23:22:42 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert(!src.empty());
|
|
|
|
_dst.create(src.size(), CV_32FC3);
|
|
|
|
Mat img = _dst.getMat();
|
2013-08-05 23:31:10 +08:00
|
|
|
Ptr<Tonemap> linear = createTonemap(1.0f);
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->process(src, img);
|
|
|
|
|
|
|
|
Mat gray_img;
|
|
|
|
cvtColor(img, gray_img, COLOR_RGB2GRAY);
|
|
|
|
Mat log_img;
|
2015-05-15 00:32:25 +08:00
|
|
|
log_(gray_img, log_img);
|
2013-08-05 23:22:42 +08:00
|
|
|
|
|
|
|
std::vector<Mat> x_contrast, y_contrast;
|
|
|
|
getContrast(log_img, x_contrast, y_contrast);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < x_contrast.size(); i++) {
|
2013-09-24 03:40:06 +08:00
|
|
|
mapContrast(x_contrast[i]);
|
|
|
|
mapContrast(y_contrast[i]);
|
2013-08-05 23:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Mat right(src.size(), CV_32F);
|
|
|
|
calculateSum(x_contrast, y_contrast, right);
|
|
|
|
|
|
|
|
Mat p, r, product, x = log_img;
|
|
|
|
calculateProduct(x, r);
|
|
|
|
r = right - r;
|
|
|
|
r.copyTo(p);
|
|
|
|
|
|
|
|
const float target_error = 1e-3f;
|
|
|
|
float target_norm = static_cast<float>(right.dot(right)) * powf(target_error, 2.0f);
|
|
|
|
int max_iterations = 100;
|
|
|
|
float rr = static_cast<float>(r.dot(r));
|
|
|
|
|
|
|
|
for(int i = 0; i < max_iterations; i++)
|
|
|
|
{
|
|
|
|
calculateProduct(p, product);
|
2018-07-17 21:14:54 +08:00
|
|
|
double dprod = p.dot(product);
|
|
|
|
CV_Assert(fabs(dprod) > 0);
|
|
|
|
float alpha = rr / static_cast<float>(dprod);
|
2013-08-05 23:22:42 +08:00
|
|
|
|
|
|
|
r -= alpha * product;
|
|
|
|
x += alpha * p;
|
|
|
|
|
|
|
|
float new_rr = static_cast<float>(r.dot(r));
|
2018-07-17 21:14:54 +08:00
|
|
|
CV_Assert(fabs(rr) > 0);
|
2013-08-05 23:22:42 +08:00
|
|
|
p = r + (new_rr / rr) * p;
|
|
|
|
rr = new_rr;
|
|
|
|
|
|
|
|
if(rr < target_norm) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exp(x, x);
|
|
|
|
mapLuminance(img, img, gray_img, x, saturation);
|
|
|
|
|
2013-08-05 23:31:10 +08:00
|
|
|
linear = createTonemap(gamma);
|
2013-08-05 23:22:42 +08:00
|
|
|
linear->process(img, img);
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getGamma() const CV_OVERRIDE { return gamma; }
|
|
|
|
void setGamma(float val) CV_OVERRIDE { gamma = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getScale() const CV_OVERRIDE { return scale; }
|
|
|
|
void setScale(float val) CV_OVERRIDE { scale = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
float getSaturation() const CV_OVERRIDE { return saturation; }
|
|
|
|
void setSaturation(float val) CV_OVERRIDE { saturation = val; }
|
2013-08-05 23:22:42 +08:00
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void write(FileStorage& fs) const CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
2016-03-23 06:19:42 +08:00
|
|
|
writeFormat(fs);
|
2013-08-05 23:22:42 +08:00
|
|
|
fs << "name" << name
|
|
|
|
<< "gamma" << gamma
|
2013-09-24 03:40:06 +08:00
|
|
|
<< "scale" << scale
|
2013-08-05 23:22:42 +08:00
|
|
|
<< "saturation" << saturation;
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:17:00 +08:00
|
|
|
void read(const FileNode& fn) CV_OVERRIDE
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
FileNode n = fn["name"];
|
|
|
|
CV_Assert(n.isString() && String(n) == name);
|
|
|
|
gamma = fn["gamma"];
|
|
|
|
scale = fn["scale"];
|
|
|
|
saturation = fn["saturation"];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
String name;
|
|
|
|
float gamma, scale, saturation;
|
|
|
|
|
|
|
|
void signedPow(Mat src, float power, Mat& dst)
|
|
|
|
{
|
|
|
|
Mat sign = (src > 0);
|
2013-08-06 19:24:01 +08:00
|
|
|
sign.convertTo(sign, CV_32F, 1.0f/255.0f);
|
|
|
|
sign = sign * 2.0f - 1.0f;
|
2013-08-05 23:22:42 +08:00
|
|
|
pow(abs(src), power, dst);
|
|
|
|
dst = dst.mul(sign);
|
|
|
|
}
|
|
|
|
|
2013-09-24 03:40:06 +08:00
|
|
|
void mapContrast(Mat& contrast)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
const float response_power = 0.4185f;
|
|
|
|
signedPow(contrast, response_power, contrast);
|
|
|
|
contrast *= scale;
|
|
|
|
signedPow(contrast, 1.0f / response_power, contrast);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getGradient(Mat src, Mat& dst, int pos)
|
|
|
|
{
|
|
|
|
dst = Mat::zeros(src.size(), CV_32F);
|
|
|
|
Mat a, b;
|
|
|
|
Mat grad = src.colRange(1, src.cols) - src.colRange(0, src.cols - 1);
|
|
|
|
grad.copyTo(dst.colRange(pos, src.cols + pos - 1));
|
|
|
|
if(pos == 1) {
|
|
|
|
src.col(0).copyTo(dst.col(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getContrast(Mat src, std::vector<Mat>& x_contrast, std::vector<Mat>& y_contrast)
|
|
|
|
{
|
|
|
|
int levels = static_cast<int>(logf(static_cast<float>(min(src.rows, src.cols))) / logf(2.0f));
|
|
|
|
x_contrast.resize(levels);
|
|
|
|
y_contrast.resize(levels);
|
|
|
|
|
|
|
|
Mat layer;
|
|
|
|
src.copyTo(layer);
|
|
|
|
for(int i = 0; i < levels; i++) {
|
|
|
|
getGradient(layer, x_contrast[i], 0);
|
|
|
|
getGradient(layer.t(), y_contrast[i], 0);
|
2017-12-13 20:00:38 +08:00
|
|
|
resize(layer, layer, Size(layer.cols / 2, layer.rows / 2), 0, 0, INTER_LINEAR);
|
2013-08-05 23:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculateSum(std::vector<Mat>& x_contrast, std::vector<Mat>& y_contrast, Mat& sum)
|
|
|
|
{
|
2015-12-08 20:15:51 +08:00
|
|
|
if (x_contrast.empty())
|
|
|
|
return;
|
|
|
|
const int last = (int)x_contrast.size() - 1;
|
|
|
|
sum = Mat::zeros(x_contrast[last].size(), CV_32F);
|
|
|
|
for(int i = last; i >= 0; i--)
|
2013-08-05 23:22:42 +08:00
|
|
|
{
|
|
|
|
Mat grad_x, grad_y;
|
|
|
|
getGradient(x_contrast[i], grad_x, 1);
|
|
|
|
getGradient(y_contrast[i], grad_y, 1);
|
2017-12-13 20:00:38 +08:00
|
|
|
resize(sum, sum, x_contrast[i].size(), 0, 0, INTER_LINEAR);
|
2013-08-05 23:22:42 +08:00
|
|
|
sum += grad_x + grad_y.t();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculateProduct(Mat src, Mat& dst)
|
|
|
|
{
|
|
|
|
std::vector<Mat> x_contrast, y_contrast;
|
|
|
|
getContrast(src, x_contrast, y_contrast);
|
|
|
|
calculateSum(x_contrast, y_contrast, dst);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ptr<TonemapMantiuk> createTonemapMantiuk(float gamma, float scale, float saturation)
|
|
|
|
{
|
2013-09-24 03:40:06 +08:00
|
|
|
return makePtr<TonemapMantiukImpl>(gamma, scale, saturation);
|
2013-07-18 04:55:39 +08:00
|
|
|
}
|
2013-07-31 20:05:31 +08:00
|
|
|
|
2013-08-03 05:45:20 +08:00
|
|
|
}
|