Commit 703cf8ce authored by Fedor Morozov's avatar Fedor Morozov

Calibration, various changes

parent ec668ce3
...@@ -69,11 +69,13 @@ bool HdrDecoder::readHeader() ...@@ -69,11 +69,13 @@ bool HdrDecoder::readHeader()
{ {
file = fopen(m_filename.c_str(), "rb"); file = fopen(m_filename.c_str(), "rb");
if(!file) { if(!file) {
CV_Error(Error::StsError, "HDR decoder: can't open file"); return false;
} }
RGBE_ReadHeader(file, &m_width, &m_height, NULL); RGBE_ReadHeader(file, &m_width, &m_height, NULL);
if(m_width <= 0 || m_height <= 0) { if(m_width <= 0 || m_height <= 0) {
CV_Error(Error::StsError, "HDR decoder: invalid image size"); fclose(file);
file = NULL;
return false;
} }
return true; return true;
} }
...@@ -82,7 +84,9 @@ bool HdrDecoder::readData(Mat& _img) ...@@ -82,7 +84,9 @@ bool HdrDecoder::readData(Mat& _img)
{ {
Mat img(m_height, m_width, CV_32FC3); Mat img(m_height, m_width, CV_32FC3);
if(!file) { if(!file) {
readHeader(); if(!readHeader()) {
return false;
}
} }
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;
...@@ -125,13 +129,10 @@ bool HdrEncoder::write( const Mat& _img, const std::vector<int>& params ) ...@@ -125,13 +129,10 @@ bool HdrEncoder::write( const Mat& _img, const std::vector<int>& params )
} else { } else {
_img.convertTo(img, CV_32FC3, 1/255.0f); _img.convertTo(img, CV_32FC3, 1/255.0f);
} }
if(!(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE)) { CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE);
CV_Error(Error::StsBadArg, "HDR encoder: wrong compression param");
}
FILE *fout = fopen(m_filename.c_str(), "wb"); FILE *fout = fopen(m_filename.c_str(), "wb");
if(!fout) { if(!fout) {
CV_Error(Error::StsError, "HDR encoder: can't open file"); return false;
} }
RGBE_WriteHeader(fout, img.cols, img.rows, NULL); RGBE_WriteHeader(fout, img.cols, img.rows, NULL);
...@@ -151,7 +152,7 @@ ImageEncoder HdrEncoder::newEncoder() const ...@@ -151,7 +152,7 @@ ImageEncoder HdrEncoder::newEncoder() const
} }
bool HdrEncoder::isFormatSupported( int depth ) const { bool HdrEncoder::isFormatSupported( int depth ) const {
return depth == CV_32F; return depth != CV_64F;
} }
} }
...@@ -410,8 +410,8 @@ TEST(Highgui_WebP, encode_decode_lossy_webp) ...@@ -410,8 +410,8 @@ TEST(Highgui_WebP, encode_decode_lossy_webp)
TEST(Highgui_Hdr, regression) TEST(Highgui_Hdr, regression)
{ {
string folder = string(cvtest::TS::ptr()->get_data_path()) + "../cv/hdr/"; string folder = string(cvtest::TS::ptr()->get_data_path()) + "../cv/hdr/";
string name_rle = folder + "grand_canal_rle.hdr"; string name_rle = folder + "rle.hdr";
string name_no_rle = folder + "grand_canal_no_rle.hdr"; string name_no_rle = folder + "no_rle.hdr";
Mat img_rle = imread(name_rle, -1); Mat img_rle = imread(name_rle, -1);
ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle; ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle;
Mat img_no_rle = imread(name_no_rle, -1); Mat img_no_rle = imread(name_no_rle, -1);
......
...@@ -94,16 +94,20 @@ CV_EXPORTS_W void fastNlMeansDenoisingColoredMulti( InputArrayOfArrays srcImgs, ...@@ -94,16 +94,20 @@ 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, const std::vector<float>& exp_times, OutputArray dst, bool align = false); CV_EXPORTS_W void makeHDR(InputArrayOfArrays srcImgs, const std::vector<float>& exp_times, OutputArray dst, Mat response = Mat());
CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, int algorithm, CV_EXPORTS_W void tonemap(InputArray src, OutputArray dst, int algorithm,
const std::vector<float>& params = std::vector<float>()); const std::vector<float>& params = std::vector<float>());
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 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 Point getExpShift(InputArray img0, InputArray img1, int max_bits = 6, int exclude_range = 4);
CV_EXPORTS_W void estimateResponse(InputArrayOfArrays srcImgs, const std::vector<float>& exp_times, OutputArray dst, int samples = 50, float lambda = 10);
CV_EXPORTS_W void alignImages(InputArrayOfArrays src, std::vector<Mat>& dst);
} // cv } // cv
#endif #endif
...@@ -117,12 +117,8 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_ ...@@ -117,12 +117,8 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_
{ {
Mat img0 = _img0.getMat(); Mat img0 = _img0.getMat();
Mat img1 = _img1.getMat(); Mat img1 = _img1.getMat();
if(img0.type() != CV_8UC1 || img1.type() != CV_8UC1) { CV_Assert(img0.type() == CV_8UC1 && img1.type() == CV_8UC1);
CV_Error(Error::StsBadArg, "Images must have CV_8UC1 type."); CV_Assert(img0.size() == img0.size());
}
if(img0.size() != img0.size()) {
CV_Error(Error::StsBadArg, "Image dimensions must be equal.");
}
int maxlevel = (int)(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1; int maxlevel = (int)(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1;
maxlevel = min(maxlevel, max_bits - 1); maxlevel = min(maxlevel, max_bits - 1);
...@@ -161,4 +157,5 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_ ...@@ -161,4 +157,5 @@ Point getExpShift(InputArray _img0, InputArray _img1, int max_bits, int exclude_
} }
return shift; return shift;
} }
}; };
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
#include "opencv2/photo.hpp" #include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
#include <iostream>
namespace cv namespace cv
{ {
...@@ -56,37 +58,51 @@ static void triangleWeights(float weights[]) ...@@ -56,37 +58,51 @@ static void triangleWeights(float weights[])
} }
} }
static void generateResponce(float responce[]) static Mat linearResponse()
{ {
for(int i = 0; i < 256; i++) { Mat response(256, 1, CV_32F);
responce[i] = log((float)i); for(int i = 1; i < 256; i++) {
response.at<float>(i) = log((float)i);
} }
responce[0] = responce[1]; response.at<float>(0) = response.at<float>(1);
return response;
} }
static void checkImages(std::vector<Mat>& images, bool hdr, const std::vector<float>& _exp_times = std::vector<float>()) static void modifyCheckResponse(Mat &response)
{ {
if(images.empty()) { if(response.empty()) {
CV_Error(Error::StsBadArg, "Need at least one image"); response = linearResponse();
} }
if(hdr && images.size() != _exp_times.size()) { CV_Assert(response.rows == 256 && (response.cols == 1 || response.cols == 3));
CV_Error(Error::StsBadArg, "Number of images and number of exposure times must be equal."); 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<Mat>& images, bool hdr, const std::vector<float>& _exp_times = std::vector<float>())
{
CV_Assert(!images.empty());
CV_Assert(!hdr || images.size() == _exp_times.size());
int width = images[0].cols; int width = images[0].cols;
int height = images[0].rows; int height = images[0].rows;
int channels = images[0].channels();
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) { CV_Assert(images[i].cols == width && images[i].rows == height);
CV_Error(Error::StsBadArg, "Image dimensions must be equal."); CV_Assert(images[i].channels() == channels && images[i].depth() == CV_8U);
}
if(images[i].type() != CV_8UC3) {
CV_Error(Error::StsBadArg, "Images must have CV_8UC3 type.");
}
} }
} }
static void alignImages(std::vector<Mat>& src, std::vector<Mat>& dst) void alignImages(InputArrayOfArrays _src, std::vector<Mat>& dst)
{ {
std::vector<Mat> src;
_src.getMatVector(src);
checkImages(src, false);
dst.resize(src.size()); dst.resize(src.size());
size_t pivot = src.size() / 2; size_t pivot = src.size() / 2;
...@@ -105,65 +121,55 @@ static void alignImages(std::vector<Mat>& src, std::vector<Mat>& dst) ...@@ -105,65 +121,55 @@ static void alignImages(std::vector<Mat>& src, std::vector<Mat>& dst)
} }
} }
void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst, bool align) void makeHDR(InputArrayOfArrays _images, const std::vector<float>& _exp_times, OutputArray _dst, Mat response)
{ {
std::vector<Mat> images; std::vector<Mat> images;
_images.getMatVector(images); _images.getMatVector(images);
checkImages(images, true, _exp_times); 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(); Mat result = _dst.getMat();
if(align) {
std::vector<Mat> new_images;
alignImages(images, new_images);
images = new_images;
}
std::vector<float> exp_times(_exp_times.size()); std::vector<float> exp_times(_exp_times.size());
for(size_t i = 0; i < exp_times.size(); i++) { for(size_t i = 0; i < exp_times.size(); i++) {
exp_times[i] = log(_exp_times[i]); exp_times[i] = log(_exp_times[i]);
} }
float weights[256], responce[256]; float weights[256];
triangleWeights(weights); triangleWeights(weights);
generateResponce(responce);
int channels = images[0].channels();
float max = 0;
float *res_ptr = result.ptr<float>(); float *res_ptr = result.ptr<float>();
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<float> sum(channels, 0);
float weight_sum = 0; float weight_sum = 0;
for(size_t im = 0; im < images.size(); im++) { for(size_t im = 0; im < images.size(); im++) {
uchar *img_ptr = images[im].ptr() + 3 * pos; uchar *img_ptr = images[im].ptr() + channels * pos;
float w = (weights[img_ptr[0]] + weights[img_ptr[1]] + float w = 0;
weights[img_ptr[2]]) / 3; for(int channel = 0; channel < channels; channel++) {
w += weights[img_ptr[channel]];
}
w /= channels;
weight_sum += w; weight_sum += w;
for(int channel = 0; channel < 3; channel++) { for(int channel = 0; channel < channels; channel++) {
sum[channel] += w * (responce[img_ptr[channel]] - exp_times[im]); sum[channel] += w * (response.at<float>(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); 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<Mat> images; std::vector<Mat> images;
_images.getMatVector(images); _images.getMatVector(images);
checkImages(images, false); checkImages(images, false);
if(align) {
std::vector<Mat> new_images;
alignImages(images, new_images);
images = new_images;
}
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(images[0].size(), CV_32FC1);
for(size_t im = 0; im < images.size(); im++) { for(size_t im = 0; im < images.size(); im++) {
...@@ -242,4 +248,47 @@ void exposureFusion(InputArrayOfArrays _images, OutputArray _dst, bool align, fl ...@@ -242,4 +248,47 @@ void exposureFusion(InputArrayOfArrays _images, OutputArray _dst, bool align, fl
res_pyr[0].copyTo(result); res_pyr[0].copyTo(result);
} }
void estimateResponse(InputArrayOfArrays _images, const std::vector<float>& exp_times, OutputArray _dst, int samples, float lambda)
{
std::vector<Mat> 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<float>(eq, val) = w[val];
A.at<float>(eq, 256 + i) = -w[val];
B.at<float>(eq, 0) = w[val] * log(exp_times[j]);
eq++;
}
}
A.at<float>(eq, 128) = 1;
eq++;
for(int i = 0; i < 254; i++) {
A.at<float>(eq, i) = lambda * w[i + 1];
A.at<float>(eq, i + 1) = -2 * lambda * w[i + 1];
A.at<float>(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
...@@ -163,20 +163,15 @@ void tonemap(InputArray _src, OutputArray _dst, int algorithm, ...@@ -163,20 +163,15 @@ void tonemap(InputArray _src, OutputArray _dst, int algorithm,
NULL, DragoMap, ReinhardDevlinMap, DurandMap}; NULL, DragoMap, ReinhardDevlinMap, DurandMap};
Mat src = _src.getMat(); Mat src = _src.getMat();
if(src.empty()) { CV_Assert(!src.empty());
CV_Error(Error::StsBadArg, "Empty input image"); CV_Assert(0 <= algorithm && algorithm < TONEMAP_COUNT);
}
if(algorithm < 0 || algorithm >= TONEMAP_COUNT) {
CV_Error(Error::StsBadArg, "Wrong algorithm index");
}
_dst.create(src.size(), CV_32FC3); _dst.create(src.size(), CV_32FC3);
Mat dst = _dst.getMat(); Mat dst = _dst.getMat();
src.copyTo(dst); src.copyTo(dst);
double min, max; double min, max;
minMaxLoc(dst, &min, &max); minMaxLoc(dst, &min, &max);
if(max - min < 1e-10f) { if(max - min < DBL_EPSILON) {
return; return;
} }
dst = (dst - min) / (max - min); dst = (dst - min) / (max - min);
......
...@@ -43,79 +43,93 @@ ...@@ -43,79 +43,93 @@
#include "test_precomp.hpp" #include "test_precomp.hpp"
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <fstream>
using namespace cv; using namespace cv;
using namespace std; 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) 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/";
vector<string>file_names(3); vector<float> times;
file_names[0] = folder + "grand_canal_1_45.jpg"; vector<Mat> images;
file_names[1] = folder + "grand_canal_1_180.jpg";
file_names[2] = folder + "grand_canal_1_750.jpg"; ifstream list_file(fuse_path + "list.txt");
vector<Mat>images(3); string name;
for(int i = 0; i < 3; i++) { float val;
images[i] = imread(file_names[i]); while(list_file >> name >> val) {
ASSERT_FALSE(images[i].empty()) << "Could not load input image " << file_names[i]; 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);
} }
list_file.close();
string expected_path = folder + "grand_canal_rle.hdr";
Mat expected = imread(expected_path, -1); Mat response, expected(256, 3, CV_32F);
ASSERT_FALSE(expected.empty()) << "Could not load input image " << expected_path; ifstream resp_file(test_path + "response.csv");
for(int i = 0; i < 256; i++) {
vector<float>times(3); for(int channel = 0; channel < 3; channel++) {
times[0] = 1.0f/45.0f; resp_file >> expected.at<float>(i, channel);
times[1] = 1.0f/180.0f; resp_file.ignore(1);
times[2] = 1.0f/750.0f; }
}
resp_file.close();
estimateResponse(images, times, response);
checkEqual(expected, response, 0.001);
Mat result; Mat result;
loadImage(test_path + "no_calibration.hdr", expected);
makeHDR(images, times, result); makeHDR(images, times, result);
double max = 1.0; checkEqual(expected, result, 0.01);
minMaxLoc(abs(result - expected), NULL, &max);
ASSERT_TRUE(max < 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"; loadImage(test_path + "exp_fusion.png", expected);
expected = imread(expected_path);
ASSERT_FALSE(expected.empty()) << "Could not load input image " << expected_path;
exposureFusion(images, result); exposureFusion(images, result);
result.convertTo(result, CV_8UC3, 255); result.convertTo(result, CV_8UC3, 255);
minMaxLoc(abs(result - expected), NULL, &max); checkEqual(expected, result, 0);
ASSERT_FALSE(max > 0);
} }
TEST(Photo_Tonemap, regression) TEST(Photo_Tonemap, regression)
{ {
string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; 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); vector<Mat>images(TONEMAP_COUNT);
for(int i = 0; i < TONEMAP_COUNT; i++) { for(int i = 0; i < TONEMAP_COUNT; i++) {
images[i] = imread(file_names[i]); stringstream stream;
ASSERT_FALSE(images[i].empty()) << "Could not load input image " << file_names[i]; stream << "tonemap" << i << ".png";
string file_name;
stream >> file_name;
loadImage(folder + "tonemap/" + file_name ,images[i]);
} }
Mat img;
string hdr_file_name = folder + "grand_canal_rle.hdr"; loadImage(folder + "rle.hdr", img);
Mat img = imread(hdr_file_name, -1);
ASSERT_FALSE(img.empty()) << "Could not load input image " << hdr_file_name;
vector<float> param(1); vector<float> param(1);
param[0] = 2.2f; param[0] = 2.2f;
for(int i = TONEMAP_DURAND; i < TONEMAP_COUNT; i++) { for(int i = 0; i < TONEMAP_COUNT; i++) {
Mat result; Mat result;
tonemap(img, result, i, param); tonemap(img, result, i, param);
result.convertTo(result, CV_8UC3, 255); result.convertTo(result, CV_8UC3, 255);
double max = 1.0; checkEqual(images[i], result, 0);
minMaxLoc(abs(result - images[i]), NULL, &max);
ASSERT_FALSE(max > 0);
} }
} }
...@@ -124,7 +138,7 @@ TEST(Photo_Align, regression) ...@@ -124,7 +138,7 @@ TEST(Photo_Align, regression)
const int TESTS_COUNT = 100; const int TESTS_COUNT = 100;
string folder = string(cvtest::TS::ptr()->get_data_path()) + "hdr/"; 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); Mat img = imread(file_name);
ASSERT_FALSE(img.empty()) << "Could not load input image " << file_name; ASSERT_FALSE(img.empty()) << "Could not load input image " << file_name;
cvtColor(img, img, COLOR_RGB2GRAY); cvtColor(img, img, COLOR_RGB2GRAY);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment