opencv/modules/photo/src/merge.cpp

368 lines
12 KiB
C++
Raw Normal View History

2013-08-01 07:24:47 +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
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
2013-08-01 07:24:47 +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*/
#include "precomp.hpp"
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include "hdr_common.hpp"
namespace cv
{
2018-03-15 21:17:00 +08:00
class MergeDebevecImpl CV_FINAL : public MergeDebevec
2013-08-01 07:24:47 +08:00
{
public:
2013-08-05 23:22:42 +08:00
MergeDebevecImpl() :
name("MergeDebevec"),
weights(triangleWeights())
2013-08-05 23:22:42 +08:00
{
}
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
2013-08-05 23:22:42 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-05 23:22:42 +08:00
std::vector<Mat> images;
src.getMatVector(images);
Mat times = _times.getMat();
2013-08-01 07:24:47 +08:00
CV_Assert(images.size() == times.total());
2013-08-05 23:22:42 +08:00
checkImageDimensions(images);
2013-08-06 19:24:01 +08:00
CV_Assert(images[0].depth() == CV_8U);
int channels = images[0].channels();
Size size = images[0].size();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
dst.create(images[0].size(), CV_32FCC);
Mat result = dst.getMat();
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
Mat response = input_response.getMat();
2013-08-06 19:24:01 +08:00
if(response.empty()) {
response = linearResponse(channels);
2013-09-06 21:30:43 +08:00
response.at<Vec3f>(0) = response.at<Vec3f>(1);
2013-08-05 23:22:42 +08:00
}
Mat log_response;
log(response, log_response);
CV_Assert(log_response.rows == LDR_SIZE && log_response.cols == 1 &&
log_response.channels() == channels);
2013-08-06 19:24:01 +08:00
Mat exp_values(times.clone());
2013-08-06 19:24:01 +08:00
log(exp_values, exp_values);
2013-08-06 19:24:01 +08:00
result = Mat::zeros(size, CV_32FCC);
std::vector<Mat> result_split;
split(result, result_split);
Mat weight_sum = Mat::zeros(size, CV_32F);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
for(size_t i = 0; i < images.size(); i++) {
std::vector<Mat> splitted;
split(images[i], splitted);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
Mat w = Mat::zeros(size, CV_32F);
for(int c = 0; c < channels; c++) {
LUT(splitted[c], weights, splitted[c]);
w += splitted[c];
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
w /= channels;
Mat response_img;
LUT(images[i], log_response, response_img);
2013-08-06 19:24:01 +08:00
split(response_img, splitted);
for(int c = 0; c < channels; c++) {
2013-10-04 21:03:15 +08:00
result_split[c] += w.mul(splitted[c] - exp_values.at<float>((int)i));
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
weight_sum += w;
}
weight_sum = 1.0f / weight_sum;
for(int c = 0; c < channels; c++) {
result_split[c] = result_split[c].mul(weight_sum);
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
merge(result_split, result);
exp(result, result);
2013-08-05 23:22:42 +08:00
}
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
2013-08-05 23:22:42 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-06 19:24:01 +08:00
process(src, dst, times, Mat());
2013-08-05 23:22:42 +08:00
}
2013-08-01 07:24:47 +08:00
protected:
2013-08-05 23:22:42 +08:00
String name;
Mat weights;
2013-08-01 07:24:47 +08:00
};
Ptr<MergeDebevec> createMergeDebevec()
{
return makePtr<MergeDebevecImpl>();
2013-08-01 07:24:47 +08:00
}
2018-03-15 21:17:00 +08:00
class MergeMertensImpl CV_FINAL : public MergeMertens
2013-08-01 07:24:47 +08:00
{
public:
MergeMertensImpl(float _wcon, float _wsat, float _wexp) :
name("MergeMertens"),
wcon(_wcon),
wsat(_wsat),
wexp(_wexp)
2013-08-05 23:22:42 +08:00
{
}
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArrayOfArrays dst, InputArray, InputArray) CV_OVERRIDE
2013-08-05 23:22:42 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-05 23:22:42 +08:00
process(src, dst);
}
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArray dst) CV_OVERRIDE
2013-08-05 23:22:42 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-05 23:22:42 +08:00
std::vector<Mat> images;
src.getMatVector(images);
checkImageDimensions(images);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
int channels = images[0].channels();
CV_Assert(channels == 1 || channels == 3);
Size size = images[0].size();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
2013-08-05 23:22:42 +08:00
std::vector<Mat> weights(images.size());
2013-08-06 19:24:01 +08:00
Mat weight_sum = Mat::zeros(size, CV_32F);
for(size_t i = 0; i < images.size(); i++) {
2013-08-05 23:22:42 +08:00
Mat img, gray, contrast, saturation, wellexp;
2013-08-06 19:24:01 +08:00
std::vector<Mat> splitted(channels);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
images[i].convertTo(img, CV_32F, 1.0f/255.0f);
if(channels == 3) {
cvtColor(img, gray, COLOR_RGB2GRAY);
} else {
img.copyTo(gray);
}
split(img, splitted);
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
Laplacian(gray, contrast, CV_32F);
contrast = abs(contrast);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
Mat mean = Mat::zeros(size, CV_32F);
for(int c = 0; c < channels; c++) {
mean += splitted[c];
}
mean /= channels;
saturation = Mat::zeros(size, CV_32F);
for(int c = 0; c < channels; c++) {
Mat deviation = splitted[c] - mean;
pow(deviation, 2.0f, deviation);
2013-08-05 23:22:42 +08:00
saturation += deviation;
}
sqrt(saturation, saturation);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
wellexp = Mat::ones(size, CV_32F);
for(int c = 0; c < channels; c++) {
Mat expo = splitted[c] - 0.5f;
pow(expo, 2.0f, expo);
expo = -expo / 0.08f;
exp(expo, expo);
wellexp = wellexp.mul(expo);
2013-08-05 23:22:42 +08:00
}
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
pow(contrast, wcon, contrast);
pow(saturation, wsat, saturation);
pow(wellexp, wexp, wellexp);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
weights[i] = contrast;
if(channels == 3) {
weights[i] = weights[i].mul(saturation);
}
weights[i] = weights[i].mul(wellexp) + 1e-12f;
2013-08-06 19:24:01 +08:00
weight_sum += weights[i];
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
int maxlevel = static_cast<int>(logf(static_cast<float>(min(size.width, size.height))) / logf(2.0f));
2013-08-05 23:22:42 +08:00
std::vector<Mat> res_pyr(maxlevel + 1);
2013-08-01 07:24:47 +08:00
2013-08-06 19:24:01 +08:00
for(size_t i = 0; i < images.size(); i++) {
weights[i] /= weight_sum;
2013-08-05 23:22:42 +08:00
Mat img;
2013-08-06 19:24:01 +08:00
images[i].convertTo(img, CV_32F, 1.0f/255.0f);
2013-08-05 23:22:42 +08:00
std::vector<Mat> img_pyr, weight_pyr;
buildPyramid(img, img_pyr, maxlevel);
2013-08-06 19:24:01 +08:00
buildPyramid(weights[i], weight_pyr, maxlevel);
2013-08-05 23:22:42 +08:00
for(int lvl = 0; lvl < maxlevel; lvl++) {
Mat up;
pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size());
img_pyr[lvl] -= up;
}
for(int lvl = 0; lvl <= maxlevel; lvl++) {
2013-08-06 19:24:01 +08:00
std::vector<Mat> splitted(channels);
split(img_pyr[lvl], splitted);
for(int c = 0; c < channels; c++) {
splitted[c] = splitted[c].mul(weight_pyr[lvl]);
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
merge(splitted, img_pyr[lvl]);
2013-08-05 23:22:42 +08:00
if(res_pyr[lvl].empty()) {
res_pyr[lvl] = img_pyr[lvl];
} else {
res_pyr[lvl] += img_pyr[lvl];
}
}
}
for(int lvl = maxlevel; lvl > 0; lvl--) {
Mat up;
pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size());
res_pyr[lvl - 1] += up;
}
2013-08-06 19:24:01 +08:00
dst.create(size, CV_32FCC);
res_pyr[0].copyTo(dst);
2013-08-05 23:22:42 +08:00
}
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
float getContrastWeight() const CV_OVERRIDE { return wcon; }
void setContrastWeight(float val) CV_OVERRIDE { wcon = val; }
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
float getSaturationWeight() const CV_OVERRIDE { return wsat; }
void setSaturationWeight(float val) CV_OVERRIDE { wsat = val; }
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
float getExposureWeight() const CV_OVERRIDE { return wexp; }
void setExposureWeight(float val) CV_OVERRIDE { wexp = val; }
2013-08-01 07:24:47 +08:00
2018-03-15 21:17:00 +08:00
void write(FileStorage& fs) const CV_OVERRIDE
2013-08-01 07:24:47 +08:00
{
writeFormat(fs);
2013-08-01 07:24:47 +08:00
fs << "name" << name
2013-08-05 23:22:42 +08:00
<< "contrast_weight" << wcon
<< "saturation_weight" << wsat
<< "exposure_weight" << wexp;
2013-08-01 07:24:47 +08:00
}
2018-03-15 21:17:00 +08:00
void read(const FileNode& fn) CV_OVERRIDE
2013-08-01 07:24:47 +08:00
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
wcon = fn["contrast_weight"];
2013-08-05 23:22:42 +08:00
wsat = fn["saturation_weight"];
wexp = fn["exposure_weight"];
2013-08-01 07:24:47 +08:00
}
protected:
2013-08-05 23:22:42 +08:00
String name;
float wcon, wsat, wexp;
2013-08-01 07:24:47 +08:00
};
Ptr<MergeMertens> createMergeMertens(float wcon, float wsat, float wexp)
{
return makePtr<MergeMertensImpl>(wcon, wsat, wexp);
2013-08-01 07:24:47 +08:00
}
2018-03-15 21:17:00 +08:00
class MergeRobertsonImpl CV_FINAL : public MergeRobertson
2013-08-26 19:23:37 +08:00
{
public:
MergeRobertsonImpl() :
name("MergeRobertson"),
weight(RobertsonWeights())
{
}
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times, InputArray input_response) CV_OVERRIDE
2013-08-26 19:23:37 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-26 19:23:37 +08:00
std::vector<Mat> images;
src.getMatVector(images);
Mat times = _times.getMat();
2013-08-26 19:23:37 +08:00
CV_Assert(images.size() == times.total());
2013-08-26 19:23:37 +08:00
checkImageDimensions(images);
CV_Assert(images[0].depth() == CV_8U);
int channels = images[0].channels();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
dst.create(images[0].size(), CV_32FCC);
Mat result = dst.getMat();
Mat response = input_response.getMat();
if(response.empty()) {
float middle = LDR_SIZE / 2.0f;
response = linearResponse(channels) / middle;
2013-08-26 19:23:37 +08:00
}
CV_Assert(response.rows == LDR_SIZE && response.cols == 1 &&
2013-08-26 19:23:37 +08:00
response.channels() == channels);
2013-08-26 19:23:37 +08:00
result = Mat::zeros(images[0].size(), CV_32FCC);
Mat wsum = Mat::zeros(images[0].size(), CV_32FCC);
for(size_t i = 0; i < images.size(); i++) {
Mat im, w;
LUT(images[i], weight, w);
LUT(images[i], response, im);
2013-10-04 21:03:15 +08:00
result += times.at<float>((int)i) * w.mul(im);
wsum += times.at<float>((int)i) * times.at<float>((int)i) * w;
2013-08-26 19:23:37 +08:00
}
result = result.mul(1 / wsum);
}
2018-03-15 21:17:00 +08:00
void process(InputArrayOfArrays src, OutputArray dst, InputArray times) CV_OVERRIDE
2013-08-26 19:23:37 +08:00
{
CV_INSTRUMENT_REGION()
2013-08-26 19:23:37 +08:00
process(src, dst, times, Mat());
}
protected:
String name;
Mat weight;
};
Ptr<MergeRobertson> createMergeRobertson()
{
return makePtr<MergeRobertsonImpl>();
2013-08-26 19:23:37 +08:00
}
}