test_oil_painting.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. Mat testOilPainting(Mat imgSrc, int halfSize, int dynRatio, int colorSpace)
  7. {
  8. vector<int> histogramme;
  9. vector<Vec3f> moyenneRGB;
  10. Mat dst(imgSrc.size(), imgSrc.type());
  11. Mat lum;
  12. if (imgSrc.channels() != 1)
  13. {
  14. cvtColor(imgSrc, lum, colorSpace);
  15. if (lum.channels() > 1)
  16. {
  17. extractChannel(lum, lum, 0);
  18. }
  19. }
  20. else
  21. lum = imgSrc.clone();
  22. lum = lum / dynRatio;
  23. if (dst.channels() == 3)
  24. for (int y = 0; y < imgSrc.rows; y++)
  25. {
  26. Vec3b *vDst = dst.ptr<Vec3b>(y);
  27. for (int x = 0; x < imgSrc.cols; x++, vDst++) //for each pixel
  28. {
  29. Mat mask(lum.size(), CV_8UC1, Scalar::all(0));
  30. Rect r(Point(x - halfSize, y - halfSize), Size(2 * halfSize + 1, 2 * halfSize + 1));
  31. r = r & Rect(Point(0, 0), lum.size());
  32. mask(r).setTo(255);
  33. int histSize[] = { 256 };
  34. float hranges[] = { 0, 256 };
  35. const float* ranges[] = { hranges };
  36. Mat hist;
  37. int channels[] = { 0 };
  38. calcHist(&lum, 1, channels, mask, hist, 1, histSize, ranges, true, false);
  39. double maxVal = 0;
  40. Point pMin, pMax;
  41. minMaxLoc(hist, 0, &maxVal, &pMin, &pMax);
  42. mask.setTo(0, lum != static_cast<int>(pMax.y));
  43. Scalar v = mean(imgSrc, mask);
  44. *vDst = Vec3b(static_cast<uchar>(v[0]), static_cast<uchar>(v[1]), static_cast<uchar>(v[2]));
  45. }
  46. }
  47. else
  48. for (int y = 0; y < imgSrc.rows; y++)
  49. {
  50. uchar *vDst = dst.ptr<uchar>(y);
  51. for (int x = 0; x < imgSrc.cols; x++, vDst++) //for each pixel
  52. {
  53. Mat mask(lum.size(), CV_8UC1, Scalar::all(0));
  54. Rect r(Point(x - halfSize, y - halfSize), Size(2 * halfSize + 1, 2 * halfSize + 1));
  55. r = r & Rect(Point(0, 0), lum.size());
  56. mask(r).setTo(255);
  57. int histSize[] = { 256 };
  58. float hranges[] = { 0, 256 };
  59. const float* ranges[] = { hranges };
  60. Mat hist;
  61. int channels[] = { 0 };
  62. calcHist(&lum, 1, channels, mask, hist, 1, histSize, ranges, true, false);
  63. double maxVal = 0;
  64. Point pMin, pMax;
  65. minMaxLoc(hist, 0, &maxVal, &pMin, &pMax);
  66. mask.setTo(0, lum != static_cast<int>(pMax.y));
  67. Scalar v = mean(imgSrc, mask);
  68. *vDst = static_cast<uchar>(v[0]);
  69. }
  70. }
  71. return dst;
  72. }
  73. TEST(xphoto_oil_painting, regression)
  74. {
  75. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cv/inpaint/";
  76. Mat orig = imread(folder+"exp1.png", IMREAD_COLOR);
  77. ASSERT_TRUE(!orig.empty());
  78. resize(orig, orig, Size(100, 100));
  79. Mat dst1, dst2, dd;
  80. xphoto::oilPainting(orig, dst1, 3, 5, COLOR_BGR2GRAY);
  81. dst2 = testOilPainting(orig, 3, 5, COLOR_BGR2GRAY);
  82. absdiff(dst1, dst2, dd);
  83. vector<Mat> plane;
  84. split(dd, plane);
  85. for (auto p : plane)
  86. {
  87. double maxVal;
  88. Point pIdx;
  89. minMaxLoc(p, NULL, &maxVal, NULL, &pIdx);
  90. ASSERT_LE(p.at<uchar>(pIdx), 2);
  91. }
  92. Mat orig2 = imread(folder + "exp1.png",IMREAD_GRAYSCALE);
  93. ASSERT_TRUE(!orig2.empty());
  94. resize(orig2, orig2, Size(100, 100));
  95. Mat dst3, dst4, ddd;
  96. xphoto::oilPainting(orig2, dst3, 3, 5, COLOR_BGR2GRAY);
  97. dst4 = testOilPainting(orig2, 3, 5, COLOR_BGR2GRAY);
  98. absdiff(dst3, dst4, ddd);
  99. double maxVal;
  100. Point pIdx;
  101. minMaxLoc(ddd, NULL, &maxVal, NULL, &pIdx);
  102. ASSERT_LE(ddd.at<uchar>(pIdx), 2);
  103. }
  104. }} // namespace