pull/1474/head
Alexander Shishkov 12 years ago
commit 3c218717a9
  1. 13
      modules/highgui/src/grfmt_hdr.cpp
  2. 4
      modules/photo/doc/hdr_imaging.rst
  3. 3
      modules/photo/include/opencv2/photo.hpp
  4. 30
      modules/photo/src/calibrate.cpp
  5. 6
      modules/photo/src/hdr_common.cpp
  6. 174
      modules/photo/src/merge.cpp
  7. 4
      modules/photo/src/tonemap.cpp
  8. 10
      modules/photo/test/test_hdr.cpp

@ -90,6 +90,7 @@ bool HdrDecoder::readData(Mat& _img)
} }
RGBE_ReadPixels_RLE(file, const_cast<float*>(img.ptr<float>()), img.cols, img.rows); RGBE_ReadPixels_RLE(file, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);
fclose(file); file = NULL; fclose(file); file = NULL;
if(_img.depth() == img.depth()) { if(_img.depth() == img.depth()) {
img.convertTo(_img, _img.type()); img.convertTo(_img, _img.type());
} else { } else {
@ -123,10 +124,16 @@ HdrEncoder::~HdrEncoder()
bool HdrEncoder::write( const Mat& _img, const std::vector<int>& params ) bool HdrEncoder::write( const Mat& _img, const std::vector<int>& params )
{ {
CV_Assert(_img.channels() == 3);
Mat img; Mat img;
if(_img.depth() != CV_32F) { CV_Assert(img.channels() == 3 || img.channels() == 1);
_img.convertTo(img, CV_32FC3, 1/255.0f); if(img.channels() == 1) {
std::vector<Mat> splitted(3, _img);
merge(splitted, img);
} else {
_img.copyTo(img);
}
if(img.depth() != CV_32F) {
img.convertTo(img, CV_32FC3, 1/255.0f);
} }
CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE); CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE);
FILE *fout = fopen(m_filename.c_str(), "wb"); FILE *fout = fopen(m_filename.c_str(), "wb");

@ -194,7 +194,7 @@ Recovers camera response.
:param src: vector of input images :param src: vector of input images
:param dst: matrix with calculated camera response, one column per channel :param dst: matrix with calculated camera response
:param times: vector of exposure time values for each image :param times: vector of exposure time values for each image
@ -232,7 +232,7 @@ Merges images.
:param times: vector of exposure time values for each image :param times: vector of exposure time values for each image
:param response: matrix with camera response, one column per channel :param response: one-column matrix with camera response
MergeDebevec MergeDebevec
-------- --------

@ -206,6 +206,9 @@ public:
CV_WRAP virtual int getSamples() const = 0; CV_WRAP virtual int getSamples() const = 0;
CV_WRAP virtual void setSamples(int samples) = 0; CV_WRAP virtual void setSamples(int samples) = 0;
CV_WRAP virtual bool getTest() const = 0;
CV_WRAP virtual void setTest(bool val) = 0;
}; };
CV_EXPORTS_W Ptr<CalibrateDebevec> createCalibrateDebevec(int samples = 50, float lambda = 10.0f); CV_EXPORTS_W Ptr<CalibrateDebevec> createCalibrateDebevec(int samples = 50, float lambda = 10.0f);

@ -55,7 +55,8 @@ public:
samples(samples), samples(samples),
lambda(lambda), lambda(lambda),
name("CalibrateDebevec"), name("CalibrateDebevec"),
w(tringleWeights()) w(tringleWeights()),
test(false)
{ {
} }
@ -63,14 +64,19 @@ public:
{ {
std::vector<Mat> images; std::vector<Mat> images;
src.getMatVector(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.size() == times.size());
CV_Assert(images[0].depth() == CV_8U);
checkImageDimensions(images); checkImageDimensions(images);
CV_Assert(images[0].depth() == CV_8U);
int channels = images[0].channels();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
for(int channel = 0; channel < images[0].channels(); channel++) { dst.create(256, 1, CV_32FCC);
Mat result = dst.getMat();
std::vector<Mat> result_split(channels);
for(int channel = 0; channel < channels; channel++) {
Mat A = Mat::zeros(samples * images.size() + 257, 256 + samples, CV_32F); Mat A = Mat::zeros(samples * images.size() + 257, 256 + samples, CV_32F);
Mat B = Mat::zeros(A.rows, 1, CV_32F); Mat B = Mat::zeros(A.rows, 1, CV_32F);
@ -78,6 +84,9 @@ public:
for(int i = 0; i < samples; i++) { for(int i = 0; i < samples; i++) {
int pos = 3 * (rand() % images[0].total()) + channel; int pos = 3 * (rand() % images[0].total()) + channel;
if(test) {
pos = 3 * i + channel;
}
for(size_t j = 0; j < images.size(); j++) { for(size_t j = 0; j < images.size(); j++) {
int val = (images[j].ptr() + pos)[0]; int val = (images[j].ptr() + pos)[0];
@ -98,11 +107,15 @@ public:
} }
Mat solution; Mat solution;
solve(A, B, solution, DECOMP_SVD); solve(A, B, solution, DECOMP_SVD);
solution.rowRange(0, 256).copyTo(response.col(channel)); solution.rowRange(0, 256).copyTo(result_split[channel]);
} }
exp(response, response); merge(result_split, result);
exp(result, result);
} }
bool getTest() const { return test; }
void setTest(bool val) { test = val; }
int getSamples() const { return samples; } int getSamples() const { return samples; }
void setSamples(int val) { samples = val; } void setSamples(int val) { samples = val; }
@ -128,6 +141,7 @@ protected:
String name; String name;
int samples; int samples;
float lambda; float lambda;
bool test;
Mat w; Mat w;
}; };

@ -61,11 +61,9 @@ void checkImageDimensions(const std::vector<Mat>& images)
Mat tringleWeights() Mat tringleWeights()
{ {
Mat w(256, 3, CV_32F); Mat w(256, 1, CV_32F);
for(int i = 0; i < 256; i++) { for(int i = 0; i < 256; i++) {
for(int j = 0; j < 3; j++) { w.at<float>(i) = i < 128 ? i + 1.0f : 256.0f - i;
w.at<float>(i, j) = i < 128 ? i + 1.0f : 256.0f - i;
}
} }
return w; return w;
} }

@ -61,62 +61,87 @@ public:
{ {
std::vector<Mat> images; std::vector<Mat> images;
src.getMatVector(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.size() == times.size());
CV_Assert(images[0].depth() == CV_8U);
checkImageDimensions(images); checkImageDimensions(images);
CV_Assert(images[0].depth() == CV_8U);
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(); int channels = images[0].channels();
float *res_ptr = result.ptr<float>(); Size size = images[0].size();
for(size_t pos = 0; pos < result.total(); pos++, res_ptr += channels) { int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
std::vector<float> sum(channels, 0); dst.create(images[0].size(), CV_32FCC);
float weight_sum = 0; Mat result = dst.getMat();
for(size_t im = 0; im < images.size(); im++) {
uchar *img_ptr = images[im].ptr() + channels * pos; Mat response = input_response.getMat();
float w = 0;
for(int channel = 0; channel < channels; channel++) { if(response.empty()) {
w += weights.at<float>(img_ptr[channel]); response = linearResponse(channels);
} }
w /= channels; log(response, response);
weight_sum += w; CV_Assert(response.rows == 256 && response.cols == 1 &&
for(int channel = 0; channel < channels; channel++) { response.channels() == channels);
sum[channel] += w * (log_response.at<float>(img_ptr[channel], channel) - exp_times[im]);
Mat exp_values(times);
log(exp_values, exp_values);
result = Mat::zeros(size, CV_32FCC);
std::vector<Mat> result_split;
split(result, result_split);
Mat weight_sum = Mat::zeros(size, CV_32F);
for(size_t i = 0; i < images.size(); i++) {
std::vector<Mat> splitted;
split(images[i], splitted);
Mat w = Mat::zeros(size, CV_32F);
for(int c = 0; c < channels; c++) {
LUT(splitted[c], weights, splitted[c]);
w += splitted[c];
} }
w /= channels;
Mat response_img;
LUT(images[i], response, response_img);
split(response_img, splitted);
for(int c = 0; c < channels; c++) {
result_split[c] += w.mul(splitted[c] - exp_values.at<float>(i));
} }
for(int channel = 0; channel < channels; channel++) { weight_sum += w;
res_ptr[channel] = exp(sum[channel] / weight_sum);
} }
weight_sum = 1.0f / weight_sum;
for(int c = 0; c < channels; c++) {
result_split[c] = result_split[c].mul(weight_sum);
} }
merge(result_split, result);
exp(result, result);
} }
void process(InputArrayOfArrays src, OutputArray dst, const std::vector<float>& times) void process(InputArrayOfArrays src, OutputArray dst, const std::vector<float>& times)
{ {
Mat response(256, 3, CV_32F); process(src, dst, times, Mat());
for(int i = 0; i < 256; i++) {
for(int j = 0; j < 3; j++) {
response.at<float>(i, j) = static_cast<float>(max(i, 1));
}
}
process(src, dst, times, response);
} }
protected: protected:
String name; String name;
Mat weights; Mat weights;
Mat linearResponse(int channels)
{
Mat single_response = Mat(256, 1, CV_32F);
for(int i = 1; i < 256; i++) {
single_response.at<float>(i) = static_cast<float>(i);
}
single_response.at<float>(0) = static_cast<float>(1);
std::vector<Mat> splitted(channels);
for(int c = 0; c < channels; c++) {
splitted[c] = single_response;
}
Mat result;
merge(splitted, result);
return result;
}
}; };
Ptr<MergeDebevec> createMergeDebevec() Ptr<MergeDebevec> createMergeDebevec()
@ -146,33 +171,48 @@ public:
src.getMatVector(images); src.getMatVector(images);
checkImageDimensions(images); checkImageDimensions(images);
int channels = images[0].channels();
CV_Assert(channels == 1 || channels == 3);
Size size = images[0].size();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
std::vector<Mat> weights(images.size()); std::vector<Mat> weights(images.size());
Mat weight_sum = Mat::zeros(images[0].size(), CV_32FC1); Mat weight_sum = Mat::zeros(size, CV_32F);
for(size_t im = 0; im < images.size(); im++) {
for(size_t i = 0; i < images.size(); i++) {
Mat img, gray, contrast, saturation, wellexp; Mat img, gray, contrast, saturation, wellexp;
std::vector<Mat> channels(3); std::vector<Mat> splitted(channels);
images[im].convertTo(img, CV_32FC3, 1.0/255.0); images[i].convertTo(img, CV_32F, 1.0f/255.0f);
if(channels == 3) {
cvtColor(img, gray, COLOR_RGB2GRAY); cvtColor(img, gray, COLOR_RGB2GRAY);
split(img, channels); } else {
img.copyTo(gray);
}
split(img, splitted);
Laplacian(gray, contrast, CV_32F); Laplacian(gray, contrast, CV_32F);
contrast = abs(contrast); contrast = abs(contrast);
Mat mean = (channels[0] + channels[1] + channels[2]) / 3.0f; Mat mean = Mat::zeros(size, CV_32F);
saturation = Mat::zeros(channels[0].size(), CV_32FC1); for(int c = 0; c < channels; c++) {
for(int i = 0; i < 3; i++) { mean += splitted[c];
Mat deviation = channels[i] - mean; }
pow(deviation, 2.0, deviation); 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);
saturation += deviation; saturation += deviation;
} }
sqrt(saturation, saturation); sqrt(saturation, saturation);
wellexp = Mat::ones(gray.size(), CV_32FC1); wellexp = Mat::ones(size, CV_32F);
for(int i = 0; i < 3; i++) { for(int c = 0; c < channels; c++) {
Mat exp = channels[i] - 0.5f; Mat exp = splitted[c] - 0.5f;
pow(exp, 2, exp); pow(exp, 2.0f, exp);
exp = -exp / 0.08; exp = -exp / 0.08f;
wellexp = wellexp.mul(exp); wellexp = wellexp.mul(exp);
} }
@ -180,33 +220,37 @@ public:
pow(saturation, wsat, saturation); pow(saturation, wsat, saturation);
pow(wellexp, wexp, wellexp); pow(wellexp, wexp, wellexp);
weights[im] = contrast; weights[i] = contrast;
weights[im] = weights[im].mul(saturation); if(channels == 3) {
weights[im] = weights[im].mul(wellexp); weights[i] = weights[i].mul(saturation);
weight_sum += weights[im]; }
weights[i] = weights[i].mul(wellexp);
weight_sum += weights[i];
} }
int maxlevel = static_cast<int>(logf(static_cast<float>(max(images[0].rows, images[0].cols))) / logf(2.0)) - 1; int maxlevel = static_cast<int>(logf(static_cast<float>(min(size.width, size.height))) / logf(2.0f));
std::vector<Mat> res_pyr(maxlevel + 1); std::vector<Mat> res_pyr(maxlevel + 1);
for(size_t im = 0; im < images.size(); im++) { for(size_t i = 0; i < images.size(); i++) {
weights[im] /= weight_sum; weights[i] /= weight_sum;
Mat img; Mat img;
images[im].convertTo(img, CV_32FC3, 1/255.0); images[i].convertTo(img, CV_32F, 1.0f/255.0f);
std::vector<Mat> img_pyr, weight_pyr; std::vector<Mat> img_pyr, weight_pyr;
buildPyramid(img, img_pyr, maxlevel); buildPyramid(img, img_pyr, maxlevel);
buildPyramid(weights[im], weight_pyr, maxlevel); buildPyramid(weights[i], weight_pyr, maxlevel);
for(int lvl = 0; lvl < maxlevel; lvl++) { for(int lvl = 0; lvl < maxlevel; lvl++) {
Mat up; Mat up;
pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size()); pyrUp(img_pyr[lvl + 1], up, img_pyr[lvl].size());
img_pyr[lvl] -= up; img_pyr[lvl] -= up;
} }
for(int lvl = 0; lvl <= maxlevel; lvl++) { for(int lvl = 0; lvl <= maxlevel; lvl++) {
std::vector<Mat> channels(3); std::vector<Mat> splitted(channels);
split(img_pyr[lvl], channels); split(img_pyr[lvl], splitted);
for(int i = 0; i < 3; i++) { for(int c = 0; c < channels; c++) {
channels[i] = channels[i].mul(weight_pyr[lvl]); splitted[c] = splitted[c].mul(weight_pyr[lvl]);
} }
merge(channels, img_pyr[lvl]); merge(splitted, img_pyr[lvl]);
if(res_pyr[lvl].empty()) { if(res_pyr[lvl].empty()) {
res_pyr[lvl] = img_pyr[lvl]; res_pyr[lvl] = img_pyr[lvl];
} else { } else {
@ -219,7 +263,7 @@ public:
pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size()); pyrUp(res_pyr[lvl], up, res_pyr[lvl - 1].size());
res_pyr[lvl - 1] += up; res_pyr[lvl - 1] += up;
} }
dst.create(images[0].size(), CV_32FC3); dst.create(size, CV_32FCC);
res_pyr[0].copyTo(dst.getMat()); res_pyr[0].copyTo(dst.getMat());
} }

@ -462,8 +462,8 @@ protected:
void signedPow(Mat src, float power, Mat& dst) void signedPow(Mat src, float power, Mat& dst)
{ {
Mat sign = (src > 0); Mat sign = (src > 0);
sign.convertTo(sign, CV_32F, 1/255.0f); sign.convertTo(sign, CV_32F, 1.0f/255.0f);
sign = sign * 2 - 1; sign = sign * 2.0f - 1.0f;
pow(abs(src), power, dst); pow(abs(src), power, dst);
dst = dst.mul(sign); dst = dst.mul(sign);
} }

@ -78,11 +78,11 @@ void loadExposureSeq(String path, vector<Mat>& images, vector<float>& times = DE
void loadResponseCSV(String path, Mat& response) void loadResponseCSV(String path, Mat& response)
{ {
response = Mat(256, 3, CV_32F); response = Mat(256, 1, CV_32FC3);
ifstream resp_file(path.c_str()); ifstream resp_file(path.c_str());
for(int i = 0; i < 256; i++) { for(int i = 0; i < 256; i++) {
for(int channel = 0; channel < 3; channel++) { for(int c = 0; c < 3; c++) {
resp_file >> response.at<float>(i, channel); resp_file >> response.at<Vec3f>(i)[c];
resp_file.ignore(1); resp_file.ignore(1);
} }
} }
@ -187,6 +187,7 @@ TEST(Photo_MergeDebevec, regression)
Mat result, expected; Mat result, expected;
loadImage(test_path + "merge/debevec.exr", expected); loadImage(test_path + "merge/debevec.exr", expected);
merge->process(images, result, times, response); merge->process(images, result, times, response);
imwrite("test.exr", result);
checkEqual(expected, result, 1e-3f); checkEqual(expected, result, 1e-3f);
} }
@ -199,9 +200,8 @@ TEST(Photo_CalibrateDebevec, regression)
Mat expected, response; Mat expected, response;
loadExposureSeq(test_path + "exposures/", images, times); loadExposureSeq(test_path + "exposures/", images, times);
loadResponseCSV(test_path + "calibrate/debevec.csv", expected); loadResponseCSV(test_path + "calibrate/debevec.csv", expected);
Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec(); Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
srand(1); calibrate->setTest(true);
calibrate->process(images, response, times); calibrate->process(images, response, times);
checkEqual(expected, response, 1e-3f); checkEqual(expected, response, 1e-3f);
} }

Loading…
Cancel
Save