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 // Intel License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
25 //
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 using namespace cv;
45 using namespace std;
46
47 class CV_ThreshTest : public cvtest::ArrayTest
48 {
49 public:
50 CV_ThreshTest();
51
52 protected:
53 void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
54 double get_success_error_level( int test_case_idx, int i, int j );
55 void run_func();
56 void prepare_to_validation( int );
57
58 int thresh_type;
59 float thresh_val;
60 float max_val;
61 };
62
63
CV_ThreshTest()64 CV_ThreshTest::CV_ThreshTest()
65 {
66 test_array[INPUT].push_back(NULL);
67 test_array[OUTPUT].push_back(NULL);
68 test_array[REF_OUTPUT].push_back(NULL);
69 optional_mask = false;
70 element_wise_relative_error = true;
71 }
72
73
get_test_array_types_and_sizes(int test_case_idx,vector<vector<Size>> & sizes,vector<vector<int>> & types)74 void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx,
75 vector<vector<Size> >& sizes, vector<vector<int> >& types )
76 {
77 RNG& rng = ts->get_rng();
78 int depth = cvtest::randInt(rng) % 3, cn = cvtest::randInt(rng) % 4 + 1;
79 cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
80 depth = depth == 0 ? CV_8U : depth == 1 ? CV_16S : CV_32F;
81
82 types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth,cn);
83 thresh_type = cvtest::randInt(rng) % 5;
84
85 if( depth == CV_8U )
86 {
87 thresh_val = (float)(cvtest::randReal(rng)*350. - 50.);
88 max_val = (float)(cvtest::randReal(rng)*350. - 50.);
89 if( cvtest::randInt(rng)%4 == 0 )
90 max_val = 255.f;
91 }
92 else if( depth == CV_16S )
93 {
94 float min_val = SHRT_MIN-100.f;
95 max_val = SHRT_MAX+100.f;
96 thresh_val = (float)(cvtest::randReal(rng)*(max_val - min_val) + min_val);
97 max_val = (float)(cvtest::randReal(rng)*(max_val - min_val) + min_val);
98 if( cvtest::randInt(rng)%4 == 0 )
99 max_val = (float)SHRT_MAX;
100 }
101 else
102 {
103 thresh_val = (float)(cvtest::randReal(rng)*1000. - 500.);
104 max_val = (float)(cvtest::randReal(rng)*1000. - 500.);
105 }
106 }
107
108
get_success_error_level(int,int,int)109 double CV_ThreshTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
110 {
111 return FLT_EPSILON*10;
112 }
113
114
run_func()115 void CV_ThreshTest::run_func()
116 {
117 cvThreshold( test_array[INPUT][0], test_array[OUTPUT][0],
118 thresh_val, max_val, thresh_type );
119 }
120
121
test_threshold(const Mat & _src,Mat & _dst,float thresh,float maxval,int thresh_type)122 static void test_threshold( const Mat& _src, Mat& _dst,
123 float thresh, float maxval, int thresh_type )
124 {
125 int i, j;
126 int depth = _src.depth(), cn = _src.channels();
127 int width_n = _src.cols*cn, height = _src.rows;
128 int ithresh = cvFloor(thresh);
129 int imaxval, ithresh2;
130
131 if( depth == CV_8U )
132 {
133 ithresh2 = saturate_cast<uchar>(ithresh);
134 imaxval = saturate_cast<uchar>(maxval);
135 }
136 else if( depth == CV_16S )
137 {
138 ithresh2 = saturate_cast<short>(ithresh);
139 imaxval = saturate_cast<short>(maxval);
140 }
141 else
142 {
143 ithresh2 = cvRound(ithresh);
144 imaxval = cvRound(maxval);
145 }
146
147 assert( depth == CV_8U || depth == CV_16S || depth == CV_32F );
148
149 switch( thresh_type )
150 {
151 case CV_THRESH_BINARY:
152 for( i = 0; i < height; i++ )
153 {
154 if( depth == CV_8U )
155 {
156 const uchar* src = _src.ptr<uchar>(i);
157 uchar* dst = _dst.ptr<uchar>(i);
158 for( j = 0; j < width_n; j++ )
159 dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0);
160 }
161 else if( depth == CV_16S )
162 {
163 const short* src = _src.ptr<short>(i);
164 short* dst = _dst.ptr<short>(i);
165 for( j = 0; j < width_n; j++ )
166 dst[j] = (short)(src[j] > ithresh ? imaxval : 0);
167 }
168 else
169 {
170 const float* src = _src.ptr<float>(i);
171 float* dst = _dst.ptr<float>(i);
172 for( j = 0; j < width_n; j++ )
173 dst[j] = src[j] > thresh ? maxval : 0.f;
174 }
175 }
176 break;
177 case CV_THRESH_BINARY_INV:
178 for( i = 0; i < height; i++ )
179 {
180 if( depth == CV_8U )
181 {
182 const uchar* src = _src.ptr<uchar>(i);
183 uchar* dst = _dst.ptr<uchar>(i);
184 for( j = 0; j < width_n; j++ )
185 dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval);
186 }
187 else if( depth == CV_16S )
188 {
189 const short* src = _src.ptr<short>(i);
190 short* dst = _dst.ptr<short>(i);
191 for( j = 0; j < width_n; j++ )
192 dst[j] = (short)(src[j] > ithresh ? 0 : imaxval);
193 }
194 else
195 {
196 const float* src = _src.ptr<float>(i);
197 float* dst = _dst.ptr<float>(i);
198 for( j = 0; j < width_n; j++ )
199 dst[j] = src[j] > thresh ? 0.f : maxval;
200 }
201 }
202 break;
203 case CV_THRESH_TRUNC:
204 for( i = 0; i < height; i++ )
205 {
206 if( depth == CV_8U )
207 {
208 const uchar* src = _src.ptr<uchar>(i);
209 uchar* dst = _dst.ptr<uchar>(i);
210 for( j = 0; j < width_n; j++ )
211 {
212 int s = src[j];
213 dst[j] = (uchar)(s > ithresh ? ithresh2 : s);
214 }
215 }
216 else if( depth == CV_16S )
217 {
218 const short* src = _src.ptr<short>(i);
219 short* dst = _dst.ptr<short>(i);
220 for( j = 0; j < width_n; j++ )
221 {
222 int s = src[j];
223 dst[j] = (short)(s > ithresh ? ithresh2 : s);
224 }
225 }
226 else
227 {
228 const float* src = _src.ptr<float>(i);
229 float* dst = _dst.ptr<float>(i);
230 for( j = 0; j < width_n; j++ )
231 {
232 float s = src[j];
233 dst[j] = s > thresh ? thresh : s;
234 }
235 }
236 }
237 break;
238 case CV_THRESH_TOZERO:
239 for( i = 0; i < height; i++ )
240 {
241 if( depth == CV_8U )
242 {
243 const uchar* src = _src.ptr<uchar>(i);
244 uchar* dst = _dst.ptr<uchar>(i);
245 for( j = 0; j < width_n; j++ )
246 {
247 int s = src[j];
248 dst[j] = (uchar)(s > ithresh ? s : 0);
249 }
250 }
251 else if( depth == CV_16S )
252 {
253 const short* src = _src.ptr<short>(i);
254 short* dst = _dst.ptr<short>(i);
255 for( j = 0; j < width_n; j++ )
256 {
257 int s = src[j];
258 dst[j] = (short)(s > ithresh ? s : 0);
259 }
260 }
261 else
262 {
263 const float* src = _src.ptr<float>(i);
264 float* dst = _dst.ptr<float>(i);
265 for( j = 0; j < width_n; j++ )
266 {
267 float s = src[j];
268 dst[j] = s > thresh ? s : 0.f;
269 }
270 }
271 }
272 break;
273 case CV_THRESH_TOZERO_INV:
274 for( i = 0; i < height; i++ )
275 {
276 if( depth == CV_8U )
277 {
278 const uchar* src = _src.ptr<uchar>(i);
279 uchar* dst = _dst.ptr<uchar>(i);
280 for( j = 0; j < width_n; j++ )
281 {
282 int s = src[j];
283 dst[j] = (uchar)(s > ithresh ? 0 : s);
284 }
285 }
286 else if( depth == CV_16S )
287 {
288 const short* src = _src.ptr<short>(i);
289 short* dst = _dst.ptr<short>(i);
290 for( j = 0; j < width_n; j++ )
291 {
292 int s = src[j];
293 dst[j] = (short)(s > ithresh ? 0 : s);
294 }
295 }
296 else
297 {
298 const float* src = _src.ptr<float>(i);
299 float* dst = _dst.ptr<float>(i);
300 for( j = 0; j < width_n; j++ )
301 {
302 float s = src[j];
303 dst[j] = s > thresh ? 0.f : s;
304 }
305 }
306 }
307 break;
308 default:
309 assert(0);
310 }
311 }
312
313
prepare_to_validation(int)314 void CV_ThreshTest::prepare_to_validation( int /*test_case_idx*/ )
315 {
316 test_threshold( test_mat[INPUT][0], test_mat[REF_OUTPUT][0],
317 thresh_val, max_val, thresh_type );
318 }
319
TEST(Imgproc_Threshold,accuracy)320 TEST(Imgproc_Threshold, accuracy) { CV_ThreshTest test; test.safe_run(); }
321