From 0cd37886241ba8a5c7f3b7e46f28be195b147551 Mon Sep 17 00:00:00 2001 From: Vitaly Tuzov Date: Tue, 1 Nov 2016 16:31:37 +0300 Subject: [PATCH] Added OpenVX based processing to threshold --- 3rdparty/openvx/include/ivx.hpp | 20 ++++++ modules/imgproc/src/thresh.cpp | 105 ++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/3rdparty/openvx/include/ivx.hpp b/3rdparty/openvx/include/ivx.hpp index f47831d1c8..0ac2108d47 100644 --- a/3rdparty/openvx/include/ivx.hpp +++ b/3rdparty/openvx/include/ivx.hpp @@ -1823,6 +1823,26 @@ static const vx_enum query(VX_THRESHOLD_FALSE_VALUE, v); return v; } + + /// vxSetThresholdAttribute(THRESHOLD_VALUE) wrapper + void setValue(vx_int32 &val) + { IVX_CHECK_STATUS(vxSetThresholdAttribute(ref, VX_THRESHOLD_THRESHOLD_VALUE, &val, sizeof(val))); } + + /// vxSetThresholdAttribute(THRESHOLD_LOWER) wrapper + void setValueLower(vx_int32 &val) + { IVX_CHECK_STATUS(vxSetThresholdAttribute(ref, VX_THRESHOLD_THRESHOLD_LOWER, &val, sizeof(val))); } + + /// vxSetThresholdAttribute(THRESHOLD_UPPER) wrapper + void setValueUpper(vx_int32 &val) + { IVX_CHECK_STATUS(vxSetThresholdAttribute(ref, VX_THRESHOLD_THRESHOLD_UPPER, &val, sizeof(val))); } + + /// vxSetThresholdAttribute(TRUE_VALUE) wrapper + void setValueTrue(vx_int32 &val) + { IVX_CHECK_STATUS(vxSetThresholdAttribute(ref, VX_THRESHOLD_TRUE_VALUE, &val, sizeof(val))); } + + /// vxSetThresholdAttribute(FALSE_VALUE) wrapper + void setValueFalse(vx_int32 &val) + { IVX_CHECK_STATUS(vxSetThresholdAttribute(ref, VX_THRESHOLD_FALSE_VALUE, &val, sizeof(val))); } }; /// vx_array wrapper diff --git a/modules/imgproc/src/thresh.cpp b/modules/imgproc/src/thresh.cpp index a5273ecd7c..e7709ba3e0 100644 --- a/modules/imgproc/src/thresh.cpp +++ b/modules/imgproc/src/thresh.cpp @@ -44,6 +44,12 @@ #include "opencl_kernels_imgproc.hpp" #include "opencv2/core/hal/intrin.hpp" +#ifdef HAVE_OPENVX +#define IVX_HIDE_INFO_WARNINGS +#define IVX_USE_OPENCV +#include "ivx.hpp" +#endif + namespace cv { @@ -1244,6 +1250,99 @@ static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, d #endif + +#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 + { + ivx::Context ctx = ivx::Context::create(); + + 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 + } + catch (ivx::RuntimeError & e) + { + CV_Error(CV_StsInternal, e.what()); + return false; + } + catch (ivx::WrapperError & e) + { + CV_Error(CV_StsInternal, e.what()); + return false; + } + + return true; +} +#endif + } double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double maxval, int type ) @@ -1296,6 +1395,12 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m src.copyTo(dst); return thresh; } + +#ifdef HAVE_OPENVX + if (openvx_threshold(src, dst, ithresh, imaxval, type)) + return thresh; +#endif + thresh = ithresh; maxval = imaxval; }