test_grayworld.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. void ref_autowbGrayworld(InputArray _src, OutputArray _dst, float thresh)
  7. {
  8. Mat src = _src.getMat();
  9. _dst.create(src.size(), src.type());
  10. Mat dst = _dst.getMat();
  11. int width = src.cols,
  12. height = src.rows,
  13. N = width*height,
  14. N3 = N*3;
  15. // Calculate sum of pixel values of each channel
  16. const uchar* src_data = src.ptr<uchar>(0);
  17. unsigned long sum1 = 0, sum2 = 0, sum3 = 0;
  18. int i = 0;
  19. unsigned int minRGB, maxRGB, thresh255 = cvRound(thresh * 255);
  20. for ( ; i < N3; i += 3 )
  21. {
  22. minRGB = std::min(src_data[i], std::min(src_data[i + 1], src_data[i + 2]));
  23. maxRGB = std::max(src_data[i], std::max(src_data[i + 1], src_data[i + 2]));
  24. if ( (maxRGB - minRGB) * 255 > thresh255 * maxRGB ) continue;
  25. sum1 += src_data[i];
  26. sum2 += src_data[i + 1];
  27. sum3 += src_data[i + 2];
  28. }
  29. // Find inverse of averages
  30. double inv1 = sum1 == 0 ? 0.f : (double)N / (double)sum1,
  31. inv2 = sum2 == 0 ? 0.f : (double)N / (double)sum2,
  32. inv3 = sum3 == 0 ? 0.f : (double)N / (double)sum3;
  33. // Find maximum
  34. double inv_max = std::max(std::max(inv1, inv2), inv3);
  35. // Scale by maximum
  36. if ( inv_max > 0 )
  37. {
  38. inv1 = (double) inv1 / inv_max;
  39. inv2 = (double) inv2 / inv_max;
  40. inv3 = (double) inv3 / inv_max;
  41. }
  42. // Fixed point arithmetic, mul by 2^8 then shift back 8 bits
  43. int i_inv1 = cvRound(inv1 * (1 << 8)),
  44. i_inv2 = cvRound(inv2 * (1 << 8)),
  45. i_inv3 = cvRound(inv3 * (1 << 8));
  46. // Scale input pixel values
  47. uchar* dst_data = dst.ptr<uchar>(0);
  48. i = 0;
  49. for ( ; i < N3; i += 3 )
  50. {
  51. dst_data[i] = (uchar)((src_data[i] * i_inv1) >> 8);
  52. dst_data[i + 1] = (uchar)((src_data[i + 1] * i_inv2) >> 8);
  53. dst_data[i + 2] = (uchar)((src_data[i + 2] * i_inv3) >> 8);
  54. }
  55. }
  56. TEST(xphoto_grayworld_white_balance, regression)
  57. {
  58. String dir = cvtest::TS::ptr()->get_data_path() + "cv/xphoto/simple_white_balance/";
  59. const int nTests = 8;
  60. const float wb_thresh = 0.5f;
  61. const float acc_thresh = 2.f;
  62. Ptr<xphoto::GrayworldWB> wb = xphoto::createGrayworldWB();
  63. wb->setSaturationThreshold(wb_thresh);
  64. for ( int i = 0; i < nTests; ++i )
  65. {
  66. String srcName = dir + format("sources/%02d.png", i + 1);
  67. Mat src = imread(srcName, IMREAD_COLOR);
  68. ASSERT_TRUE(!src.empty());
  69. Mat referenceResult;
  70. ref_autowbGrayworld(src, referenceResult, wb_thresh);
  71. Mat currentResult;
  72. wb->balanceWhite(src, currentResult);
  73. ASSERT_LE(cv::norm(currentResult, referenceResult, NORM_INF), acc_thresh);
  74. // test the 16-bit depth:
  75. Mat currentResult_16U, src_16U;
  76. src.convertTo(src_16U, CV_16UC3, 256.0);
  77. wb->balanceWhite(src_16U, currentResult_16U);
  78. currentResult_16U.convertTo(currentResult, CV_8UC3, 1/256.0);
  79. ASSERT_LE(cv::norm(currentResult, referenceResult, NORM_INF), acc_thresh);
  80. }
  81. }
  82. }} // namespace