diff --git a/modules/highgui/src/grfmt_hdr.cpp b/modules/highgui/src/grfmt_hdr.cpp index a5aab3057b..288f73d1cd 100644 --- a/modules/highgui/src/grfmt_hdr.cpp +++ b/modules/highgui/src/grfmt_hdr.cpp @@ -92,11 +92,9 @@ bool HdrDecoder::readData(Mat& img) bool HdrDecoder::checkSignature( const String& signature ) const { - if(signature.size() >= (m_signature.size()) && - !memcmp(signature.c_str(), m_signature.c_str(), m_signature.size())) - return true; - if(signature.size() >= (m_signature.size()) && - !memcmp(signature.c_str(), m_signature_alt.c_str(), m_signature_alt.size())) + if(signature.size() >= m_signature.size() && + (!memcmp(signature.c_str(), m_signature.c_str(), m_signature.size()) || + !memcmp(signature.c_str(), m_signature_alt.c_str(), m_signature_alt.size()))) return true; return false; } diff --git a/modules/highgui/src/grfmt_tiff.cpp b/modules/highgui/src/grfmt_tiff.cpp index 51d17cee71..abcd7de41b 100644 --- a/modules/highgui/src/grfmt_tiff.cpp +++ b/modules/highgui/src/grfmt_tiff.cpp @@ -47,6 +47,7 @@ #include "precomp.hpp" #include "grfmt_tiff.hpp" +#include namespace cv { @@ -413,22 +414,13 @@ bool TiffDecoder::readHdrData(Mat& img) size -= strip_size * sizeof(float); } close(); - ptr = img.ptr(); - for(size_t i = 0; i < img.total(); i++, ptr += 3) + if(photometric == PHOTOMETRIC_LOGLUV) { - if(photometric == PHOTOMETRIC_LOGLUV) - { - float r = 3.240479f * ptr[0] + -1.537150f * ptr[1] + -0.498535f * ptr[2]; - float g = -0.969256f * ptr[0] + 1.875991f * ptr[1] + 0.041556f * ptr[2]; - float b = 0.055648f * ptr[0] + -0.204043f * ptr[1] + 1.057311f * ptr[2]; - ptr[0] = b; ptr[1] = g; ptr[2] = r; - } - else - { - float tmp = ptr[0]; - ptr[0] = ptr[2]; - ptr[2] = tmp; - } + cvtColor(img, img, COLOR_XYZ2BGR); + } + else + { + cvtColor(img, img, COLOR_RGB2BGR); } return true; } @@ -614,16 +606,10 @@ bool TiffEncoder::writeLibTiff( const Mat& img, const std::vector& params) return true; } -bool TiffEncoder::writeHdr(const Mat& img) +bool TiffEncoder::writeHdr(const Mat& _img) { - float *ptr = const_cast(img.ptr()); - for(size_t i = 0; i < img.total(); i++, ptr += 3) - { - float x = 0.412453f * ptr[2] + 0.357580f * ptr[1] + 0.180423f * ptr[0]; - float y = 0.212671f * ptr[2] + 0.715160f * ptr[1] + 0.072169f * ptr[0]; - float z = 0.019334f * ptr[2] + 0.119193f * ptr[1] + 0.950227f * ptr[0]; - ptr[0] = x; ptr[1] = y; ptr[2] = z; - } + Mat img; + cvtColor(_img, img, COLOR_BGR2XYZ); TIFF* tif = TIFFOpen(m_filename.c_str(), "w"); if (!tif) { @@ -638,7 +624,7 @@ bool TiffEncoder::writeHdr(const Mat& img) TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_FLOAT); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); int strip_size = 3 * img.cols; - ptr = const_cast(img.ptr()); + float *ptr = const_cast(img.ptr()); for (int i = 0; i < img.rows; i++, ptr += strip_size) { TIFFWriteEncodedStrip(tif, i, ptr, strip_size * sizeof(float)); diff --git a/modules/highgui/src/grfmt_tiff.hpp b/modules/highgui/src/grfmt_tiff.hpp index b6f7fa099f..72547bdee7 100644 --- a/modules/highgui/src/grfmt_tiff.hpp +++ b/modules/highgui/src/grfmt_tiff.hpp @@ -53,7 +53,7 @@ enum TiffCompression { TIFF_UNCOMP = 1, TIFF_HUFFMAN = 2, - TIFF_PACKBITS = 32773, + TIFF_PACKBITS = 32773 }; enum TiffByteOrder diff --git a/modules/photo/include/opencv2/photo.hpp b/modules/photo/include/opencv2/photo.hpp index b05140c96b..a4e5781fee 100644 --- a/modules/photo/include/opencv2/photo.hpp +++ b/modules/photo/include/opencv2/photo.hpp @@ -59,6 +59,20 @@ enum INPAINT_TELEA = 1 // A. Telea algorithm }; +//! the tonemapping algorithm +enum tonemap_algorithms +{ + TONEMAP_LINEAR, + + TONEMAP_DRAGO, // Adaptive Logarithmic Mapping For + // Displaying High Contrast Scenes + TONEMAP_REINHARD, // Dynamic Range Reduction Inspired + // by Photoreceptor Physiology + TONEMAP_DURAND, // Fast Bilateral Filtering for the + // Display of High-Dynamic-Range Images + TONEMAP_COUNT +}; + //! restores the damaged image areas using one of the available intpainting algorithms CV_EXPORTS_W void inpaint( InputArray src, InputArray inpaintMask, OutputArray dst, double inpaintRadius, int flags ); @@ -80,7 +94,9 @@ 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, std::vector expTimes, OutputArray dst); +CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, const std::vector& exp_times, OutputArray dst); + +CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, tonemap_algorithms algorithm, std::vector& params = std::vector()); } // cv diff --git a/modules/photo/src/hdr_fusion.cpp b/modules/photo/src/hdr_fusion.cpp index 23f8ecb1ad..2da13f55c1 100644 --- a/modules/photo/src/hdr_fusion.cpp +++ b/modules/photo/src/hdr_fusion.cpp @@ -64,29 +64,34 @@ static void generateResponce(float responce[]) responce[0] = responce[1]; } -void makeHDR(InputArrayOfArrays _images, std::vector exp_times, OutputArray _dst) +void makeHDR(InputArrayOfArrays _images, const std::vector& _exp_times, OutputArray _dst) { std::vector images; _images.getMatVector(images); if(images.empty()) { - printf("Need at least one vector image."); + CV_Error(Error::StsBadArg, "Need at least one image"); } - if(images.size() != exp_times.size()) { - printf("Number of images and number of exposure times must be equal."); + if(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; int height = images[0].rows; for(size_t i = 0; i < images.size(); i++) { if(images[i].cols != width || images[i].rows != height) { - printf("Image dimensions must be equal."); + CV_Error(Error::StsBadArg, "Image dimensions must be equal."); } if(images[i].type() != CV_8UC3) { - printf("Images must have CV_8UC3 type."); + CV_Error(Error::StsBadArg, "Images must have CV_8UC3 type."); } } _dst.create(images[0].size(), CV_32FC3); Mat result = _dst.getMat(); + 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]; triangleWeights(weights); generateResponce(responce); @@ -104,7 +109,7 @@ void makeHDR(InputArrayOfArrays _images, std::vector exp_times, OutputArr weights[img_ptr[2]]) / 3; weight_sum += w; for(int channel = 0; channel < 3; channel++) { - sum[channel] += w * (responce[img_ptr[channel]] - log(exp_times[im])); + sum[channel] += w * (responce[img_ptr[channel]] - exp_times[im]); } } for(int channel = 0; channel < 3; channel++) { diff --git a/modules/photo/test/test_hdr.cpp b/modules/photo/test/test_hdr.cpp index 6b93aab2c8..8e895000c8 100644 --- a/modules/photo/test/test_hdr.cpp +++ b/modules/photo/test/test_hdr.cpp @@ -72,7 +72,41 @@ TEST(Photo_MakeHdr, regression) Mat result; makeHDR(images, times, result); - double min = 0.0, max = 1.0; - minMaxLoc(abs(result - expected), &min, &max); + double max = 1.0; + minMaxLoc(abs(result - expected), NULL, &max); ASSERT_TRUE(max < 0.01); +} + +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]; + } + + 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; + + vector param(1); + param[0] = 2.2f; + + for(int i = TONEMAP_DURAND; i < TONEMAP_COUNT; i++) { + + Mat result; + tonemap(img, result, static_cast(i), param); + result.convertTo(result, CV_8UC3, 255); + double max = 1.0; + minMaxLoc(abs(result - images[i]), NULL, &max); + ASSERT_FALSE(max > 0); + } } \ No newline at end of file