test_hdr.cpp 8.46 KB
Newer Older
1
 /*M///////////////////////////////////////////////////////////////////////////////////////
Fedor Morozov's avatar
Fedor Morozov committed
2 3 4 5 6 7 8 9 10 11 12
//
//  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
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Fedor Morozov's avatar
Fedor Morozov committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// 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 "test_precomp.hpp"

using namespace cv;
using namespace std;

47
void loadImage(string path, Mat &img)
48
{
49 50
    img = imread(path, -1);
    ASSERT_FALSE(img.empty()) << "Could not load input image " << path;
51 52
}

53
void checkEqual(Mat img0, Mat img1, double threshold, const string& name)
54
{
55 56
    double max = 1.0;
    minMaxLoc(abs(img0 - img1), NULL, &max);
57
    ASSERT_FALSE(max > threshold) << "max=" << max << " threshold=" << threshold << " method=" << name;
58 59
}

60 61
static vector<float> DEFAULT_VECTOR;
void loadExposureSeq(String path, vector<Mat>& images, vector<float>& times = DEFAULT_VECTOR)
62
{
63
    ifstream list_file((path + "list.txt").c_str());
64 65 66 67 68 69 70 71 72 73
    ASSERT_TRUE(list_file.is_open());
    string name;
    float val;
    while(list_file >> name >> val) {
        Mat img = imread(path + name);
        ASSERT_FALSE(img.empty()) << "Could not load input image " << path + name;
        images.push_back(img);
        times.push_back(1 / val);
    }
    list_file.close();
74 75 76 77
}

void loadResponseCSV(String path, Mat& response)
{
78
    response = Mat(256, 1, CV_32FC3);
79
    ifstream resp_file(path.c_str());
80 81 82
    for(int i = 0; i < 256; i++) {
        for(int c = 0; c < 3; c++) {
            resp_file >> response.at<Vec3f>(i)[c];
Fedor Morozov's avatar
Fedor Morozov committed
83
            resp_file.ignore(1);
84 85 86
        }
    }
    resp_file.close();
87 88
}

Fedor Morozov's avatar
Fedor Morozov committed
89
TEST(Photo_Tonemap, regression)
Fedor Morozov's avatar
Fedor Morozov committed
90
{
91 92 93 94 95 96
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/tonemap/";

    Mat img, expected, result;
    loadImage(test_path + "image.hdr", img);
    float gamma = 2.2f;

97
    Ptr<Tonemap> linear = createTonemap(gamma);
98 99 100
    linear->process(img, result);
    loadImage(test_path + "linear.png", expected);
    result.convertTo(result, CV_8UC3, 255);
101
    checkEqual(result, expected, 3, "Simple");
102 103 104 105 106

    Ptr<TonemapDrago> drago = createTonemapDrago(gamma);
    drago->process(img, result);
    loadImage(test_path + "drago.png", expected);
    result.convertTo(result, CV_8UC3, 255);
107
    checkEqual(result, expected, 3, "Drago");
108 109 110 111 112

    Ptr<TonemapDurand> durand = createTonemapDurand(gamma);
    durand->process(img, result);
    loadImage(test_path + "durand.png", expected);
    result.convertTo(result, CV_8UC3, 255);
113
    checkEqual(result, expected, 3, "Durand");
114 115 116 117 118

    Ptr<TonemapReinhard> reinhard = createTonemapReinhard(gamma);
    reinhard->process(img, result);
    loadImage(test_path + "reinhard.png", expected);
    result.convertTo(result, CV_8UC3, 255);
119
    checkEqual(result, expected, 3, "Reinhard");
120 121 122 123 124

    Ptr<TonemapMantiuk> mantiuk = createTonemapMantiuk(gamma);
    mantiuk->process(img, result);
    loadImage(test_path + "mantiuk.png", expected);
    result.convertTo(result, CV_8UC3, 255);
125
    checkEqual(result, expected, 3, "Mantiuk");
126 127
}

128 129
TEST(Photo_AlignMTB, regression)
{
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    const int TESTS_COUNT = 100;
    string folder = string(cvtest::TS::ptr()->get_data_path()) + "shared/";

    string file_name = folder + "lena.png";
    Mat img;
    loadImage(file_name, img);
    cvtColor(img, img, COLOR_RGB2GRAY);

    int max_bits = 5;
    int max_shift = 32;
    srand(static_cast<unsigned>(time(0)));
    int errors = 0;

    Ptr<AlignMTB> align = createAlignMTB(max_bits);

    for(int i = 0; i < TESTS_COUNT; i++) {
        Point shift(rand() % max_shift, rand() % max_shift);
        Mat res;
        align->shiftMat(img, res, shift);
        Point calc = align->calculateShift(img, res);
        errors += (calc != -shift);
    }
    ASSERT_TRUE(errors < 5) << errors << " errors";
153 154 155 156
}

TEST(Photo_MergeMertens, regression)
{
157
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
158

159
    vector<Mat> images;
160
    loadExposureSeq((test_path + "exposures/").c_str() , images);
161

162
    Ptr<MergeMertens> merge = createMergeMertens();
163

164 165 166 167
    Mat result, expected;
    loadImage(test_path + "merge/mertens.png", expected);
    merge->process(images, result);
    result.convertTo(result, CV_8UC3, 255);
168
    checkEqual(expected, result, 3, "Mertens");
169 170 171 172 173 174 175 176 177 178

    Mat uniform(100, 100, CV_8UC3);
    uniform = Scalar(0, 255, 0);

    images.clear();
    images.push_back(uniform);

    merge->process(images, result);
    result.convertTo(result, CV_8UC3, 255);
    checkEqual(uniform, result, 1e-2f, "Mertens");
179 180 181 182
}

TEST(Photo_MergeDebevec, regression)
{
183
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
184

185 186 187 188 189
    vector<Mat> images;
    vector<float> times;
    Mat response;
    loadExposureSeq(test_path + "exposures/", images, times);
    loadResponseCSV(test_path + "exposures/response.csv", response);
190

191
    Ptr<MergeDebevec> merge = createMergeDebevec();
192

193 194 195
    Mat result, expected;
    loadImage(test_path + "merge/debevec.hdr", expected);
    merge->process(images, result, times, response);
Fedor Morozov's avatar
Fedor Morozov committed
196

Fedor Morozov's avatar
Fedor Morozov committed
197 198 199 200
    Ptr<Tonemap> map = createTonemap();
    map->process(result, result);
    map->process(expected, expected);

201
    checkEqual(expected, result, 1e-2f, "Debevec");
Fedor Morozov's avatar
Fedor Morozov committed
202 203 204 205
}

TEST(Photo_MergeRobertson, regression)
{
206
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
Fedor Morozov's avatar
Fedor Morozov committed
207

208 209 210
    vector<Mat> images;
    vector<float> times;
    loadExposureSeq(test_path + "exposures/", images, times);
Fedor Morozov's avatar
Fedor Morozov committed
211

212
    Ptr<MergeRobertson> merge = createMergeRobertson();
Fedor Morozov's avatar
Fedor Morozov committed
213

214 215 216
    Mat result, expected;
    loadImage(test_path + "merge/robertson.hdr", expected);
    merge->process(images, result, times);
Fedor Morozov's avatar
Fedor Morozov committed
217 218 219
    Ptr<Tonemap> map = createTonemap();
    map->process(result, result);
    map->process(expected, expected);
Fedor Morozov's avatar
Fedor Morozov committed
220

221
    checkEqual(expected, result, 1e-2f, "MergeRobertson");
222 223 224 225
}

TEST(Photo_CalibrateDebevec, regression)
{
226
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
227

228 229 230 231
    vector<Mat> images;
    vector<float> times;
    Mat response, expected;
    loadExposureSeq(test_path + "exposures/", images, times);
Fedor Morozov's avatar
Fedor Morozov committed
232
    loadResponseCSV(test_path + "calibrate/debevec.csv", expected);
233
    Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
Fedor Morozov's avatar
Fedor Morozov committed
234

235
    calibrate->process(images, response, times);
Fedor Morozov's avatar
Fedor Morozov committed
236 237 238 239 240
    Mat diff = abs(response - expected);
    diff = diff.mul(1.0f / response);
    double max;
    minMaxLoc(diff, NULL, &max);
    ASSERT_FALSE(max > 0.1);
241
}
Fedor Morozov's avatar
Fedor Morozov committed
242 243 244

TEST(Photo_CalibrateRobertson, regression)
{
245
    string test_path = string(cvtest::TS::ptr()->get_data_path()) + "hdr/";
Fedor Morozov's avatar
Fedor Morozov committed
246

247 248 249 250
    vector<Mat> images;
    vector<float> times;
    Mat response, expected;
    loadExposureSeq(test_path + "exposures/", images, times);
Fedor Morozov's avatar
Fedor Morozov committed
251 252
    loadResponseCSV(test_path + "calibrate/robertson.csv", expected);

253 254
    Ptr<CalibrateRobertson> calibrate = createCalibrateRobertson();
    calibrate->process(images, response, times);
255
    checkEqual(expected, response, 1e-3f, "CalibrateRobertson");
256
}