opencv/modules/photo/src/calibrate.cpp

277 lines
9.5 KiB
C++
Raw Normal View History

2013-08-01 07:24:47 +08:00
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
2013-09-05 18:10:53 +08:00
//#include "opencv2/highgui.hpp"
2013-08-01 07:24:47 +08:00
#include "hdr_common.hpp"
namespace cv
{
2013-08-01 07:24:47 +08:00
class CalibrateDebevecImpl : public CalibrateDebevec
{
public:
CalibrateDebevecImpl(int _samples, float _lambda, bool _random) :
2013-08-05 23:22:42 +08:00
name("CalibrateDebevec"),
samples(_samples),
lambda(_lambda),
random(_random),
w(tringleWeights())
2013-08-05 23:22:42 +08:00
{
}
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times)
2013-08-05 23:22:42 +08:00
{
std::vector<Mat> images;
src.getMatVector(images);
Mat times = _times.getMat();
2013-08-01 07:24:47 +08:00
CV_Assert(images.size() == times.total());
2013-08-05 23:22:42 +08:00
checkImageDimensions(images);
2013-08-06 19:24:01 +08:00
CV_Assert(images[0].depth() == CV_8U);
int channels = images[0].channels();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
2013-08-01 07:24:47 +08:00
2013-09-06 21:30:43 +08:00
dst.create(LDR_SIZE, 1, CV_32FCC);
2013-08-06 19:24:01 +08:00
Mat result = dst.getMat();
2013-09-05 18:10:53 +08:00
std::vector<Point> sample_points;
if(random) {
for(int i = 0; i < samples; i++) {
sample_points.push_back(Point(rand() % images[0].cols, rand() % images[0].rows));
}
} else {
int x_points = static_cast<int>(sqrt(static_cast<double>(samples) * images[0].cols / images[0].rows));
2013-09-05 18:10:53 +08:00
int y_points = samples / x_points;
int step_x = images[0].cols / x_points;
int step_y = images[0].rows / y_points;
for(int i = 0, x = step_x / 2; i < x_points; i++, x += step_x) {
for(int j = 0, y = step_y; j < y_points; j++, y += step_y) {
sample_points.push_back(Point(x, y));
}
}
}
2013-08-06 19:24:01 +08:00
std::vector<Mat> result_split(channels);
for(int channel = 0; channel < channels; channel++) {
2013-10-04 21:03:15 +08:00
Mat A = Mat::zeros((int)sample_points.size() * (int)images.size() + LDR_SIZE + 1, LDR_SIZE + (int)sample_points.size(), CV_32F);
2013-08-05 23:22:42 +08:00
Mat B = Mat::zeros(A.rows, 1, CV_32F);
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
int eq = 0;
2013-09-05 18:10:53 +08:00
for(size_t i = 0; i < sample_points.size(); i++) {
2013-08-05 23:22:42 +08:00
for(size_t j = 0; j < images.size(); j++) {
2013-08-01 07:24:47 +08:00
2013-09-05 18:10:53 +08:00
int val = images[j].ptr()[3*(sample_points[i].y * images[j].cols + sample_points[j].x) + channel];
2013-08-05 23:22:42 +08:00
A.at<float>(eq, val) = w.at<float>(val);
2013-10-04 21:03:15 +08:00
A.at<float>(eq, LDR_SIZE + (int)i) = -w.at<float>(val);
B.at<float>(eq, 0) = w.at<float>(val) * log(times.at<float>((int)j));
2013-08-05 23:22:42 +08:00
eq++;
}
}
2013-09-06 21:30:43 +08:00
A.at<float>(eq, LDR_SIZE / 2) = 1;
2013-08-05 23:22:42 +08:00
eq++;
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
for(int i = 0; i < 254; i++) {
A.at<float>(eq, i) = lambda * w.at<float>(i + 1);
A.at<float>(eq, i + 1) = -2 * lambda * w.at<float>(i + 1);
A.at<float>(eq, i + 2) = lambda * w.at<float>(i + 1);
eq++;
}
Mat solution;
solve(A, B, solution, DECOMP_SVD);
2013-09-06 21:30:43 +08:00
solution.rowRange(0, LDR_SIZE).copyTo(result_split[channel]);
2013-08-05 23:22:42 +08:00
}
2013-08-06 19:24:01 +08:00
merge(result_split, result);
exp(result, result);
2013-08-05 23:22:42 +08:00
}
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
int getSamples() const { return samples; }
void setSamples(int val) { samples = val; }
2013-08-01 07:24:47 +08:00
2013-08-05 23:22:42 +08:00
float getLambda() const { return lambda; }
void setLambda(float val) { lambda = val; }
2013-08-01 07:24:47 +08:00
2013-09-05 18:10:53 +08:00
bool getRandom() const { return random; }
void setRandom(bool val) { random = val; }
2013-08-05 23:22:42 +08:00
void write(FileStorage& fs) const
2013-08-01 07:24:47 +08:00
{
fs << "name" << name
2013-08-05 23:22:42 +08:00
<< "samples" << samples
2013-09-05 18:10:53 +08:00
<< "lambda" << lambda
<< "random" << static_cast<int>(random);
2013-08-01 07:24:47 +08:00
}
void read(const FileNode& fn)
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
samples = fn["samples"];
2013-08-05 23:22:42 +08:00
lambda = fn["lambda"];
2013-09-05 18:10:53 +08:00
int random_val = fn["random"];
random = (random_val != 0);
2013-08-01 07:24:47 +08:00
}
protected:
2013-08-05 23:22:42 +08:00
String name;
int samples;
float lambda;
2013-09-05 18:10:53 +08:00
bool random;
2013-08-05 23:22:42 +08:00
Mat w;
2013-08-01 07:24:47 +08:00
};
2013-09-05 18:10:53 +08:00
Ptr<CalibrateDebevec> createCalibrateDebevec(int samples, float lambda, bool random)
2013-08-01 07:24:47 +08:00
{
return makePtr<CalibrateDebevecImpl>(samples, lambda, random);
2013-08-01 07:24:47 +08:00
}
2013-08-26 19:23:37 +08:00
class CalibrateRobertsonImpl : public CalibrateRobertson
{
public:
CalibrateRobertsonImpl(int _max_iter, float _threshold) :
2013-08-26 19:23:37 +08:00
name("CalibrateRobertson"),
max_iter(_max_iter),
threshold(_threshold),
2013-08-26 19:23:37 +08:00
weight(RobertsonWeights())
{
}
void process(InputArrayOfArrays src, OutputArray dst, InputArray _times)
2013-08-26 19:23:37 +08:00
{
std::vector<Mat> images;
src.getMatVector(images);
Mat times = _times.getMat();
2013-08-26 19:23:37 +08:00
CV_Assert(images.size() == times.total());
2013-08-26 19:23:37 +08:00
checkImageDimensions(images);
CV_Assert(images[0].depth() == CV_8U);
int channels = images[0].channels();
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
2013-09-06 21:30:43 +08:00
dst.create(LDR_SIZE, 1, CV_32FCC);
2013-08-26 19:23:37 +08:00
Mat response = dst.getMat();
2013-09-06 21:30:43 +08:00
response = linearResponse(3) / (LDR_SIZE / 2.0f);
2013-08-26 19:23:37 +08:00
2013-09-06 21:30:43 +08:00
Mat card = Mat::zeros(LDR_SIZE, 1, CV_32FCC);
for(size_t i = 0; i < images.size(); i++) {
2013-08-26 19:23:37 +08:00
uchar *ptr = images[i].ptr();
2013-09-06 21:30:43 +08:00
for(size_t pos = 0; pos < images[i].total(); pos++) {
2013-08-26 19:23:37 +08:00
for(int c = 0; c < channels; c++, ptr++) {
card.at<Vec3f>(*ptr)[c] += 1;
}
}
}
card = 1.0 / card;
Ptr<MergeRobertson> merge = createMergeRobertson();
2013-08-26 19:23:37 +08:00
for(int iter = 0; iter < max_iter; iter++) {
2013-09-06 21:30:43 +08:00
radiance = Mat::zeros(images[0].size(), CV_32FCC);
merge->process(images, radiance, times, response);
2013-08-26 19:23:37 +08:00
2013-09-06 21:30:43 +08:00
Mat new_response = Mat::zeros(LDR_SIZE, 1, CV_32FC3);
for(size_t i = 0; i < images.size(); i++) {
2013-08-26 19:23:37 +08:00
uchar *ptr = images[i].ptr();
2013-09-06 21:30:43 +08:00
float* rad_ptr = radiance.ptr<float>();
for(size_t pos = 0; pos < images[i].total(); pos++) {
2013-08-26 19:23:37 +08:00
for(int c = 0; c < channels; c++, ptr++, rad_ptr++) {
2013-10-04 21:03:15 +08:00
new_response.at<Vec3f>(*ptr)[c] += times.at<float>((int)i) * *rad_ptr;
2013-08-26 19:23:37 +08:00
}
}
}
2013-09-06 21:30:43 +08:00
new_response = new_response.mul(card);
2013-08-26 19:23:37 +08:00
for(int c = 0; c < 3; c++) {
2013-09-06 21:30:43 +08:00
float middle = new_response.at<Vec3f>(LDR_SIZE / 2)[c];
for(int i = 0; i < LDR_SIZE; i++) {
new_response.at<Vec3f>(i)[c] /= middle;
2013-08-26 19:23:37 +08:00
}
}
float diff = static_cast<float>(sum(sum(abs(new_response - response)))[0] / channels);
2013-09-06 21:30:43 +08:00
new_response.copyTo(response);
if(diff < threshold) {
break;
}
2013-08-26 19:23:37 +08:00
}
}
int getMaxIter() const { return max_iter; }
void setMaxIter(int val) { max_iter = val; }
float getThreshold() const { return threshold; }
void setThreshold(float val) { threshold = val; }
2013-09-06 21:30:43 +08:00
Mat getRadiance() const { return radiance; }
2013-08-26 19:23:37 +08:00
void write(FileStorage& fs) const
{
fs << "name" << name
<< "max_iter" << max_iter
<< "threshold" << threshold;
}
void read(const FileNode& fn)
{
FileNode n = fn["name"];
CV_Assert(n.isString() && String(n) == name);
max_iter = fn["max_iter"];
threshold = fn["threshold"];
}
protected:
String name;
int max_iter;
float threshold;
2013-09-06 21:30:43 +08:00
Mat weight, radiance;
2013-08-26 19:23:37 +08:00
};
Ptr<CalibrateRobertson> createCalibrateRobertson(int max_iter, float threshold)
{
return makePtr<CalibrateRobertsonImpl>(max_iter, threshold);
2013-08-26 19:23:37 +08:00
}
}