2010-05-12 01:44:00 +08:00
|
|
|
/*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.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., 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 the copyright holders 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 "precomp.hpp"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_imgproc.hpp"
|
2016-10-04 07:42:02 +08:00
|
|
|
#include "opencv2/core/hal/intrin.hpp"
|
2016-07-18 22:45:16 +08:00
|
|
|
|
2016-12-14 22:31:41 +08:00
|
|
|
#include "opencv2/core/openvx/ovx_defs.hpp"
|
2016-11-01 21:31:37 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
2017-09-18 20:42:44 +08:00
|
|
|
template <typename T>
|
|
|
|
static inline T threshBinary(const T& src, const T& thresh, const T& maxval)
|
|
|
|
{
|
|
|
|
return src > thresh ? maxval : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T threshBinaryInv(const T& src, const T& thresh, const T& maxval)
|
|
|
|
{
|
|
|
|
return src <= thresh ? maxval : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T threshTrunc(const T& src, const T& thresh)
|
|
|
|
{
|
|
|
|
return std::min(src, thresh);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T threshToZero(const T& src, const T& thresh)
|
|
|
|
{
|
|
|
|
return src > thresh ? src : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T threshToZeroInv(const T& src, const T& thresh)
|
|
|
|
{
|
|
|
|
return src <= thresh ? src : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void threshGeneric(Size roi, const T* src, size_t src_step, T* dst,
|
|
|
|
size_t dst_step, T thresh, T maxval, int type)
|
|
|
|
{
|
|
|
|
int i = 0, j;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
for (; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
for (j = 0; j < roi.width; j++)
|
|
|
|
dst[j] = threshBinary<T>(src[j], thresh, maxval);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for (; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
for (j = 0; j < roi.width; j++)
|
|
|
|
dst[j] = threshBinaryInv<T>(src[j], thresh, maxval);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
for (; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
for (j = 0; j < roi.width; j++)
|
|
|
|
dst[j] = threshTrunc<T>(src[j], thresh);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
for (; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
for (j = 0; j < roi.width; j++)
|
|
|
|
dst[j] = threshToZero<T>(src[j], thresh);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for (; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
for (j = 0; j < roi.width; j++)
|
|
|
|
dst[j] = threshToZeroInv<T>(src[j], thresh);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CV_Error( CV_StsBadArg, "" ); return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
static void
|
|
|
|
thresh_8u( const Mat& _src, Mat& _dst, uchar thresh, uchar maxval, int type )
|
|
|
|
{
|
|
|
|
Size roi = _src.size();
|
|
|
|
roi.width *= _src.channels();
|
2014-04-04 19:07:17 +08:00
|
|
|
size_t src_step = _src.step;
|
|
|
|
size_t dst_step = _dst.step;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( _src.isContinuous() && _dst.isContinuous() )
|
|
|
|
{
|
|
|
|
roi.width *= roi.height;
|
|
|
|
roi.height = 1;
|
2014-04-04 19:07:17 +08:00
|
|
|
src_step = dst_step = roi.width;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-05-12 23:22:55 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-02-27 17:52:11 +08:00
|
|
|
if (tegra::useTegra() && tegra::thresh_8u(_src, _dst, roi.width, roi.height, thresh, maxval, type))
|
2012-06-14 22:09:04 +08:00
|
|
|
return;
|
2011-05-12 23:22:55 +08:00
|
|
|
#endif
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined(HAVE_IPP)
|
2014-10-03 19:17:28 +08:00
|
|
|
CV_IPP_CHECK()
|
2014-04-04 19:07:17 +08:00
|
|
|
{
|
2014-10-03 19:17:28 +08:00
|
|
|
IppiSize sz = { roi.width, roi.height };
|
|
|
|
CV_SUPPRESS_DEPRECATED_START
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_TRUNC:
|
2016-08-18 14:53:00 +08:00
|
|
|
if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO:
|
2016-08-18 14:53:00 +08:00
|
|
|
if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh+1, 0) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_LTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh + 1, 0) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO_INV:
|
2016-08-18 14:53:00 +08:00
|
|
|
if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1IR, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
if (CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_8u_C1R, _src.ptr(), (int)src_step, _dst.ptr(), (int)dst_step, sz, thresh, 0) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CV_SUPPRESS_DEPRECATED_END
|
2014-04-04 19:07:17 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-22 22:03:57 +08:00
|
|
|
int j = 0;
|
|
|
|
const uchar* src = _src.ptr();
|
|
|
|
uchar* dst = _dst.ptr();
|
2019-01-16 00:15:19 +08:00
|
|
|
#if CV_SIMD
|
|
|
|
v_uint8 thresh_u = vx_setall_u8( thresh );
|
|
|
|
v_uint8 maxval16 = vx_setall_u8( maxval );
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
for( j = 0; j <= roi.width - v_uint8::nlanes; j += v_uint8::nlanes)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint8 v0;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v0 = thresh_u < v0;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v_store( dst + j, v0 );
|
2016-07-22 22:03:57 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
for( j = 0; j <= roi.width - v_uint8::nlanes; j += v_uint8::nlanes)
|
2016-07-22 22:03:57 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint8 v0;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v0 = v0 <= thresh_u;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v_store( dst + j, v0 );
|
2016-07-22 22:03:57 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_TRUNC:
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
for( j = 0; j <= roi.width - v_uint8::nlanes; j += v_uint8::nlanes)
|
2016-07-22 22:03:57 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint8 v0;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v0 = v0 - ( v0 - thresh_u );
|
|
|
|
v_store( dst + j, v0 );
|
2016-07-22 22:03:57 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_TOZERO:
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
for( j = 0; j <= roi.width - v_uint8::nlanes; j += v_uint8::nlanes)
|
2016-07-22 22:03:57 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint8 v0;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v0 = ( thresh_u < v0 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
2016-07-22 22:03:57 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
for( j = 0; j <= roi.width - v_uint8::nlanes; j += v_uint8::nlanes)
|
2016-07-22 22:03:57 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint8 v0;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v0 = ( v0 <= thresh_u ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
2014-09-26 18:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
break;
|
2014-09-26 18:55:40 +08:00
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-22 22:03:57 +08:00
|
|
|
int j_scalar = j;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( j_scalar < roi.width )
|
|
|
|
{
|
2016-07-22 22:03:57 +08:00
|
|
|
const int thresh_pivot = thresh + 1;
|
2017-06-29 21:40:22 +08:00
|
|
|
uchar tab[256] = {0};
|
2016-07-22 22:03:57 +08:00
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
memset(tab, 0, thresh_pivot);
|
|
|
|
if (thresh_pivot < 256) {
|
|
|
|
memset(tab + thresh_pivot, maxval, 256 - thresh_pivot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
memset(tab, maxval, thresh_pivot);
|
|
|
|
if (thresh_pivot < 256) {
|
|
|
|
memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
for( int i = 0; i <= thresh; i++ )
|
|
|
|
tab[i] = (uchar)i;
|
|
|
|
if (thresh_pivot < 256) {
|
|
|
|
memset(tab + thresh_pivot, thresh, 256 - thresh_pivot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
memset(tab, 0, thresh_pivot);
|
|
|
|
for( int i = thresh_pivot; i < 256; i++ )
|
|
|
|
tab[i] = (uchar)i;
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for( int i = 0; i <= thresh; i++ )
|
|
|
|
tab[i] = (uchar)i;
|
|
|
|
if (thresh_pivot < 256) {
|
|
|
|
memset(tab + thresh_pivot, 0, 256 - thresh_pivot);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = _src.ptr();
|
|
|
|
dst = _dst.ptr();
|
|
|
|
for( int i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-14 22:09:04 +08:00
|
|
|
j = j_scalar;
|
|
|
|
#if CV_ENABLE_UNROLLED
|
2012-02-21 19:31:23 +08:00
|
|
|
for( ; j <= roi.width - 4; j += 4 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
uchar t0 = tab[src[j]];
|
|
|
|
uchar t1 = tab[src[j+1]];
|
|
|
|
|
|
|
|
dst[j] = t0;
|
|
|
|
dst[j+1] = t1;
|
|
|
|
|
|
|
|
t0 = tab[src[j+2]];
|
|
|
|
t1 = tab[src[j+3]];
|
|
|
|
|
|
|
|
dst[j+2] = t0;
|
|
|
|
dst[j+3] = t1;
|
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = tab[src[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 20:33:14 +08:00
|
|
|
static void
|
|
|
|
thresh_16u(const Mat& _src, Mat& _dst, ushort thresh, ushort maxval, int type)
|
|
|
|
{
|
2017-09-18 20:42:44 +08:00
|
|
|
Size roi = _src.size();
|
|
|
|
roi.width *= _src.channels();
|
|
|
|
size_t src_step = _src.step / _src.elemSize1();
|
|
|
|
size_t dst_step = _dst.step / _dst.elemSize1();
|
2017-09-18 20:33:14 +08:00
|
|
|
|
2017-09-18 20:42:44 +08:00
|
|
|
if (_src.isContinuous() && _dst.isContinuous())
|
|
|
|
{
|
|
|
|
roi.width *= roi.height;
|
|
|
|
roi.height = 1;
|
|
|
|
src_step = dst_step = roi.width;
|
|
|
|
}
|
2017-09-18 20:33:14 +08:00
|
|
|
|
2017-09-18 20:42:44 +08:00
|
|
|
// HAVE_TEGRA_OPTIMIZATION not supported
|
2017-09-18 20:33:14 +08:00
|
|
|
|
2017-09-18 20:42:44 +08:00
|
|
|
// HAVE_IPP not supported
|
2017-09-18 20:33:14 +08:00
|
|
|
|
2017-09-18 20:42:44 +08:00
|
|
|
const ushort* src = _src.ptr<ushort>();
|
|
|
|
ushort* dst = _dst.ptr<ushort>();
|
2019-01-16 00:15:19 +08:00
|
|
|
#if CV_SIMD
|
|
|
|
int i, j;
|
|
|
|
v_uint16 thresh_u = vx_setall_u16(thresh);
|
|
|
|
v_uint16 maxval16 = vx_setall_u16(maxval);
|
2017-09-18 20:42:44 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
for (j = 0; j <= roi.width - 2*v_uint16::nlanes; j += 2*v_uint16::nlanes)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint16 v0, v1;
|
|
|
|
v0 = vx_load(src + j);
|
|
|
|
v1 = vx_load(src + j + v_uint16::nlanes);
|
|
|
|
v0 = thresh_u < v0;
|
|
|
|
v1 = thresh_u < v1;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v1 = v1 & maxval16;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
v_store(dst + j + v_uint16::nlanes, v1);
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if (j <= roi.width - v_uint16::nlanes)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint16 v0 = vx_load(src + j);
|
|
|
|
v0 = thresh_u < v0;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
j += v_uint16::nlanes;
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for (; j < roi.width; j++)
|
|
|
|
dst[j] = threshBinary<ushort>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
2017-09-18 20:42:44 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for (; j <= roi.width - 2*v_uint16::nlanes; j += 2*v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0, v1;
|
|
|
|
v0 = vx_load(src + j);
|
|
|
|
v1 = vx_load(src + j + v_uint16::nlanes);
|
|
|
|
v0 = v0 <= thresh_u;
|
|
|
|
v1 = v1 <= thresh_u;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v1 = v1 & maxval16;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
v_store(dst + j + v_uint16::nlanes, v1);
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if (j <= roi.width - v_uint16::nlanes)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint16 v0 = vx_load(src + j);
|
|
|
|
v0 = v0 <= thresh_u;
|
|
|
|
v0 = v0 & maxval16;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
j += v_uint16::nlanes;
|
|
|
|
}
|
2017-09-18 20:42:44 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for (; j < roi.width; j++)
|
|
|
|
dst[j] = threshBinaryInv<ushort>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for (; j <= roi.width - 2*v_uint16::nlanes; j += 2*v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0, v1;
|
|
|
|
v0 = vx_load(src + j);
|
|
|
|
v1 = vx_load(src + j + v_uint16::nlanes);
|
|
|
|
v0 = v_min(v0, thresh_u);
|
|
|
|
v1 = v_min(v1, thresh_u);
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
v_store(dst + j + v_uint16::nlanes, v1);
|
|
|
|
}
|
|
|
|
if (j <= roi.width - v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0 = vx_load(src + j);
|
|
|
|
v0 = v_min(v0, thresh_u);
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
j += v_uint16::nlanes;
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for (; j < roi.width; j++)
|
|
|
|
dst[j] = threshTrunc<ushort>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for (; j <= roi.width - 2*v_uint16::nlanes; j += 2*v_uint16::nlanes)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_uint16 v0, v1;
|
|
|
|
v0 = vx_load(src + j);
|
|
|
|
v1 = vx_load(src + j + v_uint16::nlanes);
|
|
|
|
v0 = (thresh_u < v0) & v0;
|
|
|
|
v1 = (thresh_u < v1) & v1;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
v_store(dst + j + v_uint16::nlanes, v1);
|
|
|
|
}
|
|
|
|
if (j <= roi.width - v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0 = vx_load(src + j);
|
|
|
|
v0 = (thresh_u < v0) & v0;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
j += v_uint16::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; j < roi.width; j++)
|
|
|
|
dst[j] = threshToZero<ushort>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
2017-09-18 20:42:44 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step)
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for (; j <= roi.width - 2*v_uint16::nlanes; j += 2*v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0, v1;
|
|
|
|
v0 = vx_load(src + j);
|
|
|
|
v1 = vx_load(src + j + v_uint16::nlanes);
|
|
|
|
v0 = (v0 <= thresh_u) & v0;
|
|
|
|
v1 = (v1 <= thresh_u) & v1;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
v_store(dst + j + v_uint16::nlanes, v1);
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if (j <= roi.width - v_uint16::nlanes)
|
|
|
|
{
|
|
|
|
v_uint16 v0 = vx_load(src + j);
|
|
|
|
v0 = (v0 <= thresh_u) & v0;
|
|
|
|
v_store(dst + j, v0);
|
|
|
|
j += v_uint16::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; j < roi.width; j++)
|
|
|
|
dst[j] = threshToZeroInv<ushort>(src[j], thresh);
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
break;
|
2017-09-18 20:42:44 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
#else
|
|
|
|
threshGeneric<ushort>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
|
2017-09-18 20:33:14 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
static void
|
2011-11-22 23:16:45 +08:00
|
|
|
thresh_16s( const Mat& _src, Mat& _dst, short thresh, short maxval, int type )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
Size roi = _src.size();
|
|
|
|
roi.width *= _src.channels();
|
2014-08-13 19:08:27 +08:00
|
|
|
const short* src = _src.ptr<short>();
|
|
|
|
short* dst = _dst.ptr<short>();
|
2010-05-12 01:44:00 +08:00
|
|
|
size_t src_step = _src.step/sizeof(src[0]);
|
|
|
|
size_t dst_step = _dst.step/sizeof(dst[0]);
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( _src.isContinuous() && _dst.isContinuous() )
|
|
|
|
{
|
|
|
|
roi.width *= roi.height;
|
|
|
|
roi.height = 1;
|
2014-04-04 19:07:17 +08:00
|
|
|
src_step = dst_step = roi.width;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2012-06-08 16:49:59 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-02-27 17:52:11 +08:00
|
|
|
if (tegra::useTegra() && tegra::thresh_16s(_src, _dst, roi.width, roi.height, thresh, maxval, type))
|
2012-06-14 22:09:04 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined(HAVE_IPP)
|
2014-10-03 19:17:28 +08:00
|
|
|
CV_IPP_CHECK()
|
2014-04-04 19:07:17 +08:00
|
|
|
{
|
2014-10-03 19:17:28 +08:00
|
|
|
IppiSize sz = { roi.width, roi.height };
|
|
|
|
CV_SUPPRESS_DEPRECATED_START
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_TRUNC:
|
2016-08-18 14:53:00 +08:00
|
|
|
if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GT_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
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)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO:
|
2016-08-18 14:53:00 +08:00
|
|
|
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)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
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)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO_INV:
|
2016-08-18 14:53:00 +08:00
|
|
|
if (_src.data == _dst.data && CV_INSTRUMENT_FUN_IPP(ippiThreshold_GTVal_16s_C1IR, dst, (int)dst_step*sizeof(dst[0]), sz, thresh, 0) >= 0)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-18 14:53:00 +08:00
|
|
|
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)
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CV_SUPPRESS_DEPRECATED_END
|
2014-04-04 19:07:17 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
#if CV_SIMD
|
|
|
|
int i, j;
|
|
|
|
v_int16 thresh8 = vx_setall_s16( thresh );
|
|
|
|
v_int16 maxval8 = vx_setall_s16( maxval );
|
2016-10-04 07:42:02 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_int16::nlanes; j += 2*v_int16::nlanes )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_int16 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_int16::nlanes );
|
|
|
|
v0 = thresh8 < v0;
|
|
|
|
v1 = thresh8 < v1;
|
|
|
|
v0 = v0 & maxval8;
|
|
|
|
v1 = v1 & maxval8;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_int16::nlanes, v1 );
|
2014-09-26 18:55:40 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if( j <= roi.width - v_int16::nlanes )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_int16 v0 = vx_load( src + j );
|
|
|
|
v0 = thresh8 < v0;
|
|
|
|
v0 = v0 & maxval8;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_int16::nlanes;
|
2014-09-26 18:55:40 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshBinary<short>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
2016-10-04 07:42:02 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_int16::nlanes; j += 2*v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_int16::nlanes );
|
|
|
|
v0 = v0 <= thresh8;
|
|
|
|
v1 = v1 <= thresh8;
|
|
|
|
v0 = v0 & maxval8;
|
|
|
|
v1 = v1 & maxval8;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_int16::nlanes, v1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if( j <= roi.width - v_int16::nlanes )
|
2016-10-04 07:42:02 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_int16 v0 = vx_load( src + j );
|
|
|
|
v0 = v0 <= thresh8;
|
|
|
|
v0 = v0 & maxval8;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_int16::nlanes;
|
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshBinaryInv<short>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_int16::nlanes; j += 2*v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_int16::nlanes );
|
|
|
|
v0 = v_min( v0, thresh8 );
|
|
|
|
v1 = v_min( v1, thresh8 );
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_int16::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0 = vx_load( src + j );
|
|
|
|
v0 = v_min( v0, thresh8 );
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_int16::nlanes;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshTrunc<short>( src[j], thresh );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_int16::nlanes; j += 2*v_int16::nlanes )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_int16 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_int16::nlanes );
|
|
|
|
v0 = ( thresh8 < v0 ) & v0;
|
|
|
|
v1 = ( thresh8 < v1 ) & v1;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_int16::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0 = vx_load( src + j );
|
|
|
|
v0 = ( thresh8 < v0 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_int16::nlanes;
|
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshToZero<short>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_int16::nlanes; j += 2*v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_int16::nlanes );
|
|
|
|
v0 = ( v0 <= thresh8 ) & v0;
|
|
|
|
v1 = ( v1 <= thresh8 ) & v1;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_int16::nlanes, v1 );
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
if( j <= roi.width - v_int16::nlanes )
|
|
|
|
{
|
|
|
|
v_int16 v0 = vx_load( src + j );
|
|
|
|
v0 = ( v0 <= thresh8 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_int16::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshToZeroInv<short>(src[j], thresh);
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error( CV_StsBadArg, "" ); return;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
#else
|
|
|
|
threshGeneric<short>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
|
2016-10-04 07:42:02 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2011-11-22 23:16:45 +08:00
|
|
|
static void
|
|
|
|
thresh_32f( const Mat& _src, Mat& _dst, float thresh, float maxval, int type )
|
|
|
|
{
|
|
|
|
Size roi = _src.size();
|
|
|
|
roi.width *= _src.channels();
|
2014-08-13 19:08:27 +08:00
|
|
|
const float* src = _src.ptr<float>();
|
|
|
|
float* dst = _dst.ptr<float>();
|
2011-11-22 23:16:45 +08:00
|
|
|
size_t src_step = _src.step/sizeof(src[0]);
|
|
|
|
size_t dst_step = _dst.step/sizeof(dst[0]);
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2011-11-22 23:16:45 +08:00
|
|
|
if( _src.isContinuous() && _dst.isContinuous() )
|
|
|
|
{
|
|
|
|
roi.width *= roi.height;
|
|
|
|
roi.height = 1;
|
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2012-06-08 16:49:59 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2015-02-27 17:52:11 +08:00
|
|
|
if (tegra::useTegra() && tegra::thresh_32f(_src, _dst, roi.width, roi.height, thresh, maxval, type))
|
2012-06-14 22:09:04 +08:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined(HAVE_IPP)
|
2014-10-03 19:17:28 +08:00
|
|
|
CV_IPP_CHECK()
|
2014-04-04 19:07:17 +08:00
|
|
|
{
|
2014-10-03 19:17:28 +08:00
|
|
|
IppiSize sz = { roi.width, roi.height };
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case THRESH_TRUNC:
|
2016-08-18 14:53:00 +08:00
|
|
|
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))
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO:
|
2016-08-18 14:53:00 +08:00
|
|
|
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))
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO_INV:
|
2016-08-18 14:53:00 +08:00
|
|
|
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))
|
2014-10-03 19:17:28 +08:00
|
|
|
{
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
|
|
|
break;
|
|
|
|
}
|
2014-04-04 19:07:17 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
#if CV_SIMD
|
|
|
|
int i, j;
|
|
|
|
v_float32 thresh4 = vx_setall_f32( thresh );
|
|
|
|
v_float32 maxval4 = vx_setall_f32( maxval );
|
2016-05-17 21:56:03 +08:00
|
|
|
|
2019-01-16 00:15:19 +08:00
|
|
|
switch( type )
|
2016-05-17 21:56:03 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
case THRESH_BINARY:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
j = 0;
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j <= roi.width - 2*v_float32::nlanes; j += 2*v_float32::nlanes )
|
|
|
|
{
|
|
|
|
v_float32 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float32::nlanes );
|
|
|
|
v0 = thresh4 < v0;
|
|
|
|
v1 = thresh4 < v1;
|
|
|
|
v0 = v0 & maxval4;
|
|
|
|
v1 = v1 & maxval4;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float32::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float32::nlanes )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_float32 v0 = vx_load( src + j );
|
|
|
|
v0 = thresh4 < v0;
|
|
|
|
v0 = v0 & maxval4;
|
2016-10-04 07:42:02 +08:00
|
|
|
v_store( dst + j, v0 );
|
2019-01-16 00:15:19 +08:00
|
|
|
j += v_float32::nlanes;
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
2019-01-16 00:15:19 +08:00
|
|
|
dst[j] = threshBinary<float>(src[j], thresh, maxval);
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
break;
|
2016-05-17 21:56:03 +08:00
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
j = 0;
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j <= roi.width - 2*v_float32::nlanes; j += 2*v_float32::nlanes )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_float32 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float32::nlanes );
|
|
|
|
v0 = v0 <= thresh4;
|
|
|
|
v1 = v1 <= thresh4;
|
|
|
|
v0 = v0 & maxval4;
|
|
|
|
v1 = v1 & maxval4;
|
2016-10-04 07:42:02 +08:00
|
|
|
v_store( dst + j, v0 );
|
2019-01-16 00:15:19 +08:00
|
|
|
v_store( dst + j + v_float32::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float32::nlanes )
|
|
|
|
{
|
|
|
|
v_float32 v0 = vx_load( src + j );
|
|
|
|
v0 = v0 <= thresh4;
|
|
|
|
v0 = v0 & maxval4;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float32::nlanes;
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
2019-01-16 00:15:19 +08:00
|
|
|
dst[j] = threshBinaryInv<float>(src[j], thresh, maxval);
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
break;
|
2016-06-21 10:36:52 +08:00
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
case THRESH_TRUNC:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
j = 0;
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j <= roi.width - 2*v_float32::nlanes; j += 2*v_float32::nlanes )
|
|
|
|
{
|
|
|
|
v_float32 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float32::nlanes );
|
|
|
|
v0 = v_min( v0, thresh4 );
|
|
|
|
v1 = v_min( v1, thresh4 );
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float32::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float32::nlanes )
|
2016-10-04 07:42:02 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_float32 v0 = vx_load( src + j );
|
|
|
|
v0 = v_min( v0, thresh4 );
|
2016-10-04 07:42:02 +08:00
|
|
|
v_store( dst + j, v0 );
|
2019-01-16 00:15:19 +08:00
|
|
|
j += v_float32::nlanes;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2016-05-17 21:56:03 +08:00
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
for( ; j < roi.width; j++ )
|
2019-01-16 00:15:19 +08:00
|
|
|
dst[j] = threshTrunc<float>(src[j], thresh);
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-05-17 21:56:03 +08:00
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
case THRESH_TOZERO:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
j = 0;
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j <= roi.width - 2*v_float32::nlanes; j += 2*v_float32::nlanes )
|
|
|
|
{
|
|
|
|
v_float32 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float32::nlanes );
|
|
|
|
v0 = ( thresh4 < v0 ) & v0;
|
|
|
|
v1 = ( thresh4 < v1 ) & v1;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float32::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float32::nlanes )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_float32 v0 = vx_load( src + j );
|
|
|
|
v0 = ( thresh4 < v0 ) & v0;
|
2016-10-04 07:42:02 +08:00
|
|
|
v_store( dst + j, v0 );
|
2019-01-16 00:15:19 +08:00
|
|
|
j += v_float32::nlanes;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
2019-01-16 00:15:19 +08:00
|
|
|
dst[j] = threshToZero<float>(src[j], thresh);
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
break;
|
2016-06-21 10:36:52 +08:00
|
|
|
|
2016-10-04 07:42:02 +08:00
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
2016-06-21 10:36:52 +08:00
|
|
|
{
|
2016-10-04 07:42:02 +08:00
|
|
|
j = 0;
|
2019-01-16 00:15:19 +08:00
|
|
|
for( ; j <= roi.width - 2*v_float32::nlanes; j += 2*v_float32::nlanes )
|
2016-10-04 07:42:02 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
v_float32 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float32::nlanes );
|
|
|
|
v0 = ( v0 <= thresh4 ) & v0;
|
|
|
|
v1 = ( v1 <= thresh4 ) & v1;
|
2016-10-04 07:42:02 +08:00
|
|
|
v_store( dst + j, v0 );
|
2019-01-16 00:15:19 +08:00
|
|
|
v_store( dst + j + v_float32::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float32::nlanes )
|
|
|
|
{
|
|
|
|
v_float32 v0 = vx_load( src + j );
|
|
|
|
v0 = ( v0 <= thresh4 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float32::nlanes;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
2019-01-16 00:15:19 +08:00
|
|
|
dst[j] = threshToZeroInv<float>(src[j], thresh);
|
2016-06-21 10:36:52 +08:00
|
|
|
}
|
2016-10-04 07:42:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
2019-01-16 00:15:19 +08:00
|
|
|
CV_Error( CV_StsBadArg, "" ); return;
|
2016-10-04 07:42:02 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
#else
|
|
|
|
threshGeneric<float>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
|
2016-10-04 07:42:02 +08:00
|
|
|
#endif
|
2019-01-16 00:15:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thresh_64f(const Mat& _src, Mat& _dst, double thresh, double maxval, int type)
|
|
|
|
{
|
|
|
|
Size roi = _src.size();
|
|
|
|
roi.width *= _src.channels();
|
|
|
|
const double* src = _src.ptr<double>();
|
|
|
|
double* dst = _dst.ptr<double>();
|
|
|
|
size_t src_step = _src.step / sizeof(src[0]);
|
|
|
|
size_t dst_step = _dst.step / sizeof(dst[0]);
|
|
|
|
|
|
|
|
if (_src.isContinuous() && _dst.isContinuous())
|
|
|
|
{
|
|
|
|
roi.width *= roi.height;
|
|
|
|
roi.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CV_SIMD_64F
|
|
|
|
int i, j;
|
|
|
|
v_float64 thresh2 = vx_setall_f64( thresh );
|
|
|
|
v_float64 maxval2 = vx_setall_f64( maxval );
|
|
|
|
|
|
|
|
switch( type )
|
2016-10-04 07:42:02 +08:00
|
|
|
{
|
2019-01-16 00:15:19 +08:00
|
|
|
case THRESH_BINARY:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_float64::nlanes; j += 2*v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float64::nlanes );
|
|
|
|
v0 = thresh2 < v0;
|
|
|
|
v1 = thresh2 < v1;
|
|
|
|
v0 = v0 & maxval2;
|
|
|
|
v1 = v1 & maxval2;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float64::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0 = vx_load( src + j );
|
|
|
|
v0 = thresh2 < v0;
|
|
|
|
v0 = v0 & maxval2;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float64::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshBinary<double>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_float64::nlanes; j += 2*v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float64::nlanes );
|
|
|
|
v0 = v0 <= thresh2;
|
|
|
|
v1 = v1 <= thresh2;
|
|
|
|
v0 = v0 & maxval2;
|
|
|
|
v1 = v1 & maxval2;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float64::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0 = vx_load( src + j );
|
|
|
|
v0 = v0 <= thresh2;
|
|
|
|
v0 = v0 & maxval2;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float64::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshBinaryInv<double>(src[j], thresh, maxval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_float64::nlanes; j += 2*v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float64::nlanes );
|
|
|
|
v0 = v_min( v0, thresh2 );
|
|
|
|
v1 = v_min( v1, thresh2 );
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float64::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0 = vx_load( src + j );
|
|
|
|
v0 = v_min( v0, thresh2 );
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float64::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshTrunc<double>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_float64::nlanes; j += 2*v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float64::nlanes );
|
|
|
|
v0 = ( thresh2 < v0 ) & v0;
|
|
|
|
v1 = ( thresh2 < v1 ) & v1;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float64::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0 = vx_load( src + j );
|
|
|
|
v0 = ( thresh2 < v0 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float64::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshToZero<double>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
for( ; j <= roi.width - 2*v_float64::nlanes; j += 2*v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0, v1;
|
|
|
|
v0 = vx_load( src + j );
|
|
|
|
v1 = vx_load( src + j + v_float64::nlanes );
|
|
|
|
v0 = ( v0 <= thresh2 ) & v0;
|
|
|
|
v1 = ( v1 <= thresh2 ) & v1;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
v_store( dst + j + v_float64::nlanes, v1 );
|
|
|
|
}
|
|
|
|
if( j <= roi.width - v_float64::nlanes )
|
|
|
|
{
|
|
|
|
v_float64 v0 = vx_load( src + j );
|
|
|
|
v0 = ( v0 <= thresh2 ) & v0;
|
|
|
|
v_store( dst + j, v0 );
|
|
|
|
j += v_float64::nlanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; j < roi.width; j++ )
|
|
|
|
dst[j] = threshToZeroInv<double>(src[j], thresh);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsBadArg, ""); return;
|
2016-05-17 21:56:03 +08:00
|
|
|
}
|
2019-01-16 00:15:19 +08:00
|
|
|
#else
|
|
|
|
threshGeneric<double>(roi, src, src_step, dst, dst_step, thresh, maxval, type);
|
|
|
|
#endif
|
2016-05-17 21:56:03 +08:00
|
|
|
}
|
|
|
|
|
2015-05-15 16:15:00 +08:00
|
|
|
#ifdef HAVE_IPP
|
|
|
|
static bool ipp_getThreshVal_Otsu_8u( const unsigned char* _src, int step, Size size, unsigned char &thresh)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION_IPP();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2017-08-17 19:57:58 +08:00
|
|
|
// Performance degradations
|
|
|
|
#if IPP_VERSION_X100 >= 201800
|
2015-05-15 16:15:00 +08:00
|
|
|
IppiSize srcSize = { size.width, size.height };
|
2015-06-24 18:50:17 +08:00
|
|
|
|
2017-08-17 19:57:58 +08:00
|
|
|
if(CV_INSTRUMENT_FUN_IPP(ippiComputeThreshold_Otsu_8u_C1R, _src, step, srcSize, &thresh) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-05-15 16:15:00 +08:00
|
|
|
#else
|
|
|
|
CV_UNUSED(_src); CV_UNUSED(step); CV_UNUSED(size); CV_UNUSED(thresh);
|
|
|
|
return false;
|
2017-08-17 19:57:58 +08:00
|
|
|
#endif
|
2015-05-15 16:15:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
static double
|
|
|
|
getThreshVal_Otsu_8u( const Mat& _src )
|
|
|
|
{
|
|
|
|
Size size = _src.size();
|
2014-04-25 21:31:45 +08:00
|
|
|
int step = (int) _src.step;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( _src.isContinuous() )
|
|
|
|
{
|
|
|
|
size.width *= size.height;
|
|
|
|
size.height = 1;
|
2014-04-25 21:31:45 +08:00
|
|
|
step = size.width;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-04-25 21:31:45 +08:00
|
|
|
|
2015-05-15 16:15:00 +08:00
|
|
|
#ifdef HAVE_IPP
|
2017-09-05 22:10:16 +08:00
|
|
|
unsigned char thresh = 0;
|
2017-08-17 19:57:58 +08:00
|
|
|
CV_IPP_RUN_FAST(ipp_getThreshVal_Otsu_8u(_src.ptr(), step, size, thresh), thresh);
|
2014-04-25 21:31:45 +08:00
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
const int N = 256;
|
|
|
|
int i, j, h[N] = {0};
|
2019-07-16 21:10:49 +08:00
|
|
|
#if CV_ENABLE_UNROLLED
|
|
|
|
int h_unrolled[3][N] = {};
|
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < size.height; i++ )
|
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
const uchar* src = _src.ptr() + step*i;
|
2012-06-14 22:09:04 +08:00
|
|
|
j = 0;
|
|
|
|
#if CV_ENABLE_UNROLLED
|
2012-02-21 19:31:23 +08:00
|
|
|
for( ; j <= size.width - 4; j += 4 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int v0 = src[j], v1 = src[j+1];
|
2019-07-16 21:10:49 +08:00
|
|
|
h[v0]++; h_unrolled[0][v1]++;
|
2010-05-12 01:44:00 +08:00
|
|
|
v0 = src[j+2]; v1 = src[j+3];
|
2019-07-16 21:10:49 +08:00
|
|
|
h_unrolled[1][v0]++; h_unrolled[2][v1]++;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-02-21 19:31:23 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ; j < size.width; j++ )
|
|
|
|
h[src[j]]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
double mu = 0, scale = 1./(size.width*size.height);
|
|
|
|
for( i = 0; i < N; i++ )
|
2019-07-16 21:10:49 +08:00
|
|
|
{
|
|
|
|
#if CV_ENABLE_UNROLLED
|
|
|
|
h[i] += h_unrolled[0][i] + h_unrolled[1][i] + h_unrolled[2][i];
|
|
|
|
#endif
|
2010-11-04 20:38:27 +08:00
|
|
|
mu += i*(double)h[i];
|
2019-07-16 21:10:49 +08:00
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
mu *= scale;
|
|
|
|
double mu1 = 0, q1 = 0;
|
|
|
|
double max_sigma = 0, max_val = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < N; i++ )
|
|
|
|
{
|
|
|
|
double p_i, q2, mu2, sigma;
|
|
|
|
|
|
|
|
p_i = h[i]*scale;
|
|
|
|
mu1 *= q1;
|
|
|
|
q1 += p_i;
|
|
|
|
q2 = 1. - q1;
|
|
|
|
|
|
|
|
if( std::min(q1,q2) < FLT_EPSILON || std::max(q1,q2) > 1. - FLT_EPSILON )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mu1 = (mu1 + i*p_i)/q1;
|
|
|
|
mu2 = (mu - q1*mu1)/q2;
|
|
|
|
sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
|
|
|
|
if( sigma > max_sigma )
|
|
|
|
{
|
|
|
|
max_sigma = sigma;
|
|
|
|
max_val = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_val;
|
|
|
|
}
|
|
|
|
|
2014-10-22 22:54:39 +08:00
|
|
|
static double
|
|
|
|
getThreshVal_Triangle_8u( const Mat& _src )
|
|
|
|
{
|
|
|
|
Size size = _src.size();
|
|
|
|
int step = (int) _src.step;
|
|
|
|
if( _src.isContinuous() )
|
|
|
|
{
|
|
|
|
size.width *= size.height;
|
|
|
|
size.height = 1;
|
|
|
|
step = size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int N = 256;
|
|
|
|
int i, j, h[N] = {0};
|
2019-07-16 21:10:49 +08:00
|
|
|
#if CV_ENABLE_UNROLLED
|
|
|
|
int h_unrolled[3][N] = {};
|
|
|
|
#endif
|
2014-10-22 22:54:39 +08:00
|
|
|
for( i = 0; i < size.height; i++ )
|
|
|
|
{
|
|
|
|
const uchar* src = _src.ptr() + step*i;
|
|
|
|
j = 0;
|
|
|
|
#if CV_ENABLE_UNROLLED
|
|
|
|
for( ; j <= size.width - 4; j += 4 )
|
|
|
|
{
|
|
|
|
int v0 = src[j], v1 = src[j+1];
|
2019-07-16 21:10:49 +08:00
|
|
|
h[v0]++; h_unrolled[0][v1]++;
|
2014-10-22 22:54:39 +08:00
|
|
|
v0 = src[j+2]; v1 = src[j+3];
|
2019-07-16 21:10:49 +08:00
|
|
|
h_unrolled[1][v0]++; h_unrolled[2][v1]++;
|
2014-10-22 22:54:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for( ; j < size.width; j++ )
|
|
|
|
h[src[j]]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int left_bound = 0, right_bound = 0, max_ind = 0, max = 0;
|
|
|
|
int temp;
|
|
|
|
bool isflipped = false;
|
|
|
|
|
2019-07-16 21:10:49 +08:00
|
|
|
#if CV_ENABLE_UNROLLED
|
|
|
|
for( i = 0; i < N; i++ )
|
|
|
|
{
|
|
|
|
h[i] += h_unrolled[0][i] + h_unrolled[1][i] + h_unrolled[2][i];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-22 22:54:39 +08:00
|
|
|
for( i = 0; i < N; i++ )
|
|
|
|
{
|
|
|
|
if( h[i] > 0 )
|
|
|
|
{
|
|
|
|
left_bound = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( left_bound > 0 )
|
|
|
|
left_bound--;
|
|
|
|
|
|
|
|
for( i = N-1; i > 0; i-- )
|
|
|
|
{
|
|
|
|
if( h[i] > 0 )
|
|
|
|
{
|
|
|
|
right_bound = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( right_bound < N-1 )
|
|
|
|
right_bound++;
|
|
|
|
|
|
|
|
for( i = 0; i < N; i++ )
|
|
|
|
{
|
|
|
|
if( h[i] > max)
|
|
|
|
{
|
|
|
|
max = h[i];
|
|
|
|
max_ind = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( max_ind-left_bound < right_bound-max_ind)
|
|
|
|
{
|
|
|
|
isflipped = true;
|
|
|
|
i = 0, j = N-1;
|
|
|
|
while( i < j )
|
|
|
|
{
|
|
|
|
temp = h[i]; h[i] = h[j]; h[j] = temp;
|
|
|
|
i++; j--;
|
|
|
|
}
|
|
|
|
left_bound = N-1-right_bound;
|
|
|
|
max_ind = N-1-max_ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
double thresh = left_bound;
|
|
|
|
double a, b, dist = 0, tempdist;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not need to compute precise distance here. Distance is maximized, so some constants can
|
|
|
|
* be omitted. This speeds up a computation a bit.
|
|
|
|
*/
|
|
|
|
a = max; b = left_bound-max_ind;
|
|
|
|
for( i = left_bound+1; i <= max_ind; i++ )
|
|
|
|
{
|
|
|
|
tempdist = a*i + b*h[i];
|
|
|
|
if( tempdist > dist)
|
|
|
|
{
|
|
|
|
dist = tempdist;
|
|
|
|
thresh = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thresh--;
|
|
|
|
|
|
|
|
if( isflipped )
|
|
|
|
thresh = N-1-thresh;
|
|
|
|
|
|
|
|
return thresh;
|
|
|
|
}
|
|
|
|
|
2012-10-10 02:38:04 +08:00
|
|
|
class ThresholdRunner : public ParallelLoopBody
|
2012-01-06 22:29:31 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-10-12 02:37:14 +08:00
|
|
|
ThresholdRunner(Mat _src, Mat _dst, double _thresh, double _maxval, int _thresholdType)
|
2012-01-06 22:29:31 +08:00
|
|
|
{
|
|
|
|
src = _src;
|
|
|
|
dst = _dst;
|
|
|
|
|
|
|
|
thresh = _thresh;
|
|
|
|
maxval = _maxval;
|
|
|
|
thresholdType = _thresholdType;
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:16:51 +08:00
|
|
|
void operator () (const Range& range) const CV_OVERRIDE
|
2012-01-06 22:29:31 +08:00
|
|
|
{
|
2012-10-12 02:37:14 +08:00
|
|
|
int row0 = range.start;
|
|
|
|
int row1 = range.end;
|
2012-01-06 22:29:31 +08:00
|
|
|
|
|
|
|
Mat srcStripe = src.rowRange(row0, row1);
|
|
|
|
Mat dstStripe = dst.rowRange(row0, row1);
|
|
|
|
|
2017-11-28 22:42:29 +08:00
|
|
|
CALL_HAL(threshold, cv_hal_threshold, srcStripe.data, srcStripe.step, dstStripe.data, dstStripe.step,
|
|
|
|
srcStripe.cols, srcStripe.rows, srcStripe.depth(), srcStripe.channels(),
|
|
|
|
thresh, maxval, thresholdType);
|
|
|
|
|
2012-01-06 22:29:31 +08:00
|
|
|
if (srcStripe.depth() == CV_8U)
|
|
|
|
{
|
|
|
|
thresh_8u( srcStripe, dstStripe, (uchar)thresh, (uchar)maxval, thresholdType );
|
|
|
|
}
|
|
|
|
else if( srcStripe.depth() == CV_16S )
|
|
|
|
{
|
|
|
|
thresh_16s( srcStripe, dstStripe, (short)thresh, (short)maxval, thresholdType );
|
2017-09-18 20:33:14 +08:00
|
|
|
}
|
2017-09-18 20:42:44 +08:00
|
|
|
else if( srcStripe.depth() == CV_16U )
|
2017-09-18 20:33:14 +08:00
|
|
|
{
|
|
|
|
thresh_16u( srcStripe, dstStripe, (ushort)thresh, (ushort)maxval, thresholdType );
|
2012-01-06 22:29:31 +08:00
|
|
|
}
|
|
|
|
else if( srcStripe.depth() == CV_32F )
|
|
|
|
{
|
|
|
|
thresh_32f( srcStripe, dstStripe, (float)thresh, (float)maxval, thresholdType );
|
|
|
|
}
|
2016-05-17 21:56:03 +08:00
|
|
|
else if( srcStripe.depth() == CV_64F )
|
|
|
|
{
|
2016-05-18 07:32:29 +08:00
|
|
|
thresh_64f(srcStripe, dstStripe, thresh, maxval, thresholdType);
|
2016-05-17 21:56:03 +08:00
|
|
|
}
|
2012-01-06 22:29:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mat src;
|
|
|
|
Mat dst;
|
|
|
|
|
|
|
|
double thresh;
|
|
|
|
double maxval;
|
|
|
|
int thresholdType;
|
|
|
|
};
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2013-12-03 04:13:05 +08:00
|
|
|
static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, double maxval, int thresh_type )
|
|
|
|
{
|
2014-03-08 05:29:27 +08:00
|
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
|
2014-03-12 03:41:44 +08:00
|
|
|
kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
|
2013-12-03 04:13:05 +08:00
|
|
|
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
|
|
|
|
|
|
|
|
if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
|
|
|
|
thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
|
|
|
|
(!doubleSupport && depth == CV_64F))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
|
|
|
|
"THRESH_TOZERO", "THRESH_TOZERO_INV" };
|
2014-05-16 14:22:03 +08:00
|
|
|
ocl::Device dev = ocl::Device::getDefault();
|
|
|
|
int stride_size = dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU) ? 4 : 1;
|
|
|
|
|
2013-12-03 04:13:05 +08:00
|
|
|
ocl::Kernel k("threshold", ocl::imgproc::threshold_oclsrc,
|
2014-05-16 14:22:03 +08:00
|
|
|
format("-D %s -D T=%s -D T1=%s -D STRIDE_SIZE=%d%s", thresholdMap[thresh_type],
|
|
|
|
ocl::typeToStr(ktype), ocl::typeToStr(depth), stride_size,
|
2014-03-12 03:41:44 +08:00
|
|
|
doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
|
2013-12-03 04:13:05 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat src = _src.getUMat();
|
|
|
|
_dst.create(src.size(), type);
|
|
|
|
UMat dst = _dst.getUMat();
|
|
|
|
|
|
|
|
if (depth <= CV_32S)
|
|
|
|
thresh = cvFloor(thresh);
|
|
|
|
|
2014-05-13 19:21:47 +08:00
|
|
|
const double min_vals[] = { 0, CHAR_MIN, 0, SHRT_MIN, INT_MIN, -FLT_MAX, -DBL_MAX, 0 };
|
|
|
|
double min_val = min_vals[depth];
|
|
|
|
|
2014-03-08 05:29:27 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst, cn, kercn),
|
2014-03-12 03:41:44 +08:00
|
|
|
ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(thresh))),
|
2014-05-13 19:21:47 +08:00
|
|
|
ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(maxval))),
|
|
|
|
ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(min_val))));
|
2013-12-03 04:13:05 +08:00
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalsize[2] = { (size_t)dst.cols * cn / kercn, (size_t)dst.rows };
|
2014-05-16 14:22:03 +08:00
|
|
|
globalsize[1] = (globalsize[1] + stride_size - 1) / stride_size;
|
2013-12-03 04:13:05 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
|
|
|
}
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2016-11-01 21:31:37 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_OPENVX
|
|
|
|
#define IMPL_OPENVX_TOZERO 1
|
|
|
|
static bool openvx_threshold(Mat src, Mat dst, int thresh, int maxval, int type)
|
|
|
|
{
|
|
|
|
Mat a = src;
|
|
|
|
|
|
|
|
int trueVal, falseVal;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case THRESH_BINARY:
|
|
|
|
#ifndef VX_VERSION_1_1
|
|
|
|
if (maxval != 255)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
trueVal = maxval;
|
|
|
|
falseVal = 0;
|
|
|
|
break;
|
|
|
|
case THRESH_TOZERO:
|
|
|
|
#if IMPL_OPENVX_TOZERO
|
|
|
|
trueVal = 255;
|
|
|
|
falseVal = 0;
|
|
|
|
if (dst.data == src.data)
|
|
|
|
{
|
|
|
|
a = Mat(src.size(), src.type());
|
|
|
|
src.copyTo(a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case THRESH_BINARY_INV:
|
|
|
|
#ifdef VX_VERSION_1_1
|
|
|
|
trueVal = 0;
|
|
|
|
falseVal = maxval;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case THRESH_TOZERO_INV:
|
|
|
|
#ifdef VX_VERSION_1_1
|
|
|
|
#if IMPL_OPENVX_TOZERO
|
|
|
|
trueVal = 0;
|
|
|
|
falseVal = 255;
|
|
|
|
if (dst.data == src.data)
|
|
|
|
{
|
|
|
|
a = Mat(src.size(), src.type());
|
|
|
|
src.copyTo(a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
case THRESH_TRUNC:
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-14 18:32:17 +08:00
|
|
|
ivx::Context ctx = ovx::getOpenVXContext();
|
2016-11-01 21:31:37 +08:00
|
|
|
|
|
|
|
ivx::Threshold thh = ivx::Threshold::createBinary(ctx, VX_TYPE_UINT8, thresh);
|
|
|
|
thh.setValueTrue(trueVal);
|
|
|
|
thh.setValueFalse(falseVal);
|
|
|
|
|
|
|
|
ivx::Image
|
|
|
|
ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
|
|
|
ivx::Image::createAddressing(a.cols*a.channels(), a.rows, 1, (vx_int32)(a.step)), src.data),
|
|
|
|
ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
|
|
|
ivx::Image::createAddressing(dst.cols*dst.channels(), dst.rows, 1, (vx_int32)(dst.step)), dst.data);
|
|
|
|
|
|
|
|
ivx::IVX_CHECK_STATUS(vxuThreshold(ctx, ia, thh, ib));
|
|
|
|
#if IMPL_OPENVX_TOZERO
|
|
|
|
if (type == THRESH_TOZERO || type == THRESH_TOZERO_INV)
|
|
|
|
{
|
|
|
|
ivx::Image
|
|
|
|
ic = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
|
|
|
ivx::Image::createAddressing(dst.cols*dst.channels(), dst.rows, 1, (vx_int32)(dst.step)), dst.data);
|
|
|
|
ivx::IVX_CHECK_STATUS(vxuAnd(ctx, ib, ia, ic));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const ivx::RuntimeError & e)
|
2016-11-01 21:31:37 +08:00
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-11-01 21:31:37 +08:00
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const ivx::WrapperError & e)
|
2016-11-01 21:31:37 +08:00
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-11-01 21:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double maxval, int type )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
CV_OCL_RUN_(_src.dims() <= 2 && _dst.isUMat(),
|
|
|
|
ocl_threshold(_src, _dst, thresh, maxval, type), thresh)
|
2013-12-03 04:13:05 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat src = _src.getMat();
|
2014-11-05 23:04:26 +08:00
|
|
|
int automatic_thresh = (type & ~CV_THRESH_MASK);
|
2010-05-12 01:44:00 +08:00
|
|
|
type &= THRESH_MASK;
|
|
|
|
|
2014-11-05 23:04:26 +08:00
|
|
|
CV_Assert( automatic_thresh != (CV_THRESH_OTSU | CV_THRESH_TRIANGLE) );
|
|
|
|
if( automatic_thresh == CV_THRESH_OTSU )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
CV_Assert( src.type() == CV_8UC1 );
|
2014-11-05 23:04:26 +08:00
|
|
|
thresh = getThreshVal_Otsu_8u( src );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-11-05 23:04:26 +08:00
|
|
|
else if( automatic_thresh == CV_THRESH_TRIANGLE )
|
2014-10-22 22:54:39 +08:00
|
|
|
{
|
|
|
|
CV_Assert( src.type() == CV_8UC1 );
|
2014-11-05 23:04:26 +08:00
|
|
|
thresh = getThreshVal_Triangle_8u( src );
|
2014-10-22 22:54:39 +08:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
_dst.create( src.size(), src.type() );
|
|
|
|
Mat dst = _dst.getMat();
|
2012-01-06 22:29:31 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( src.depth() == CV_8U )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int ithresh = cvFloor(thresh);
|
|
|
|
thresh = ithresh;
|
|
|
|
int imaxval = cvRound(maxval);
|
|
|
|
if( type == THRESH_TRUNC )
|
|
|
|
imaxval = ithresh;
|
|
|
|
imaxval = saturate_cast<uchar>(imaxval);
|
|
|
|
|
|
|
|
if( ithresh < 0 || ithresh >= 255 )
|
|
|
|
{
|
|
|
|
if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
|
|
|
|
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < 0) ||
|
|
|
|
(type == THRESH_TOZERO && ithresh >= 255) )
|
|
|
|
{
|
|
|
|
int v = type == THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) :
|
|
|
|
type == THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) :
|
2011-11-22 23:16:45 +08:00
|
|
|
/*type == THRESH_TRUNC ? imaxval :*/ 0;
|
2011-06-14 04:56:27 +08:00
|
|
|
dst.setTo(v);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
2011-04-17 21:14:45 +08:00
|
|
|
src.copyTo(dst);
|
2012-10-10 02:38:04 +08:00
|
|
|
return thresh;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2016-11-01 21:31:37 +08:00
|
|
|
|
2017-03-28 23:02:42 +08:00
|
|
|
CV_OVX_RUN(!ovx::skipSmallImages<VX_KERNEL_THRESHOLD>(src.cols, src.rows),
|
2016-12-14 22:31:41 +08:00
|
|
|
openvx_threshold(src, dst, ithresh, imaxval, type), (double)ithresh)
|
2016-11-01 21:31:37 +08:00
|
|
|
|
2012-10-10 02:38:04 +08:00
|
|
|
thresh = ithresh;
|
|
|
|
maxval = imaxval;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-11-22 23:16:45 +08:00
|
|
|
else if( src.depth() == CV_16S )
|
|
|
|
{
|
|
|
|
int ithresh = cvFloor(thresh);
|
|
|
|
thresh = ithresh;
|
|
|
|
int imaxval = cvRound(maxval);
|
|
|
|
if( type == THRESH_TRUNC )
|
|
|
|
imaxval = ithresh;
|
|
|
|
imaxval = saturate_cast<short>(imaxval);
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2011-11-22 23:16:45 +08:00
|
|
|
if( ithresh < SHRT_MIN || ithresh >= SHRT_MAX )
|
|
|
|
{
|
|
|
|
if( type == THRESH_BINARY || type == THRESH_BINARY_INV ||
|
|
|
|
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < SHRT_MIN) ||
|
|
|
|
(type == THRESH_TOZERO && ithresh >= SHRT_MAX) )
|
|
|
|
{
|
|
|
|
int v = type == THRESH_BINARY ? (ithresh >= SHRT_MAX ? 0 : imaxval) :
|
|
|
|
type == THRESH_BINARY_INV ? (ithresh >= SHRT_MAX ? imaxval : 0) :
|
|
|
|
/*type == THRESH_TRUNC ? imaxval :*/ 0;
|
|
|
|
dst.setTo(v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
src.copyTo(dst);
|
2012-10-10 02:38:04 +08:00
|
|
|
return thresh;
|
2011-11-22 23:16:45 +08:00
|
|
|
}
|
2012-10-10 02:38:04 +08:00
|
|
|
thresh = ithresh;
|
|
|
|
maxval = imaxval;
|
2011-11-22 23:16:45 +08:00
|
|
|
}
|
2017-09-18 20:42:44 +08:00
|
|
|
else if (src.depth() == CV_16U )
|
|
|
|
{
|
|
|
|
int ithresh = cvFloor(thresh);
|
|
|
|
thresh = ithresh;
|
|
|
|
int imaxval = cvRound(maxval);
|
|
|
|
if (type == THRESH_TRUNC)
|
|
|
|
imaxval = ithresh;
|
|
|
|
imaxval = saturate_cast<ushort>(imaxval);
|
|
|
|
|
|
|
|
int ushrt_min = 0;
|
2018-01-05 12:30:08 +08:00
|
|
|
if (ithresh < ushrt_min || ithresh >= (int)USHRT_MAX)
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
|
|
|
if (type == THRESH_BINARY || type == THRESH_BINARY_INV ||
|
|
|
|
((type == THRESH_TRUNC || type == THRESH_TOZERO_INV) && ithresh < ushrt_min) ||
|
2018-01-05 12:30:08 +08:00
|
|
|
(type == THRESH_TOZERO && ithresh >= (int)USHRT_MAX))
|
2017-09-18 20:42:44 +08:00
|
|
|
{
|
2018-01-05 12:30:08 +08:00
|
|
|
int v = type == THRESH_BINARY ? (ithresh >= (int)USHRT_MAX ? 0 : imaxval) :
|
|
|
|
type == THRESH_BINARY_INV ? (ithresh >= (int)USHRT_MAX ? imaxval : 0) :
|
2017-09-18 20:42:44 +08:00
|
|
|
/*type == THRESH_TRUNC ? imaxval :*/ 0;
|
|
|
|
dst.setTo(v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
src.copyTo(dst);
|
|
|
|
return thresh;
|
|
|
|
}
|
|
|
|
thresh = ithresh;
|
|
|
|
maxval = imaxval;
|
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
else if( src.depth() == CV_32F )
|
2012-10-10 02:38:04 +08:00
|
|
|
;
|
2016-05-17 21:56:03 +08:00
|
|
|
else if( src.depth() == CV_64F )
|
|
|
|
;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
CV_Error( CV_StsUnsupportedFormat, "" );
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
parallel_for_(Range(0, dst.rows),
|
|
|
|
ThresholdRunner(src, dst, thresh, maxval, type),
|
|
|
|
dst.total()/(double)(1<<16));
|
2010-05-12 01:44:00 +08:00
|
|
|
return thresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void cv::adaptiveThreshold( InputArray _src, OutputArray _dst, double maxValue,
|
2011-04-17 21:14:45 +08:00
|
|
|
int method, int type, int blockSize, double delta )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert( src.type() == CV_8UC1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_Assert( blockSize % 2 == 1 && blockSize > 1 );
|
2011-04-17 21:14:45 +08:00
|
|
|
Size size = src.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
_dst.create( size, src.type() );
|
|
|
|
Mat dst = _dst.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( maxValue < 0 )
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
dst = Scalar(0);
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-06-14 22:09:04 +08:00
|
|
|
|
2017-11-23 16:40:54 +08:00
|
|
|
CALL_HAL(adaptiveThreshold, cv_hal_adaptiveThreshold, src.data, src.step, dst.data, dst.step, src.cols, src.rows,
|
|
|
|
maxValue, method, type, blockSize, delta);
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat mean;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( src.data != dst.data )
|
|
|
|
mean = dst;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-06-23 04:21:30 +08:00
|
|
|
if (method == ADAPTIVE_THRESH_MEAN_C)
|
2011-04-17 21:14:45 +08:00
|
|
|
boxFilter( src, mean, src.type(), Size(blockSize, blockSize),
|
2017-11-23 16:40:54 +08:00
|
|
|
Point(-1,-1), true, BORDER_REPLICATE|BORDER_ISOLATED );
|
2015-06-23 04:21:30 +08:00
|
|
|
else if (method == ADAPTIVE_THRESH_GAUSSIAN_C)
|
|
|
|
{
|
|
|
|
Mat srcfloat,meanfloat;
|
|
|
|
src.convertTo(srcfloat,CV_32F);
|
|
|
|
meanfloat=srcfloat;
|
2017-11-28 20:55:58 +08:00
|
|
|
GaussianBlur(srcfloat, meanfloat, Size(blockSize, blockSize), 0, 0, BORDER_REPLICATE|BORDER_ISOLATED);
|
2015-08-06 10:23:31 +08:00
|
|
|
meanfloat.convertTo(mean, src.type());
|
2015-06-23 04:21:30 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
CV_Error( CV_StsBadFlag, "Unknown/unsupported adaptive threshold method" );
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
uchar imaxval = saturate_cast<uchar>(maxValue);
|
|
|
|
int idelta = type == THRESH_BINARY ? cvCeil(delta) : cvFloor(delta);
|
2012-06-14 22:09:04 +08:00
|
|
|
uchar tab[768];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( type == CV_THRESH_BINARY )
|
|
|
|
for( i = 0; i < 768; i++ )
|
|
|
|
tab[i] = (uchar)(i - 255 > -idelta ? imaxval : 0);
|
|
|
|
else if( type == CV_THRESH_BINARY_INV )
|
|
|
|
for( i = 0; i < 768; i++ )
|
|
|
|
tab[i] = (uchar)(i - 255 <= -idelta ? imaxval : 0);
|
|
|
|
else
|
|
|
|
CV_Error( CV_StsBadFlag, "Unknown/unsupported threshold type" );
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( src.isContinuous() && mean.isContinuous() && dst.isContinuous() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
size.width *= size.height;
|
|
|
|
size.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < size.height; i++ )
|
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
const uchar* sdata = src.ptr(i);
|
|
|
|
const uchar* mdata = mean.ptr(i);
|
|
|
|
uchar* ddata = dst.ptr(i);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( j = 0; j < size.width; j++ )
|
2011-04-17 21:14:45 +08:00
|
|
|
ddata[j] = tab[sdata[j] - mdata[j] + 255];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL double
|
|
|
|
cvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, int type )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), dst0 = dst;
|
|
|
|
|
2010-10-20 20:34:59 +08:00
|
|
|
CV_Assert( src.size == dst.size && src.channels() == dst.channels() &&
|
2010-05-12 01:44:00 +08:00
|
|
|
(src.depth() == dst.depth() || dst.depth() == CV_8U));
|
|
|
|
|
|
|
|
thresh = cv::threshold( src, dst, thresh, maxval, type );
|
|
|
|
if( dst0.data != dst.data )
|
|
|
|
dst.convertTo( dst0, dst0.depth() );
|
|
|
|
return thresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvAdaptiveThreshold( const void *srcIm, void *dstIm, double maxValue,
|
|
|
|
int method, int type, int blockSize, double delta )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcIm), dst = cv::cvarrToMat(dstIm);
|
2010-10-20 20:34:59 +08:00
|
|
|
CV_Assert( src.size == dst.size && src.type() == dst.type() );
|
2010-05-12 01:44:00 +08:00
|
|
|
cv::adaptiveThreshold( src, dst, maxValue, method, type, blockSize, delta );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|