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"
45 #include "opencv2/core/hal/intrin.hpp"
47 #include "opencv2/core/openvx/ovx_defs.hpp"
53 static inline T threshBinary(const T& src, const T& thresh, const T& maxval)
55 return src > thresh ? maxval : 0;
59 static inline T threshBinaryInv(const T& src, const T& thresh, const T& maxval)
61 return src <= thresh ? maxval : 0;
65 static inline T threshTrunc(const T& src, const T& thresh)
67 return std::min(src, thresh);
71 static inline T threshToZero(const T& src, const T& thresh)
73 return src > thresh ? src : 0;
77 static inline T threshToZeroInv(const T& src, const T& thresh)
79 return src <= thresh ? src : 0;
83 static void threshGeneric(Size roi, const T* src, size_t src_step, T* dst,
84 size_t dst_step, T thresh, T maxval, int type)
90 for (; i < roi.height; i++, src += src_step, dst += dst_step)
91 for (j = 0; j < roi.width; j++)
92 dst[j] = threshBinary<T>(src[j], thresh, maxval);
95 case THRESH_BINARY_INV:
96 for (; i < roi.height; i++, src += src_step, dst += dst_step)
97 for (j = 0; j < roi.width; j++)
98 dst[j] = threshBinaryInv<T>(src[j], thresh, maxval);
102 for (; i < roi.height; i++, src += src_step, dst += dst_step)
103 for (j = 0; j < roi.width; j++)
104 dst[j] = threshTrunc<T>(src[j], thresh);
108 for (; i < roi.height; i++, src += src_step, dst += dst_step)
109 for (j = 0; j < roi.width; j++)
110 dst[j] = threshToZero<T>(src[j], thresh);
113 case THRESH_TOZERO_INV:
114 for (; i < roi.height; i++, src += src_step, dst += dst_step)
115 for (j = 0; j < roi.width; j++)
116 dst[j] = threshToZeroInv<T>(src[j], thresh);
120 CV_Error( CV_StsBadArg, "" ); return;
125 thresh_8u( const Mat& _src, Mat& _dst, uchar thresh, uchar maxval, int type )
127 Size roi = _src.size();
128 roi.width *= _src.channels();
129 size_t src_step = _src.step;
130 size_t dst_step = _dst.step;
132 if( _src.isContinuous() && _dst.isContinuous() )
134 roi.width *= roi.height;
136 src_step = dst_step = roi.width;
139 #if defined(HAVE_IPP)
142 IppiSize sz = { roi.width, roi.height };
143 CV_SUPPRESS_DEPRECATED_START
147 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
149 CV_IMPL_ADD(CV_IMPL_IPP);
152 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
154 CV_IMPL_ADD(CV_IMPL_IPP);
160 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh+1, 0) >= 0)
162 CV_IMPL_ADD(CV_IMPL_IPP);
165 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh + 1, 0) >= 0)
167 CV_IMPL_ADD(CV_IMPL_IPP);
172 case THRESH_TOZERO_INV:
173 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
175 CV_IMPL_ADD(CV_IMPL_IPP);
178 if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
180 CV_IMPL_ADD(CV_IMPL_IPP);
186 CV_SUPPRESS_DEPRECATED_END
191 const uchar* src = _src.ptr();
192 uchar* dst = _dst.ptr();
194 bool useSIMD = checkHardwareSupport( CV_CPU_SSE2 ) || checkHardwareSupport( CV_CPU_NEON );
197 v_uint8x16 thresh_u = v_setall_u8( thresh );
198 v_uint8x16 maxval16 = v_setall_u8( maxval );
203 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
205 for( j = 0; j <= roi.width - 16; j += 16 )
208 v0 = v_load( src + j );
211 v_store( dst + j, v0 );
216 case THRESH_BINARY_INV:
217 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
219 for( j = 0; j <= roi.width - 16; j += 16 )
222 v0 = v_load( src + j );
225 v_store( dst + j, v0 );
231 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
233 for( j = 0; j <= roi.width - 16; j += 16 )
236 v0 = v_load( src + j );
237 v0 = v0 - ( v0 - thresh_u );
238 v_store( dst + j, v0 );
244 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
246 for( j = 0; j <= roi.width - 16; j += 16 )
249 v0 = v_load( src + j );
250 v0 = ( thresh_u < v0 ) & v0;
251 v_store( dst + j, v0 );
256 case THRESH_TOZERO_INV:
257 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
259 for( j = 0; j <= roi.width - 16; j += 16 )
262 v0 = v_load( src + j );
263 v0 = ( v0 <= thresh_u ) & v0;
264 v_store( dst + j, v0 );
273 if( j_scalar < roi.width )
275 const int thresh_pivot = thresh + 1;
276 uchar tab[256] = {0};
280 memset(tab, 0, thresh_pivot);
281 if (thresh_pivot < 256) {
282 memset(tab + thresh_pivot, maxval, 256 - thresh_pivot);
285 case THRESH_BINARY_INV:
286 memset(tab, maxval, thresh_pivot);
287 if (thresh_pivot < 256) {
288 memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
292 for( int i = 0; i <= thresh; i++ )
294 if (thresh_pivot < 256) {
295 memset(tab + thresh_pivot, thresh, 256 - thresh_pivot);
299 memset(tab, 0, thresh_pivot);
300 for( int i = thresh_pivot; i < 256; i++ )
303 case THRESH_TOZERO_INV:
304 for( int i = 0; i <= thresh; i++ )
306 if (thresh_pivot < 256) {
307 memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
314 for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
317 #if CV_ENABLE_UNROLLED
318 for( ; j <= roi.width - 4; j += 4 )
320 uchar t0 = tab[src[j]];
321 uchar t1 = tab[src[j+1]];
333 for( ; j < roi.width; j++ )
334 dst[j] = tab[src[j]];
340 thresh_16u(const Mat& _src, Mat& _dst, ushort thresh, ushort maxval, int type)
342 Size roi = _src.size();
343 roi.width *= _src.channels();
344 size_t src_step = _src.step / _src.elemSize1();
345 size_t dst_step = _dst.step / _dst.elemSize1();
347 if (_src.isContinuous() && _dst.isContinuous())
349 roi.width *= roi.height;
351 src_step = dst_step = roi.width;
354 // HAVE_IPP not supported
356 const ushort* src = _src.ptr<ushort>();
357 ushort* dst = _dst.ptr<ushort>();
359 bool useSIMD = checkHardwareSupport(CV_CPU_SSE2) || checkHardwareSupport(CV_CPU_NEON);
363 v_uint16x8 thresh_u = v_setall_u16(thresh);
364 v_uint16x8 maxval16 = v_setall_u16(maxval);
369 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
371 for (j = 0; j <= roi.width - 16; j += 16)
374 v0 = v_load(src + j);
375 v1 = v_load(src + j + 8);
380 v_store(dst + j, v0);
381 v_store(dst + j + 8, v1);
384 for (; j < roi.width; j++)
385 dst[j] = threshBinary<ushort>(src[j], thresh, maxval);
389 case THRESH_BINARY_INV:
390 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
393 for (; j <= roi.width - 16; j += 16)
396 v0 = v_load(src + j);
397 v1 = v_load(src + j + 8);
402 v_store(dst + j, v0);
403 v_store(dst + j + 8, v1);
406 for (; j < roi.width; j++)
407 dst[j] = threshBinaryInv<ushort>(src[j], thresh, maxval);
412 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
415 for (; j <= roi.width - 16; j += 16)
418 v0 = v_load(src + j);
419 v1 = v_load(src + j + 8);
420 v0 = v_min(v0, thresh_u);
421 v1 = v_min(v1, thresh_u);
422 v_store(dst + j, v0);
423 v_store(dst + j + 8, v1);
426 for (; j < roi.width; j++)
427 dst[j] = threshTrunc<ushort>(src[j], thresh);
432 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
435 for (; j <= roi.width - 16; j += 16)
438 v0 = v_load(src + j);
439 v1 = v_load(src + j + 8);
440 v0 = (thresh_u < v0) & v0;
441 v1 = (thresh_u < v1) & v1;
442 v_store(dst + j, v0);
443 v_store(dst + j + 8, v1);
446 for (; j < roi.width; j++)
447 dst[j] = threshToZero<ushort>(src[j], thresh);
451 case THRESH_TOZERO_INV:
452 for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
455 for (; j <= roi.width - 16; j += 16)
458 v0 = v_load(src + j);
459 v1 = v_load(src + j + 8);
460 v0 = (v0 <= thresh_u) & v0;
461 v1 = (v1 <= thresh_u) & v1;
462 v_store(dst + j, v0);
463 v_store(dst + j + 8, v1);
466 for (; j < roi.width; j++)
467 dst[j] = threshToZeroInv<ushort>(src[j], thresh);
475 threshGeneric<ushort>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
480 thresh_16s( const Mat& _src, Mat& _dst, short thresh, short maxval, int type )
482 Size roi = _src.size();
483 roi.width *= _src.channels();
484 const short* src = _src.ptr<short>();
485 short* dst = _dst.ptr<short>();
486 size_t src_step = _src.step/sizeof(src[0]);
487 size_t dst_step = _dst.step/sizeof(dst[0]);
489 if( _src.isContinuous() && _dst.isContinuous() )
491 roi.width *= roi.height;
493 src_step = dst_step = roi.width;
496 #if defined(HAVE_IPP)
499 IppiSize sz = { roi.width, roi.height };
500 CV_SUPPRESS_DEPRECATED_START
504 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh) >= 0)
506 CV_IMPL_ADD(CV_IMPL_IPP);
509 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)
511 CV_IMPL_ADD(CV_IMPL_IPP);
517 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)
519 CV_IMPL_ADD(CV_IMPL_IPP);
522 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)
524 CV_IMPL_ADD(CV_IMPL_IPP);
529 case THRESH_TOZERO_INV:
530 if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0) >= 0)
532 CV_IMPL_ADD(CV_IMPL_IPP);
535 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)
537 CV_IMPL_ADD(CV_IMPL_IPP);
543 CV_SUPPRESS_DEPRECATED_END
548 bool useSIMD = checkHardwareSupport( CV_CPU_SSE2 ) || checkHardwareSupport( CV_CPU_NEON );
552 v_int16x8 thresh8 = v_setall_s16( thresh );
553 v_int16x8 maxval8 = v_setall_s16( maxval );
558 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
561 for( ; j <= roi.width - 16; j += 16 )
564 v0 = v_load( src + j );
565 v1 = v_load( src + j + 8 );
570 v_store( dst + j, v0 );
571 v_store( dst + j + 8, v1 );
574 for( ; j < roi.width; j++ )
575 dst[j] = threshBinary<short>(src[j], thresh, maxval);
579 case THRESH_BINARY_INV:
580 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
583 for( ; j <= roi.width - 16; j += 16 )
586 v0 = v_load( src + j );
587 v1 = v_load( src + j + 8 );
592 v_store( dst + j, v0 );
593 v_store( dst + j + 8, v1 );
596 for( ; j < roi.width; j++ )
597 dst[j] = threshBinaryInv<short>(src[j], thresh, maxval);
602 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
605 for( ; j <= roi.width - 16; j += 16 )
608 v0 = v_load( src + j );
609 v1 = v_load( src + j + 8 );
610 v0 = v_min( v0, thresh8 );
611 v1 = v_min( v1, thresh8 );
612 v_store( dst + j, v0 );
613 v_store( dst + j + 8, v1 );
616 for( ; j < roi.width; j++ )
617 dst[j] = threshTrunc<short>( src[j], thresh );
622 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
625 for( ; j <= roi.width - 16; j += 16 )
628 v0 = v_load( src + j );
629 v1 = v_load( src + j + 8 );
630 v0 = ( thresh8 < v0 ) & v0;
631 v1 = ( thresh8 < v1 ) & v1;
632 v_store( dst + j, v0 );
633 v_store( dst + j + 8, v1 );
636 for( ; j < roi.width; j++ )
637 dst[j] = threshToZero<short>(src[j], thresh);
641 case THRESH_TOZERO_INV:
642 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
645 for( ; j <= roi.width - 16; j += 16 )
648 v0 = v_load( src + j );
649 v1 = v_load( src + j + 8 );
650 v0 = ( v0 <= thresh8 ) & v0;
651 v1 = ( v1 <= thresh8 ) & v1;
652 v_store( dst + j, v0 );
653 v_store( dst + j + 8, v1 );
656 for( ; j < roi.width; j++ )
657 dst[j] = threshToZeroInv<short>(src[j], thresh);
661 CV_Error( CV_StsBadArg, "" ); return;
667 threshGeneric<short>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
673 thresh_32f( const Mat& _src, Mat& _dst, float thresh, float maxval, int type )
675 Size roi = _src.size();
676 roi.width *= _src.channels();
677 const float* src = _src.ptr<float>();
678 float* dst = _dst.ptr<float>();
679 size_t src_step = _src.step/sizeof(src[0]);
680 size_t dst_step = _dst.step/sizeof(dst[0]);
682 if( _src.isContinuous() && _dst.isContinuous() )
684 roi.width *= roi.height;
688 #if defined(HAVE_IPP)
691 IppiSize sz = { roi.width, roi.height };
695 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))
697 CV_IMPL_ADD(CV_IMPL_IPP);
703 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))
705 CV_IMPL_ADD(CV_IMPL_IPP);
710 case THRESH_TOZERO_INV:
711 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))
713 CV_IMPL_ADD(CV_IMPL_IPP);
723 bool useSIMD = checkHardwareSupport( CV_CPU_SSE2 ) || checkHardwareSupport( CV_CPU_NEON );
727 v_float32x4 thresh4 = v_setall_f32( thresh );
728 v_float32x4 maxval4 = v_setall_f32( maxval );
733 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
736 for( ; j <= roi.width - 8; j += 8 )
739 v0 = v_load( src + j );
740 v1 = v_load( src + j + 4 );
745 v_store( dst + j, v0 );
746 v_store( dst + j + 4, v1 );
749 for( ; j < roi.width; j++ )
750 dst[j] = threshBinary<float>(src[j], thresh, maxval);
754 case THRESH_BINARY_INV:
755 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
758 for( ; j <= roi.width - 8; j += 8 )
761 v0 = v_load( src + j );
762 v1 = v_load( src + j + 4 );
767 v_store( dst + j, v0 );
768 v_store( dst + j + 4, v1 );
771 for( ; j < roi.width; j++ )
772 dst[j] = threshBinaryInv<float>(src[j], thresh, maxval);
777 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
780 for( ; j <= roi.width - 8; j += 8 )
783 v0 = v_load( src + j );
784 v1 = v_load( src + j + 4 );
785 v0 = v_min( v0, thresh4 );
786 v1 = v_min( v1, thresh4 );
787 v_store( dst + j, v0 );
788 v_store( dst + j + 4, v1 );
791 for( ; j < roi.width; j++ )
792 dst[j] = threshTrunc<float>(src[j], thresh);
797 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
800 for( ; j <= roi.width - 8; j += 8 )
803 v0 = v_load( src + j );
804 v1 = v_load( src + j + 4 );
805 v0 = ( thresh4 < v0 ) & v0;
806 v1 = ( thresh4 < v1 ) & v1;
807 v_store( dst + j, v0 );
808 v_store( dst + j + 4, v1 );
811 for( ; j < roi.width; j++ )
812 dst[j] = threshToZero<float>(src[j], thresh);
816 case THRESH_TOZERO_INV:
817 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
820 for( ; j <= roi.width - 8; j += 8 )
823 v0 = v_load( src + j );
824 v1 = v_load( src + j + 4 );
825 v0 = ( v0 <= thresh4 ) & v0;
826 v1 = ( v1 <= thresh4 ) & v1;
827 v_store( dst + j, v0 );
828 v_store( dst + j + 4, v1 );
831 for( ; j < roi.width; j++ )
832 dst[j] = threshToZeroInv<float>(src[j], thresh);
836 CV_Error( CV_StsBadArg, "" ); return;
842 threshGeneric<float>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
847 thresh_64f(const Mat& _src, Mat& _dst, double thresh, double maxval, int type)
849 Size roi = _src.size();
850 roi.width *= _src.channels();
851 const double* src = _src.ptr<double>();
852 double* dst = _dst.ptr<double>();
853 size_t src_step = _src.step / sizeof(src[0]);
854 size_t dst_step = _dst.step / sizeof(dst[0]);
856 if (_src.isContinuous() && _dst.isContinuous())
858 roi.width *= roi.height;
863 bool useSIMD = checkHardwareSupport( CV_CPU_SSE2 ) || checkHardwareSupport( CV_CPU_NEON );
867 v_float64x2 thresh2 = v_setall_f64( thresh );
868 v_float64x2 maxval2 = v_setall_f64( maxval );
873 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
876 for( ; j <= roi.width - 4; j += 4 )
879 v0 = v_load( src + j );
880 v1 = v_load( src + j + 2 );
885 v_store( dst + j, v0 );
886 v_store( dst + j + 2, v1 );
889 for( ; j < roi.width; j++ )
890 dst[j] = threshBinary<double>(src[j], thresh, maxval);
894 case THRESH_BINARY_INV:
895 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
898 for( ; j <= roi.width - 4; j += 4 )
901 v0 = v_load( src + j );
902 v1 = v_load( src + j + 2 );
907 v_store( dst + j, v0 );
908 v_store( dst + j + 2, v1 );
911 for( ; j < roi.width; j++ )
912 dst[j] = threshBinaryInv<double>(src[j], thresh, maxval);
917 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
920 for( ; j <= roi.width - 4; j += 4 )
923 v0 = v_load( src + j );
924 v1 = v_load( src + j + 2 );
925 v0 = v_min( v0, thresh2 );
926 v1 = v_min( v1, thresh2 );
927 v_store( dst + j, v0 );
928 v_store( dst + j + 2, v1 );
931 for( ; j < roi.width; j++ )
932 dst[j] = threshTrunc<double>(src[j], thresh);
937 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
940 for( ; j <= roi.width - 4; j += 4 )
943 v0 = v_load( src + j );
944 v1 = v_load( src + j + 2 );
945 v0 = ( thresh2 < v0 ) & v0;
946 v1 = ( thresh2 < v1 ) & v1;
947 v_store( dst + j, v0 );
948 v_store( dst + j + 2, v1 );
951 for( ; j < roi.width; j++ )
952 dst[j] = threshToZero<double>(src[j], thresh);
956 case THRESH_TOZERO_INV:
957 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
960 for( ; j <= roi.width - 4; j += 4 )
963 v0 = v_load( src + j );
964 v1 = v_load( src + j + 2 );
965 v0 = ( v0 <= thresh2 ) & v0;
966 v1 = ( v1 <= thresh2 ) & v1;
967 v_store( dst + j, v0 );
968 v_store( dst + j + 2, v1 );
971 for( ; j < roi.width; j++ )
972 dst[j] = threshToZeroInv<double>(src[j], thresh);
976 CV_Error(CV_StsBadArg, ""); return;
982 threshGeneric<double>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
987 static bool ipp_getThreshVal_Otsu_8u( const unsigned char* _src, int step, Size size, unsigned char &thresh)
989 CV_INSTRUMENT_REGION_IPP()
991 // Performance degradations
992 #if IPP_VERSION_X100 >= 201800
993 IppiSize srcSize = { size.width, size.height };
995 if(CV_INSTRUMENT_FUN_IPP(ippiComputeThreshold_Otsu_8u_C1R, _src, step, srcSize, &thresh) < 0)
1000 CV_UNUSED(_src); CV_UNUSED(step); CV_UNUSED(size); CV_UNUSED(thresh);
1007 getThreshVal_Otsu_8u( const Mat& _src )
1009 Size size = _src.size();
1010 int step = (int) _src.step;
1011 if( _src.isContinuous() )
1013 size.width *= size.height;
1019 unsigned char thresh = 0;
1020 CV_IPP_RUN_FAST(ipp_getThreshVal_Otsu_8u(_src.ptr(), step, size, thresh), thresh);
1024 int i, j, h[N] = {0};
1025 for( i = 0; i < size.height; i++ )
1027 const uchar* src = _src.ptr() + step*i;
1029 #if CV_ENABLE_UNROLLED
1030 for( ; j <= size.width - 4; j += 4 )
1032 int v0 = src[j], v1 = src[j+1];
1034 v0 = src[j+2]; v1 = src[j+3];
1038 for( ; j < size.width; j++ )
1042 double mu = 0, scale = 1./(size.width*size.height);
1043 for( i = 0; i < N; i++ )
1044 mu += i*(double)h[i];
1047 double mu1 = 0, q1 = 0;
1048 double max_sigma = 0, max_val = 0;
1050 for( i = 0; i < N; i++ )
1052 double p_i, q2, mu2, sigma;
1059 if( std::min(q1,q2) < FLT_EPSILON || std::max(q1,q2) > 1. - FLT_EPSILON )
1062 mu1 = (mu1 + i*p_i)/q1;
1063 mu2 = (mu - q1*mu1)/q2;
1064 sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
1065 if( sigma > max_sigma )
1076 getThreshVal_Triangle_8u( const Mat& _src )
1078 Size size = _src.size();
1079 int step = (int) _src.step;
1080 if( _src.isContinuous() )
1082 size.width *= size.height;
1088 int i, j, h[N] = {0};
1089 for( i = 0; i < size.height; i++ )
1091 const uchar* src = _src.ptr() + step*i;
1093 #if CV_ENABLE_UNROLLED
1094 for( ; j <= size.width - 4; j += 4 )
1096 int v0 = src[j], v1 = src[j+1];
1098 v0 = src[j+2]; v1 = src[j+3];
1102 for( ; j < size.width; j++ )
1106 int left_bound = 0, right_bound = 0, max_ind = 0, max = 0;
1108 bool isflipped = false;
1110 for( i = 0; i < N; i++ )
1118 if( left_bound > 0 )
1121 for( i = N-1; i > 0; i-- )
1129 if( right_bound < N-1 )
1132 for( i = 0; i < N; i++ )
1141 if( max_ind-left_bound < right_bound-max_ind)
1147 temp = h[i]; h[i] = h[j]; h[j] = temp;
1150 left_bound = N-1-right_bound;
1151 max_ind = N-1-max_ind;
1154 double thresh = left_bound;
1155 double a, b, dist = 0, tempdist;
1158 * We do not need to compute precise distance here. Distance is maximized, so some constants can
1159 * be omitted. This speeds up a computation a bit.
1161 a = max; b = left_bound-max_ind;
1162 for( i = left_bound+1; i <= max_ind; i++ )
1164 tempdist = a*i + b*h[i];
1165 if( tempdist > dist)
1174 thresh = N-1-thresh;
1179 class ThresholdRunner : public ParallelLoopBody
1182 ThresholdRunner(Mat _src, Mat _dst, double _thresh, double _maxval, int _thresholdType)
1189 thresholdType = _thresholdType;
1192 void operator () (const Range& range) const CV_OVERRIDE
1194 int row0 = range.start;
1195 int row1 = range.end;
1197 Mat srcStripe = src.rowRange(row0, row1);
1198 Mat dstStripe = dst.rowRange(row0, row1);
1200 CALL_HAL(threshold, cv_hal_threshold, srcStripe.data, srcStripe.step, dstStripe.data, dstStripe.step,
1201 srcStripe.cols, srcStripe.rows, srcStripe.depth(), srcStripe.channels(),
1202 thresh, maxval, thresholdType);
1204 if (srcStripe.depth() == CV_8U)
1206 thresh_8u( srcStripe, dstStripe, (uchar)thresh, (uchar)maxval, thresholdType );
1208 else if( srcStripe.depth() == CV_16S )
1210 thresh_16s( srcStripe, dstStripe, (short)thresh, (short)maxval, thresholdType );
1212 else if( srcStripe.depth() == CV_16U )
1214 thresh_16u( srcStripe, dstStripe, (ushort)thresh, (ushort)maxval, thresholdType );
1216 else if( srcStripe.depth() == CV_32F )
1218 thresh_32f( srcStripe, dstStripe, (float)thresh, (float)maxval, thresholdType );
1220 else if( srcStripe.depth() == CV_64F )
1222 thresh_64f(srcStripe, dstStripe, thresh, maxval, thresholdType);
1237 static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, double maxval, int thresh_type )
1239 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
1240 kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
1241 bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
1243 if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
1244 thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
1245 (!doubleSupport && depth == CV_64F))
1248 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
1249 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
1250 ocl::Device dev = ocl::Device::getDefault();
1251 int stride_size = dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU) ? 4 : 1;
1253 ocl::Kernel k("threshold", ocl::imgproc::threshold_oclsrc,
1254 format("-D %s -D T=%s -D T1=%s -D STRIDE_SIZE=%d%s", thresholdMap[thresh_type],
1255 ocl::typeToStr(ktype), ocl::typeToStr(depth), stride_size,
1256 doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
1260 UMat src = _src.getUMat();
1261 _dst.create(src.size(), type);
1262 UMat dst = _dst.getUMat();
1264 if (depth <= CV_32S)
1265 thresh = cvFloor(thresh);
1267 const double min_vals[] = { 0, CHAR_MIN, 0, SHRT_MIN, INT_MIN, -FLT_MAX, -DBL_MAX, 0 };
1268 double min_val = min_vals[depth];
1270 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst, cn, kercn),
1271 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(thresh))),
1272 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(maxval))),
1273 ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(min_val))));
1275 size_t globalsize[2] = { (size_t)dst.cols * cn / kercn, (size_t)dst.rows };
1276 globalsize[1] = (globalsize[1] + stride_size - 1) / stride_size;
1277 return k.run(2, globalsize, NULL, false);
1284 #define IMPL_OPENVX_TOZERO 1
1285 static bool openvx_threshold(Mat src, Mat dst, int thresh, int maxval, int type)
1289 int trueVal, falseVal;
1293 #ifndef VX_VERSION_1_1
1301 #if IMPL_OPENVX_TOZERO
1304 if (dst.data == src.data)
1306 a = Mat(src.size(), src.type());
1311 case THRESH_BINARY_INV:
1312 #ifdef VX_VERSION_1_1
1317 case THRESH_TOZERO_INV:
1318 #ifdef VX_VERSION_1_1
1319 #if IMPL_OPENVX_TOZERO
1322 if (dst.data == src.data)
1324 a = Mat(src.size(), src.type());
1337 ivx::Context ctx = ovx::getOpenVXContext();
1339 ivx::Threshold thh = ivx::Threshold::createBinary(ctx, VX_TYPE_UINT8, thresh);
1340 thh.setValueTrue(trueVal);
1341 thh.setValueFalse(falseVal);
1344 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1345 ivx::Image::createAddressing(a.cols*a.channels(), a.rows, 1, (vx_int32)(a.step)), src.data),
1346 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1347 ivx::Image::createAddressing(dst.cols*dst.channels(), dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1349 ivx::IVX_CHECK_STATUS(vxuThreshold(ctx, ia, thh, ib));
1350 #if IMPL_OPENVX_TOZERO
1351 if (type == THRESH_TOZERO || type == THRESH_TOZERO_INV)
1354 ic = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1355 ivx::Image::createAddressing(dst.cols*dst.channels(), dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1356 ivx::IVX_CHECK_STATUS(vxuAnd(ctx, ib, ia, ic));
1360 catch (ivx::RuntimeError & e)
1362 VX_DbgThrow(e.what());
1364 catch (ivx::WrapperError & e)
1366 VX_DbgThrow(e.what());
1375 double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double maxval, int type )
1377 CV_INSTRUMENT_REGION()
1379 CV_OCL_RUN_(_src.dims() <= 2 && _dst.isUMat(),
1380 ocl_threshold(_src, _dst, thresh, maxval, type), thresh)
1382 Mat src = _src.getMat();
1383 int automatic_thresh = (type & ~CV_THRESH_MASK);
1384 type &= THRESH_MASK;
1386 CV_Assert( automatic_thresh != (CV_THRESH_OTSU | CV_THRESH_TRIANGLE) );
1387 if( automatic_thresh == CV_THRESH_OTSU )
1389 CV_Assert( src.type() == CV_8UC1 );
1390 thresh = getThreshVal_Otsu_8u( src );
1392 else if( automatic_thresh == CV_THRESH_TRIANGLE )
1394 CV_Assert( src.type() == CV_8UC1 );
1395 thresh = getThreshVal_Triangle_8u( src );
1398 _dst.create( src.size(), src.type() );
1399 Mat dst = _dst.getMat();
1401 if( src.depth() == CV_8U )
1403 int ithresh = cvFloor(thresh);
1405 int imaxval = cvRound(maxval);
1406 if( type == THRESH_TRUNC )
1408 imaxval = saturate_cast<uchar>(imaxval);
1410 if( ithresh < 0 || ithresh >= 255 )
1412 if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
1413 ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < 0) ||
1414 (type == THRESH_TOZERO && ithresh >= 255) )
1416 int v = type == THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) :
1417 type == THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) :
1418 /*type == THRESH_TRUNC ? imaxval :*/ 0;
1426 CV_OVX_RUN(!ovx::skipSmallImages<VX_KERNEL_THRESHOLD>(src.cols, src.rows),
1427 openvx_threshold(src, dst, ithresh, imaxval, type), (double)ithresh)
1432 else if( src.depth() == CV_16S )
1434 int ithresh = cvFloor(thresh);
1436 int imaxval = cvRound(maxval);
1437 if( type == THRESH_TRUNC )
1439 imaxval = saturate_cast<short>(imaxval);
1441 if( ithresh < SHRT_MIN || ithresh >= SHRT_MAX )
1443 if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
1444 ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < SHRT_MIN) ||
1445 (type == THRESH_TOZERO && ithresh >= SHRT_MAX) )
1447 int v = type == THRESH_BINARY ? (ithresh >= SHRT_MAX ? 0 : imaxval) :
1448 type == THRESH_BINARY_INV ? (ithresh >= SHRT_MAX ? imaxval : 0) :
1449 /*type == THRESH_TRUNC ? imaxval :*/ 0;
1459 else if (src.depth() == CV_16U )
1461 int ithresh = cvFloor(thresh);
1463 int imaxval = cvRound(maxval);
1464 if (type == THRESH_TRUNC)
1466 imaxval = saturate_cast<ushort>(imaxval);
1469 if (ithresh < ushrt_min || ithresh >= (int)USHRT_MAX)
1471 if (type == THRESH_BINARY || type == THRESH_BINARY_INV ||
1472 ((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < ushrt_min) ||
1473 (type == THRESH_TOZERO && ithresh >= (int)USHRT_MAX))
1475 int v = type == THRESH_BINARY ? (ithresh >= (int)USHRT_MAX ? 0 : imaxval) :
1476 type == THRESH_BINARY_INV ? (ithresh >= (int)USHRT_MAX ? imaxval : 0) :
1477 /*type == THRESH_TRUNC ? imaxval :*/ 0;
1487 else if( src.depth() == CV_32F )
1489 else if( src.depth() == CV_64F )
1492 CV_Error( CV_StsUnsupportedFormat, "" );
1494 parallel_for_(Range(0, dst.rows),
1495 ThresholdRunner(src, dst, thresh, maxval, type),
1496 dst.total()/(double)(1<<16));
1501 void cv::adaptiveThreshold( InputArray _src, OutputArray _dst, double maxValue,
1502 int method, int type, int blockSize, double delta )
1504 CV_INSTRUMENT_REGION()
1506 Mat src = _src.getMat();
1507 CV_Assert( src.type() == CV_8UC1 );
1508 CV_Assert( blockSize % 2 == 1 && blockSize > 1 );
1509 Size size = src.size();
1511 _dst.create( size, src.type() );
1512 Mat dst = _dst.getMat();
1520 CALL_HAL(adaptiveThreshold, cv_hal_adaptiveThreshold, src.data, src.step, dst.data, dst.step, src.cols, src.rows,
1521 maxValue, method, type, blockSize, delta);
1525 if( src.data != dst.data )
1528 if (method == ADAPTIVE_THRESH_MEAN_C)
1529 boxFilter( src, mean, src.type(), Size(blockSize, blockSize),
1530 Point(-1,-1), true, BORDER_REPLICATE|BORDER_ISOLATED );
1531 else if (method == ADAPTIVE_THRESH_GAUSSIAN_C)
1533 Mat srcfloat,meanfloat;
1534 src.convertTo(srcfloat,CV_32F);
1536 GaussianBlur(srcfloat, meanfloat, Size(blockSize, blockSize), 0, 0, BORDER_REPLICATE|BORDER_ISOLATED);
1537 meanfloat.convertTo(mean, src.type());
1540 CV_Error( CV_StsBadFlag, "Unknown/unsupported adaptive threshold method" );
1543 uchar imaxval = saturate_cast<uchar>(maxValue);
1544 int idelta = type == THRESH_BINARY ? cvCeil(delta) : cvFloor(delta);
1547 if( type == CV_THRESH_BINARY )
1548 for( i = 0; i < 768; i++ )
1549 tab[i] = (uchar)(i - 255 > -idelta ? imaxval : 0);
1550 else if( type == CV_THRESH_BINARY_INV )
1551 for( i = 0; i < 768; i++ )
1552 tab[i] = (uchar)(i - 255 <= -idelta ? imaxval : 0);
1554 CV_Error( CV_StsBadFlag, "Unknown/unsupported threshold type" );
1556 if( src.isContinuous() && mean.isContinuous() && dst.isContinuous() )
1558 size.width *= size.height;
1562 for( i = 0; i < size.height; i++ )
1564 const uchar* sdata = src.ptr(i);
1565 const uchar* mdata = mean.ptr(i);
1566 uchar* ddata = dst.ptr(i);
1568 for( j = 0; j < size.width; j++ )
1569 ddata[j] = tab[sdata[j] - mdata[j] + 255];
1574 cvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, int type )
1576 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), dst0 = dst;
1578 CV_Assert( src.size == dst.size && src.channels() == dst.channels() &&
1579 (src.depth() == dst.depth() || dst.depth() == CV_8U));
1581 thresh = cv::threshold( src, dst, thresh, maxval, type );
1582 if( dst0.data != dst.data )
1583 dst.convertTo( dst0, dst0.depth() );
1589 cvAdaptiveThreshold( const void *srcIm, void *dstIm, double maxValue,
1590 int method, int type, int blockSize, double delta )
1592 cv::Mat src = cv::cvarrToMat(srcIm), dst = cv::cvarrToMat(dstIm);
1593 CV_Assert( src.size == dst.size && src.type() == dst.type() );
1594 cv::adaptiveThreshold( src, dst, maxValue, method, type, blockSize, delta );