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