test_thresh.cpp 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"

44
namespace opencv_test { namespace {
45 46 47 48 49 50 51 52 53 54 55 56 57

class CV_ThreshTest : public cvtest::ArrayTest
{
public:
    CV_ThreshTest();

protected:
    void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
    double get_success_error_level( int test_case_idx, int i, int j );
    void run_func();
    void prepare_to_validation( int );

    int thresh_type;
58 59
    double thresh_val;
    double max_val;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
};


CV_ThreshTest::CV_ThreshTest()
{
    test_array[INPUT].push_back(NULL);
    test_array[OUTPUT].push_back(NULL);
    test_array[REF_OUTPUT].push_back(NULL);
    optional_mask = false;
    element_wise_relative_error = true;
}


void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx,
                                                vector<vector<Size> >& sizes, vector<vector<int> >& types )
{
    RNG& rng = ts->get_rng();
77
    int depth = cvtest::randInt(rng) % 5, cn = cvtest::randInt(rng) % 4 + 1;
78
    cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
79
    depth = depth == 0 ? CV_8U : depth == 1 ? CV_16S : depth == 2 ? CV_16U : depth == 3 ? CV_32F : CV_64F;
80 81 82 83 84 85

    types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth,cn);
    thresh_type = cvtest::randInt(rng) % 5;

    if( depth == CV_8U )
    {
86 87
        thresh_val = (cvtest::randReal(rng)*350. - 50.);
        max_val = (cvtest::randReal(rng)*350. - 50.);
88
        if( cvtest::randInt(rng)%4 == 0 )
89 90 91 92
            max_val = 255.f;
    }
    else if( depth == CV_16S )
    {
93
        double min_val = SHRT_MIN-100.f;
94
        max_val = SHRT_MAX+100.f;
95 96
        thresh_val = (cvtest::randReal(rng)*(max_val - min_val) + min_val);
        max_val = (cvtest::randReal(rng)*(max_val - min_val) + min_val);
97
        if( cvtest::randInt(rng)%4 == 0 )
98
            max_val = (double)SHRT_MAX;
99
    }
100 101 102 103 104 105 106 107 108
    else if( depth == CV_16U )
    {
        double min_val = -100.f;
        max_val = USHRT_MAX+100.f;
        thresh_val = (cvtest::randReal(rng)*(max_val - min_val) + min_val);
        max_val = (cvtest::randReal(rng)*(max_val - min_val) + min_val);
        if( cvtest::randInt(rng)%4 == 0 )
            max_val = (double)USHRT_MAX;
    }
109 110
    else
    {
111 112
        thresh_val = (cvtest::randReal(rng)*1000. - 500.);
        max_val = (cvtest::randReal(rng)*1000. - 500.);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
}


double CV_ThreshTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
{
    return FLT_EPSILON*10;
}


void CV_ThreshTest::run_func()
{
    cvThreshold( test_array[INPUT][0], test_array[OUTPUT][0],
                 thresh_val, max_val, thresh_type );
}


static void test_threshold( const Mat& _src, Mat& _dst,
131
                            double thresh, double maxval, int thresh_type )
132 133 134 135
{
    int i, j;
    int depth = _src.depth(), cn = _src.channels();
    int width_n = _src.cols*cn, height = _src.rows;
136 137
    int ithresh = cvFloor(thresh);
    int imaxval, ithresh2;
138

139 140 141
    if( depth == CV_8U )
    {
        ithresh2 = saturate_cast<uchar>(ithresh);
142
        imaxval = saturate_cast<uchar>(maxval);
143 144 145 146
    }
    else if( depth == CV_16S )
    {
        ithresh2 = saturate_cast<short>(ithresh);
147 148
        imaxval = saturate_cast<short>(maxval);
    }
149 150 151 152 153
    else if( depth == CV_16U )
    {
        ithresh2 = saturate_cast<ushort>(ithresh);
        imaxval = saturate_cast<ushort>(maxval);
    }
154 155 156 157
    else
    {
        ithresh2 = cvRound(ithresh);
        imaxval = cvRound(maxval);
158
    }
159

160
    assert( depth == CV_8U || depth == CV_16S || depth == CV_16U || depth == CV_32F || depth == CV_64F );
161

162 163 164
    switch( thresh_type )
    {
    case CV_THRESH_BINARY:
165
        for( i = 0; i < height; i++ )
166 167
        {
            if( depth == CV_8U )
168 169 170
            {
                const uchar* src = _src.ptr<uchar>(i);
                uchar* dst = _dst.ptr<uchar>(i);
171 172
                for( j = 0; j < width_n; j++ )
                    dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0);
173 174 175 176 177 178 179 180
            }
            else if( depth == CV_16S )
            {
                const short* src = _src.ptr<short>(i);
                short* dst = _dst.ptr<short>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = (short)(src[j] > ithresh ? imaxval : 0);
            }
181 182 183 184 185 186 187
            else if( depth == CV_16U )
            {
                const ushort* src = _src.ptr<ushort>(i);
                ushort* dst = _dst.ptr<ushort>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = (ushort)(src[j] > ithresh ? imaxval : 0);
            }
188
            else if( depth == CV_32F )
189 190 191
            {
                const float* src = _src.ptr<float>(i);
                float* dst = _dst.ptr<float>(i);
192
                for( j = 0; j < width_n; j++ )
atinfinity's avatar
atinfinity committed
193
                    dst[j] = (float)(src[j] > thresh ? maxval : 0.f);
194
            }
195 196 197 198 199 200 201
            else
            {
                const double* src = _src.ptr<double>(i);
                double* dst = _dst.ptr<double>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = src[j] > thresh ? maxval : 0.0;
            }
202 203 204
        }
        break;
    case CV_THRESH_BINARY_INV:
205
        for( i = 0; i < height; i++ )
206 207
        {
            if( depth == CV_8U )
208 209 210
            {
                const uchar* src = _src.ptr<uchar>(i);
                uchar* dst = _dst.ptr<uchar>(i);
211 212
                for( j = 0; j < width_n; j++ )
                    dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval);
213 214 215 216 217 218 219 220
            }
            else if( depth == CV_16S )
            {
                const short* src = _src.ptr<short>(i);
                short* dst = _dst.ptr<short>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = (short)(src[j] > ithresh ? 0 : imaxval);
            }
221 222 223 224 225 226 227
            else if( depth == CV_16U )
            {
                const ushort* src = _src.ptr<ushort>(i);
                ushort* dst = _dst.ptr<ushort>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = (ushort)(src[j] > ithresh ? 0 : imaxval);
            }
228
            else if( depth == CV_32F )
229 230 231
            {
                const float* src = _src.ptr<float>(i);
                float* dst = _dst.ptr<float>(i);
232
                for( j = 0; j < width_n; j++ )
atinfinity's avatar
atinfinity committed
233
                    dst[j] = (float)(src[j] > thresh ? 0.f : maxval);
234
            }
235 236 237 238 239 240 241
            else
            {
                const double* src = _src.ptr<double>(i);
                double* dst = _dst.ptr<double>(i);
                for( j = 0; j < width_n; j++ )
                    dst[j] = src[j] > thresh ? 0.0 : maxval;
            }
242 243 244
        }
        break;
    case CV_THRESH_TRUNC:
245
        for( i = 0; i < height; i++ )
246 247
        {
            if( depth == CV_8U )
248 249 250
            {
                const uchar* src = _src.ptr<uchar>(i);
                uchar* dst = _dst.ptr<uchar>(i);
251 252 253 254 255
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (uchar)(s > ithresh ? ithresh2 : s);
                }
256 257 258 259 260 261 262 263 264 265 266
            }
            else if( depth == CV_16S )
            {
                const short* src = _src.ptr<short>(i);
                short* dst = _dst.ptr<short>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (short)(s > ithresh ? ithresh2 : s);
                }
            }
267 268 269 270 271 272 273 274 275 276
            else if( depth == CV_16U )
            {
                const ushort* src = _src.ptr<ushort>(i);
                ushort* dst = _dst.ptr<ushort>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (ushort)(s > ithresh ? ithresh2 : s);
                }
            }
277
            else if( depth == CV_32F )
278 279 280
            {
                const float* src = _src.ptr<float>(i);
                float* dst = _dst.ptr<float>(i);
281 282
                for( j = 0; j < width_n; j++ )
                {
283
                    float s = src[j];
atinfinity's avatar
atinfinity committed
284
                    dst[j] = (float)(s > thresh ? thresh : s);
285
                }
286
            }
287 288 289 290 291 292 293 294 295 296
            else
            {
                const double* src = _src.ptr<double>(i);
                double* dst = _dst.ptr<double>(i);
                for( j = 0; j < width_n; j++ )
                {
                    double s = src[j];
                    dst[j] = s > thresh ? thresh : s;
                }
            }
297 298 299
        }
        break;
    case CV_THRESH_TOZERO:
300
        for( i = 0; i < height; i++ )
301 302
        {
            if( depth == CV_8U )
303 304 305
            {
                const uchar* src = _src.ptr<uchar>(i);
                uchar* dst = _dst.ptr<uchar>(i);
306 307 308 309 310
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (uchar)(s > ithresh ? s : 0);
                }
311 312 313 314 315 316 317 318 319 320 321
            }
            else if( depth == CV_16S )
            {
                const short* src = _src.ptr<short>(i);
                short* dst = _dst.ptr<short>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (short)(s > ithresh ? s : 0);
                }
            }
322 323 324 325 326 327 328 329 330 331
            else if( depth == CV_16U )
            {
                const ushort* src = _src.ptr<ushort>(i);
                ushort* dst = _dst.ptr<ushort>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (ushort)(s > ithresh ? s : 0);
                }
            }
332
            else if( depth == CV_32F )
333 334 335
            {
                const float* src = _src.ptr<float>(i);
                float* dst = _dst.ptr<float>(i);
336 337
                for( j = 0; j < width_n; j++ )
                {
338 339
                    float s = src[j];
                    dst[j] = s > thresh ? s : 0.f;
340
                }
341
            }
342 343 344 345 346 347 348 349 350 351
            else
            {
                const double* src = _src.ptr<double>(i);
                double* dst = _dst.ptr<double>(i);
                for( j = 0; j < width_n; j++ )
                {
                    double s = src[j];
                    dst[j] = s > thresh ? s : 0.0;
                }
            }
352 353 354
        }
        break;
    case CV_THRESH_TOZERO_INV:
355
        for( i = 0; i < height; i++ )
356 357
        {
            if( depth == CV_8U )
358 359 360
            {
                const uchar* src = _src.ptr<uchar>(i);
                uchar* dst = _dst.ptr<uchar>(i);
361 362 363 364 365
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (uchar)(s > ithresh ? 0 : s);
                }
366 367 368 369 370 371 372 373 374 375
            }
            else if( depth == CV_16S )
            {
                const short* src = _src.ptr<short>(i);
                short* dst = _dst.ptr<short>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (short)(s > ithresh ? 0 : s);
                }
376 377 378 379 380 381 382 383 384 385
            }
            else if( depth == CV_16U )
            {
                const ushort* src = _src.ptr<ushort>(i);
                ushort* dst = _dst.ptr<ushort>(i);
                for( j = 0; j < width_n; j++ )
                {
                    int s = src[j];
                    dst[j] = (ushort)(s > ithresh ? 0 : s);
                }
386
            }
387
            else if (depth == CV_32F)
388 389 390
            {
                const float* src = _src.ptr<float>(i);
                float* dst = _dst.ptr<float>(i);
391 392
                for( j = 0; j < width_n; j++ )
                {
393 394
                    float s = src[j];
                    dst[j] = s > thresh ? 0.f : s;
395
                }
396
            }
397 398 399 400 401 402 403 404 405 406
            else
            {
                const double* src = _src.ptr<double>(i);
                double* dst = _dst.ptr<double>(i);
                for( j = 0; j < width_n; j++ )
                {
                    double s = src[j];
                    dst[j] = s > thresh ? 0.0 : s;
                }
            }
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
        }
        break;
    default:
        assert(0);
    }
}


void CV_ThreshTest::prepare_to_validation( int /*test_case_idx*/ )
{
    test_threshold( test_mat[INPUT][0], test_mat[REF_OUTPUT][0],
                   thresh_val, max_val, thresh_type );
}

TEST(Imgproc_Threshold, accuracy) { CV_ThreshTest test; test.safe_run(); }
422

423
BIGDATA_TEST(Imgproc_Threshold, huge)
424
{
425 426
    Mat m(65000, 40000, CV_8U);
    ASSERT_FALSE(m.isContinuous());
427

428 429 430
    uint64 i, n = (uint64)m.rows*m.cols;
    for( i = 0; i < n; i++ )
        m.data[i] = (uchar)(i & 255);
431

432 433 434
    cv::threshold(m, m, 127, 255, cv::THRESH_BINARY);
    int nz = cv::countNonZero(m);  // FIXIT 'int' is not enough here (overflow is possible with other inputs)
    ASSERT_EQ((uint64)nz, n / 2);
435 436
}

437
}} // namespace