Merge pull request #2266 from vpisarev:ipp_norm_fix
[profile/ivi/opencv.git] / modules / cudaimgproc / test / test_histogram.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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 #ifdef HAVE_CUDA
46
47 using namespace cvtest;
48
49 ///////////////////////////////////////////////////////////////////////////////////////////////////////
50 // HistEven
51
52 PARAM_TEST_CASE(HistEven, cv::cuda::DeviceInfo, cv::Size)
53 {
54     cv::cuda::DeviceInfo devInfo;
55     cv::Size size;
56
57     virtual void SetUp()
58     {
59         devInfo = GET_PARAM(0);
60         size = GET_PARAM(1);
61
62         cv::cuda::setDevice(devInfo.deviceID());
63     }
64 };
65
66 CUDA_TEST_P(HistEven, Accuracy)
67 {
68     cv::Mat src = randomMat(size, CV_8UC1);
69
70     int hbins = 30;
71     float hranges[] = {50.0f, 200.0f};
72
73     cv::cuda::GpuMat hist;
74     cv::cuda::histEven(loadMat(src), hist, hbins, (int) hranges[0], (int) hranges[1]);
75
76     cv::Mat hist_gold;
77
78     int histSize[] = {hbins};
79     const float* ranges[] = {hranges};
80     int channels[] = {0};
81     cv::calcHist(&src, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);
82
83     hist_gold = hist_gold.t();
84     hist_gold.convertTo(hist_gold, CV_32S);
85
86     EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
87 }
88
89 INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HistEven, testing::Combine(
90     ALL_DEVICES,
91     DIFFERENT_SIZES));
92
93 ///////////////////////////////////////////////////////////////////////////////////////////////////////
94 // CalcHist
95
96 PARAM_TEST_CASE(CalcHist, cv::cuda::DeviceInfo, cv::Size)
97 {
98     cv::cuda::DeviceInfo devInfo;
99
100     cv::Size size;
101
102     virtual void SetUp()
103     {
104         devInfo = GET_PARAM(0);
105         size = GET_PARAM(1);
106
107         cv::cuda::setDevice(devInfo.deviceID());
108     }
109 };
110
111 CUDA_TEST_P(CalcHist, Accuracy)
112 {
113     cv::Mat src = randomMat(size, CV_8UC1);
114
115     cv::cuda::GpuMat hist;
116     cv::cuda::calcHist(loadMat(src), hist);
117
118     cv::Mat hist_gold;
119
120     const int hbins = 256;
121     const float hranges[] = {0.0f, 256.0f};
122     const int histSize[] = {hbins};
123     const float* ranges[] = {hranges};
124     const int channels[] = {0};
125
126     cv::calcHist(&src, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);
127     hist_gold = hist_gold.reshape(1, 1);
128     hist_gold.convertTo(hist_gold, CV_32S);
129
130     EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
131 }
132
133 INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, CalcHist, testing::Combine(
134     ALL_DEVICES,
135     DIFFERENT_SIZES));
136
137 ///////////////////////////////////////////////////////////////////////////////////////////////////////
138 // EqualizeHist
139
140 PARAM_TEST_CASE(EqualizeHist, cv::cuda::DeviceInfo, cv::Size)
141 {
142     cv::cuda::DeviceInfo devInfo;
143     cv::Size size;
144
145     virtual void SetUp()
146     {
147         devInfo = GET_PARAM(0);
148         size = GET_PARAM(1);
149
150         cv::cuda::setDevice(devInfo.deviceID());
151     }
152 };
153
154 CUDA_TEST_P(EqualizeHist, Accuracy)
155 {
156     cv::Mat src = randomMat(size, CV_8UC1);
157
158     cv::cuda::GpuMat dst;
159     cv::cuda::equalizeHist(loadMat(src), dst);
160
161     cv::Mat dst_gold;
162     cv::equalizeHist(src, dst_gold);
163
164     EXPECT_MAT_NEAR(dst_gold, dst, 3.0);
165 }
166
167 INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, EqualizeHist, testing::Combine(
168     ALL_DEVICES,
169     DIFFERENT_SIZES));
170
171 ///////////////////////////////////////////////////////////////////////////////////////////////////////
172 // CLAHE
173
174 namespace
175 {
176     IMPLEMENT_PARAM_CLASS(ClipLimit, double)
177 }
178
179 PARAM_TEST_CASE(CLAHE, cv::cuda::DeviceInfo, cv::Size, ClipLimit)
180 {
181     cv::cuda::DeviceInfo devInfo;
182     cv::Size size;
183     double clipLimit;
184
185     virtual void SetUp()
186     {
187         devInfo = GET_PARAM(0);
188         size = GET_PARAM(1);
189         clipLimit = GET_PARAM(2);
190
191         cv::cuda::setDevice(devInfo.deviceID());
192     }
193 };
194
195 CUDA_TEST_P(CLAHE, Accuracy)
196 {
197     cv::Mat src = randomMat(size, CV_8UC1);
198
199     cv::Ptr<cv::cuda::CLAHE> clahe = cv::cuda::createCLAHE(clipLimit);
200     cv::cuda::GpuMat dst;
201     clahe->apply(loadMat(src), dst);
202
203     cv::Ptr<cv::CLAHE> clahe_gold = cv::createCLAHE(clipLimit);
204     cv::Mat dst_gold;
205     clahe_gold->apply(src, dst_gold);
206
207     ASSERT_MAT_NEAR(dst_gold, dst, 1.0);
208 }
209
210 INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, CLAHE, testing::Combine(
211     ALL_DEVICES,
212     DIFFERENT_SIZES,
213     testing::Values(0.0, 40.0)));
214
215 #endif // HAVE_CUDA