Commit 302bf23f authored by Fedor Morozov's avatar Fedor Morozov

All hdr functions as Algorithms

parent 4d2ea847
......@@ -409,7 +409,7 @@ TEST(Highgui_WebP, encode_decode_lossy_webp)
TEST(Highgui_Hdr, regression)
{
string folder = string(cvtest::TS::ptr()->get_data_path()) + "../cv/hdr/";
string folder = string(cvtest::TS::ptr()->get_data_path()) + "../cv/hdr/format/";
string name_rle = folder + "rle.hdr";
string name_no_rle = folder + "no_rle.hdr";
Mat img_rle = imread(name_rle, -1);
......
......@@ -138,6 +138,88 @@ public:
CV_EXPORTS_W Ptr<TonemapReinhardDevlin>
createTonemapReinhardDevlin(float gamma = 1.0f, float intensity = 0.0f, float light_adapt = 1.0f, float color_adapt = 0.0f);
class CV_EXPORTS_W ExposureAlign : public Algorithm
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArrayOfArrays dst,
const std::vector<float>& times, InputArray response) = 0;
};
class CV_EXPORTS_W AlignMTB : public ExposureAlign
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArrayOfArrays dst,
const std::vector<float>& times, InputArray response) = 0;
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArrayOfArrays dst) = 0;
CV_WRAP virtual void calculateShift(InputArray img0, InputArray img1, Point& shift) = 0;
CV_WRAP virtual void shiftMat(InputArray src, OutputArray dst, const Point shift) = 0;
CV_WRAP virtual int getMaxBits() const = 0;
CV_WRAP virtual void setMaxBits(int max_bits) = 0;
CV_WRAP virtual int getExcludeRange() const = 0;
CV_WRAP virtual void setExcludeRange(int exclude_range) = 0;
};
CV_EXPORTS_W Ptr<AlignMTB> createAlignMTB(int max_bits = 6, int exclude_range = 4);
class CV_EXPORTS_W ExposureCalibrate : public Algorithm
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst, std::vector<float>& times) = 0;
};
class CV_EXPORTS_W CalibrateDebevec : public ExposureCalibrate
{
public:
CV_WRAP virtual float getLambda() const = 0;
CV_WRAP virtual void setLambda(float lambda) = 0;
CV_WRAP virtual int getSamples() const = 0;
CV_WRAP virtual void setSamples(int samples) = 0;
};
CV_EXPORTS_W Ptr<CalibrateDebevec> createCalibrateDebevec(int samples = 50, float lambda = 10.0f);
class CV_EXPORTS_W ExposureMerge : public Algorithm
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst,
const std::vector<float>& times, InputArray response) = 0;
};
class CV_EXPORTS_W MergeDebevec : public ExposureMerge
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst,
const std::vector<float>& times, InputArray response) = 0;
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst, const std::vector<float>& times) = 0;
};
CV_EXPORTS_W Ptr<MergeDebevec> createMergeDebevec();
class CV_EXPORTS_W MergeMertens : public ExposureMerge
{
public:
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst,
const std::vector<float>& times, InputArray response) = 0;
CV_WRAP virtual void process(InputArrayOfArrays src, OutputArray dst) = 0;
CV_WRAP virtual float getContrastWeight() const = 0;
CV_WRAP virtual void setContrastWeight(float contrast_weiht) = 0;
CV_WRAP virtual float getSaturationWeight() const = 0;
CV_WRAP virtual void setSaturationWeight(float saturation_weight) = 0;
CV_WRAP virtual float getExposureWeight() const = 0;
CV_WRAP virtual void setExposureWeight(float exposure_weight) = 0;
};
CV_EXPORTS_W Ptr<MergeMertens>
createMergeMertens(float contrast_weight = 1.0f, float saturation_weight = 1.0f, float exposure_weight = 0.0f);
} // cv
#endif
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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
{
class AlignMTBImpl : public AlignMTB
{
public:
AlignMTBImpl(int max_bits, int exclude_range) :
max_bits(max_bits),
exclude_range(exclude_range),
name("AlignMTB")
{
}
void process(InputArrayOfArrays src, OutputArrayOfArrays dst,
const std::vector<float>& times, InputArray response)
{
process(src, dst);
}
void process(InputArrayOfArrays _src, OutputArray _dst)
{
std::vector<Mat> src, dst;
_src.getMatVector(src);
_dst.getMatVector(dst);
checkImageDimensions(src);
dst.resize(src.size());
size_t pivot = src.size() / 2;
dst[pivot] = src[pivot];
Mat gray_base;
cvtColor(src[pivot], gray_base, COLOR_RGB2GRAY);
for(size_t i = 0; i < src.size(); i++) {
if(i == pivot) {
continue;
}
Mat gray;
cvtColor(src[i], gray, COLOR_RGB2GRAY);
Point shift;
calculateShift(gray_base, gray, shift);
shiftMat(src[i], dst[i], shift);
}
}
void calculateShift(InputArray _img0, InputArray _img1, Point& shift)
{
Mat img0 = _img0.getMat();
Mat img1 = _img1.getMat();
CV_Assert(img0.channels() == 1 && img0.type() == img1.type());
CV_Assert(img0.size() == img0.size());
int maxlevel = static_cast<int>(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1;
maxlevel = min(maxlevel, max_bits - 1);
std::vector<Mat> pyr0;
std::vector<Mat> pyr1;
buildPyr(img0, pyr0, maxlevel);
buildPyr(img1, pyr1, maxlevel);
shift = Point(0, 0);
for(int level = maxlevel; level >= 0; level--) {
shift *= 2;
Mat tb1, tb2, eb1, eb2;
computeBitmaps(pyr0[level], tb1, eb1, exclude_range);
computeBitmaps(pyr1[level], tb2, eb2, exclude_range);
int min_err = pyr0[level].total();
Point new_shift(shift);
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
Point test_shift = shift + Point(i, j);
Mat shifted_tb2, shifted_eb2, diff;
shiftMat(tb2, shifted_tb2, test_shift);
shiftMat(eb2, shifted_eb2, test_shift);
bitwise_xor(tb1, shifted_tb2, diff);
bitwise_and(diff, eb1, diff);
bitwise_and(diff, shifted_eb2, diff);
int err = countNonZero(diff);
if(err < min_err) {
new_shift = test_shift;
min_err = err;
}
}
}
shift = new_shift;
}
}
void shiftMat(InputArray _src, OutputArray _dst, const Point shift)
{
Mat src = _src.getMat();
_dst.create(src.size(), src.type());
Mat dst = _dst.getMat();
dst = Mat::zeros(src.size(), src.type());
int width = src.cols - abs(shift.x);
int height = src.rows - abs(shift.y);
Rect dst_rect(max(shift.x, 0), max(shift.y, 0), width, height);
Rect src_rect(max(-shift.x, 0), max(-shift.y, 0), width, height);
src(src_rect).copyTo(dst(dst_rect));
}
int getMaxBits() const { return max_bits; }
void setMaxBits(int val) { max_bits = val; }
int getExcludeRange() const { return exclude_range; }
void setExcludeRange(int val) { exclude_range = val; }
void write(FileStorage& fs) const
{
fs << "name" << name
<< "max_bits" << max_bits
<< "exclude_range" << exclude_range;
}
void read(const FileNode& fn)
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
max_bits = fn["max_bits"];
exclude_range = fn["exclude_range"];
}
protected:
String name;
int max_bits, exclude_range;
void downsample(Mat& src, Mat& dst)
{
dst = Mat(src.rows / 2, src.cols / 2, CV_8UC1);
int offset = src.cols * 2;
uchar *src_ptr = src.ptr();
uchar *dst_ptr = dst.ptr();
for(int y = 0; y < dst.rows; y ++) {
uchar *ptr = src_ptr;
for(int x = 0; x < dst.cols; x++) {
dst_ptr[0] = ptr[0];
dst_ptr++;
ptr += 2;
}
src_ptr += offset;
}
}
void buildPyr(Mat& img, std::vector<Mat>& pyr, int maxlevel)
{
pyr.resize(maxlevel + 1);
pyr[0] = img.clone();
for(int level = 0; level < maxlevel; level++) {
downsample(pyr[level], pyr[level + 1]);
}
}
int getMedian(Mat& img)
{
int channels = 0;
Mat hist;
int hist_size = 256;
float range[] = {0, 256} ;
const float* ranges[] = {range};
calcHist(&img, 1, &channels, Mat(), hist, 1, &hist_size, ranges);
float *ptr = hist.ptr<float>();
int median = 0, sum = 0;
int thresh = img.total() / 2;
while(sum < thresh && median < 256) {
sum += static_cast<int>(ptr[median]);
median++;
}
return median;
}
void computeBitmaps(Mat& img, Mat& tb, Mat& eb, int exclude_range)
{
int median = getMedian(img);
compare(img, median, tb, CMP_GT);
compare(abs(img - median), exclude_range, eb, CMP_GT);
}
};
CV_EXPORTS_W Ptr<AlignMTB> createAlignMTB(int max_bits, int exclude_range)
{
return new AlignMTBImpl(max_bits, exclude_range);
}
}
\ No newline at end of file
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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
{
class CalibrateDebevecImpl : public CalibrateDebevec
{
public:
CalibrateDebevecImpl(int samples, float lambda) :
samples(samples),
lambda(lambda),
name("CalibrateDebevec"),
w(tringleWeights())
{
}
void process(InputArrayOfArrays src, OutputArray dst, std::vector<float>& times)
{
std::vector<Mat> images;
src.getMatVector(images);
dst.create(256, images[0].channels(), CV_32F);
Mat response = dst.getMat();
CV_Assert(!images.empty() && images.size() == times.size());
CV_Assert(images[0].depth() == CV_8U);
checkImageDimensions(images);
for(int channel = 0; channel < images[0].channels(); channel++) {
Mat A = Mat::zeros(samples * images.size() + 257, 256 + samples, CV_32F);
Mat B = Mat::zeros(A.rows, 1, CV_32F);
int eq = 0;
for(int i = 0; i < samples; i++) {
int pos = 3 * (rand() % images[0].total()) + channel;
for(size_t j = 0; j < images.size(); j++) {
int val = (images[j].ptr() + pos)[0];
A.at<float>(eq, val) = w.at<float>(val);
A.at<float>(eq, 256 + i) = -w.at<float>(val);
B.at<float>(eq, 0) = w.at<float>(val) * log(times[j]);
eq++;
}
}
A.at<float>(eq, 128) = 1;
eq++;
for(int i = 0; i < 254; i++) {
A.at<float>(eq, i) = lambda * w.at<float>(i + 1);
A.at<float>(eq, i + 1) = -2 * lambda * w.at<float>(i + 1);
A.at<float>(eq, i + 2) = lambda * w.at<float>(i + 1);
eq++;
}
Mat solution;
solve(A, B, solution, DECOMP_SVD);
solution.rowRange(0, 256).copyTo(response.col(channel));
}
exp(response, response);
}
int getSamples() const { return samples; }
void setSamples(int val) { samples = val; }
float getLambda() const { return lambda; }
void setLambda(float val) { lambda = val; }
void write(FileStorage& fs) const
{
fs << "name" << name
<< "samples" << samples
<< "lambda" << lambda;
}
void read(const FileNode& fn)
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
samples = fn["samples"];
lambda = fn["lambda"];
}
protected:
String name;
int samples;
float lambda;
Mat w;
};
Ptr<CalibrateDebevec> createCalibrateDebevec(int samples, float lambda)
{
return new CalibrateDebevecImpl(samples, lambda);
}
}
\ No newline at end of file
......@@ -116,7 +116,7 @@ static void fastNlMeansDenoisingMultiCheckPreconditions(
int imgToDenoiseIndex, int temporalWindowSize,
int templateWindowSize, int searchWindowSize)
{
int src_imgs_size = (int)srcImgs.size();
int src_imgs_size = static_cast<int>(srcImgs.size());
if (src_imgs_size == 0) {
CV_Error(Error::StsBadArg, "Input images vector should not be empty!");
}
......@@ -198,7 +198,7 @@ void cv::fastNlMeansDenoisingColoredMulti( InputArrayOfArrays _srcImgs, OutputAr
_dst.create(srcImgs[0].size(), srcImgs[0].type());
Mat dst = _dst.getMat();
int src_imgs_size = (int)srcImgs.size();
int src_imgs_size = static_cast<int>(srcImgs.size());
if (srcImgs[0].type() != CV_8UC3) {
CV_Error(Error::StsBadArg, "Type of input images should be CV_8UC3!");
......
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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 "hdr_common.hpp"
namespace cv
{
void checkImageDimensions(const std::vector<Mat>& images)
{
CV_Assert(!images.empty());
int width = images[0].cols;
int height = images[0].rows;
int type = images[0].type();
for(size_t i = 0; i < images.size(); i++) {
CV_Assert(images[i].cols == width && images[i].rows == height);
CV_Assert(images[i].type() == type);
}
}
Mat tringleWeights()
{
Mat w(256, 3, CV_32F);
for(int i = 0; i < 256; i++) {
for(int j = 0; j < 3; j++) {
w.at<float>(i, j) = i < 128 ? i + 1.0f : 256.0f - i;
}
}
return w;
}
};
\ No newline at end of file
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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*/
#ifndef __OPENCV_HDR_COMMON_HPP__
#define __OPENCV_HDR_COMMON_HPP__
#include "precomp.hpp"
#include "opencv2/photo.hpp"
namespace cv
{
void checkImageDimensions(const std::vector<Mat>& images);
Mat tringleWeights();
};
#endif
\ No newline at end of file
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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"
#include <iostream>
namespace cv
{
class MergeDebevecImpl : public MergeDebevec
{
public:
MergeDebevecImpl() :
name("MergeDebevec"),
weights(tringleWeights())
{
}
void process(InputArrayOfArrays src, OutputArray dst, const std::vector<float>& times, InputArray input_response)
{
std::vector<Mat> images;
src.getMatVector(images);
dst.create(images[0].size(), CV_MAKETYPE(CV_32F, images[0].channels()));
Mat result = dst.getMat();
CV_Assert(images.size() == times.size());
CV_Assert(images[0].depth() == CV_8U);
checkImageDimensions(images);
Mat response = input_response.getMat();
CV_Assert(response.rows == 256 && response.cols >= images[0].channels());
Mat log_response;
log(response, log_response);
std::vector<float> exp_times(times.size());
for(size_t i = 0; i < exp_times.size(); i++) {
exp_times[i] = logf(times[i]);
}
int channels = images[0].channels();
float *res_ptr = result.ptr<float>();
for(size_t pos = 0; pos < result.total(); pos++, res_ptr += channels) {
std::vector<float> sum(channels, 0);
float weight_sum = 0;
for(size_t im = 0; im < images.size(); im++) {
uchar *img_ptr = images[im].ptr() + channels * pos;
float w = 0;
for(int channel = 0; channel < channels; channel++) {
w += weights.at<float>(img_ptr[channel]);
}
w /= channels;
weight_sum += w;
for(int channel = 0; channel < channels; channel++) {
sum[channel] += w * (log_response.at<float>(img_ptr[channel], channel) - exp_times[im]);
}
}
for(int channel = 0; channel < channels; channel++) {
res_ptr[channel] = exp(sum[channel] / weight_sum);
}
}
}
void process(InputArrayOfArrays src, OutputArray dst, const std::vector<float>& times)
{
Mat response(256, 3, CV_32F);
for(int i = 0; i < 256; i++) {
for(int j = 0; j < 3; j++) {
response.at<float>(i, j) = max(i, 1);
}
}
process(src, dst, times, response);
}
protected:
String name;
Mat weights;
};
Ptr<MergeDebevec> createMergeDebevec()
{
return new MergeDebevecImpl;
}
class MergeMertensImpl : public MergeMertens
{
public:
MergeMertensImpl(float wcon, float wsat, float wexp) :
wcon(wcon),
wsat(wsat),
wexp(wexp),
name("MergeMertens")
{
}
void process(InputArrayOfArrays src, OutputArrayOfArrays dst, const std::vector<float>& times, InputArray response)
{
process(src, dst);
}
void process(InputArrayOfArrays src, OutputArray dst)
{
std::vector<Mat> images;
src.getMatVector(images);
checkImageDimensions(images);
std::vector<Mat> weights(images.size());
Mat weight_sum = Mat::zeros(images[0].size(), CV_32FC1);
for(size_t im = 0; im < images.size(); im++) {
Mat img, gray, contrast, saturation, wellexp;
std::vector<Mat> channels(3);
images[im].convertTo(img, CV_32FC3, 1.0/255.0);
cvtColor(img, gray, COLOR_RGB2GRAY);
split(img, channels);
Laplacian(gray, contrast, CV_32F);
contrast = abs(contrast);
Mat mean = (channels[0] + channels[1] + channels[2]) / 3.0f;
saturation = Mat::zeros(channels[0].size(), CV_32FC1);
for(int i = 0; i < 3; i++) {
Mat deviation = channels[i] - mean;
pow(deviation, 2.0, deviation);
saturation += deviation;
}
sqrt(saturation, saturation);
wellexp = Mat::ones(gray.size(), CV_32FC1);
for(int i = 0; i < 3; i++) {
Mat exp = channels[i] - 0.5f;
pow(exp, 2, exp);
exp = -exp / 0.08;
wellexp = wellexp.mul(exp);
}
pow(contrast, wcon, contrast);
pow(saturation, wsat, saturation);
pow(wellexp, wexp, wellexp);
weights[im] = contrast;
weights[im] = weights[im].mul(saturation);
weights[im] = weights[im].mul(wellexp);
weight_sum += weights[im];
}
int maxlevel = static_cast<int>(logf(static_cast<float>(max(images[0].rows, images[0].cols))) / logf(2.0)) - 1;
std::vector<Mat> res_pyr(maxlevel + 1);
for(size_t im = 0; im < images.size(); im++) {
weights[im] /= weight_sum;
Mat img;
images[im].convertTo(img, CV_32FC3, 1/255.0);
std::vector<Mat> img_pyr, weight_pyr;
buildPyramid(img, img_pyr, maxlevel);
buildPyramid(weights[im], weight_pyr, maxlevel);
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++) {
std::vector<Mat> channels(3);
split(img_pyr[lvl], channels);
for(int i = 0; i < 3; i++) {
channels[i] = channels[i].mul(weight_pyr[lvl]);
}
merge(channels, img_pyr[lvl]);
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;
}
dst.create(images[0].size(), CV_32FC3);
res_pyr[0].copyTo(dst.getMat());
}
float getContrastWeight() const { return wcon; }
void setContrastWeight(float val) { wcon = val; }
float getSaturationWeight() const { return wsat; }
void setSaturationWeight(float val) { wsat = val; }
float getExposureWeight() const { return wexp; }
void setExposureWeight(float val) { wexp = val; }
void write(FileStorage& fs) const
{
fs << "name" << name
<< "contrast_weight" << wcon
<< "saturation_weight" << wsat
<< "exposure_weight" << wexp;
}
void read(const FileNode& fn)
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
wcon = fn["contrast_weight"];
wsat = fn["saturation_weight"];
wexp = fn["exposure_weight"];
}
protected:
String name;
float wcon, wsat, wexp;
};
Ptr<MergeMertens> createMergeMertens(float wcon, float wsat, float wexp)
{
return new MergeMertensImpl(wcon, wsat, wexp);
}
}
\ No newline at end of file
......@@ -132,7 +132,7 @@ public:
Mat map;
log(gray_img + 1.0f, map);
Mat div;
pow(gray_img / (float)max, logf(bias) / logf(0.5f), 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);
map = map.mul(1.0f / gray_img);
......@@ -210,7 +210,7 @@ public:
double min, max;
minMaxLoc(map_img, &min, &max);
float scale = contrast / (float)(max - min);
float scale = contrast / static_cast<float>(max - min);
exp(map_img * (scale - 1.0f) + log_img, map_img);
log_img.release();
......@@ -294,22 +294,22 @@ public:
Mat log_img;
log(gray_img, log_img);
float log_mean = (float)sum(log_img)[0] / log_img.total();
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 = (float)((log_max - log_mean) / (log_max - log_min));
float map_key = 0.3f + 0.7f * pow((float)key, 1.4f);
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 = (float)mean(gray_img)[0];
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 * (float)chan_mean[i] + (1.0f - color_adapt) * gray_mean;
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);
......
......@@ -58,7 +58,35 @@ void checkEqual(Mat img0, Mat img1, double threshold)
{
double max = 1.0;
minMaxLoc(abs(img0 - img1), NULL, &max);
ASSERT_FALSE(max > threshold);
ASSERT_FALSE(max > threshold) << max;
}
void loadExposureSeq(String path, vector<Mat>& images, vector<float>& times = vector<float>())
{
ifstream list_file(path + "list.txt");
ASSERT_TRUE(list_file.is_open());
string name;
float val;
while(list_file >> name >> val) {
Mat img = imread(path + name);
ASSERT_FALSE(img.empty()) << "Could not load input image " << path + name;
images.push_back(img);
times.push_back(1 / val);
}
list_file.close();
}
void loadResponseCSV(String path, Mat& response)
{
response = Mat(256, 3, CV_32F);
ifstream resp_file(path);
for(int i = 0; i < 256; i++) {
for(int channel = 0; channel < 3; channel++) {
resp_file >> response.at<float>(i, channel);
resp_file.ignore(1);
}
}
resp_file.close();
}
TEST(Photo_Tonemap, regression)
......@@ -90,130 +118,85 @@ TEST(Photo_Tonemap, regression)
Ptr<TonemapReinhardDevlin> reinhard_devlin = createTonemapReinhardDevlin(gamma);
reinhard_devlin->process(img, result);
loadImage(test_path + "reinhard_devlin.png", expected);
loadImage(test_path + "reinharddevlin.png", expected);
result.convertTo(result, CV_8UC3, 255);
checkEqual(result, expected, 0);
}
TEST(Photo_AlignMTB, regression)
{
const int TESTS_COUNT = 100;
string folder = string(cvtest::TS::ptr()->get_data_path()) + "shared/";
string file_name = folder + "lena.png";
Mat img;
loadImage(file_name, img);
cvtColor(img, img, COLOR_RGB2GRAY);
int max_bits = 5;
int max_shift = 32;
srand(static_cast<unsigned>(time(0)));
int errors = 0;
//void loadExposureSeq(String fuse_path, vector<Mat>& images, vector<float>& times = vector<float>())
//{
// ifstream list_file(fuse_path + "list.txt");
// ASSERT_TRUE(list_file.is_open());
// string name;
// float val;
// while(list_file >> name >> val) {
// Mat img = imread(fuse_path + name);
// ASSERT_FALSE(img.empty()) << "Could not load input image " << fuse_path + name;
// images.push_back(img);
// times.push_back(1 / val);
// }
// list_file.close();
//}
////
////TEST(Photo_MergeMertens, regression)
////{
//// string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
//// string fuse_path = test_path + "fusion/";
////
//// vector<Mat> images;
//// loadExposureSeq(fuse_path, images);
////
//// MergeMertens merge;
////
//// Mat result, expected;
//// loadImage(test_path + "exp_fusion.png", expected);
//// merge.process(images, result);
//// result.convertTo(result, CV_8UC3, 255);
//// checkEqual(expected, result, 0);
////}
//
//TEST(Photo_Debevec, regression)
//{
// string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
// string fuse_path = test_path + "fusion/";
//
// vector<float> times;
// vector<Mat> images;
//
// loadExposureSeq(fuse_path, images, times);
//
// Mat response, expected(256, 3, CV_32F);
// ifstream resp_file(test_path + "response.csv");
// for(int i = 0; i < 256; i++) {
// for(int channel = 0; channel < 3; channel++) {
// resp_file >> expected.at<float>(i, channel);
// resp_file.ignore(1);
// }
// }
// resp_file.close();
//
// CalibrateDebevec calib;
// MergeDebevec merge;
//
// //calib.process(images, response, times);
// //checkEqual(expected, response, 0.001);
// //
// Mat result;
// loadImage(test_path + "no_calibration.hdr", expected);
// merge.process(images, result, times);
// checkEqual(expected, result, 0.01);
//
// //loadImage(test_path + "rle.hdr", expected);
// //merge.process(images, result, times, response);
// //checkEqual(expected, result, 0.01);
//}
//
//TEST(Photo_Tonemap, regression)
//{
// initModule_photo();
// string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/tonemap/";
// Mat img;
// loadImage(test_path + "../rle.hdr", img);
//
// vector<String> algorithms;
// Algorithm::getList(algorithms);
// for(size_t i = 0; i < algorithms.size(); i++) {
// String str = algorithms[i];
// size_t dot = str.find('.');
// if(dot != String::npos && str.substr(0, dot).compare("Tonemap") == 0) {
// String algo_name = str.substr(dot + 1, str.size());
// Mat expected;
// loadImage(test_path + algo_name.toLowerCase() + ".png", expected);
// Ptr<Tonemap> mapper = Tonemap::create(algo_name);
// ASSERT_FALSE(mapper.empty()) << algo_name;
// Mat result;
// mapper->process(img, result);
// result.convertTo(result, CV_8UC3, 255);
// checkEqual(expected, result, 0);
// }
// }
////}
////
////TEST(Photo_AlignMTB, regression)
////{
//// const int TESTS_COUNT = 100;
//// string folder = string(cvtest::TS::ptr()->get_data_path()) + "shared/";
////
//// string file_name = folder + "lena.png";
//// Mat img = imread(file_name);
//// ASSERT_FALSE(img.empty()) << "Could not load input image " << file_name;
//// cvtColor(img, img, COLOR_RGB2GRAY);
////
//// int max_bits = 5;
//// int max_shift = 32;
//// srand(static_cast<unsigned>(time(0)));
//// int errors = 0;
////
//// AlignMTB align(max_bits);
////
//// for(int i = 0; i < TESTS_COUNT; i++) {
//// Point shift(rand() % max_shift, rand() % max_shift);
//// Mat res;
//// align.shiftMat(img, shift, res);
//// Point calc = align.getExpShift(img, res);
//// errors += (calc != -shift);
//// }
//// ASSERT_TRUE(errors < 5);
////}
Ptr<AlignMTB> align = createAlignMTB(max_bits);
for(int i = 0; i < TESTS_COUNT; i++) {
Point shift(rand() % max_shift, rand() % max_shift);
Mat res;
align->shiftMat(img, res, shift);
Point calc;
align->calculateShift(img, res, calc);
errors += (calc != -shift);
}
ASSERT_TRUE(errors < 5) << errors << " errors";
}
TEST(Photo_MergeMertens, regression)
{
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
vector<Mat> images;
loadExposureSeq(test_path + "exposures/", images);
Ptr<MergeMertens> merge = createMergeMertens();
Mat result, expected;
loadImage(test_path + "merge/mertens.png", expected);
merge->process(images, result);
result.convertTo(result, CV_8UC3, 255);
checkEqual(expected, result, 0);
}
TEST(Photo_MergeDebevec, regression)
{
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
vector<Mat> images;
vector<float> times;
Mat response;
loadExposureSeq(test_path + "exposures/", images, times);
loadResponseCSV(test_path + "exposures/response.csv", response);
Ptr<MergeDebevec> merge = createMergeDebevec();
Mat result, expected;
loadImage(test_path + "merge/debevec.exr", expected);
merge->process(images, result, times, response);
checkEqual(expected, result, 1e-3f);
}
TEST(Photo_CalibrateDebevec, regression)
{
string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
vector<Mat> images;
vector<float> times;
Mat expected, response;
loadExposureSeq(test_path + "exposures/", images, times);
loadResponseCSV(test_path + "calibrate/debevec.csv", expected);
Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
srand(1);
calibrate->process(images, response, times);
checkEqual(expected, response, 1e-3f);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment