From 703cf8cef74273bd4e3b491e25005eb0e314a642 Mon Sep 17 00:00:00 2001 From: Fedor Morozov Date: Sun, 21 Jul 2013 13:48:57 +0400 Subject: [PATCH] Calibration, various changes --- modules/highgui/src/grfmt_hdr.cpp | 19 ++--- modules/highgui/test/test_grfmt.cpp | 4 +- modules/photo/include/opencv2/photo.hpp | 8 +- modules/photo/src/align.cpp | 9 +-- modules/photo/src/hdr_fusion.cpp | 139 +++++++++++++++++++++----------- modules/photo/src/tonemap.cpp | 11 +-- modules/photo/test/test_hdr.cpp | 106 +++++++++++++----------- 7 files changed, 178 insertions(+), 118 deletions(-) diff --git a/modules/highgui/src/grfmt_hdr.cpp b/modules/highgui/src/grfmt_hdr.cpp index cf4a0e8..13886b8 100644 --- a/modules/highgui/src/grfmt_hdr.cpp +++ b/modules/highgui/src/grfmt_hdr.cpp @@ -69,11 +69,13 @@ bool HdrDecoder::readHeader() { file = fopen(m_filename.c_str(), "rb"); if(!file) { - CV_Error(Error::StsError, "HDR decoder: can't open file"); + return false; } RGBE_ReadHeader(file, &m_width, &m_height, NULL); if(m_width <= 0 || m_height <= 0) { - CV_Error(Error::StsError, "HDR decoder: invalid image size"); + fclose(file); + file = NULL; + return false; } return true; } @@ -82,7 +84,9 @@ bool HdrDecoder::readData(Mat& _img) { Mat img(m_height, m_width, CV_32FC3); if(!file) { - readHeader(); + if(!readHeader()) { + return false; + } } RGBE_ReadPixels_RLE(file, const_cast(img.ptr()), img.cols, img.rows); fclose(file); file = NULL; @@ -125,13 +129,10 @@ bool HdrEncoder::write( const Mat& _img, const std::vector& params ) } else { _img.convertTo(img, CV_32FC3, 1/255.0f); } - if(!(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE)) { - CV_Error(Error::StsBadArg, "HDR encoder: wrong compression param"); - } - + CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE); FILE *fout = fopen(m_filename.c_str(), "wb"); if(!fout) { - CV_Error(Error::StsError, "HDR encoder: can't open file"); + return false; } RGBE_WriteHeader(fout, img.cols, img.rows, NULL); @@ -151,7 +152,7 @@ ImageEncoder HdrEncoder::newEncoder() const } bool HdrEncoder::isFormatSupported( int depth ) const { - return depth == CV_32F; + return depth != CV_64F; } } diff --git a/modules/highgui/test/test_grfmt.cpp b/modules/highgui/test/test_grfmt.cpp index 5dae421..36664a9 100644 --- a/modules/highgui/test/test_grfmt.cpp +++ b/modules/highgui/test/test_grfmt.cpp @@ -410,8 +410,8 @@ TEST(Highgui_WebP, encode_decode_lossy_webp) TEST(Highgui_Hdr, regression) { string folder = string(cvtest::TS::ptr()->get_data_path()) + "../cv/hdr/"; - string name_rle = folder + "grand_canal_rle.hdr"; - string name_no_rle = folder + "grand_canal_no_rle.hdr"; + string name_rle = folder + "rle.hdr"; + string name_no_rle = folder + "no_rle.hdr"; Mat img_rle = imread(name_rle, -1); ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle; Mat img_no_rle = imread(name_no_rle, -1); diff --git a/modules/photo/include/opencv2/photo.hpp b/modules/photo/include/opencv2/photo.hpp index b3d3aaf..4b2a87c 100644 --- a/modules/photo/include/opencv2/photo.hpp +++ b/modules/photo/include/opencv2/photo.hpp @@ -94,16 +94,20 @@ CV_EXPORTS_W void fastNlMeansDenoisingColoredMulti( InputArrayOfArrays srcImgs, float h = 3, float hColor = 3, int templateWindowSize = 7, int searchWindowSize = 21); -CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, const std::vector& exp_times, OutputArray dst, bool align = false); +CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, const std::vector& exp_times, OutputArray dst, Mat response = Mat()); CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, int algorithm, const std::vector& params = std::vector()); -CV_EXPORTS_W void exposureFusion(InputArrayOfArrays srcImgs, OutputArray dst, bool align = false, float wc = 1, float ws = 1, float we = 0); +CV_EXPORTS_W void exposureFusion(InputArrayOfArrays srcImgs, OutputArray dst, float wc = 1, float ws = 1, float we = 0); CV_EXPORTS_W void shiftMat(InputArray src, Point shift, OutputArray dst); CV_EXPORTS_W Point getExpShift(InputArray img0, InputArray img1, int max_bits = 6, int exclude_range = 4); + +CV_EXPORTS_W void estimateResponse(InputArrayOfArrays srcImgs, const std::vector& exp_times, OutputArray dst, int samples = 50, float lambda = 10); + +CV_EXPORTS_W void alignImages(InputArrayOfArrays src, std::vector& dst); } // cv #endif diff --git a/modules/photo/src/align.cpp b/modules/photo/src/align.cpp index 6caa191..7261c85 100644 --- a/modules/photo/src/align.cpp +++ b/modules/photo/src/align.cpp @@ -117,12 +117,8 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_ { Mat img0 = _img0.getMat(); Mat img1 = _img1.getMat(); - if(img0.type() != CV_8UC1 || img1.type() != CV_8UC1) { - CV_Error(Error::StsBadArg, "Images must have CV_8UC1 type."); - } - if(img0.size() != img0.size()) { - CV_Error(Error::StsBadArg, "Image dimensions must be equal."); - } + 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); @@ -161,4 +157,5 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_ } return shift; } + }; diff --git a/modules/photo/src/hdr_fusion.cpp b/modules/photo/src/hdr_fusion.cpp index 5f2bf81..dd9b9b0 100644 --- a/modules/photo/src/hdr_fusion.cpp +++ b/modules/photo/src/hdr_fusion.cpp @@ -43,6 +43,8 @@ #include "opencv2/photo.hpp" #include "opencv2/imgproc.hpp" +#include + namespace cv { @@ -56,37 +58,51 @@ static void triangleWeights(float weights[]) } } -static void generateResponce(float responce[]) +static Mat linearResponse() { - for(int i = 0; i < 256; i++) { - responce[i] = log((float)i); + Mat response(256, 1, CV_32F); + for(int i = 1; i < 256; i++) { + response.at(i) = log((float)i); } - responce[0] = responce[1]; + response.at(0) = response.at(1); + return response; } -static void checkImages(std::vector& images, bool hdr, const std::vector& _exp_times = std::vector()) +static void modifyCheckResponse(Mat &response) { - if(images.empty()) { - CV_Error(Error::StsBadArg, "Need at least one image"); + if(response.empty()) { + response = linearResponse(); } - if(hdr && images.size() != _exp_times.size()) { - CV_Error(Error::StsBadArg, "Number of images and number of exposure times must be equal."); + 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(std::vector& images, bool hdr, const std::vector& _exp_times = std::vector()) +{ + 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++) { - if(images[i].cols != width || images[i].rows != height) { - CV_Error(Error::StsBadArg, "Image dimensions must be equal."); - } - if(images[i].type() != CV_8UC3) { - CV_Error(Error::StsBadArg, "Images must have CV_8UC3 type."); - } + CV_Assert(images[i].cols == width && images[i].rows == height); + CV_Assert(images[i].channels() == channels && images[i].depth() == CV_8U); } } -static void alignImages(std::vector& src, std::vector& dst) +void alignImages(InputArrayOfArrays _src, std::vector& dst) { + std::vector src; + _src.getMatVector(src); + checkImages(src, false); dst.resize(src.size()); size_t pivot = src.size() / 2; @@ -105,65 +121,55 @@ static void alignImages(std::vector& src, std::vector& dst) } } -void makeHDR(InputArrayOfArrays _images, const std::vector& _exp_times, OutputArray _dst, bool align) +void makeHDR(InputArrayOfArrays _images, const std::vector& _exp_times, OutputArray _dst, Mat response) { std::vector images; _images.getMatVector(images); checkImages(images, true, _exp_times); - _dst.create(images[0].size(), CV_32FC3); + modifyCheckResponse(response); + _dst.create(images[0].size(), CV_MAKETYPE(CV_32F, images[0].channels())); Mat result = _dst.getMat(); - if(align) { - std::vector new_images; - alignImages(images, new_images); - images = new_images; - } std::vector exp_times(_exp_times.size()); for(size_t i = 0; i < exp_times.size(); i++) { exp_times[i] = log(_exp_times[i]); } - float weights[256], responce[256]; + float weights[256]; triangleWeights(weights); - generateResponce(responce); - - float max = 0; + + int channels = images[0].channels(); float *res_ptr = result.ptr(); - for(size_t pos = 0; pos < result.total(); pos++, res_ptr += 3) { + for(size_t pos = 0; pos < result.total(); pos++, res_ptr += channels) { - float sum[3] = {0, 0, 0}; + std::vector sum(channels, 0); float weight_sum = 0; for(size_t im = 0; im < images.size(); im++) { - uchar *img_ptr = images[im].ptr() + 3 * pos; - float w = (weights[img_ptr[0]] + weights[img_ptr[1]] + - weights[img_ptr[2]]) / 3; + 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 < 3; channel++) { - sum[channel] += w * (responce[img_ptr[channel]] - exp_times[im]); + for(int channel = 0; channel < channels; channel++) { + sum[channel] += w * (response.at(img_ptr[channel], channel) - exp_times[im]); } } - for(int channel = 0; channel < 3; channel++) { + for(int channel = 0; channel < channels; channel++) { res_ptr[channel] = exp(sum[channel] / weight_sum); - if(res_ptr[channel] > max) { - max = res_ptr[channel]; - } } } - result = result / max; + tonemap(result, result, 0); } -void exposureFusion(InputArrayOfArrays _images, OutputArray _dst, bool align, float wc, float ws, float we) +void exposureFusion(InputArrayOfArrays _images, OutputArray _dst, float wc, float ws, float we) { std::vector images; _images.getMatVector(images); checkImages(images, false); - if(align) { - std::vector new_images; - alignImages(images, new_images); - images = new_images; - } std::vector weights(images.size()); Mat weight_sum = Mat::zeros(images[0].size(), CV_32FC1); for(size_t im = 0; im < images.size(); im++) { @@ -242,4 +248,47 @@ void exposureFusion(InputArrayOfArrays _images, OutputArray _dst, bool align, fl res_pyr[0].copyTo(result); } +void estimateResponse(InputArrayOfArrays _images, const std::vector& exp_times, OutputArray _dst, int samples, float lambda) +{ + std::vector 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(eq, val) = w[val]; + A.at(eq, 256 + i) = -w[val]; + B.at(eq, 0) = w[val] * log(exp_times[j]); + eq++; + } + } + A.at(eq, 128) = 1; + eq++; + + for(int i = 0; i < 254; i++) { + A.at(eq, i) = lambda * w[i + 1]; + A.at(eq, i + 1) = -2 * lambda * w[i + 1]; + A.at(eq, i + 2) = lambda * w[i + 1]; + eq++; + } + Mat solution; + solve(A, B, solution, DECOMP_SVD); + solution.rowRange(0, 256).copyTo(response.col(channel)); + } +} + }; \ No newline at end of file diff --git a/modules/photo/src/tonemap.cpp b/modules/photo/src/tonemap.cpp index 1f05586..af2b5d4 100644 --- a/modules/photo/src/tonemap.cpp +++ b/modules/photo/src/tonemap.cpp @@ -163,20 +163,15 @@ void tonemap(InputArray _src, OutputArray _dst, int algorithm, NULL, DragoMap, ReinhardDevlinMap, DurandMap}; Mat src = _src.getMat(); - if(src.empty()) { - CV_Error(Error::StsBadArg, "Empty input image"); - } - if(algorithm < 0 || algorithm >= TONEMAP_COUNT) { - CV_Error(Error::StsBadArg, "Wrong algorithm index"); - } - + CV_Assert(!src.empty()); + CV_Assert(0 <= algorithm && algorithm < TONEMAP_COUNT); _dst.create(src.size(), CV_32FC3); Mat dst = _dst.getMat(); src.copyTo(dst); double min, max; minMaxLoc(dst, &min, &max); - if(max - min < 1e-10f) { + if(max - min < DBL_EPSILON) { return; } dst = (dst - min) / (max - min); diff --git a/modules/photo/test/test_hdr.cpp b/modules/photo/test/test_hdr.cpp index 1db6171..2e3b257 100644 --- a/modules/photo/test/test_hdr.cpp +++ b/modules/photo/test/test_hdr.cpp @@ -43,79 +43,93 @@ #include "test_precomp.hpp" #include #include +#include using namespace cv; using namespace std; +void loadImage(string path, Mat &img) +{ + img = imread(path, -1); + ASSERT_FALSE(img.empty()) << "Could not load input image " << path; +} + +void checkEqual(Mat img0, Mat img1, double threshold) +{ + double max = 1.0; + minMaxLoc(abs(img0 - img1), NULL, &max); + ASSERT_FALSE(max > threshold); +} + TEST(Photo_HdrFusion, regression) { - string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; + string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; + string fuse_path = test_path + "fusion/"; - vectorfile_names(3); - file_names[0] = folder + "grand_canal_1_45.jpg"; - file_names[1] = folder + "grand_canal_1_180.jpg"; - file_names[2] = folder + "grand_canal_1_750.jpg"; - vectorimages(3); - for(int i = 0; i < 3; i++) { - images[i] = imread(file_names[i]); - ASSERT_FALSE(images[i].empty()) << "Could not load input image " << file_names[i]; + vector times; + vector images; + + ifstream list_file(fuse_path + "list.txt"); + 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); } - - string expected_path = folder + "grand_canal_rle.hdr"; - Mat expected = imread(expected_path, -1); - ASSERT_FALSE(expected.empty()) << "Could not load input image " << expected_path; - - vectortimes(3); - times[0] = 1.0f/45.0f; - times[1] = 1.0f/180.0f; - times[2] = 1.0f/750.0f; - + list_file.close(); + + 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(i, channel); + resp_file.ignore(1); + } + } + resp_file.close(); + + estimateResponse(images, times, response); + checkEqual(expected, response, 0.001); + Mat result; + loadImage(test_path + "no_calibration.hdr", expected); makeHDR(images, times, result); - double max = 1.0; - minMaxLoc(abs(result - expected), NULL, &max); - ASSERT_TRUE(max < 0.01); + checkEqual(expected, result, 0.01); + + loadImage(test_path + "rle.hdr", expected); + makeHDR(images, times, result, response); + checkEqual(expected, result, 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; + loadImage(test_path + "exp_fusion.png", expected); exposureFusion(images, result); result.convertTo(result, CV_8UC3, 255); - minMaxLoc(abs(result - expected), NULL, &max); - ASSERT_FALSE(max > 0); + checkEqual(expected, result, 0); } TEST(Photo_Tonemap, regression) { string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; - - vectorfile_names(TONEMAP_COUNT); - file_names[TONEMAP_DRAGO] = folder + "grand_canal_drago_2.2.png"; - file_names[TONEMAP_REINHARD] = folder + "grand_canal_reinhard_2.2.png"; - file_names[TONEMAP_DURAND] = folder + "grand_canal_durand_2.2.png"; - file_names[TONEMAP_LINEAR] = folder + "grand_canal_linear_map_2.2.png"; - vectorimages(TONEMAP_COUNT); for(int i = 0; i < TONEMAP_COUNT; i++) { - images[i] = imread(file_names[i]); - ASSERT_FALSE(images[i].empty()) << "Could not load input image " << file_names[i]; + stringstream stream; + stream << "tonemap" << i << ".png"; + string file_name; + stream >> file_name; + loadImage(folder + "tonemap/" + file_name ,images[i]); } - - string hdr_file_name = folder + "grand_canal_rle.hdr"; - Mat img = imread(hdr_file_name, -1); - ASSERT_FALSE(img.empty()) << "Could not load input image " << hdr_file_name; - + Mat img; + loadImage(folder + "rle.hdr", img); vector param(1); param[0] = 2.2f; - for(int i = TONEMAP_DURAND; i < TONEMAP_COUNT; i++) { + for(int i = 0; i < TONEMAP_COUNT; i++) { Mat result; tonemap(img, result, i, param); result.convertTo(result, CV_8UC3, 255); - double max = 1.0; - minMaxLoc(abs(result - images[i]), NULL, &max); - ASSERT_FALSE(max > 0); + checkEqual(images[i], result, 0); } } @@ -124,7 +138,7 @@ TEST(Photo_Align, regression) const int TESTS_COUNT = 100; string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; - string file_name = folder + "grand_canal_1_45.jpg"; + string file_name = folder + "exp_fusion.png"; Mat img = imread(file_name); ASSERT_FALSE(img.empty()) << "Could not load input image " << file_name; cvtColor(img, img, COLOR_RGB2GRAY); -- 2.7.4