test_grayworld.cpp 3.52 KB
Newer Older
1 2 3
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
4 5
#include "test_precomp.hpp"

6
namespace opencv_test { namespace {
7

8
    void ref_autowbGrayworld(InputArray _src, OutputArray _dst, float thresh)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    {
        Mat src = _src.getMat();

        _dst.create(src.size(), src.type());
        Mat dst = _dst.getMat();

        int width  = src.cols,
            height = src.rows,
            N      = width*height,
            N3     = N*3;

        // Calculate sum of pixel values of each channel
        const uchar* src_data = src.ptr<uchar>(0);
        unsigned long sum1 = 0, sum2 = 0, sum3 = 0;
        int i = 0;
24
        unsigned int minRGB, maxRGB, thresh255 = cvRound(thresh * 255);
25
        for ( ; i < N3; i += 3 )
26
        {
27 28
            minRGB = std::min(src_data[i], std::min(src_data[i + 1], src_data[i + 2]));
            maxRGB = std::max(src_data[i], std::max(src_data[i + 1], src_data[i + 2]));
29
            if ( (maxRGB - minRGB) * 255 > thresh255 * maxRGB ) continue;
30
            sum1 += src_data[i];
31 32 33 34 35 36 37 38 39 40 41 42 43
            sum2 += src_data[i + 1];
            sum3 += src_data[i + 2];
        }

        // Find inverse of averages
        double inv1 = sum1 == 0 ? 0.f : (double)N / (double)sum1,
               inv2 = sum2 == 0 ? 0.f : (double)N / (double)sum2,
               inv3 = sum3 == 0 ? 0.f : (double)N / (double)sum3;

        // Find maximum
        double inv_max = std::max(std::max(inv1, inv2), inv3);

        // Scale by maximum
44
        if ( inv_max > 0 )
45 46 47 48 49 50
        {
            inv1 = (double) inv1 / inv_max;
            inv2 = (double) inv2 / inv_max;
            inv3 = (double) inv3 / inv_max;
        }

51
        // Fixed point arithmetic, mul by 2^8 then shift back 8 bits
52 53 54
        int i_inv1 = cvRound(inv1 * (1 << 8)),
            i_inv2 = cvRound(inv2 * (1 << 8)),
            i_inv3 = cvRound(inv3 * (1 << 8));
55

56 57 58
        // Scale input pixel values
        uchar* dst_data = dst.ptr<uchar>(0);
        i = 0;
59
        for ( ; i < N3; i += 3 )
60
        {
61 62 63
            dst_data[i]     = (uchar)((src_data[i]     * i_inv1) >> 8);
            dst_data[i + 1] = (uchar)((src_data[i + 1] * i_inv2) >> 8);
            dst_data[i + 2] = (uchar)((src_data[i + 2] * i_inv3) >> 8);
64 65 66 67 68
        }
    }

    TEST(xphoto_grayworld_white_balance, regression)
    {
Maksim Shabunin's avatar
Maksim Shabunin committed
69
        String dir = cvtest::TS::ptr()->get_data_path() + "cv/xphoto/simple_white_balance/";
70
        const int nTests = 8;
71 72
        const float wb_thresh = 0.5f;
        const float acc_thresh = 2.f;
73 74
        Ptr<xphoto::GrayworldWB> wb = xphoto::createGrayworldWB();
        wb->setSaturationThreshold(wb_thresh);
75

76
        for ( int i = 0; i < nTests; ++i )
77 78 79 80 81 82
        {
            String srcName = dir + format("sources/%02d.png", i + 1);
            Mat src = imread(srcName, IMREAD_COLOR);
            ASSERT_TRUE(!src.empty());

            Mat referenceResult;
83
            ref_autowbGrayworld(src, referenceResult, wb_thresh);
84 85

            Mat currentResult;
86
            wb->balanceWhite(src, currentResult);
87
            ASSERT_LE(cv::norm(currentResult, referenceResult, NORM_INF), acc_thresh);
88

89 90 91
            // test the 16-bit depth:
            Mat currentResult_16U, src_16U;
            src.convertTo(src_16U, CV_16UC3, 256.0);
92
            wb->balanceWhite(src_16U, currentResult_16U);
93
            currentResult_16U.convertTo(currentResult, CV_8UC3, 1/256.0);
94
            ASSERT_LE(cv::norm(currentResult, referenceResult, NORM_INF), acc_thresh);
95 96 97
        }
    }

98 99

}} // namespace