parent
258b98d15b
commit
4d2ea847fa
5 changed files with 465 additions and 735 deletions
@ -1,161 +0,0 @@ |
||||
/*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" |
||||
|
||||
namespace cv |
||||
{ |
||||
|
||||
static 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; |
||||
} |
||||
} |
||||
|
||||
static 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]); |
||||
} |
||||
} |
||||
|
||||
static 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 += (int)ptr[median]; |
||||
median++; |
||||
} |
||||
return median; |
||||
} |
||||
|
||||
static 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); |
||||
} |
||||
|
||||
void shiftMat(InputArray _src, Point shift, OutputArray _dst)
|
||||
{ |
||||
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)); |
||||
} |
||||
|
||||
Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_range) |
||||
{ |
||||
Mat img0 = _img0.getMat(); |
||||
Mat img1 = _img1.getMat(); |
||||
CV_Assert(img0.type() == CV_8UC1 && img1.type() == CV_8UC1); |
||||
CV_Assert(img0.size() == img0.size()); |
||||
int maxlevel = (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);
|
||||
|
||||
Point shift(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, test_shift, shifted_tb2); |
||||
shiftMat(eb2, test_shift, shifted_eb2); |
||||
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; |
||||
} |
||||
return shift; |
||||
} |
||||
|
||||
}; |
@ -1,294 +0,0 @@ |
||||
/*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 "opencv2/photo.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
|
||||
#include <iostream> |
||||
|
||||
namespace cv |
||||
{ |
||||
|
||||
static void triangleWeights(float weights[]) |
||||
{ |
||||
for(int i = 0; i < 128; i++) { |
||||
weights[i] = i + 1.0f; |
||||
} |
||||
for(int i = 128; i < 256; i++) { |
||||
weights[i] = 256.0f - i; |
||||
} |
||||
} |
||||
|
||||
static Mat linearResponse() |
||||
{ |
||||
Mat response(256, 1, CV_32F); |
||||
for(int i = 1; i < 256; i++) { |
||||
response.at<float>(i) = logf((float)i); |
||||
} |
||||
response.at<float>(0) = response.at<float>(1); |
||||
return response; |
||||
} |
||||
|
||||
static void modifyCheckResponse(Mat &response) |
||||
{ |
||||
if(response.empty()) { |
||||
response = linearResponse(); |
||||
} |
||||
CV_Assert(response.rows == 256 && (response.cols == 1 || response.cols == 3)); |
||||
response.convertTo(response, CV_32F); |
||||
if(response.cols == 1) { |
||||
Mat result(256, 3, CV_32F); |
||||
for(int i = 0; i < 3; i++) { |
||||
response.copyTo(result.col(i)); |
||||
} |
||||
response = result; |
||||
} |
||||
} |
||||
|
||||
static void checkImages(const std::vector<Mat>& images, bool hdr, const std::vector<float>& _exp_times = std::vector<float>()) |
||||
{ |
||||
CV_Assert(!images.empty()); |
||||
CV_Assert(!hdr || images.size() == _exp_times.size()); |
||||
int width = images[0].cols; |
||||
int height = images[0].rows; |
||||
int channels = images[0].channels(); |
||||
for(size_t i = 0; i < images.size(); i++) { |
||||
|
||||
CV_Assert(images[i].cols == width && images[i].rows == height); |
||||
CV_Assert(images[i].channels() == channels && images[i].depth() == CV_8U); |
||||
} |
||||
} |
||||
|
||||
void alignImages(InputArrayOfArrays _src, std::vector<Mat>& dst) |
||||
{ |
||||
std::vector<Mat> src; |
||||
_src.getMatVector(src); |
||||
checkImages(src, false); |
||||
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 = getExpShift(gray_base, gray); |
||||
shiftMat(src[i], shift, dst[i]); |
||||
} |
||||
} |
||||
|
||||
void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst, Mat response) |
||||
{ |
||||
std::vector<Mat> images; |
||||
_images.getMatVector(images); |
||||
checkImages(images, true, _exp_times); |
||||
modifyCheckResponse(response); |
||||
_dst.create(images[0].size(), CV_MAKETYPE(CV_32F, images[0].channels())); |
||||
Mat result = _dst.getMat(); |
||||
|
||||
std::vector<float> exp_times(_exp_times.size()); |
||||
for(size_t i = 0; i < exp_times.size(); i++) { |
||||
exp_times[i] = logf(_exp_times[i]); |
||||
} |
||||
|
||||
float weights[256]; |
||||
triangleWeights(weights); |
||||
|
||||
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[img_ptr[channel]]; |
||||
} |
||||
w /= channels; |
||||
weight_sum += w; |
||||
for(int channel = 0; channel < channels; channel++) { |
||||
sum[channel] += w * (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 exposureFusion(InputArrayOfArrays _images, OutputArray _dst, float wc, float ws, float we) |
||||
{ |
||||
std::vector<Mat> images; |
||||
_images.getMatVector(images); |
||||
checkImages(images, false); |
||||
|
||||
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, wc, contrast); |
||||
pow(saturation, ws, saturation); |
||||
pow(wellexp, we, 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); |
||||
Mat result = _dst.getMat(); |
||||
res_pyr[0].copyTo(result); |
||||
} |
||||
|
||||
void estimateResponse(InputArrayOfArrays _images, const std::vector<float>& exp_times, OutputArray _dst, int samples, float lambda) |
||||
{ |
||||
std::vector<Mat> images; |
||||
_images.getMatVector(images); |
||||
checkImages(images, true, exp_times); |
||||
_dst.create(256, images[0].channels(), CV_32F); |
||||
Mat response = _dst.getMat(); |
||||
|
||||
float w[256]; |
||||
triangleWeights(w); |
||||
|
||||
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[val]; |
||||
A.at<float>(eq, 256 + i) = -w[val]; |
||||
B.at<float>(eq, 0) = w[val] * log(exp_times[j]);
|
||||
eq++; |
||||
} |
||||
} |
||||
A.at<float>(eq, 128) = 1; |
||||
eq++; |
||||
|
||||
for(int i = 0; i < 254; i++) { |
||||
A.at<float>(eq, i) = lambda * w[i + 1]; |
||||
A.at<float>(eq, i + 1) = -2 * lambda * w[i + 1]; |
||||
A.at<float>(eq, i + 2) = lambda * w[i + 1]; |
||||
eq++; |
||||
} |
||||
Mat solution; |
||||
solve(A, B, solution, DECOMP_SVD); |
||||
solution.rowRange(0, 256).copyTo(response.col(channel)); |
||||
} |
||||
} |
||||
|
||||
}; |
||||
|
Loading…
Reference in new issue