mirror of
https://github.com/opencv/opencv.git
synced 2025-06-11 11:45:30 +08:00
Exposure fusion. Code, tests.
This commit is contained in:
parent
a5e11079d7
commit
0aee5b61e3
@ -96,8 +96,10 @@ CV_EXPORTS_W void fastNlMeansDenoisingColoredMulti( InputArrayOfArrays srcImgs,
|
||||
|
||||
CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, const std::vector<float>& exp_times, OutputArray dst);
|
||||
|
||||
CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, tonemap_algorithms algorithm, std::vector<float>& params = std::vector<float>());
|
||||
CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, tonemap_algorithms algorithm,
|
||||
const std::vector<float>& params = std::vector<float>());
|
||||
|
||||
CV_EXPORTS_W void exposureFusion(InputArrayOfArrays srcImgs, OutputArray dst, float wc = 1, float ws = 1, float we = 0);
|
||||
} // cv
|
||||
|
||||
#endif
|
||||
|
@ -64,14 +64,12 @@ static void generateResponce(float responce[])
|
||||
responce[0] = responce[1];
|
||||
}
|
||||
|
||||
void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst)
|
||||
static void checkImages(std::vector<Mat>& images, bool hdr, const std::vector<float>& _exp_times = std::vector<float>())
|
||||
{
|
||||
std::vector<Mat> images;
|
||||
_images.getMatVector(images);
|
||||
if(images.empty()) {
|
||||
CV_Error(Error::StsBadArg, "Need at least one image");
|
||||
}
|
||||
if(images.size() != _exp_times.size()) {
|
||||
if(hdr && images.size() != _exp_times.size()) {
|
||||
CV_Error(Error::StsBadArg, "Number of images and number of exposure times must be equal.");
|
||||
}
|
||||
int width = images[0].cols;
|
||||
@ -85,8 +83,16 @@ void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, O
|
||||
CV_Error(Error::StsBadArg, "Images must have CV_8UC3 type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst)
|
||||
{
|
||||
std::vector<Mat> images;
|
||||
_images.getMatVector(images);
|
||||
checkImages(images, true, _exp_times);
|
||||
_dst.create(images[0].size(), CV_32FC3);
|
||||
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] = log(_exp_times[i]);
|
||||
@ -122,4 +128,88 @@ void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, O
|
||||
result = result / max;
|
||||
}
|
||||
|
||||
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 = (int)(log((double)max(images[0].rows, images[0].cols)) / log(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);
|
||||
}
|
||||
|
||||
};
|
@ -45,7 +45,8 @@
|
||||
|
||||
namespace cv
|
||||
{
|
||||
static float getParam(std::vector<float>& params, size_t i, float defval)
|
||||
|
||||
static float getParam(const std::vector<float>& params, size_t i, float defval)
|
||||
{
|
||||
if(params.size() > i) {
|
||||
return params[i];
|
||||
@ -54,7 +55,8 @@ namespace cv
|
||||
}
|
||||
|
||||
}
|
||||
static void DragoMap(Mat& src_img, Mat &dst_img, std::vector<float>& params)
|
||||
|
||||
static void DragoMap(Mat& src_img, Mat &dst_img, const std::vector<float>& params)
|
||||
{
|
||||
float bias_value = getParam(params, 1, 0.85f);
|
||||
Mat gray_img;
|
||||
@ -87,7 +89,7 @@ namespace cv
|
||||
merge(channels, dst_img);
|
||||
}
|
||||
|
||||
static void ReinhardDevlinMap(Mat& src_img, Mat &dst_img, std::vector<float>& params)
|
||||
static void ReinhardDevlinMap(Mat& src_img, Mat &dst_img, const std::vector<float>& params)
|
||||
{
|
||||
float intensity = getParam(params, 1, 0.0f);
|
||||
float color_adapt = getParam(params, 2, 0.0f);
|
||||
@ -123,7 +125,7 @@ namespace cv
|
||||
merge(channels, dst_img);
|
||||
}
|
||||
|
||||
static void DurandMap(Mat& src_img, Mat& dst_img, std::vector<float>& params)
|
||||
static void DurandMap(Mat& src_img, Mat& dst_img, const std::vector<float>& params)
|
||||
{
|
||||
float contrast = getParam(params, 1, 4.0f);
|
||||
float sigma_color = getParam(params, 2, 2.0f);
|
||||
@ -154,10 +156,9 @@ namespace cv
|
||||
}
|
||||
|
||||
void tonemap(InputArray _src, OutputArray _dst, tonemap_algorithms algorithm,
|
||||
std::vector<float>& params)
|
||||
const std::vector<float>& params)
|
||||
{
|
||||
typedef void (*tonemap_func)(Mat&, Mat&, std::vector<float>&);
|
||||
const unsigned param_count[TONEMAP_COUNT] = {0, 1, 3, 3};
|
||||
typedef void (*tonemap_func)(Mat&, Mat&, const std::vector<float>&);
|
||||
tonemap_func functions[TONEMAP_COUNT] = {
|
||||
NULL, DragoMap, ReinhardDevlinMap, DurandMap};
|
||||
|
||||
|
@ -47,7 +47,7 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
TEST(Photo_MakeHdr, regression)
|
||||
TEST(Photo_HdrFusion, regression)
|
||||
{
|
||||
string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
|
||||
|
||||
@ -75,6 +75,14 @@ TEST(Photo_MakeHdr, regression)
|
||||
double max = 1.0;
|
||||
minMaxLoc(abs(result - expected), NULL, &max);
|
||||
ASSERT_TRUE(max < 0.01);
|
||||
|
||||
expected_path = folder + "grand_canal_exp_fusion.png";
|
||||
expected = imread(expected_path);
|
||||
ASSERT_FALSE(expected.empty()) << "Could not load input image " << expected_path;
|
||||
exposureFusion(images, result);
|
||||
result.convertTo(result, CV_8UC3, 255);
|
||||
minMaxLoc(abs(result - expected), NULL, &max);
|
||||
ASSERT_FALSE(max > 0);
|
||||
}
|
||||
|
||||
TEST(Photo_Tonemap, regression)
|
||||
|
Loading…
Reference in New Issue
Block a user