Fixes, missing files, tests

pull/1474/head
Fedor Morozov 12 years ago
parent 122f85f352
commit a5e11079d7
  1. 8
      modules/highgui/src/grfmt_hdr.cpp
  2. 36
      modules/highgui/src/grfmt_tiff.cpp
  3. 2
      modules/highgui/src/grfmt_tiff.hpp
  4. 18
      modules/photo/include/opencv2/photo.hpp
  5. 19
      modules/photo/src/hdr_fusion.cpp
  6. 38
      modules/photo/test/test_hdr.cpp

@ -92,11 +92,9 @@ bool HdrDecoder::readData(Mat& img)
bool HdrDecoder::checkSignature( const String& signature ) const bool HdrDecoder::checkSignature( const String& signature ) const
{ {
if(signature.size() >= (m_signature.size()) && if(signature.size() >= m_signature.size() &&
!memcmp(signature.c_str(), m_signature.c_str(), m_signature.size())) (!memcmp(signature.c_str(), m_signature.c_str(), m_signature.size()) ||
return true; !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_alt.c_str(), m_signature_alt.size()))
return true; return true;
return false; return false;
} }

@ -47,6 +47,7 @@
#include "precomp.hpp" #include "precomp.hpp"
#include "grfmt_tiff.hpp" #include "grfmt_tiff.hpp"
#include <opencv2/imgproc.hpp>
namespace cv namespace cv
{ {
@ -413,22 +414,13 @@ bool TiffDecoder::readHdrData(Mat& img)
size -= strip_size * sizeof(float); size -= strip_size * sizeof(float);
} }
close(); close();
ptr = img.ptr<float>(); if(photometric == PHOTOMETRIC_LOGLUV)
for(size_t i = 0; i < img.total(); i++, ptr += 3)
{ {
if(photometric == PHOTOMETRIC_LOGLUV) cvtColor(img, img, COLOR_XYZ2BGR);
{ }
float r = 3.240479f * ptr[0] + -1.537150f * ptr[1] + -0.498535f * ptr[2]; else
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]; cvtColor(img, img, COLOR_RGB2BGR);
ptr[0] = b; ptr[1] = g; ptr[2] = r;
}
else
{
float tmp = ptr[0];
ptr[0] = ptr[2];
ptr[2] = tmp;
}
} }
return true; return true;
} }
@ -614,16 +606,10 @@ bool TiffEncoder::writeLibTiff( const Mat& img, const std::vector<int>& params)
return true; return true;
} }
bool TiffEncoder::writeHdr(const Mat& img) bool TiffEncoder::writeHdr(const Mat& _img)
{ {
float *ptr = const_cast<float*>(img.ptr<float>()); Mat img;
for(size_t i = 0; i < img.total(); i++, ptr += 3) cvtColor(_img, img, COLOR_BGR2XYZ);
{
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;
}
TIFF* tif = TIFFOpen(m_filename.c_str(), "w"); TIFF* tif = TIFFOpen(m_filename.c_str(), "w");
if (!tif) if (!tif)
{ {
@ -638,7 +624,7 @@ bool TiffEncoder::writeHdr(const Mat& img)
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_FLOAT); TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_FLOAT);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
int strip_size = 3 * img.cols; int strip_size = 3 * img.cols;
ptr = const_cast<float*>(img.ptr<float>()); float *ptr = const_cast<float*>(img.ptr<float>());
for (int i = 0; i < img.rows; i++, ptr += strip_size) for (int i = 0; i < img.rows; i++, ptr += strip_size)
{ {
TIFFWriteEncodedStrip(tif, i, ptr, strip_size * sizeof(float)); TIFFWriteEncodedStrip(tif, i, ptr, strip_size * sizeof(float));

@ -53,7 +53,7 @@ enum TiffCompression
{ {
TIFF_UNCOMP = 1, TIFF_UNCOMP = 1,
TIFF_HUFFMAN = 2, TIFF_HUFFMAN = 2,
TIFF_PACKBITS = 32773, TIFF_PACKBITS = 32773
}; };
enum TiffByteOrder enum TiffByteOrder

@ -59,6 +59,20 @@ enum
INPAINT_TELEA = 1 // A. Telea algorithm 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 //! restores the damaged image areas using one of the available intpainting algorithms
CV_EXPORTS_W void inpaint( InputArray src, InputArray inpaintMask, CV_EXPORTS_W void inpaint( InputArray src, InputArray inpaintMask,
OutputArray dst, double inpaintRadius, int flags ); OutputArray dst, double inpaintRadius, int flags );
@ -80,7 +94,9 @@ CV_EXPORTS_W void fastNlMeansDenoisingColoredMulti( InputArrayOfArrays srcImgs,
float h = 3, float hColor = 3, float h = 3, float hColor = 3,
int templateWindowSize = 7, int searchWindowSize = 21); int templateWindowSize = 7, int searchWindowSize = 21);
CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, std::vector<float> expTimes, OutputArray dst); 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 } // cv

@ -64,29 +64,34 @@ static void generateResponce(float responce[])
responce[0] = responce[1]; responce[0] = responce[1];
} }
void makeHDR(InputArrayOfArrays _images, std::vector<float> exp_times, OutputArray _dst) void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst)
{ {
std::vector<Mat> images; std::vector<Mat> images;
_images.getMatVector(images); _images.getMatVector(images);
if(images.empty()) { 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()) { if(images.size() != _exp_times.size()) {
printf("Number of images and number of exposure times must be equal."); CV_Error(Error::StsBadArg, "Number of images and number of exposure times must be equal.");
} }
int width = images[0].cols; int width = images[0].cols;
int height = images[0].rows; int height = images[0].rows;
for(size_t i = 0; i < images.size(); i++) { for(size_t i = 0; i < images.size(); i++) {
if(images[i].cols != width || images[i].rows != height) { 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) { 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); _dst.create(images[0].size(), CV_32FC3);
Mat result = _dst.getMat(); 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]);
}
float weights[256], responce[256]; float weights[256], responce[256];
triangleWeights(weights); triangleWeights(weights);
generateResponce(responce); generateResponce(responce);
@ -104,7 +109,7 @@ void makeHDR(InputArrayOfArrays _images, std::vector<float> exp_times, OutputArr
weights[img_ptr[2]]) / 3; weights[img_ptr[2]]) / 3;
weight_sum += w; weight_sum += w;
for(int channel = 0; channel < 3; channel++) { 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++) { for(int channel = 0; channel < 3; channel++) {

@ -72,7 +72,41 @@ TEST(Photo_MakeHdr, regression)
Mat result; Mat result;
makeHDR(images, times, result); makeHDR(images, times, result);
double min = 0.0, max = 1.0; double max = 1.0;
minMaxLoc(abs(result - expected), &min, &max); minMaxLoc(abs(result - expected), NULL, &max);
ASSERT_TRUE(max < 0.01); ASSERT_TRUE(max < 0.01);
} }
TEST(Photo_Tonemap, regression)
{
string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
vector<string>file_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";
vector<Mat>images(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<float> param(1);
param[0] = 2.2f;
for(int i = TONEMAP_DURAND; i < TONEMAP_COUNT; i++) {
Mat result;
tonemap(img, result, static_cast<tonemap_algorithms>(i), param);
result.convertTo(result, CV_8UC3, 255);
double max = 1.0;
minMaxLoc(abs(result - images[i]), NULL, &max);
ASSERT_FALSE(max > 0);
}
}
Loading…
Cancel
Save