parent
4d2ea847fa
commit
302bf23f82
10 changed files with 966 additions and 132 deletions
@ -0,0 +1,235 @@ |
||||
/*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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,139 @@ |
||||
/*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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,74 @@ |
||||
/*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; |
||||
} |
||||
|
||||
}; |
@ -0,0 +1,58 @@ |
||||
/*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 |
@ -0,0 +1,263 @@ |
||||
/*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); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue