CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / test / test_color.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Peng Xiao, pengxiao@multicorewareinc.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other oclMaterials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #include "precomp.hpp"
47 #ifdef HAVE_OPENCL
48
49 //#define MAT_DEBUG
50 #ifdef MAT_DEBUG
51 #define MAT_DIFF(mat, mat2)\
52 {\
53     for(int i = 0; i < mat.rows; i ++)\
54     {\
55         for(int j = 0; j < mat.cols; j ++)\
56         {\
57             cv::Vec4b s = mat.at<cv::Vec4b>(i, j);\
58             cv::Vec4b s2 = mat2.at<cv::Vec4b>(i, j);\
59             if(s != s2) printf("*");\
60             else printf(".");\
61         }\
62         puts("\n");\
63     }\
64 }
65 #else
66 #define MAT_DIFF(mat, mat2)
67 #endif
68
69
70 namespace
71 {
72
73 ///////////////////////////////////////////////////////////////////////////////////////////////////////
74 // cvtColor
75 PARAM_TEST_CASE(CvtColor, cv::Size, MatDepth)
76 {
77     cv::Size size;
78     int depth;
79     bool useRoi;
80
81     cv::Mat img;
82
83     virtual void SetUp()
84     {
85         size = GET_PARAM(0);
86         depth = GET_PARAM(1);
87
88         img = randomMat(size, CV_MAKE_TYPE(depth, 3), 0.0, depth == CV_32F ? 1.0 : 255.0);
89     }
90 };
91
92 #define CVTCODE(name) cv::COLOR_ ## name
93 #define TEST_P_CVTCOLOR(name) TEST_P(CvtColor, name)\
94 {\
95     cv::Mat src = img;\
96     cv::ocl::oclMat ocl_img, dst;\
97     ocl_img.upload(img);\
98     cv::ocl::cvtColor(ocl_img, dst, CVTCODE(name));\
99     cv::Mat dst_gold;\
100     cv::cvtColor(src, dst_gold, CVTCODE(name));\
101     cv::Mat dst_mat;\
102     dst.download(dst_mat);\
103     EXPECT_MAT_NEAR(dst_gold, dst_mat, 1e-5);\
104 }
105
106 //add new ones here using macro
107 TEST_P_CVTCOLOR(RGB2GRAY)
108 TEST_P_CVTCOLOR(BGR2GRAY)
109 TEST_P_CVTCOLOR(RGBA2GRAY)
110 TEST_P_CVTCOLOR(BGRA2GRAY)
111
112 TEST_P_CVTCOLOR(RGB2YUV)
113 TEST_P_CVTCOLOR(BGR2YUV)
114 TEST_P_CVTCOLOR(YUV2RGB)
115 TEST_P_CVTCOLOR(YUV2BGR)
116 TEST_P_CVTCOLOR(RGB2YCrCb)
117 TEST_P_CVTCOLOR(BGR2YCrCb)
118
119 PARAM_TEST_CASE(CvtColor_Gray2RGB, cv::Size, MatDepth, int)
120 {
121     cv::Size size;
122     int code;
123     int depth;
124     cv::Mat img;
125
126     virtual void SetUp()
127     {
128         size  = GET_PARAM(0);
129         depth = GET_PARAM(1);
130         code  = GET_PARAM(2);
131         img   = randomMat(size, CV_MAKETYPE(depth, 1), 0.0, depth == CV_32F ? 1.0 : 255.0);
132     }
133 };
134 TEST_P(CvtColor_Gray2RGB, Accuracy)
135 {
136     cv::Mat src = img;
137     cv::ocl::oclMat ocl_img, dst;
138     ocl_img.upload(src);
139     cv::ocl::cvtColor(ocl_img, dst, code);
140     cv::Mat dst_gold;
141     cv::cvtColor(src, dst_gold, code);
142     cv::Mat dst_mat;
143     dst.download(dst_mat);
144     EXPECT_MAT_NEAR(dst_gold, dst_mat, 1e-5);
145 }
146
147
148 PARAM_TEST_CASE(CvtColor_YUV420, cv::Size, int)
149 {
150     cv::Size size;
151     int code;
152
153     cv::Mat img;
154
155     virtual void SetUp()
156     {
157         size = GET_PARAM(0);
158         code = GET_PARAM(1);
159         img  = randomMat(size, CV_8UC1, 0.0, 255.0);
160     }
161 };
162
163 TEST_P(CvtColor_YUV420, Accuracy)
164 {
165     cv::Mat src = img;
166     cv::ocl::oclMat ocl_img, dst;
167     ocl_img.upload(src);
168     cv::ocl::cvtColor(ocl_img, dst, code);
169     cv::Mat dst_gold;
170     cv::cvtColor(src, dst_gold, code);
171     cv::Mat dst_mat;
172     dst.download(dst_mat);
173     MAT_DIFF(dst_mat, dst_gold);
174     EXPECT_MAT_NEAR(dst_gold, dst_mat, 1e-5);
175 }
176
177 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, CvtColor, testing::Combine(
178                             DIFFERENT_SIZES,
179                             testing::Values(MatDepth(CV_8U), MatDepth(CV_16U), MatDepth(CV_32F))
180                         ));
181
182 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, CvtColor_YUV420, testing::Combine(
183                             testing::Values(cv::Size(128, 45), cv::Size(46, 132), cv::Size(1024, 1023)),
184                             testing::Values((int)CV_YUV2RGBA_NV12, (int)CV_YUV2BGRA_NV12, (int)CV_YUV2RGB_NV12, (int)CV_YUV2BGR_NV12)
185                         ));
186
187 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, CvtColor_Gray2RGB, testing::Combine(
188                             DIFFERENT_SIZES,
189                             testing::Values(MatDepth(CV_8U), MatDepth(CV_16U), MatDepth(CV_32F)),
190                             testing::Values((int)CV_GRAY2BGR, (int)CV_GRAY2BGRA, (int)CV_GRAY2RGB, (int)CV_GRAY2RGBA)
191                         ));
192 }
193 #endif