1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "precomp.hpp"
44 #include "opencl_kernels_imgproc.hpp"
46 #if CV_NEON && defined(__aarch64__)
49 // Workaround with missing definitions of vreinterpretq_u64_f64/vreinterpretq_f64_u64
50 template <typename T> static inline
51 uint64x2_t vreinterpretq_u64_f64(T a)
53 return (uint64x2_t) a;
55 template <typename T> static inline
56 float64x2_t vreinterpretq_f64_u64(T a)
58 return (float64x2_t) a;
67 thresh_8u( const Mat& _src, Mat& _dst, uchar thresh, uchar maxval, int type )
69 Size roi = _src.size();
70 roi.width *= _src.channels();
71 size_t src_step = _src.step;
72 size_t dst_step = _dst.step;
74 if( _src.isContinuous() && _dst.isContinuous() )
76 roi.width *= roi.height;
78 src_step = dst_step = roi.width;
81 #ifdef HAVE_TEGRA_OPTIMIZATION
82 if (tegra::useTegra() && tegra::thresh_8u(_src, _dst, roi.width, roi.height, thresh, maxval, type))
89 IppiSize sz = { roi.width, roi.height };
90 CV_SUPPRESS_DEPRECATED_START
94 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
96 CV_IMPL_ADD(CV_IMPL_IPP);
99 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
101 CV_IMPL_ADD(CV_IMPL_IPP);
107 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh+1, 0) >= 0)
109 CV_IMPL_ADD(CV_IMPL_IPP);
112 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh + 1, 0) >= 0)
114 CV_IMPL_ADD(CV_IMPL_IPP);
119 case THRESH_TOZERO_INV:
120 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
122 CV_IMPL_ADD(CV_IMPL_IPP);
125 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
127 CV_IMPL_ADD(CV_IMPL_IPP);
133 CV_SUPPRESS_DEPRECATED_END
138 const uchar* src = _src.ptr();
139 uchar* dst = _dst.ptr();
141 if( (roi.width >= 8) && checkHardwareSupport(CV_CPU_SSE2) )
143 __m128i _x80 = _mm_set1_epi8('\x80');
144 __m128i thresh_u = _mm_set1_epi8(thresh);
145 __m128i thresh_s = _mm_set1_epi8(thresh ^ 0x80);
146 __m128i maxval_ = _mm_set1_epi8(maxval);
151 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
153 for( j = 0; j <= roi.width - 32; j += 32 )
156 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
157 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 16) );
158 v0 = _mm_cmpgt_epi8( _mm_xor_si128(v0, _x80), thresh_s );
159 v1 = _mm_cmpgt_epi8( _mm_xor_si128(v1, _x80), thresh_s );
160 v0 = _mm_and_si128( v0, maxval_ );
161 v1 = _mm_and_si128( v1, maxval_ );
162 _mm_storeu_si128( (__m128i*)(dst + j), v0 );
163 _mm_storeu_si128( (__m128i*)(dst + j + 16), v1 );
166 for( ; j <= roi.width - 8; j += 8 )
168 __m128i v0 = _mm_loadl_epi64( (const __m128i*)(src + j) );
169 v0 = _mm_cmpgt_epi8( _mm_xor_si128(v0, _x80), thresh_s );
170 v0 = _mm_and_si128( v0, maxval_ );
171 _mm_storel_epi64( (__m128i*)(dst + j), v0 );
176 case THRESH_BINARY_INV:
177 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
179 for( j = 0; j <= roi.width - 32; j += 32 )
182 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
183 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 16) );
184 v0 = _mm_cmpgt_epi8( _mm_xor_si128(v0, _x80), thresh_s );
185 v1 = _mm_cmpgt_epi8( _mm_xor_si128(v1, _x80), thresh_s );
186 v0 = _mm_andnot_si128( v0, maxval_ );
187 v1 = _mm_andnot_si128( v1, maxval_ );
188 _mm_storeu_si128( (__m128i*)(dst + j), v0 );
189 _mm_storeu_si128( (__m128i*)(dst + j + 16), v1 );
192 for( ; j <= roi.width - 8; j += 8 )
194 __m128i v0 = _mm_loadl_epi64( (const __m128i*)(src + j) );
195 v0 = _mm_cmpgt_epi8( _mm_xor_si128(v0, _x80), thresh_s );
196 v0 = _mm_andnot_si128( v0, maxval_ );
197 _mm_storel_epi64( (__m128i*)(dst + j), v0 );
203 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
205 for( j = 0; j <= roi.width - 32; j += 32 )
208 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
209 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 16) );
210 v0 = _mm_subs_epu8( v0, _mm_subs_epu8( v0, thresh_u ));
211 v1 = _mm_subs_epu8( v1, _mm_subs_epu8( v1, thresh_u ));
212 _mm_storeu_si128( (__m128i*)(dst + j), v0 );
213 _mm_storeu_si128( (__m128i*)(dst + j + 16), v1 );
216 for( ; j <= roi.width - 8; j += 8 )
218 __m128i v0 = _mm_loadl_epi64( (const __m128i*)(src + j) );
219 v0 = _mm_subs_epu8( v0, _mm_subs_epu8( v0, thresh_u ));
220 _mm_storel_epi64( (__m128i*)(dst + j), v0 );
226 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
228 for( j = 0; j <= roi.width - 32; j += 32 )
231 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
232 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 16) );
233 v0 = _mm_and_si128( v0, _mm_cmpgt_epi8(_mm_xor_si128(v0, _x80), thresh_s ));
234 v1 = _mm_and_si128( v1, _mm_cmpgt_epi8(_mm_xor_si128(v1, _x80), thresh_s ));
235 _mm_storeu_si128( (__m128i*)(dst + j), v0 );
236 _mm_storeu_si128( (__m128i*)(dst + j + 16), v1 );
239 for( ; j <= roi.width - 8; j += 8 )
241 __m128i v0 = _mm_loadl_epi64( (const __m128i*)(src + j) );
242 v0 = _mm_and_si128( v0, _mm_cmpgt_epi8(_mm_xor_si128(v0, _x80), thresh_s ));
243 _mm_storel_epi64( (__m128i*)(dst + j), v0 );
248 case THRESH_TOZERO_INV:
249 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
251 for( j = 0; j <= roi.width - 32; j += 32 )
254 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
255 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 16) );
256 v0 = _mm_andnot_si128( _mm_cmpgt_epi8(_mm_xor_si128(v0, _x80), thresh_s ), v0 );
257 v1 = _mm_andnot_si128( _mm_cmpgt_epi8(_mm_xor_si128(v1, _x80), thresh_s ), v1 );
258 _mm_storeu_si128( (__m128i*)(dst + j), v0 );
259 _mm_storeu_si128( (__m128i*)(dst + j + 16), v1 );
262 for( ; j <= roi.width - 8; j += 8 )
264 __m128i v0 = _mm_loadl_epi64( (const __m128i*)(src + j) );
265 v0 = _mm_andnot_si128( _mm_cmpgt_epi8(_mm_xor_si128(v0, _x80), thresh_s ), v0 );
266 _mm_storel_epi64( (__m128i*)(dst + j), v0 );
273 if( roi.width >= 16 )
275 uint8x16_t v_thresh = vdupq_n_u8(thresh), v_maxval = vdupq_n_u8(maxval);
280 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
282 for ( j = 0; j <= roi.width - 16; j += 16)
283 vst1q_u8(dst + j, vandq_u8(vcgtq_u8(vld1q_u8(src + j), v_thresh), v_maxval));
287 case THRESH_BINARY_INV:
288 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
290 for ( j = 0; j <= roi.width - 16; j += 16)
291 vst1q_u8(dst + j, vandq_u8(vcleq_u8(vld1q_u8(src + j), v_thresh), v_maxval));
296 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
298 for ( j = 0; j <= roi.width - 16; j += 16)
299 vst1q_u8(dst + j, vminq_u8(vld1q_u8(src + j), v_thresh));
304 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
306 for ( j = 0; j <= roi.width - 16; j += 16)
308 uint8x16_t v_src = vld1q_u8(src + j), v_mask = vcgtq_u8(v_src, v_thresh);
309 vst1q_u8(dst + j, vandq_u8(v_mask, v_src));
314 case THRESH_TOZERO_INV:
315 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
317 for ( j = 0; j <= roi.width - 16; j += 16)
319 uint8x16_t v_src = vld1q_u8(src + j), v_mask = vcleq_u8(v_src, v_thresh);
320 vst1q_u8(dst + j, vandq_u8(v_mask, v_src));
329 if( j_scalar < roi.width )
331 const int thresh_pivot = thresh + 1;
336 memset(tab, 0, thresh_pivot);
337 if (thresh_pivot < 256) {
338 memset(tab + thresh_pivot, maxval, 256 - thresh_pivot);
341 case THRESH_BINARY_INV:
342 memset(tab, maxval, thresh_pivot);
343 if (thresh_pivot < 256) {
344 memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
348 for( int i = 0; i <= thresh; i++ )
350 if (thresh_pivot < 256) {
351 memset(tab + thresh_pivot, thresh, 256 - thresh_pivot);
355 memset(tab, 0, thresh_pivot);
356 for( int i = thresh_pivot; i < 256; i++ )
359 case THRESH_TOZERO_INV:
360 for( int i = 0; i <= thresh; i++ )
362 if (thresh_pivot < 256) {
363 memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
370 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
373 #if CV_ENABLE_UNROLLED
374 for( ; j <= roi.width - 4; j += 4 )
376 uchar t0 = tab[src[j]];
377 uchar t1 = tab[src[j+1]];
389 for( ; j < roi.width; j++ )
390 dst[j] = tab[src[j]];
397 thresh_16s( const Mat& _src, Mat& _dst, short thresh, short maxval, int type )
400 Size roi = _src.size();
401 roi.width *= _src.channels();
402 const short* src = _src.ptr<short>();
403 short* dst = _dst.ptr<short>();
404 size_t src_step = _src.step/sizeof(src[0]);
405 size_t dst_step = _dst.step/sizeof(dst[0]);
408 volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
411 if( _src.isContinuous() && _dst.isContinuous() )
413 roi.width *= roi.height;
415 src_step = dst_step = roi.width;
418 #ifdef HAVE_TEGRA_OPTIMIZATION
419 if (tegra::useTegra() && tegra::thresh_16s(_src, _dst, roi.width, roi.height, thresh, maxval, type))
423 #if defined(HAVE_IPP)
426 IppiSize sz = { roi.width, roi.height };
427 CV_SUPPRESS_DEPRECATED_START
431 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh) >= 0)
433 CV_IMPL_ADD(CV_IMPL_IPP);
436 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_16s_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh) >= 0)
438 CV_IMPL_ADD(CV_IMPL_IPP);
444 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh + 1, 0) >= 0)
446 CV_IMPL_ADD(CV_IMPL_IPP);
449 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_16s_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh + 1, 0) >= 0)
451 CV_IMPL_ADD(CV_IMPL_IPP);
456 case THRESH_TOZERO_INV:
457 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0) >= 0)
459 CV_IMPL_ADD(CV_IMPL_IPP);
462 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_16s_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0) >= 0)
464 CV_IMPL_ADD(CV_IMPL_IPP);
470 CV_SUPPRESS_DEPRECATED_END
477 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
483 __m128i thresh8 = _mm_set1_epi16(thresh), maxval8 = _mm_set1_epi16(maxval);
484 for( ; j <= roi.width - 16; j += 16 )
487 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
488 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 8) );
489 v0 = _mm_cmpgt_epi16( v0, thresh8 );
490 v1 = _mm_cmpgt_epi16( v1, thresh8 );
491 v0 = _mm_and_si128( v0, maxval8 );
492 v1 = _mm_and_si128( v1, maxval8 );
493 _mm_storeu_si128((__m128i*)(dst + j), v0 );
494 _mm_storeu_si128((__m128i*)(dst + j + 8), v1 );
498 int16x8_t v_thresh = vdupq_n_s16(thresh), v_maxval = vdupq_n_s16(maxval);
500 for( ; j <= roi.width - 8; j += 8 )
502 uint16x8_t v_mask = vcgtq_s16(vld1q_s16(src + j), v_thresh);
503 vst1q_s16(dst + j, vandq_s16(vreinterpretq_s16_u16(v_mask), v_maxval));
507 for( ; j < roi.width; j++ )
508 dst[j] = src[j] > thresh ? maxval : 0;
512 case THRESH_BINARY_INV:
513 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
519 __m128i thresh8 = _mm_set1_epi16(thresh), maxval8 = _mm_set1_epi16(maxval);
520 for( ; j <= roi.width - 16; j += 16 )
523 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
524 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 8) );
525 v0 = _mm_cmpgt_epi16( v0, thresh8 );
526 v1 = _mm_cmpgt_epi16( v1, thresh8 );
527 v0 = _mm_andnot_si128( v0, maxval8 );
528 v1 = _mm_andnot_si128( v1, maxval8 );
529 _mm_storeu_si128((__m128i*)(dst + j), v0 );
530 _mm_storeu_si128((__m128i*)(dst + j + 8), v1 );
534 int16x8_t v_thresh = vdupq_n_s16(thresh), v_maxval = vdupq_n_s16(maxval);
536 for( ; j <= roi.width - 8; j += 8 )
538 uint16x8_t v_mask = vcleq_s16(vld1q_s16(src + j), v_thresh);
539 vst1q_s16(dst + j, vandq_s16(vreinterpretq_s16_u16(v_mask), v_maxval));
543 for( ; j < roi.width; j++ )
544 dst[j] = src[j] <= thresh ? maxval : 0;
549 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
555 __m128i thresh8 = _mm_set1_epi16(thresh);
556 for( ; j <= roi.width - 16; j += 16 )
559 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
560 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 8) );
561 v0 = _mm_min_epi16( v0, thresh8 );
562 v1 = _mm_min_epi16( v1, thresh8 );
563 _mm_storeu_si128((__m128i*)(dst + j), v0 );
564 _mm_storeu_si128((__m128i*)(dst + j + 8), v1 );
568 int16x8_t v_thresh = vdupq_n_s16(thresh);
570 for( ; j <= roi.width - 8; j += 8 )
571 vst1q_s16(dst + j, vminq_s16(vld1q_s16(src + j), v_thresh));
574 for( ; j < roi.width; j++ )
575 dst[j] = std::min(src[j], thresh);
580 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
586 __m128i thresh8 = _mm_set1_epi16(thresh);
587 for( ; j <= roi.width - 16; j += 16 )
590 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
591 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 8) );
592 v0 = _mm_and_si128(v0, _mm_cmpgt_epi16(v0, thresh8));
593 v1 = _mm_and_si128(v1, _mm_cmpgt_epi16(v1, thresh8));
594 _mm_storeu_si128((__m128i*)(dst + j), v0 );
595 _mm_storeu_si128((__m128i*)(dst + j + 8), v1 );
599 int16x8_t v_thresh = vdupq_n_s16(thresh);
601 for( ; j <= roi.width - 8; j += 8 )
603 int16x8_t v_src = vld1q_s16(src + j);
604 uint16x8_t v_mask = vcgtq_s16(v_src, v_thresh);
605 vst1q_s16(dst + j, vandq_s16(vreinterpretq_s16_u16(v_mask), v_src));
609 for( ; j < roi.width; j++ )
612 dst[j] = v > thresh ? v : 0;
617 case THRESH_TOZERO_INV:
618 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
624 __m128i thresh8 = _mm_set1_epi16(thresh);
625 for( ; j <= roi.width - 16; j += 16 )
628 v0 = _mm_loadu_si128( (const __m128i*)(src + j) );
629 v1 = _mm_loadu_si128( (const __m128i*)(src + j + 8) );
630 v0 = _mm_andnot_si128(_mm_cmpgt_epi16(v0, thresh8), v0);
631 v1 = _mm_andnot_si128(_mm_cmpgt_epi16(v1, thresh8), v1);
632 _mm_storeu_si128((__m128i*)(dst + j), v0 );
633 _mm_storeu_si128((__m128i*)(dst + j + 8), v1 );
637 int16x8_t v_thresh = vdupq_n_s16(thresh);
639 for( ; j <= roi.width - 8; j += 8 )
641 int16x8_t v_src = vld1q_s16(src + j);
642 uint16x8_t v_mask = vcleq_s16(v_src, v_thresh);
643 vst1q_s16(dst + j, vandq_s16(vreinterpretq_s16_u16(v_mask), v_src));
646 for( ; j < roi.width; j++ )
649 dst[j] = v <= thresh ? v : 0;
654 return CV_Error( CV_StsBadArg, "" );
660 thresh_32f( const Mat& _src, Mat& _dst, float thresh, float maxval, int type )
663 Size roi = _src.size();
664 roi.width *= _src.channels();
665 const float* src = _src.ptr<float>();
666 float* dst = _dst.ptr<float>();
667 size_t src_step = _src.step/sizeof(src[0]);
668 size_t dst_step = _dst.step/sizeof(dst[0]);
671 volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE);
674 if( _src.isContinuous() && _dst.isContinuous() )
676 roi.width *= roi.height;
680 #ifdef HAVE_TEGRA_OPTIMIZATION
681 if (tegra::useTegra() && tegra::thresh_32f(_src, _dst, roi.width, roi.height, thresh, maxval, type))
685 #if defined(HAVE_IPP)
688 IppiSize sz = { roi.width, roi.height };
692 if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh))
694 CV_IMPL_ADD(CV_IMPL_IPP);
700 if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh + FLT_EPSILON, 0))
702 CV_IMPL_ADD(CV_IMPL_IPP);
707 case THRESH_TOZERO_INV:
708 if (0 <= CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_32f_C1R, src, (int)src_step*sizeof(src[0]), dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0))
710 CV_IMPL_ADD(CV_IMPL_IPP);
722 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
728 __m128 thresh4 = _mm_set1_ps(thresh), maxval4 = _mm_set1_ps(maxval);
729 for( ; j <= roi.width - 8; j += 8 )
732 v0 = _mm_loadu_ps( src + j );
733 v1 = _mm_loadu_ps( src + j + 4 );
734 v0 = _mm_cmpgt_ps( v0, thresh4 );
735 v1 = _mm_cmpgt_ps( v1, thresh4 );
736 v0 = _mm_and_ps( v0, maxval4 );
737 v1 = _mm_and_ps( v1, maxval4 );
738 _mm_storeu_ps( dst + j, v0 );
739 _mm_storeu_ps( dst + j + 4, v1 );
743 float32x4_t v_thresh = vdupq_n_f32(thresh);
744 uint32x4_t v_maxval = vreinterpretq_u32_f32(vdupq_n_f32(maxval));
746 for( ; j <= roi.width - 4; j += 4 )
748 float32x4_t v_src = vld1q_f32(src + j);
749 uint32x4_t v_dst = vandq_u32(vcgtq_f32(v_src, v_thresh), v_maxval);
750 vst1q_f32(dst + j, vreinterpretq_f32_u32(v_dst));
754 for( ; j < roi.width; j++ )
755 dst[j] = src[j] > thresh ? maxval : 0;
759 case THRESH_BINARY_INV:
760 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
766 __m128 thresh4 = _mm_set1_ps(thresh), maxval4 = _mm_set1_ps(maxval);
767 for( ; j <= roi.width - 8; j += 8 )
770 v0 = _mm_loadu_ps( src + j );
771 v1 = _mm_loadu_ps( src + j + 4 );
772 v0 = _mm_cmple_ps( v0, thresh4 );
773 v1 = _mm_cmple_ps( v1, thresh4 );
774 v0 = _mm_and_ps( v0, maxval4 );
775 v1 = _mm_and_ps( v1, maxval4 );
776 _mm_storeu_ps( dst + j, v0 );
777 _mm_storeu_ps( dst + j + 4, v1 );
781 float32x4_t v_thresh = vdupq_n_f32(thresh);
782 uint32x4_t v_maxval = vreinterpretq_u32_f32(vdupq_n_f32(maxval));
784 for( ; j <= roi.width - 4; j += 4 )
786 float32x4_t v_src = vld1q_f32(src + j);
787 uint32x4_t v_dst = vandq_u32(vcleq_f32(v_src, v_thresh), v_maxval);
788 vst1q_f32(dst + j, vreinterpretq_f32_u32(v_dst));
792 for( ; j < roi.width; j++ )
793 dst[j] = src[j] <= thresh ? maxval : 0;
798 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
804 __m128 thresh4 = _mm_set1_ps(thresh);
805 for( ; j <= roi.width - 8; j += 8 )
808 v0 = _mm_loadu_ps( src + j );
809 v1 = _mm_loadu_ps( src + j + 4 );
810 v0 = _mm_min_ps( v0, thresh4 );
811 v1 = _mm_min_ps( v1, thresh4 );
812 _mm_storeu_ps( dst + j, v0 );
813 _mm_storeu_ps( dst + j + 4, v1 );
817 float32x4_t v_thresh = vdupq_n_f32(thresh);
819 for( ; j <= roi.width - 4; j += 4 )
820 vst1q_f32(dst + j, vminq_f32(vld1q_f32(src + j), v_thresh));
823 for( ; j < roi.width; j++ )
824 dst[j] = std::min(src[j], thresh);
829 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
835 __m128 thresh4 = _mm_set1_ps(thresh);
836 for( ; j <= roi.width - 8; j += 8 )
839 v0 = _mm_loadu_ps( src + j );
840 v1 = _mm_loadu_ps( src + j + 4 );
841 v0 = _mm_and_ps(v0, _mm_cmpgt_ps(v0, thresh4));
842 v1 = _mm_and_ps(v1, _mm_cmpgt_ps(v1, thresh4));
843 _mm_storeu_ps( dst + j, v0 );
844 _mm_storeu_ps( dst + j + 4, v1 );
848 float32x4_t v_thresh = vdupq_n_f32(thresh);
850 for( ; j <= roi.width - 4; j += 4 )
852 float32x4_t v_src = vld1q_f32(src + j);
853 uint32x4_t v_dst = vandq_u32(vcgtq_f32(v_src, v_thresh),
854 vreinterpretq_u32_f32(v_src));
855 vst1q_f32(dst + j, vreinterpretq_f32_u32(v_dst));
859 for( ; j < roi.width; j++ )
862 dst[j] = v > thresh ? v : 0;
867 case THRESH_TOZERO_INV:
868 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
874 __m128 thresh4 = _mm_set1_ps(thresh);
875 for( ; j <= roi.width - 8; j += 8 )
878 v0 = _mm_loadu_ps( src + j );
879 v1 = _mm_loadu_ps( src + j + 4 );
880 v0 = _mm_and_ps(v0, _mm_cmple_ps(v0, thresh4));
881 v1 = _mm_and_ps(v1, _mm_cmple_ps(v1, thresh4));
882 _mm_storeu_ps( dst + j, v0 );
883 _mm_storeu_ps( dst + j + 4, v1 );
887 float32x4_t v_thresh = vdupq_n_f32(thresh);
889 for( ; j <= roi.width - 4; j += 4 )
891 float32x4_t v_src = vld1q_f32(src + j);
892 uint32x4_t v_dst = vandq_u32(vcleq_f32(v_src, v_thresh),
893 vreinterpretq_u32_f32(v_src));
894 vst1q_f32(dst + j, vreinterpretq_f32_u32(v_dst));
897 for( ; j < roi.width; j++ )
900 dst[j] = v <= thresh ? v : 0;
905 return CV_Error( CV_StsBadArg, "" );
910 thresh_64f(const Mat& _src, Mat& _dst, double thresh, double maxval, int type)
913 Size roi = _src.size();
914 roi.width *= _src.channels();
915 const double* src = _src.ptr<double>();
916 double* dst = _dst.ptr<double>();
917 size_t src_step = _src.step / sizeof(src[0]);
918 size_t dst_step = _dst.step / sizeof(dst[0]);
921 volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
924 if (_src.isContinuous() && _dst.isContinuous())
926 roi.width *= roi.height;
933 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
939 __m128d thresh2 = _mm_set1_pd(thresh), maxval2 = _mm_set1_pd(maxval);
940 for( ; j <= roi.width - 8; j += 8 )
942 __m128d v0, v1, v2, v3;
943 v0 = _mm_loadu_pd( src + j );
944 v1 = _mm_loadu_pd( src + j + 2 );
945 v2 = _mm_loadu_pd( src + j + 4 );
946 v3 = _mm_loadu_pd( src + j + 6 );
947 v0 = _mm_cmpgt_pd( v0, thresh2 );
948 v1 = _mm_cmpgt_pd( v1, thresh2 );
949 v2 = _mm_cmpgt_pd( v2, thresh2 );
950 v3 = _mm_cmpgt_pd( v3, thresh2 );
951 v0 = _mm_and_pd( v0, maxval2 );
952 v1 = _mm_and_pd( v1, maxval2 );
953 v2 = _mm_and_pd( v2, maxval2 );
954 v3 = _mm_and_pd( v3, maxval2 );
955 _mm_storeu_pd( dst + j, v0 );
956 _mm_storeu_pd( dst + j + 2, v1 );
957 _mm_storeu_pd( dst + j + 4, v2 );
958 _mm_storeu_pd( dst + j + 6, v3 );
961 #elif CV_NEON && defined(__aarch64__)
962 float64x2_t v_thresh = vdupq_n_f64(thresh);
963 uint64x2_t v_maxval = vreinterpretq_u64_f64(vdupq_n_f64(maxval));
965 for( ; j <= roi.width - 4; j += 4 )
967 float64x2_t v_src0 = vld1q_f64(src + j);
968 float64x2_t v_src1 = vld1q_f64(src + j + 2);
969 uint64x2_t v_dst0 = vandq_u64(vcgtq_f64(v_src0, v_thresh), v_maxval);
970 uint64x2_t v_dst1 = vandq_u64(vcgtq_f64(v_src1, v_thresh), v_maxval);
971 vst1q_f64(dst + j, vreinterpretq_f64_u64(v_dst0));
972 vst1q_f64(dst + j + 2, vreinterpretq_f64_u64(v_dst1));
976 for (; j < roi.width; j++)
977 dst[j] = src[j] > thresh ? maxval : 0;
981 case THRESH_BINARY_INV:
982 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
989 __m128d thresh2 = _mm_set1_pd(thresh), maxval2 = _mm_set1_pd(maxval);
990 for( ; j <= roi.width - 8; j += 8 )
992 __m128d v0, v1, v2, v3;
993 v0 = _mm_loadu_pd( src + j );
994 v1 = _mm_loadu_pd( src + j + 2 );
995 v2 = _mm_loadu_pd( src + j + 4 );
996 v3 = _mm_loadu_pd( src + j + 6 );
997 v0 = _mm_cmple_pd( v0, thresh2 );
998 v1 = _mm_cmple_pd( v1, thresh2 );
999 v2 = _mm_cmple_pd( v2, thresh2 );
1000 v3 = _mm_cmple_pd( v3, thresh2 );
1001 v0 = _mm_and_pd( v0, maxval2 );
1002 v1 = _mm_and_pd( v1, maxval2 );
1003 v2 = _mm_and_pd( v2, maxval2 );
1004 v3 = _mm_and_pd( v3, maxval2 );
1005 _mm_storeu_pd( dst + j, v0 );
1006 _mm_storeu_pd( dst + j + 2, v1 );
1007 _mm_storeu_pd( dst + j + 4, v2 );
1008 _mm_storeu_pd( dst + j + 6, v3 );
1011 #elif CV_NEON && defined(__aarch64__)
1012 float64x2_t v_thresh = vdupq_n_f64(thresh);
1013 uint64x2_t v_maxval = vreinterpretq_u64_f64(vdupq_n_f64(maxval));
1015 for( ; j <= roi.width - 4; j += 4 )
1017 float64x2_t v_src0 = vld1q_f64(src + j);
1018 float64x2_t v_src1 = vld1q_f64(src + j + 2);
1019 uint64x2_t v_dst0 = vandq_u64(vcleq_f64(v_src0, v_thresh), v_maxval);
1020 uint64x2_t v_dst1 = vandq_u64(vcleq_f64(v_src1, v_thresh), v_maxval);
1021 vst1q_f64(dst + j, vreinterpretq_f64_u64(v_dst0));
1022 vst1q_f64(dst + j + 2, vreinterpretq_f64_u64(v_dst1));
1025 for (; j < roi.width; j++)
1026 dst[j] = src[j] <= thresh ? maxval : 0;
1031 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
1038 __m128d thresh2 = _mm_set1_pd(thresh);
1039 for( ; j <= roi.width - 8; j += 8 )
1041 __m128d v0, v1, v2, v3;
1042 v0 = _mm_loadu_pd( src + j );
1043 v1 = _mm_loadu_pd( src + j + 2 );
1044 v2 = _mm_loadu_pd( src + j + 4 );
1045 v3 = _mm_loadu_pd( src + j + 6 );
1046 v0 = _mm_min_pd( v0, thresh2 );
1047 v1 = _mm_min_pd( v1, thresh2 );
1048 v2 = _mm_min_pd( v2, thresh2 );
1049 v3 = _mm_min_pd( v3, thresh2 );
1050 _mm_storeu_pd( dst + j, v0 );
1051 _mm_storeu_pd( dst + j + 2, v1 );
1052 _mm_storeu_pd( dst + j + 4, v2 );
1053 _mm_storeu_pd( dst + j + 6, v3 );
1056 #elif CV_NEON && defined(__aarch64__)
1057 float64x2_t v_thresh = vdupq_n_f64(thresh);
1059 for( ; j <= roi.width - 4; j += 4 )
1061 float64x2_t v_src0 = vld1q_f64(src + j);
1062 float64x2_t v_src1 = vld1q_f64(src + j + 2);
1063 float64x2_t v_dst0 = vminq_f64(v_src0, v_thresh);
1064 float64x2_t v_dst1 = vminq_f64(v_src1, v_thresh);
1065 vst1q_f64(dst + j, v_dst0);
1066 vst1q_f64(dst + j + 2, v_dst1);
1069 for (; j < roi.width; j++)
1070 dst[j] = std::min(src[j], thresh);
1075 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
1082 __m128d thresh2 = _mm_set1_pd(thresh);
1083 for( ; j <= roi.width - 8; j += 8 )
1085 __m128d v0, v1, v2, v3;
1086 v0 = _mm_loadu_pd( src + j );
1087 v1 = _mm_loadu_pd( src + j + 2 );
1088 v2 = _mm_loadu_pd( src + j + 4 );
1089 v3 = _mm_loadu_pd( src + j + 6 );
1090 v0 = _mm_and_pd( v0, _mm_cmpgt_pd(v0, thresh2));
1091 v1 = _mm_and_pd( v1, _mm_cmpgt_pd(v1, thresh2));
1092 v2 = _mm_and_pd( v2, _mm_cmpgt_pd(v2, thresh2));
1093 v3 = _mm_and_pd( v3, _mm_cmpgt_pd(v3, thresh2));
1094 _mm_storeu_pd( dst + j, v0 );
1095 _mm_storeu_pd( dst + j + 2, v1 );
1096 _mm_storeu_pd( dst + j + 4, v2 );
1097 _mm_storeu_pd( dst + j + 6, v3 );
1100 #elif CV_NEON && defined(__aarch64__)
1101 float64x2_t v_thresh = vdupq_n_f64(thresh);
1103 for( ; j <= roi.width - 4; j += 4 )
1105 float64x2_t v_src0 = vld1q_f64(src + j);
1106 float64x2_t v_src1 = vld1q_f64(src + j + 2);
1107 uint64x2_t v_dst0 = vandq_u64(vcgtq_f64(v_src0, v_thresh),
1108 vreinterpretq_u64_f64(v_src0));
1109 uint64x2_t v_dst1 = vandq_u64(vcgtq_f64(v_src1, v_thresh),
1110 vreinterpretq_u64_f64(v_src1));
1111 vst1q_f64(dst + j, vreinterpretq_f64_u64(v_dst0));
1112 vst1q_f64(dst + j + 2, vreinterpretq_f64_u64(v_dst1));
1115 for (; j < roi.width; j++)
1118 dst[j] = v > thresh ? v : 0;
1123 case THRESH_TOZERO_INV:
1124 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
1131 __m128d thresh2 = _mm_set1_pd(thresh);
1132 for( ; j <= roi.width - 8; j += 8 )
1134 __m128d v0, v1, v2, v3;
1135 v0 = _mm_loadu_pd( src + j );
1136 v1 = _mm_loadu_pd( src + j + 2 );
1137 v2 = _mm_loadu_pd( src + j + 4 );
1138 v3 = _mm_loadu_pd( src + j + 6 );
1139 v0 = _mm_and_pd( v0, _mm_cmple_pd(v0, thresh2));
1140 v1 = _mm_and_pd( v1, _mm_cmple_pd(v1, thresh2));
1141 v2 = _mm_and_pd( v2, _mm_cmple_pd(v2, thresh2));
1142 v3 = _mm_and_pd( v3, _mm_cmple_pd(v3, thresh2));
1143 _mm_storeu_pd( dst + j, v0 );
1144 _mm_storeu_pd( dst + j + 2, v1 );
1145 _mm_storeu_pd( dst + j + 4, v2 );
1146 _mm_storeu_pd( dst + j + 6, v3 );
1149 #elif CV_NEON && defined(__aarch64__)
1150 float64x2_t v_thresh = vdupq_n_f64(thresh);
1152 for( ; j <= roi.width - 4; j += 4 )
1154 float64x2_t v_src0 = vld1q_f64(src + j);
1155 float64x2_t v_src1 = vld1q_f64(src + j + 2);
1156 uint64x2_t v_dst0 = vandq_u64(vcleq_f64(v_src0, v_thresh),
1157 vreinterpretq_u64_f64(v_src0));
1158 uint64x2_t v_dst1 = vandq_u64(vcleq_f64(v_src1, v_thresh),
1159 vreinterpretq_u64_f64(v_src1));
1160 vst1q_f64(dst + j, vreinterpretq_f64_u64(v_dst0));
1161 vst1q_f64(dst + j + 2, vreinterpretq_f64_u64(v_dst1));
1164 for (; j < roi.width; j++)
1167 dst[j] = v <= thresh ? v : 0;
1172 return CV_Error(CV_StsBadArg, "");
1177 static bool ipp_getThreshVal_Otsu_8u( const unsigned char* _src, int step, Size size, unsigned char &thresh)
1179 CV_INSTRUMENT_REGION_IPP()
1181 #if IPP_VERSION_X100 >= 810
1183 IppiSize srcSize = { size.width, size.height };
1184 CV_SUPPRESS_DEPRECATED_START
1185 ippStatus = CV_INSTRUMENT_FUN_IPP(ippiComputeThreshold_Otsu_8u_C1R, _src, step, srcSize, &thresh);
1186 CV_SUPPRESS_DEPRECATED_END
1191 CV_UNUSED(_src); CV_UNUSED(step); CV_UNUSED(size); CV_UNUSED(thresh);
1198 getThreshVal_Otsu_8u( const Mat& _src )
1200 Size size = _src.size();
1201 int step = (int) _src.step;
1202 if( _src.isContinuous() )
1204 size.width *= size.height;
1210 unsigned char thresh;
1211 CV_IPP_RUN(IPP_VERSION_X100 >= 810, ipp_getThreshVal_Otsu_8u(_src.ptr(), step, size, thresh), thresh);
1215 int i, j, h[N] = {0};
1216 for( i = 0; i < size.height; i++ )
1218 const uchar* src = _src.ptr() + step*i;
1220 #if CV_ENABLE_UNROLLED
1221 for( ; j <= size.width - 4; j += 4 )
1223 int v0 = src[j], v1 = src[j+1];
1225 v0 = src[j+2]; v1 = src[j+3];
1229 for( ; j < size.width; j++ )
1233 double mu = 0, scale = 1./(size.width*size.height);
1234 for( i = 0; i < N; i++ )
1235 mu += i*(double)h[i];
1238 double mu1 = 0, q1 = 0;
1239 double max_sigma = 0, max_val = 0;
1241 for( i = 0; i < N; i++ )
1243 double p_i, q2, mu2, sigma;
1250 if( std::min(q1,q2) < FLT_EPSILON || std::max(q1,q2) > 1. - FLT_EPSILON )
1253 mu1 = (mu1 + i*p_i)/q1;
1254 mu2 = (mu - q1*mu1)/q2;
1255 sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
1256 if( sigma > max_sigma )
1267 getThreshVal_Triangle_8u( const Mat& _src )
1269 Size size = _src.size();
1270 int step = (int) _src.step;
1271 if( _src.isContinuous() )
1273 size.width *= size.height;
1279 int i, j, h[N] = {0};
1280 for( i = 0; i < size.height; i++ )
1282 const uchar* src = _src.ptr() + step*i;
1284 #if CV_ENABLE_UNROLLED
1285 for( ; j <= size.width - 4; j += 4 )
1287 int v0 = src[j], v1 = src[j+1];
1289 v0 = src[j+2]; v1 = src[j+3];
1293 for( ; j < size.width; j++ )
1297 int left_bound = 0, right_bound = 0, max_ind = 0, max = 0;
1299 bool isflipped = false;
1301 for( i = 0; i < N; i++ )
1309 if( left_bound > 0 )
1312 for( i = N-1; i > 0; i-- )
1320 if( right_bound < N-1 )
1323 for( i = 0; i < N; i++ )
1332 if( max_ind-left_bound < right_bound-max_ind)
1338 temp = h[i]; h[i] = h[j]; h[j] = temp;
1341 left_bound = N-1-right_bound;
1342 max_ind = N-1-max_ind;
1345 double thresh = left_bound;
1346 double a, b, dist = 0, tempdist;
1349 * We do not need to compute precise distance here. Distance is maximized, so some constants can
1350 * be omitted. This speeds up a computation a bit.
1352 a = max; b = left_bound-max_ind;
1353 for( i = left_bound+1; i <= max_ind; i++ )
1355 tempdist = a*i + b*h[i];
1356 if( tempdist > dist)
1365 thresh = N-1-thresh;
1370 class ThresholdRunner : public ParallelLoopBody
1373 ThresholdRunner(Mat _src, Mat _dst, double _thresh, double _maxval, int _thresholdType)
1380 thresholdType = _thresholdType;
1383 void operator () ( const Range& range ) const
1385 int row0 = range.start;
1386 int row1 = range.end;
1388 Mat srcStripe = src.rowRange(row0, row1);
1389 Mat dstStripe = dst.rowRange(row0, row1);
1391 if (srcStripe.depth() == CV_8U)
1393 thresh_8u( srcStripe, dstStripe, (uchar)thresh, (uchar)maxval, thresholdType );
1395 else if( srcStripe.depth() == CV_16S )
1397 thresh_16s( srcStripe, dstStripe, (short)thresh, (short)maxval, thresholdType );
1399 else if( srcStripe.depth() == CV_32F )
1401 thresh_32f( srcStripe, dstStripe, (float)thresh, (float)maxval, thresholdType );
1403 else if( srcStripe.depth() == CV_64F )
1405 thresh_64f(srcStripe, dstStripe, thresh, maxval, thresholdType);
1420 static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, double maxval, int thresh_type )
1422 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
1423 kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
1424 bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
1426 if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
1427 thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
1428 (!doubleSupport && depth == CV_64F))
1431 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
1432 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
1433 ocl::Device dev = ocl::Device::getDefault();
1434 int stride_size = dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU) ? 4 : 1;
1436 ocl::Kernel k("threshold", ocl::imgproc::threshold_oclsrc,
1437 format("-D %s -D T=%s -D T1=%s -D STRIDE_SIZE=%d%s", thresholdMap[thresh_type],
1438 ocl::typeToStr(ktype), ocl::typeToStr(depth), stride_size,
1439 doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
1443 UMat src = _src.getUMat();
1444 _dst.create(src.size(), type);
1445 UMat dst = _dst.getUMat();
1447 if (depth <= CV_32S)
1448 thresh = cvFloor(thresh);
1450 const double min_vals[] = { 0, CHAR_MIN, 0, SHRT_MIN, INT_MIN, -FLT_MAX, -DBL_MAX, 0 };
1451 double min_val = min_vals[depth];
1453 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst, cn, kercn),
1454 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(thresh))),
1455 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(maxval))),
1456 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(min_val))));
1458 size_t globalsize[2] = { (size_t)dst.cols * cn / kercn, (size_t)dst.rows };
1459 globalsize[1] = (globalsize[1] + stride_size - 1) / stride_size;
1460 return k.run(2, globalsize, NULL, false);
1467 double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double maxval, int type )
1469 CV_INSTRUMENT_REGION()
1471 CV_OCL_RUN_(_src.dims() <= 2 && _dst.isUMat(),
1472 ocl_threshold(_src, _dst, thresh, maxval, type), thresh)
1474 Mat src = _src.getMat();
1475 int automatic_thresh = (type & ~CV_THRESH_MASK);
1476 type &= THRESH_MASK;
1478 CV_Assert( automatic_thresh != (CV_THRESH_OTSU | CV_THRESH_TRIANGLE) );
1479 if( automatic_thresh == CV_THRESH_OTSU )
1481 CV_Assert( src.type() == CV_8UC1 );
1482 thresh = getThreshVal_Otsu_8u( src );
1484 else if( automatic_thresh == CV_THRESH_TRIANGLE )
1486 CV_Assert( src.type() == CV_8UC1 );
1487 thresh = getThreshVal_Triangle_8u( src );
1490 _dst.create( src.size(), src.type() );
1491 Mat dst = _dst.getMat();
1493 if( src.depth() == CV_8U )
1495 int ithresh = cvFloor(thresh);
1497 int imaxval = cvRound(maxval);
1498 if( type == THRESH_TRUNC )
1500 imaxval = saturate_cast<uchar>(imaxval);
1502 if( ithresh < 0 || ithresh >= 255 )
1504 if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
1505 ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < 0) ||
1506 (type == THRESH_TOZERO && ithresh >= 255) )
1508 int v = type == THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) :
1509 type == THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) :
1510 /*type == THRESH_TRUNC ? imaxval :*/ 0;
1520 else if( src.depth() == CV_16S )
1522 int ithresh = cvFloor(thresh);
1524 int imaxval = cvRound(maxval);
1525 if( type == THRESH_TRUNC )
1527 imaxval = saturate_cast<short>(imaxval);
1529 if( ithresh < SHRT_MIN || ithresh >= SHRT_MAX )
1531 if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
1532 ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < SHRT_MIN) ||
1533 (type == THRESH_TOZERO && ithresh >= SHRT_MAX) )
1535 int v = type == THRESH_BINARY ? (ithresh >= SHRT_MAX ? 0 : imaxval) :
1536 type == THRESH_BINARY_INV ? (ithresh >= SHRT_MAX ? imaxval : 0) :
1537 /*type == THRESH_TRUNC ? imaxval :*/ 0;
1547 else if( src.depth() == CV_32F )
1549 else if( src.depth() == CV_64F )
1552 CV_Error( CV_StsUnsupportedFormat, "" );
1554 parallel_for_(Range(0, dst.rows),
1555 ThresholdRunner(src, dst, thresh, maxval, type),
1556 dst.total()/(double)(1<<16));
1561 void cv::adaptiveThreshold( InputArray _src, OutputArray _dst, double maxValue,
1562 int method, int type, int blockSize, double delta )
1564 CV_INSTRUMENT_REGION()
1566 Mat src = _src.getMat();
1567 CV_Assert( src.type() == CV_8UC1 );
1568 CV_Assert( blockSize % 2 == 1 && blockSize > 1 );
1569 Size size = src.size();
1571 _dst.create( size, src.type() );
1572 Mat dst = _dst.getMat();
1582 if( src.data != dst.data )
1585 if (method == ADAPTIVE_THRESH_MEAN_C)
1586 boxFilter( src, mean, src.type(), Size(blockSize, blockSize),
1587 Point(-1,-1), true, BORDER_REPLICATE );
1588 else if (method == ADAPTIVE_THRESH_GAUSSIAN_C)
1590 Mat srcfloat,meanfloat;
1591 src.convertTo(srcfloat,CV_32F);
1593 GaussianBlur(srcfloat, meanfloat, Size(blockSize, blockSize), 0, 0, BORDER_REPLICATE);
1594 meanfloat.convertTo(mean, src.type());
1597 CV_Error( CV_StsBadFlag, "Unknown/unsupported adaptive threshold method" );
1600 uchar imaxval = saturate_cast<uchar>(maxValue);
1601 int idelta = type == THRESH_BINARY ? cvCeil(delta) : cvFloor(delta);
1604 if( type == CV_THRESH_BINARY )
1605 for( i = 0; i < 768; i++ )
1606 tab[i] = (uchar)(i - 255 > -idelta ? imaxval : 0);
1607 else if( type == CV_THRESH_BINARY_INV )
1608 for( i = 0; i < 768; i++ )
1609 tab[i] = (uchar)(i - 255 <= -idelta ? imaxval : 0);
1611 CV_Error( CV_StsBadFlag, "Unknown/unsupported threshold type" );
1613 if( src.isContinuous() && mean.isContinuous() && dst.isContinuous() )
1615 size.width *= size.height;
1619 for( i = 0; i < size.height; i++ )
1621 const uchar* sdata = src.ptr(i);
1622 const uchar* mdata = mean.ptr(i);
1623 uchar* ddata = dst.ptr(i);
1625 for( j = 0; j < size.width; j++ )
1626 ddata[j] = tab[sdata[j] - mdata[j] + 255];
1631 cvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, int type )
1633 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), dst0 = dst;
1635 CV_Assert( src.size == dst.size && src.channels() == dst.channels() &&
1636 (src.depth() == dst.depth() || dst.depth() == CV_8U));
1638 thresh = cv::threshold( src, dst, thresh, maxval, type );
1639 if( dst0.data != dst.data )
1640 dst.convertTo( dst0, dst0.depth() );
1646 cvAdaptiveThreshold( const void *srcIm, void *dstIm, double maxValue,
1647 int method, int type, int blockSize, double delta )
1649 cv::Mat src = cv::cvarrToMat(srcIm), dst = cv::cvarrToMat(dstIm);
1650 CV_Assert( src.size == dst.size && src.type() == dst.type() );
1651 cv::adaptiveThreshold( src, dst, maxValue, method, type, blockSize, delta );