Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/ocl/test_histogram.cpp
16344 views
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, Institute Of Software Chinese Academy Of Science, all rights reserved.
14
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16
// Copyright (C) 2014, Itseez, Inc., all rights reserved.
17
// Third party copyrights are property of their respective owners.
18
//
19
// @Authors
20
// Niko Li, [email protected]
21
// Jia Haipeng, [email protected]
22
// Shengen Yan, [email protected]
23
// Jiang Liyuan, [email protected]
24
// Rock Li, [email protected]
25
// Wu Zailong, [email protected]
26
// Xu Pang, [email protected]
27
// Sen Liu, [email protected]
28
//
29
// Redistribution and use in source and binary forms, with or without modification,
30
// are permitted provided that the following conditions are met:
31
//
32
// * Redistribution's of source code must retain the above copyright notice,
33
// this list of conditions and the following disclaimer.
34
//
35
// * Redistribution's in binary form must reproduce the above copyright notice,
36
// this list of conditions and the following disclaimer in the documentation
37
// and/or other materials provided with the distribution.
38
//
39
// * The name of the copyright holders may not be used to endorse or promote products
40
// derived from this software without specific prior written permission.
41
//
42
// This software is provided by the copyright holders and contributors "as is" and
43
// any express or implied warranties, including, but not limited to, the implied
44
// warranties of merchantability and fitness for a particular purpose are disclaimed.
45
// In no event shall the Intel Corporation or contributors be liable for any direct,
46
// indirect, incidental, special, exemplary, or consequential damages
47
// (including, but not limited to, procurement of substitute goods or services;
48
// loss of use, data, or profits; or business interruption) however caused
49
// and on any theory of liability, whether in contract, strict liability,
50
// or tort (including negligence or otherwise) arising in any way out of
51
// the use of this software, even if advised of the possibility of such damage.
52
//
53
//M*/
54
55
#include "../test_precomp.hpp"
56
#include "opencv2/ts/ocl_test.hpp"
57
58
#ifdef HAVE_OPENCL
59
60
namespace opencv_test {
61
namespace ocl {
62
63
///////////////////////////////////////////////////////////////////////////////
64
65
PARAM_TEST_CASE(CalcBackProject, MatDepth, int, bool)
66
{
67
int depth, N;
68
bool useRoi;
69
70
std::vector<float> ranges;
71
std::vector<int> channels;
72
double scale;
73
74
std::vector<Mat> images;
75
std::vector<Mat> images_roi;
76
std::vector<UMat> uimages;
77
std::vector<UMat> uimages_roi;
78
79
TEST_DECLARE_INPUT_PARAMETER(hist);
80
TEST_DECLARE_OUTPUT_PARAMETER(dst);
81
82
virtual void SetUp()
83
{
84
depth = GET_PARAM(0);
85
N = GET_PARAM(1);
86
useRoi = GET_PARAM(2);
87
88
ASSERT_GE(2, N);
89
90
images.resize(N);
91
images_roi.resize(N);
92
uimages.resize(N);
93
uimages_roi.resize(N);
94
}
95
96
void random_roi()
97
{
98
Size roiSize = randomSize(1, MAX_VALUE);
99
100
int totalChannels = 0;
101
102
ranges.clear();
103
channels.clear();
104
105
for (int i = 0; i < N; ++i)
106
{
107
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
108
int cn = randomInt(1, 5);
109
randomSubMat(images[i], images_roi[i], roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 0, 125);
110
111
ranges.push_back(10);
112
ranges.push_back(100);
113
114
channels.push_back(randomInt(0, cn) + totalChannels);
115
totalChannels += cn;
116
}
117
118
Mat tmpHist;
119
{
120
std::vector<int> hist_size(N);
121
for (int i = 0 ; i < N; ++i)
122
hist_size[i] = randomInt(10, 50);
123
124
cv::calcHist(images_roi, channels, noArray(), tmpHist, hist_size, ranges);
125
ASSERT_EQ(CV_32FC1, tmpHist.type());
126
}
127
128
Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
129
randomSubMat(hist, hist_roi, tmpHist.size(), histBorder, tmpHist.type(), 0, MAX_VALUE);
130
tmpHist.copyTo(hist_roi);
131
132
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
133
randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, 1), 5, 16);
134
135
for (int i = 0; i < N; ++i)
136
{
137
images[i].copyTo(uimages[i]);
138
139
Size _wholeSize;
140
Point ofs;
141
images_roi[i].locateROI(_wholeSize, ofs);
142
143
uimages_roi[i] = uimages[i](Rect(ofs.x, ofs.y, images_roi[i].cols, images_roi[i].rows));
144
}
145
146
UMAT_UPLOAD_INPUT_PARAMETER(hist);
147
UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
148
149
scale = randomDouble(0.1, 1);
150
}
151
152
void test_by_pict()
153
{
154
Mat frame1 = readImage("optflow/RubberWhale1.png", IMREAD_GRAYSCALE);
155
156
UMat usrc;
157
frame1.copyTo(usrc);
158
int histSize = randomInt(3, 29);
159
float hue_range[] = { 0, 180 };
160
const float* ranges1 = { hue_range };
161
Mat hist1;
162
163
//compute histogram
164
calcHist(&frame1, 1, 0, Mat(), hist1, 1, &histSize, &ranges1, true, false);
165
normalize(hist1, hist1, 0, 255, NORM_MINMAX, -1, Mat());
166
167
Mat dst1;
168
UMat udst1, src, uhist1;
169
hist1.copyTo(uhist1);
170
std::vector<UMat> uims;
171
uims.push_back(usrc);
172
std::vector<float> urngs;
173
urngs.push_back(0);
174
urngs.push_back(180);
175
std::vector<int> chs;
176
chs.push_back(0);
177
178
OCL_OFF(calcBackProject(&frame1, 1, 0, hist1, dst1, &ranges1, 1, true));
179
OCL_ON(calcBackProject(uims, chs, uhist1, udst1, urngs, 1.0));
180
181
if (cv::ocl::useOpenCL() && cv::ocl::Device::getDefault().isAMD())
182
{
183
Size dstSize = dst1.size();
184
int nDiffs = (int)(0.03f*dstSize.height*dstSize.width);
185
186
//check if the dst mats are the same except 3% difference
187
EXPECT_MAT_N_DIFF(dst1, udst1, nDiffs);
188
}
189
else
190
{
191
EXPECT_MAT_NEAR(dst1, udst1, 0.0);
192
}
193
}
194
};
195
196
//////////////////////////////// CalcBackProject //////////////////////////////////////////////
197
198
OCL_TEST_P(CalcBackProject, Mat)
199
{
200
for (int j = 0; j < test_loop_times; j++)
201
{
202
random_roi();
203
204
OCL_OFF(cv::calcBackProject(images_roi, channels, hist_roi, dst_roi, ranges, scale));
205
OCL_ON(cv::calcBackProject(uimages_roi, channels, uhist_roi, udst_roi, ranges, scale));
206
207
Size dstSize = dst_roi.size();
208
int nDiffs = std::max((int)(0.07f*dstSize.area()), 1);
209
210
//check if the dst mats are the same except 7% difference
211
EXPECT_MAT_N_DIFF(dst_roi, udst_roi, nDiffs);
212
}
213
}
214
215
OCL_TEST_P(CalcBackProject, Mat_RealImage)
216
{
217
//check on given image
218
test_by_pict();
219
}
220
221
//////////////////////////////// CalcHist //////////////////////////////////////////////
222
223
PARAM_TEST_CASE(CalcHist, bool)
224
{
225
bool useRoi;
226
227
TEST_DECLARE_INPUT_PARAMETER(src);
228
TEST_DECLARE_OUTPUT_PARAMETER(hist);
229
230
virtual void SetUp()
231
{
232
useRoi = GET_PARAM(0);
233
}
234
235
void random_roi()
236
{
237
Size roiSize = randomSize(1, MAX_VALUE);
238
239
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
240
randomSubMat(src, src_roi, roiSize, srcBorder, CV_8UC1, 0, 256);
241
242
Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
243
randomSubMat(hist, hist_roi, Size(1, 256), histBorder, CV_32SC1, 0, MAX_VALUE);
244
245
UMAT_UPLOAD_INPUT_PARAMETER(src);
246
UMAT_UPLOAD_OUTPUT_PARAMETER(hist);
247
}
248
};
249
250
OCL_TEST_P(CalcHist, Mat)
251
{
252
const std::vector<int> channels(1, 0);
253
std::vector<float> ranges(2);
254
std::vector<int> histSize(1, 256);
255
ranges[0] = 0;
256
ranges[1] = 256;
257
258
for (int j = 0; j < test_loop_times; j++)
259
{
260
random_roi();
261
262
OCL_OFF(cv::calcHist(std::vector<Mat>(1, src_roi), channels, noArray(), hist_roi, histSize, ranges, false));
263
OCL_ON(cv::calcHist(std::vector<UMat>(1, usrc_roi), channels, noArray(), uhist_roi, histSize, ranges, false));
264
265
OCL_EXPECT_MATS_NEAR(hist, 0.0);
266
}
267
}
268
269
/////////////////////////////////////////////////////////////////////////////////////
270
271
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcBackProject, Combine(Values((MatDepth)CV_8U), Values(1, 2), Bool()));
272
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcHist, Values(true, false));
273
274
} } // namespace opencv_test::ocl
275
276
#endif // HAVE_OPENCL
277
278