a
This commit is contained in:
+4094
File diff suppressed because it is too large
Load Diff
+48
@@ -0,0 +1,48 @@
|
||||
/*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.
|
||||
// Copyright (C) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifdef __OPENCV_BUILD
|
||||
#error this is a compatibility header which should not be used inside the OpenCV library
|
||||
#endif
|
||||
|
||||
#include "opencv2/calib3d.hpp"
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
/*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.
|
||||
// Copyright (C) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifndef OPENCV_CALIB3D_C_H
|
||||
#define OPENCV_CALIB3D_C_H
|
||||
|
||||
#include "opencv2/core/types_c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Calculates fundamental matrix given a set of corresponding points */
|
||||
#define CV_FM_7POINT 1
|
||||
#define CV_FM_8POINT 2
|
||||
|
||||
#define CV_LMEDS 4
|
||||
#define CV_RANSAC 8
|
||||
|
||||
#define CV_FM_LMEDS_ONLY CV_LMEDS
|
||||
#define CV_FM_RANSAC_ONLY CV_RANSAC
|
||||
#define CV_FM_LMEDS CV_LMEDS
|
||||
#define CV_FM_RANSAC CV_RANSAC
|
||||
|
||||
enum
|
||||
{
|
||||
CV_ITERATIVE = 0,
|
||||
CV_EPNP = 1, // F.Moreno-Noguer, V.Lepetit and P.Fua "EPnP: Efficient Perspective-n-Point Camera Pose Estimation"
|
||||
CV_P3P = 2, // X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang; "Complete Solution Classification for the Perspective-Three-Point Problem"
|
||||
CV_DLS = 3 // Joel A. Hesch and Stergios I. Roumeliotis. "A Direct Least-Squares (DLS) Method for PnP"
|
||||
};
|
||||
|
||||
#define CV_CALIB_CB_ADAPTIVE_THRESH 1
|
||||
#define CV_CALIB_CB_NORMALIZE_IMAGE 2
|
||||
#define CV_CALIB_CB_FILTER_QUADS 4
|
||||
#define CV_CALIB_CB_FAST_CHECK 8
|
||||
|
||||
#define CV_CALIB_USE_INTRINSIC_GUESS 1
|
||||
#define CV_CALIB_FIX_ASPECT_RATIO 2
|
||||
#define CV_CALIB_FIX_PRINCIPAL_POINT 4
|
||||
#define CV_CALIB_ZERO_TANGENT_DIST 8
|
||||
#define CV_CALIB_FIX_FOCAL_LENGTH 16
|
||||
#define CV_CALIB_FIX_K1 32
|
||||
#define CV_CALIB_FIX_K2 64
|
||||
#define CV_CALIB_FIX_K3 128
|
||||
#define CV_CALIB_FIX_K4 2048
|
||||
#define CV_CALIB_FIX_K5 4096
|
||||
#define CV_CALIB_FIX_K6 8192
|
||||
#define CV_CALIB_RATIONAL_MODEL 16384
|
||||
#define CV_CALIB_THIN_PRISM_MODEL 32768
|
||||
#define CV_CALIB_FIX_S1_S2_S3_S4 65536
|
||||
#define CV_CALIB_TILTED_MODEL 262144
|
||||
#define CV_CALIB_FIX_TAUX_TAUY 524288
|
||||
#define CV_CALIB_FIX_TANGENT_DIST 2097152
|
||||
|
||||
#define CV_CALIB_NINTRINSIC 18
|
||||
|
||||
#define CV_CALIB_FIX_INTRINSIC 256
|
||||
#define CV_CALIB_SAME_FOCAL_LENGTH 512
|
||||
|
||||
#define CV_CALIB_ZERO_DISPARITY 1024
|
||||
|
||||
/* stereo correspondence parameters and functions */
|
||||
#define CV_STEREO_BM_NORMALIZED_RESPONSE 0
|
||||
#define CV_STEREO_BM_XSOBEL 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
class CV_EXPORTS CvLevMarq
|
||||
{
|
||||
public:
|
||||
CvLevMarq();
|
||||
CvLevMarq( int nparams, int nerrs, CvTermCriteria criteria=
|
||||
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON),
|
||||
bool completeSymmFlag=false );
|
||||
~CvLevMarq();
|
||||
void init( int nparams, int nerrs, CvTermCriteria criteria=
|
||||
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON),
|
||||
bool completeSymmFlag=false );
|
||||
bool update( const CvMat*& param, CvMat*& J, CvMat*& err );
|
||||
bool updateAlt( const CvMat*& param, CvMat*& JtJ, CvMat*& JtErr, double*& errNorm );
|
||||
|
||||
void clear();
|
||||
void step();
|
||||
enum { DONE=0, STARTED=1, CALC_J=2, CHECK_ERR=3 };
|
||||
|
||||
cv::Ptr<CvMat> mask;
|
||||
cv::Ptr<CvMat> prevParam;
|
||||
cv::Ptr<CvMat> param;
|
||||
cv::Ptr<CvMat> J;
|
||||
cv::Ptr<CvMat> err;
|
||||
cv::Ptr<CvMat> JtJ;
|
||||
cv::Ptr<CvMat> JtJN;
|
||||
cv::Ptr<CvMat> JtErr;
|
||||
cv::Ptr<CvMat> JtJV;
|
||||
cv::Ptr<CvMat> JtJW;
|
||||
double prevErrNorm, errNorm;
|
||||
int lambdaLg10;
|
||||
CvTermCriteria criteria;
|
||||
int state;
|
||||
int iters;
|
||||
bool completeSymmFlag;
|
||||
int solveMethod;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* OPENCV_CALIB3D_C_H */
|
||||
+3374
File diff suppressed because it is too large
Load Diff
+155
@@ -0,0 +1,155 @@
|
||||
#ifndef OPENCV_CVCONFIG_H_INCLUDED
|
||||
#define OPENCV_CVCONFIG_H_INCLUDED
|
||||
|
||||
/* OpenCV compiled as static or dynamic libs */
|
||||
#define BUILD_SHARED_LIBS
|
||||
|
||||
/* OpenCV intrinsics optimized code */
|
||||
#define CV_ENABLE_INTRINSICS
|
||||
|
||||
/* OpenCV additional optimized code */
|
||||
/* #undef CV_DISABLE_OPTIMIZATION */
|
||||
|
||||
/* Compile for 'real' NVIDIA GPU architectures */
|
||||
#define CUDA_ARCH_BIN ""
|
||||
|
||||
/* NVIDIA GPU features are used */
|
||||
#define CUDA_ARCH_FEATURES ""
|
||||
|
||||
/* Compile for 'virtual' NVIDIA PTX architectures */
|
||||
#define CUDA_ARCH_PTX ""
|
||||
|
||||
/* AMD's Basic Linear Algebra Subprograms Library*/
|
||||
/* #undef HAVE_CLAMDBLAS */
|
||||
|
||||
/* AMD's OpenCL Fast Fourier Transform Library*/
|
||||
/* #undef HAVE_CLAMDFFT */
|
||||
|
||||
/* Clp support */
|
||||
/* #undef HAVE_CLP */
|
||||
|
||||
/* NVIDIA CUDA Runtime API*/
|
||||
/* #undef HAVE_CUDA */
|
||||
|
||||
/* NVIDIA CUDA Basic Linear Algebra Subprograms (BLAS) API*/
|
||||
/* #undef HAVE_CUBLAS */
|
||||
|
||||
/* NVIDIA CUDA Deep Neural Network (cuDNN) API*/
|
||||
/* #undef HAVE_CUDNN */
|
||||
|
||||
/* NVIDIA CUDA Fast Fourier Transform (FFT) API*/
|
||||
/* #undef HAVE_CUFFT */
|
||||
|
||||
/* DirectX */
|
||||
/* #undef HAVE_DIRECTX */
|
||||
/* #undef HAVE_DIRECTX_NV12 */
|
||||
/* #undef HAVE_D3D11 */
|
||||
/* #undef HAVE_D3D10 */
|
||||
/* #undef HAVE_D3D9 */
|
||||
|
||||
/* Eigen Matrix & Linear Algebra Library */
|
||||
/* #undef HAVE_EIGEN */
|
||||
|
||||
/* Geospatial Data Abstraction Library */
|
||||
/* #undef HAVE_GDAL */
|
||||
|
||||
/* Halide support */
|
||||
/* #undef HAVE_HALIDE */
|
||||
|
||||
/* Vulkan support */
|
||||
/* #undef HAVE_VULKAN */
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#define HAVE_INTTYPES_H 1
|
||||
|
||||
/* Intel Integrated Performance Primitives */
|
||||
/* #undef HAVE_IPP */
|
||||
/* #undef HAVE_IPP_ICV */
|
||||
/* #undef HAVE_IPP_IW */
|
||||
/* #undef HAVE_IPP_IW_LL */
|
||||
|
||||
/* JPEG-2000 codec */
|
||||
#define HAVE_OPENJPEG
|
||||
/* #undef HAVE_JASPER */
|
||||
|
||||
/* AVIF codec */
|
||||
/* #undef HAVE_AVIF */
|
||||
|
||||
/* IJG JPEG codec */
|
||||
#define HAVE_JPEG
|
||||
|
||||
/* libpng/png.h needs to be included */
|
||||
/* #undef HAVE_LIBPNG_PNG_H */
|
||||
|
||||
/* GDCM DICOM codec */
|
||||
/* #undef HAVE_GDCM */
|
||||
|
||||
/* NVIDIA Video Decoding API*/
|
||||
/* #undef HAVE_NVCUVID */
|
||||
/* #undef HAVE_NVCUVID_HEADER */
|
||||
/* #undef HAVE_DYNLINK_NVCUVID_HEADER */
|
||||
|
||||
/* NVIDIA Video Encoding API*/
|
||||
/* #undef HAVE_NVCUVENC */
|
||||
|
||||
/* OpenCL Support */
|
||||
#define HAVE_OPENCL
|
||||
/* #undef HAVE_OPENCL_STATIC */
|
||||
/* #undef HAVE_OPENCL_SVM */
|
||||
|
||||
/* NVIDIA OpenCL D3D Extensions support */
|
||||
/* #undef HAVE_OPENCL_D3D11_NV */
|
||||
|
||||
/* OpenEXR codec */
|
||||
/* #undef HAVE_OPENEXR */
|
||||
|
||||
/* OpenGL support*/
|
||||
/* #undef HAVE_OPENGL */
|
||||
|
||||
/* PNG codec */
|
||||
#define HAVE_PNG
|
||||
|
||||
/* PNG codec */
|
||||
/* #undef HAVE_SPNG */
|
||||
|
||||
/* Posix threads (pthreads) */
|
||||
#define HAVE_PTHREAD
|
||||
|
||||
/* parallel_for with pthreads */
|
||||
#define HAVE_PTHREADS_PF
|
||||
|
||||
/* Intel Threading Building Blocks */
|
||||
/* #undef HAVE_TBB */
|
||||
|
||||
/* Ste||ar Group High Performance ParallelX */
|
||||
/* #undef HAVE_HPX */
|
||||
|
||||
/* TIFF codec */
|
||||
#define HAVE_TIFF
|
||||
|
||||
/* Define if your processor stores words with the most significant byte
|
||||
first (like Motorola and SPARC, unlike Intel and VAX). */
|
||||
/* #undef WORDS_BIGENDIAN */
|
||||
|
||||
/* VA library (libva) */
|
||||
/* #undef HAVE_VA */
|
||||
|
||||
/* Intel VA-API/OpenCL */
|
||||
/* #undef HAVE_VA_INTEL */
|
||||
|
||||
/* Lapack */
|
||||
/* #undef HAVE_LAPACK */
|
||||
|
||||
/* Library was compiled with functions instrumentation */
|
||||
/* #undef ENABLE_INSTRUMENTATION */
|
||||
|
||||
/* OpenVX */
|
||||
/* #undef HAVE_OPENVX */
|
||||
|
||||
/* OpenCV trace utilities */
|
||||
#define OPENCV_TRACE
|
||||
|
||||
/* Library QR-code decoding */
|
||||
#define HAVE_QUIRC
|
||||
|
||||
#endif // OPENCV_CVCONFIG_H_INCLUDED
|
||||
@@ -0,0 +1,78 @@
|
||||
/*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) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifndef OPENCV_DNN_HPP
|
||||
#define OPENCV_DNN_HPP
|
||||
|
||||
// This is an umbrella header to include into you project.
|
||||
// We are free to change headers layout in dnn subfolder, so please include
|
||||
// this header for future compatibility
|
||||
|
||||
|
||||
/** @defgroup dnn Deep Neural Network module
|
||||
@{
|
||||
This module contains:
|
||||
- API for new layers creation, layers are building bricks of neural networks;
|
||||
- set of built-in most-useful Layers;
|
||||
- API to construct and modify comprehensive neural networks from layers;
|
||||
- functionality for loading serialized networks models from different frameworks.
|
||||
|
||||
Functionality of this module is designed only for forward pass computations (i.e. network testing).
|
||||
A network training is in principle not supported.
|
||||
@}
|
||||
*/
|
||||
/** @example samples/dnn/classification.cpp
|
||||
Check @ref tutorial_dnn_googlenet "the corresponding tutorial" for more details
|
||||
*/
|
||||
/** @example samples/dnn/colorization.cpp
|
||||
*/
|
||||
/** @example samples/dnn/object_detection.cpp
|
||||
Check @ref tutorial_dnn_yolo "the corresponding tutorial" for more details
|
||||
*/
|
||||
/** @example samples/dnn/openpose.cpp
|
||||
*/
|
||||
/** @example samples/dnn/segmentation.cpp
|
||||
*/
|
||||
/** @example samples/dnn/text_detection.cpp
|
||||
*/
|
||||
#include <opencv2/dnn/dnn.hpp>
|
||||
|
||||
#endif /* OPENCV_DNN_HPP */
|
||||
+1109
File diff suppressed because it is too large
Load Diff
+160
@@ -0,0 +1,160 @@
|
||||
/*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) 2013, OpenCV Foundation, 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 <opencv2/core.hpp>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
|
||||
#include <opencv2/dnn/dnn.hpp>
|
||||
|
||||
#ifndef OPENCV_DNN_DNN_DICT_HPP
|
||||
#define OPENCV_DNN_DNN_DICT_HPP
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
//! @addtogroup dnn
|
||||
//! @{
|
||||
|
||||
/** @brief This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64.
|
||||
* @todo Maybe int64 is useless because double type exactly stores at least 2^52 integers.
|
||||
*/
|
||||
struct CV_EXPORTS_W DictValue
|
||||
{
|
||||
DictValue(const DictValue &r);
|
||||
explicit DictValue(bool i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i ? 1 : 0; } //!< Constructs integer scalar
|
||||
explicit DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
|
||||
CV_WRAP explicit DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
|
||||
explicit DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar
|
||||
CV_WRAP explicit DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
|
||||
CV_WRAP explicit DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
|
||||
explicit DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload
|
||||
|
||||
template<typename TypeIter>
|
||||
static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array
|
||||
template<typename TypeIter>
|
||||
static DictValue arrayReal(TypeIter begin, int size); //!< Constructs floating point array
|
||||
template<typename TypeIter>
|
||||
static DictValue arrayString(TypeIter begin, int size); //!< Constructs array of strings
|
||||
|
||||
template<typename T>
|
||||
T get(int idx = -1) const; //!< Tries to convert array element with specified index to requested type and returns its.
|
||||
|
||||
int size() const;
|
||||
|
||||
CV_WRAP bool isInt() const;
|
||||
CV_WRAP bool isString() const;
|
||||
CV_WRAP bool isReal() const;
|
||||
|
||||
CV_WRAP int getIntValue(int idx = -1) const;
|
||||
CV_WRAP double getRealValue(int idx = -1) const;
|
||||
CV_WRAP String getStringValue(int idx = -1) const;
|
||||
|
||||
DictValue &operator=(const DictValue &r);
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &stream, const DictValue &dictv);
|
||||
|
||||
~DictValue();
|
||||
|
||||
private:
|
||||
|
||||
Param type;
|
||||
|
||||
union
|
||||
{
|
||||
AutoBuffer<int64, 1> *pi;
|
||||
AutoBuffer<double, 1> *pd;
|
||||
AutoBuffer<String, 1> *ps;
|
||||
void *pv;
|
||||
};
|
||||
|
||||
DictValue(Param _type, void *_p) : type(_type), pv(_p) {}
|
||||
void release();
|
||||
};
|
||||
|
||||
/** @brief This class implements name-value dictionary, values are instances of DictValue. */
|
||||
class CV_EXPORTS Dict
|
||||
{
|
||||
typedef std::map<String, DictValue> _Dict;
|
||||
_Dict dict;
|
||||
|
||||
public:
|
||||
|
||||
//! Checks a presence of the @p key in the dictionary.
|
||||
bool has(const String &key) const;
|
||||
|
||||
//! If the @p key in the dictionary then returns pointer to its value, else returns NULL.
|
||||
DictValue *ptr(const String &key);
|
||||
|
||||
/** @overload */
|
||||
const DictValue *ptr(const String &key) const;
|
||||
|
||||
//! If the @p key in the dictionary then returns its value, else an error will be generated.
|
||||
const DictValue &get(const String &key) const;
|
||||
|
||||
/** @overload */
|
||||
template <typename T>
|
||||
T get(const String &key) const;
|
||||
|
||||
//! If the @p key in the dictionary then returns its value, else returns @p defaultValue.
|
||||
template <typename T>
|
||||
T get(const String &key, const T &defaultValue) const;
|
||||
|
||||
//! Sets new @p value for the @p key, or adds new key-value pair into the dictionary.
|
||||
template<typename T>
|
||||
const T &set(const String &key, const T &value);
|
||||
|
||||
//! Erase @p key from the dictionary.
|
||||
void erase(const String &key);
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &stream, const Dict &dict);
|
||||
|
||||
std::map<String, DictValue>::const_iterator begin() const;
|
||||
|
||||
std::map<String, DictValue>::const_iterator end() const;
|
||||
};
|
||||
|
||||
//! @}
|
||||
CV__DNN_INLINE_NS_END
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
+1920
File diff suppressed because it is too large
Load Diff
+412
@@ -0,0 +1,412 @@
|
||||
/*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) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifndef OPENCV_DNN_DNN_INL_HPP
|
||||
#define OPENCV_DNN_DNN_INL_HPP
|
||||
|
||||
#include <opencv2/dnn.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
template<typename TypeIter>
|
||||
DictValue DictValue::arrayInt(TypeIter begin, int size)
|
||||
{
|
||||
DictValue res(Param::INT, new AutoBuffer<int64, 1>(size));
|
||||
for (int j = 0; j < size; begin++, j++)
|
||||
(*res.pi)[j] = *begin;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename TypeIter>
|
||||
DictValue DictValue::arrayReal(TypeIter begin, int size)
|
||||
{
|
||||
DictValue res(Param::REAL, new AutoBuffer<double, 1>(size));
|
||||
for (int j = 0; j < size; begin++, j++)
|
||||
(*res.pd)[j] = *begin;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename TypeIter>
|
||||
DictValue DictValue::arrayString(TypeIter begin, int size)
|
||||
{
|
||||
DictValue res(Param::STRING, new AutoBuffer<String, 1>(size));
|
||||
for (int j = 0; j < size; begin++, j++)
|
||||
(*res.ps)[j] = *begin;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline DictValue DictValue::get<DictValue>(int idx) const
|
||||
{
|
||||
CV_Assert(idx == -1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int64 DictValue::get<int64>(int idx) const
|
||||
{
|
||||
CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
|
||||
idx = (idx == -1) ? 0 : idx;
|
||||
|
||||
if (type == Param::INT)
|
||||
{
|
||||
return (*pi)[idx];
|
||||
}
|
||||
else if (type == Param::REAL)
|
||||
{
|
||||
double doubleValue = (*pd)[idx];
|
||||
|
||||
double fracpart, intpart;
|
||||
fracpart = std::modf(doubleValue, &intpart);
|
||||
CV_Assert(fracpart == 0.0);
|
||||
|
||||
return (int64)doubleValue;
|
||||
}
|
||||
else if (type == Param::STRING)
|
||||
{
|
||||
return std::atoi((*ps)[idx].c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
CV_Assert(isInt() || isReal() || isString());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DictValue::get<int>(int idx) const
|
||||
{
|
||||
return (int)get<int64>(idx);
|
||||
}
|
||||
|
||||
inline int DictValue::getIntValue(int idx) const
|
||||
{
|
||||
return (int)get<int64>(idx);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline unsigned DictValue::get<unsigned>(int idx) const
|
||||
{
|
||||
return (unsigned)get<int64>(idx);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool DictValue::get<bool>(int idx) const
|
||||
{
|
||||
return (get<int64>(idx) != 0);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline double DictValue::get<double>(int idx) const
|
||||
{
|
||||
CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
|
||||
idx = (idx == -1) ? 0 : idx;
|
||||
|
||||
if (type == Param::REAL)
|
||||
{
|
||||
return (*pd)[idx];
|
||||
}
|
||||
else if (type == Param::INT)
|
||||
{
|
||||
return (double)(*pi)[idx];
|
||||
}
|
||||
else if (type == Param::STRING)
|
||||
{
|
||||
return std::atof((*ps)[idx].c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
CV_Assert(isReal() || isInt() || isString());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline double DictValue::getRealValue(int idx) const
|
||||
{
|
||||
return get<double>(idx);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline float DictValue::get<float>(int idx) const
|
||||
{
|
||||
return (float)get<double>(idx);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline String DictValue::get<String>(int idx) const
|
||||
{
|
||||
CV_Assert(isString());
|
||||
CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
|
||||
return (*ps)[(idx == -1) ? 0 : idx];
|
||||
}
|
||||
|
||||
|
||||
inline String DictValue::getStringValue(int idx) const
|
||||
{
|
||||
return get<String>(idx);
|
||||
}
|
||||
|
||||
inline void DictValue::release()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Param::INT:
|
||||
delete pi;
|
||||
break;
|
||||
case Param::STRING:
|
||||
delete ps;
|
||||
break;
|
||||
case Param::REAL:
|
||||
delete pd;
|
||||
break;
|
||||
case Param::BOOLEAN:
|
||||
case Param::MAT:
|
||||
case Param::MAT_VECTOR:
|
||||
case Param::ALGORITHM:
|
||||
case Param::FLOAT:
|
||||
case Param::UNSIGNED_INT:
|
||||
case Param::UINT64:
|
||||
case Param::UCHAR:
|
||||
case Param::SCALAR:
|
||||
break; // unhandled
|
||||
}
|
||||
}
|
||||
|
||||
inline DictValue::~DictValue()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
inline DictValue & DictValue::operator=(const DictValue &r)
|
||||
{
|
||||
if (&r == this)
|
||||
return *this;
|
||||
|
||||
if (r.type == Param::INT)
|
||||
{
|
||||
AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
|
||||
release();
|
||||
pi = tmp;
|
||||
}
|
||||
else if (r.type == Param::STRING)
|
||||
{
|
||||
AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
|
||||
release();
|
||||
ps = tmp;
|
||||
}
|
||||
else if (r.type == Param::REAL)
|
||||
{
|
||||
AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
|
||||
release();
|
||||
pd = tmp;
|
||||
}
|
||||
|
||||
type = r.type;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline DictValue::DictValue(const DictValue &r)
|
||||
: pv(NULL)
|
||||
{
|
||||
type = r.type;
|
||||
|
||||
if (r.type == Param::INT)
|
||||
pi = new AutoBuffer<int64, 1>(*r.pi);
|
||||
else if (r.type == Param::STRING)
|
||||
ps = new AutoBuffer<String, 1>(*r.ps);
|
||||
else if (r.type == Param::REAL)
|
||||
pd = new AutoBuffer<double, 1>(*r.pd);
|
||||
}
|
||||
|
||||
inline bool DictValue::isString() const
|
||||
{
|
||||
return (type == Param::STRING);
|
||||
}
|
||||
|
||||
inline bool DictValue::isInt() const
|
||||
{
|
||||
return (type == Param::INT);
|
||||
}
|
||||
|
||||
inline bool DictValue::isReal() const
|
||||
{
|
||||
return (type == Param::REAL || type == Param::INT);
|
||||
}
|
||||
|
||||
inline int DictValue::size() const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Param::INT:
|
||||
return (int)pi->size();
|
||||
case Param::STRING:
|
||||
return (int)ps->size();
|
||||
case Param::REAL:
|
||||
return (int)pd->size();
|
||||
case Param::BOOLEAN:
|
||||
case Param::MAT:
|
||||
case Param::MAT_VECTOR:
|
||||
case Param::ALGORITHM:
|
||||
case Param::FLOAT:
|
||||
case Param::UNSIGNED_INT:
|
||||
case Param::UINT64:
|
||||
case Param::UCHAR:
|
||||
case Param::SCALAR:
|
||||
break; // unhandled
|
||||
}
|
||||
CV_Error_(Error::StsInternal, ("Unhandled type (%d)", static_cast<int>(type)));
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (dictv.isInt())
|
||||
{
|
||||
for (i = 0; i < dictv.size() - 1; i++)
|
||||
stream << dictv.get<int64>(i) << ", ";
|
||||
stream << dictv.get<int64>(i);
|
||||
}
|
||||
else if (dictv.isReal())
|
||||
{
|
||||
for (i = 0; i < dictv.size() - 1; i++)
|
||||
stream << dictv.get<double>(i) << ", ";
|
||||
stream << dictv.get<double>(i);
|
||||
}
|
||||
else if (dictv.isString())
|
||||
{
|
||||
for (i = 0; i < dictv.size() - 1; i++)
|
||||
stream << "\"" << dictv.get<String>(i) << "\", ";
|
||||
stream << dictv.get<String>(i);
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
inline bool Dict::has(const String &key) const
|
||||
{
|
||||
return dict.count(key) != 0;
|
||||
}
|
||||
|
||||
inline DictValue *Dict::ptr(const String &key)
|
||||
{
|
||||
_Dict::iterator i = dict.find(key);
|
||||
return (i == dict.end()) ? NULL : &i->second;
|
||||
}
|
||||
|
||||
inline const DictValue *Dict::ptr(const String &key) const
|
||||
{
|
||||
_Dict::const_iterator i = dict.find(key);
|
||||
return (i == dict.end()) ? NULL : &i->second;
|
||||
}
|
||||
|
||||
inline const DictValue &Dict::get(const String &key) const
|
||||
{
|
||||
_Dict::const_iterator i = dict.find(key);
|
||||
if (i == dict.end())
|
||||
CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
|
||||
return i->second;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T Dict::get(const String &key) const
|
||||
{
|
||||
return this->get(key).get<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T Dict::get(const String &key, const T &defaultValue) const
|
||||
{
|
||||
_Dict::const_iterator i = dict.find(key);
|
||||
|
||||
if (i != dict.end())
|
||||
return i->second.get<T>();
|
||||
else
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline const T &Dict::set(const String &key, const T &value)
|
||||
{
|
||||
_Dict::iterator i = dict.find(key);
|
||||
|
||||
if (i != dict.end())
|
||||
i->second = DictValue(value);
|
||||
else
|
||||
dict.insert(std::make_pair(key, DictValue(value)));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
inline void Dict::erase(const String &key)
|
||||
{
|
||||
dict.erase(key);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
|
||||
{
|
||||
Dict::_Dict::const_iterator it;
|
||||
for (it = dict.dict.begin(); it != dict.dict.end(); it++)
|
||||
stream << it->first << " : " << it->second << "\n";
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline std::map<String, DictValue>::const_iterator Dict::begin() const
|
||||
{
|
||||
return dict.begin();
|
||||
}
|
||||
|
||||
inline std::map<String, DictValue>::const_iterator Dict::end() const
|
||||
{
|
||||
return dict.end();
|
||||
}
|
||||
|
||||
CV__DNN_INLINE_NS_END
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
#ifndef OPENCV_DNN_LAYER_DETAILS_HPP
|
||||
#define OPENCV_DNN_LAYER_DETAILS_HPP
|
||||
|
||||
#include <opencv2/dnn/layer.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
/** @brief Registers layer constructor in runtime.
|
||||
* @param type string, containing type name of the layer.
|
||||
* @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
|
||||
* @details This macros must be placed inside the function code.
|
||||
*/
|
||||
#define CV_DNN_REGISTER_LAYER_FUNC(type, constructorFunc) \
|
||||
cv::dnn::LayerFactory::registerLayer(#type, constructorFunc);
|
||||
|
||||
/** @brief Registers layer class in runtime.
|
||||
* @param type string, containing type name of the layer.
|
||||
* @param class C++ class, derived from Layer.
|
||||
* @details This macros must be placed inside the function code.
|
||||
*/
|
||||
#define CV_DNN_REGISTER_LAYER_CLASS(type, class) \
|
||||
cv::dnn::LayerFactory::registerLayer(#type, cv::dnn::details::_layerDynamicRegisterer<class>);
|
||||
|
||||
/** @brief Registers layer constructor on module load time.
|
||||
* @param type string, containing type name of the layer.
|
||||
* @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
|
||||
* @details This macros must be placed outside the function code.
|
||||
*/
|
||||
#define CV_DNN_REGISTER_LAYER_FUNC_STATIC(type, constructorFunc) \
|
||||
static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, constructorFunc);
|
||||
|
||||
/** @brief Registers layer class on module load time.
|
||||
* @param type string, containing type name of the layer.
|
||||
* @param class C++ class, derived from Layer.
|
||||
* @details This macros must be placed outside the function code.
|
||||
*/
|
||||
#define CV_DNN_REGISTER_LAYER_CLASS_STATIC(type, class) \
|
||||
Ptr<Layer> __LayerStaticRegisterer_func_##type(LayerParams ¶ms) \
|
||||
{ return Ptr<Layer>(new class(params)); } \
|
||||
static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, __LayerStaticRegisterer_func_##type);
|
||||
|
||||
namespace details {
|
||||
|
||||
template<typename LayerClass>
|
||||
Ptr<Layer> _layerDynamicRegisterer(LayerParams ¶ms)
|
||||
{
|
||||
return Ptr<Layer>(LayerClass::create(params));
|
||||
}
|
||||
|
||||
//allows automatically register created layer on module load time
|
||||
class _LayerStaticRegisterer
|
||||
{
|
||||
String type;
|
||||
public:
|
||||
|
||||
_LayerStaticRegisterer(const String &layerType, LayerFactory::Constructor layerConstructor)
|
||||
{
|
||||
this->type = layerType;
|
||||
LayerFactory::registerLayer(layerType, layerConstructor);
|
||||
}
|
||||
|
||||
~_LayerStaticRegisterer()
|
||||
{
|
||||
LayerFactory::unregisterLayer(type);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
CV__DNN_INLINE_NS_END
|
||||
}} // namespace
|
||||
|
||||
#endif
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
/*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) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifndef OPENCV_DNN_LAYER_HPP
|
||||
#define OPENCV_DNN_LAYER_HPP
|
||||
#include <opencv2/dnn.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
//! @addtogroup dnn
|
||||
//! @{
|
||||
//!
|
||||
//! @defgroup dnnLayerFactory Utilities for New Layers Registration
|
||||
//! @{
|
||||
|
||||
/** @brief %Layer factory allows to create instances of registered layers. */
|
||||
class CV_EXPORTS LayerFactory
|
||||
{
|
||||
public:
|
||||
|
||||
//! Each Layer class must provide this function to the factory
|
||||
typedef Ptr<Layer>(*Constructor)(LayerParams ¶ms);
|
||||
|
||||
//! Registers the layer class with typename @p type and specified @p constructor. Thread-safe.
|
||||
static void registerLayer(const String &type, Constructor constructor);
|
||||
|
||||
//! Unregisters registered layer with specified type name. Thread-safe.
|
||||
static void unregisterLayer(const String &type);
|
||||
|
||||
//! Check if layer is registered.
|
||||
static bool isLayerRegistered(const std::string& type);
|
||||
|
||||
/** @brief Creates instance of registered layer.
|
||||
* @param type type name of creating layer.
|
||||
* @param params parameters which will be used for layer initialization.
|
||||
* @note Thread-safe.
|
||||
*/
|
||||
static Ptr<Layer> createLayerInstance(const String &type, LayerParams& params);
|
||||
|
||||
private:
|
||||
LayerFactory();
|
||||
};
|
||||
|
||||
//! @}
|
||||
//! @}
|
||||
CV__DNN_INLINE_NS_END
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+290
@@ -0,0 +1,290 @@
|
||||
/*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) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifndef OPENCV_DNN_DNN_SHAPE_UTILS_HPP
|
||||
#define OPENCV_DNN_DNN_SHAPE_UTILS_HPP
|
||||
|
||||
#include <opencv2/dnn/dnn.hpp>
|
||||
#include <opencv2/core/types_c.h> // CV_MAX_DIM
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
//Slicing
|
||||
|
||||
struct _Range : public cv::Range
|
||||
{
|
||||
_Range(const Range &r) : cv::Range(r) {}
|
||||
_Range(int start_, int size_ = 1) : cv::Range(start_, start_ + size_) {}
|
||||
};
|
||||
|
||||
static inline Mat slice(const Mat &m, const _Range &r0)
|
||||
{
|
||||
Range ranges[CV_MAX_DIM];
|
||||
for (int i = 1; i < m.dims; i++)
|
||||
ranges[i] = Range::all();
|
||||
ranges[0] = r0;
|
||||
return m(&ranges[0]);
|
||||
}
|
||||
|
||||
static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1)
|
||||
{
|
||||
CV_Assert(m.dims >= 2);
|
||||
Range ranges[CV_MAX_DIM];
|
||||
for (int i = 2; i < m.dims; i++)
|
||||
ranges[i] = Range::all();
|
||||
ranges[0] = r0;
|
||||
ranges[1] = r1;
|
||||
return m(&ranges[0]);
|
||||
}
|
||||
|
||||
static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2)
|
||||
{
|
||||
CV_Assert(m.dims >= 3);
|
||||
Range ranges[CV_MAX_DIM];
|
||||
for (int i = 3; i < m.dims; i++)
|
||||
ranges[i] = Range::all();
|
||||
ranges[0] = r0;
|
||||
ranges[1] = r1;
|
||||
ranges[2] = r2;
|
||||
return m(&ranges[0]);
|
||||
}
|
||||
|
||||
static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, const _Range &r3)
|
||||
{
|
||||
CV_Assert(m.dims >= 4);
|
||||
Range ranges[CV_MAX_DIM];
|
||||
for (int i = 4; i < m.dims; i++)
|
||||
ranges[i] = Range::all();
|
||||
ranges[0] = r0;
|
||||
ranges[1] = r1;
|
||||
ranges[2] = r2;
|
||||
ranges[3] = r3;
|
||||
return m(&ranges[0]);
|
||||
}
|
||||
|
||||
static inline Mat getPlane(const Mat &m, int n, int cn)
|
||||
{
|
||||
CV_Assert(m.dims > 2);
|
||||
int sz[CV_MAX_DIM];
|
||||
for(int i = 2; i < m.dims; i++)
|
||||
{
|
||||
sz[i-2] = m.size.p[i];
|
||||
}
|
||||
return Mat(m.dims - 2, sz, m.type(), (void*)m.ptr<float>(n, cn));
|
||||
}
|
||||
|
||||
static inline MatShape shape(const int* dims, const int n)
|
||||
{
|
||||
MatShape shape;
|
||||
shape.assign(dims, dims + n);
|
||||
return shape;
|
||||
}
|
||||
|
||||
static inline MatShape shape(const Mat& mat)
|
||||
{
|
||||
return shape(mat.size.p, mat.dims);
|
||||
}
|
||||
|
||||
static inline MatShape shape(const MatSize& sz)
|
||||
{
|
||||
return shape(sz.p, sz.dims());
|
||||
}
|
||||
|
||||
static inline MatShape shape(const UMat& mat)
|
||||
{
|
||||
return shape(mat.size.p, mat.dims);
|
||||
}
|
||||
|
||||
#if 0 // issues with MatExpr wrapped into InputArray
|
||||
static inline
|
||||
MatShape shape(InputArray input)
|
||||
{
|
||||
int sz[CV_MAX_DIM];
|
||||
int ndims = input.sizend(sz);
|
||||
return shape(sz, ndims);
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace {inline bool is_neg(int i) { return i < 0; }}
|
||||
|
||||
static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
|
||||
{
|
||||
int dims[] = {a0, a1, a2, a3};
|
||||
MatShape s = shape(dims, 4);
|
||||
s.erase(std::remove_if(s.begin(), s.end(), is_neg), s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline int total(const MatShape& shape, int start = -1, int end = -1)
|
||||
{
|
||||
if (shape.empty())
|
||||
return 0;
|
||||
|
||||
int dims = (int)shape.size();
|
||||
|
||||
if (start == -1) start = 0;
|
||||
if (end == -1) end = dims;
|
||||
|
||||
CV_CheckLE(0, start, "");
|
||||
CV_CheckLE(start, end, "");
|
||||
CV_CheckLE(end, dims, "");
|
||||
|
||||
int elems = 1;
|
||||
for (int i = start; i < end; i++)
|
||||
{
|
||||
elems *= shape[i];
|
||||
}
|
||||
return elems;
|
||||
}
|
||||
|
||||
// TODO: rename to countDimsElements()
|
||||
static inline int total(const Mat& mat, int start = -1, int end = -1)
|
||||
{
|
||||
if (mat.empty())
|
||||
return 0;
|
||||
|
||||
int dims = mat.dims;
|
||||
|
||||
if (start == -1) start = 0;
|
||||
if (end == -1) end = dims;
|
||||
|
||||
CV_CheckLE(0, start, "");
|
||||
CV_CheckLE(start, end, "");
|
||||
CV_CheckLE(end, dims, "");
|
||||
|
||||
int elems = 1;
|
||||
for (int i = start; i < end; i++)
|
||||
{
|
||||
elems *= mat.size[i];
|
||||
}
|
||||
return elems;
|
||||
}
|
||||
|
||||
static inline MatShape concat(const MatShape& a, const MatShape& b)
|
||||
{
|
||||
MatShape c = a;
|
||||
c.insert(c.end(), b.begin(), b.end());
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
template<typename _Tp>
|
||||
static inline std::string toString(const std::vector<_Tp>& shape, const String& name = "")
|
||||
{
|
||||
std::ostringstream ss;
|
||||
if (!name.empty())
|
||||
ss << name << ' ';
|
||||
ss << '[';
|
||||
for(size_t i = 0, n = shape.size(); i < n; ++i)
|
||||
ss << ' ' << shape[i];
|
||||
ss << " ]";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<typename _Tp>
|
||||
static inline void print(const std::vector<_Tp>& shape, const String& name = "")
|
||||
{
|
||||
std::cout << toString(shape, name) << std::endl;
|
||||
}
|
||||
template<typename _Tp>
|
||||
static inline std::ostream& operator<<(std::ostream &out, const std::vector<_Tp>& shape)
|
||||
{
|
||||
out << toString(shape);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// @brief Converts axis from `[-dims; dims)` (similar to Python's slice notation) to `[0; dims)` range.
|
||||
static inline
|
||||
int normalize_axis(int axis, int dims)
|
||||
{
|
||||
CV_Check(axis, axis >= -dims && axis < dims, "");
|
||||
axis = (axis < 0) ? (dims + axis) : axis;
|
||||
CV_DbgCheck(axis, axis >= 0 && axis < dims, "");
|
||||
return axis;
|
||||
}
|
||||
|
||||
static inline
|
||||
int normalize_axis(int axis, const MatShape& shape)
|
||||
{
|
||||
return normalize_axis(axis, (int)shape.size());
|
||||
}
|
||||
|
||||
static inline
|
||||
Range normalize_axis_range(const Range& r, int axisSize)
|
||||
{
|
||||
if (r == Range::all())
|
||||
return Range(0, axisSize);
|
||||
CV_CheckGE(r.start, 0, "");
|
||||
Range clamped(r.start,
|
||||
r.end > 0 ? std::min(r.end, axisSize) : axisSize + r.end + 1);
|
||||
CV_DbgCheckGE(clamped.start, 0, "");
|
||||
CV_CheckLT(clamped.start, clamped.end, "");
|
||||
CV_CheckLE(clamped.end, axisSize, "");
|
||||
return clamped;
|
||||
}
|
||||
|
||||
static inline
|
||||
bool isAllOnes(const MatShape &inputShape, int startPos, int endPos)
|
||||
{
|
||||
CV_Assert(!inputShape.empty());
|
||||
|
||||
CV_CheckGE((int) inputShape.size(), startPos, "");
|
||||
CV_CheckGE(startPos, 0, "");
|
||||
CV_CheckLE(startPos, endPos, "");
|
||||
CV_CheckLE((size_t)endPos, inputShape.size(), "");
|
||||
|
||||
for (size_t i = startPos; i < endPos; i++)
|
||||
{
|
||||
if (inputShape[i] != 1)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CV__DNN_INLINE_NS_END
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#ifndef OPENCV_DNN_UTILS_DEBUG_UTILS_HPP
|
||||
#define OPENCV_DNN_UTILS_DEBUG_UTILS_HPP
|
||||
|
||||
#include "../dnn.hpp"
|
||||
|
||||
namespace cv { namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
/**
|
||||
* @brief Skip model import after diagnostic run in readNet() functions.
|
||||
* @param[in] skip Indicates whether to skip the import.
|
||||
*
|
||||
* This is an internal OpenCV function not intended for users.
|
||||
*/
|
||||
CV_EXPORTS void skipModelImport(bool skip);
|
||||
|
||||
CV__DNN_INLINE_NS_END
|
||||
}} // namespace
|
||||
|
||||
#endif // OPENCV_DNN_UTILS_DEBUG_UTILS_HPP
|
||||
Vendored
+82
@@ -0,0 +1,82 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2019, Intel Corporation, all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
#ifndef OPENCV_DNN_UTILS_INF_ENGINE_HPP
|
||||
#define OPENCV_DNN_UTILS_INF_ENGINE_HPP
|
||||
|
||||
#include "../dnn.hpp"
|
||||
|
||||
namespace cv { namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
|
||||
/* Values for 'OPENCV_DNN_BACKEND_INFERENCE_ENGINE_TYPE' parameter */
|
||||
/// @deprecated
|
||||
#define CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API "NN_BUILDER"
|
||||
/// @deprecated
|
||||
#define CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH "NGRAPH"
|
||||
|
||||
/** @brief Returns Inference Engine internal backend API.
|
||||
*
|
||||
* See values of `CV_DNN_BACKEND_INFERENCE_ENGINE_*` macros.
|
||||
*
|
||||
* `OPENCV_DNN_BACKEND_INFERENCE_ENGINE_TYPE` runtime parameter (environment variable) is ignored since 4.6.0.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
CV_EXPORTS_W cv::String getInferenceEngineBackendType();
|
||||
|
||||
/** @brief Specify Inference Engine internal backend API.
|
||||
*
|
||||
* See values of `CV_DNN_BACKEND_INFERENCE_ENGINE_*` macros.
|
||||
*
|
||||
* @returns previous value of internal backend API
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
CV_EXPORTS_W cv::String setInferenceEngineBackendType(const cv::String& newBackendType);
|
||||
|
||||
|
||||
/** @brief Release a Myriad device (binded by OpenCV).
|
||||
*
|
||||
* Single Myriad device cannot be shared across multiple processes which uses
|
||||
* Inference Engine's Myriad plugin.
|
||||
*/
|
||||
CV_EXPORTS_W void resetMyriadDevice();
|
||||
|
||||
|
||||
/* Values for 'OPENCV_DNN_IE_VPU_TYPE' parameter */
|
||||
#define CV_DNN_INFERENCE_ENGINE_VPU_TYPE_UNSPECIFIED ""
|
||||
/// Intel(R) Movidius(TM) Neural Compute Stick, NCS (USB 03e7:2150), Myriad2 (https://software.intel.com/en-us/movidius-ncs)
|
||||
#define CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2 "Myriad2"
|
||||
/// Intel(R) Neural Compute Stick 2, NCS2 (USB 03e7:2485), MyriadX (https://software.intel.com/ru-ru/neural-compute-stick)
|
||||
#define CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X "MyriadX"
|
||||
#define CV_DNN_INFERENCE_ENGINE_CPU_TYPE_ARM_COMPUTE "ARM_COMPUTE"
|
||||
#define CV_DNN_INFERENCE_ENGINE_CPU_TYPE_X86 "X86"
|
||||
|
||||
|
||||
/** @brief Returns Inference Engine VPU type.
|
||||
*
|
||||
* See values of `CV_DNN_INFERENCE_ENGINE_VPU_TYPE_*` macros.
|
||||
*/
|
||||
CV_EXPORTS_W cv::String getInferenceEngineVPUType();
|
||||
|
||||
/** @brief Returns Inference Engine CPU type.
|
||||
*
|
||||
* Specify OpenVINO plugin: CPU or ARM.
|
||||
*/
|
||||
CV_EXPORTS_W cv::String getInferenceEngineCPUType();
|
||||
|
||||
/** @brief Release a HDDL plugin.
|
||||
*/
|
||||
CV_EXPORTS_W void releaseHDDLPlugin();
|
||||
|
||||
|
||||
CV__DNN_INLINE_NS_END
|
||||
}} // namespace
|
||||
|
||||
#endif // OPENCV_DNN_UTILS_INF_ENGINE_HPP
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#ifndef OPENCV_DNN_VERSION_HPP
|
||||
#define OPENCV_DNN_VERSION_HPP
|
||||
|
||||
/// Use with major OpenCV version only.
|
||||
#define OPENCV_DNN_API_VERSION 20230620
|
||||
|
||||
#if !defined CV_DOXYGEN && !defined CV_STATIC_ANALYSIS && !defined CV_DNN_DONT_ADD_INLINE_NS
|
||||
#define CV__DNN_INLINE_NS __CV_CAT(dnn4_v, OPENCV_DNN_API_VERSION)
|
||||
#define CV__DNN_INLINE_NS_BEGIN namespace CV__DNN_INLINE_NS {
|
||||
#define CV__DNN_INLINE_NS_END }
|
||||
namespace cv { namespace dnn { namespace CV__DNN_INLINE_NS { } using namespace CV__DNN_INLINE_NS; }}
|
||||
#else
|
||||
#define CV__DNN_INLINE_NS_BEGIN
|
||||
#define CV__DNN_INLINE_NS_END
|
||||
#endif
|
||||
|
||||
#endif // OPENCV_DNN_VERSION_HPP
|
||||
+1603
File diff suppressed because it is too large
Load Diff
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
/*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.
|
||||
// Copyright (C) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifdef __OPENCV_BUILD
|
||||
#error this is a compatibility header which should not be used inside the OpenCV library
|
||||
#endif
|
||||
|
||||
#include "opencv2/features2d.hpp"
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
#ifndef OPENCV_FEATURE2D_HAL_INTERFACE_H
|
||||
#define OPENCV_FEATURE2D_HAL_INTERFACE_H
|
||||
|
||||
#include "opencv2/core/cvdef.h"
|
||||
//! @addtogroup features2d_hal_interface
|
||||
//! @{
|
||||
|
||||
//! @name Fast feature detector types
|
||||
//! @sa cv::FastFeatureDetector
|
||||
//! @{
|
||||
#define CV_HAL_TYPE_5_8 0
|
||||
#define CV_HAL_TYPE_7_12 1
|
||||
#define CV_HAL_TYPE_9_16 2
|
||||
//! @}
|
||||
|
||||
//! @name Key point
|
||||
//! @sa cv::KeyPoint
|
||||
//! @{
|
||||
struct CV_EXPORTS cvhalKeyPoint
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float size;
|
||||
float angle;
|
||||
float response;
|
||||
int octave;
|
||||
int class_id;
|
||||
};
|
||||
//! @}
|
||||
|
||||
//! @}
|
||||
|
||||
#endif
|
||||
+629
@@ -0,0 +1,629 @@
|
||||
/*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*/
|
||||
|
||||
#ifndef OPENCV_FLANN_HPP
|
||||
#define OPENCV_FLANN_HPP
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/flann/miniflann.hpp"
|
||||
#include "opencv2/flann/flann_base.hpp"
|
||||
|
||||
/**
|
||||
@defgroup flann Clustering and Search in Multi-Dimensional Spaces
|
||||
|
||||
This section documents OpenCV's interface to the FLANN library. FLANN (Fast Library for Approximate
|
||||
Nearest Neighbors) is a library that contains a collection of algorithms optimized for fast nearest
|
||||
neighbor search in large datasets and for high dimensional features. More information about FLANN
|
||||
can be found in @cite Muja2009 .
|
||||
*/
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
CV_EXPORTS flann_distance_t flann_distance_type();
|
||||
CV_DEPRECATED CV_EXPORTS void set_distance_type(flann_distance_t distance_type, int order);
|
||||
}
|
||||
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace flann
|
||||
{
|
||||
|
||||
|
||||
//! @addtogroup flann
|
||||
//! @{
|
||||
|
||||
template <typename T> struct CvType {};
|
||||
template <> struct CvType<unsigned char> { static int type() { return CV_8U; } };
|
||||
template <> struct CvType<char> { static int type() { return CV_8S; } };
|
||||
template <> struct CvType<unsigned short> { static int type() { return CV_16U; } };
|
||||
template <> struct CvType<short> { static int type() { return CV_16S; } };
|
||||
template <> struct CvType<int> { static int type() { return CV_32S; } };
|
||||
template <> struct CvType<float> { static int type() { return CV_32F; } };
|
||||
template <> struct CvType<double> { static int type() { return CV_64F; } };
|
||||
|
||||
|
||||
// bring the flann parameters into this namespace
|
||||
using ::cvflann::get_param;
|
||||
using ::cvflann::print_params;
|
||||
|
||||
// bring the flann distances into this namespace
|
||||
using ::cvflann::L2_Simple;
|
||||
using ::cvflann::L2;
|
||||
using ::cvflann::L1;
|
||||
using ::cvflann::MinkowskiDistance;
|
||||
using ::cvflann::MaxDistance;
|
||||
using ::cvflann::HammingLUT;
|
||||
using ::cvflann::Hamming;
|
||||
using ::cvflann::Hamming2;
|
||||
using ::cvflann::DNAmmingLUT;
|
||||
using ::cvflann::DNAmming2;
|
||||
using ::cvflann::HistIntersectionDistance;
|
||||
using ::cvflann::HellingerDistance;
|
||||
using ::cvflann::ChiSquareDistance;
|
||||
using ::cvflann::KL_Divergence;
|
||||
|
||||
|
||||
/** @brief The FLANN nearest neighbor index class. This class is templated with the type of elements for which
|
||||
the index is built.
|
||||
|
||||
`Distance` functor specifies the metric to be used to calculate the distance between two points.
|
||||
There are several `Distance` functors that are readily available:
|
||||
|
||||
cv::cvflann::L2_Simple - Squared Euclidean distance functor.
|
||||
This is the simpler, unrolled version. This is preferable for very low dimensionality data (eg 3D points)
|
||||
|
||||
cv::flann::L2 - Squared Euclidean distance functor, optimized version.
|
||||
|
||||
cv::flann::L1 - Manhattan distance functor, optimized version.
|
||||
|
||||
cv::flann::MinkowskiDistance - The Minkowski distance functor.
|
||||
This is highly optimised with loop unrolling.
|
||||
The computation of squared root at the end is omitted for efficiency.
|
||||
|
||||
cv::flann::MaxDistance - The max distance functor. It computes the
|
||||
maximum distance between two vectors. This distance is not a valid kdtree distance, it's not
|
||||
dimensionwise additive.
|
||||
|
||||
cv::flann::HammingLUT - %Hamming distance functor. It counts the bit
|
||||
differences between two strings using a lookup table implementation.
|
||||
|
||||
cv::flann::Hamming - %Hamming distance functor. Population count is
|
||||
performed using library calls, if available. Lookup table implementation is used as a fallback.
|
||||
|
||||
cv::flann::Hamming2 - %Hamming distance functor. Population count is
|
||||
implemented in 12 arithmetic operations (one of which is multiplication).
|
||||
|
||||
cv::flann::DNAmmingLUT - %Adaptation of the Hamming distance functor to DNA comparison.
|
||||
As the four bases A, C, G, T of the DNA (or A, G, C, U for RNA) can be coded on 2 bits,
|
||||
it counts the bits pairs differences between two sequences using a lookup table implementation.
|
||||
|
||||
cv::flann::DNAmming2 - %Adaptation of the Hamming distance functor to DNA comparison.
|
||||
Bases differences count are vectorised thanks to arithmetic operations using standard
|
||||
registers (AVX2 and AVX-512 should come in a near future).
|
||||
|
||||
cv::flann::HistIntersectionDistance - The histogram
|
||||
intersection distance functor.
|
||||
|
||||
cv::flann::HellingerDistance - The Hellinger distance functor.
|
||||
|
||||
cv::flann::ChiSquareDistance - The chi-square distance functor.
|
||||
|
||||
cv::flann::KL_Divergence - The Kullback-Leibler divergence functor.
|
||||
|
||||
Although the provided implementations cover a vast range of cases, it is also possible to use
|
||||
a custom implementation. The distance functor is a class whose `operator()` computes the distance
|
||||
between two features. If the distance is also a kd-tree compatible distance, it should also provide an
|
||||
`accum_dist()` method that computes the distance between individual feature dimensions.
|
||||
|
||||
In addition to `operator()` and `accum_dist()`, a distance functor should also define the
|
||||
`ElementType` and the `ResultType` as the types of the elements it operates on and the type of the
|
||||
result it computes. If a distance functor can be used as a kd-tree distance (meaning that the full
|
||||
distance between a pair of features can be accumulated from the partial distances between the
|
||||
individual dimensions) a typedef `is_kdtree_distance` should be present inside the distance functor.
|
||||
If the distance is not a kd-tree distance, but it's a distance in a vector space (the individual
|
||||
dimensions of the elements it operates on can be accessed independently) a typedef
|
||||
`is_vector_space_distance` should be defined inside the functor. If neither typedef is defined, the
|
||||
distance is assumed to be a metric distance and will only be used with indexes operating on
|
||||
generic metric distances.
|
||||
*/
|
||||
template <typename Distance>
|
||||
class GenericIndex
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
/** @brief Constructs a nearest neighbor search index for a given dataset.
|
||||
|
||||
@param features Matrix of containing the features(points) to index. The size of the matrix is
|
||||
num_features x feature_dimensionality and the data type of the elements in the matrix must
|
||||
coincide with the type of the index.
|
||||
@param params Structure containing the index parameters. The type of index that will be
|
||||
constructed depends on the type of this parameter. See the description.
|
||||
@param distance
|
||||
|
||||
The method constructs a fast search structure from a set of features using the specified algorithm
|
||||
with specified parameters, as defined by params. params is a reference to one of the following class
|
||||
IndexParams descendants:
|
||||
|
||||
- **LinearIndexParams** When passing an object of this type, the index will perform a linear,
|
||||
brute-force search. :
|
||||
@code
|
||||
struct LinearIndexParams : public IndexParams
|
||||
{
|
||||
};
|
||||
@endcode
|
||||
- **KDTreeIndexParams** When passing an object of this type the index constructed will consist of
|
||||
a set of randomized kd-trees which will be searched in parallel. :
|
||||
@code
|
||||
struct KDTreeIndexParams : public IndexParams
|
||||
{
|
||||
KDTreeIndexParams( int trees = 4 );
|
||||
};
|
||||
@endcode
|
||||
- **HierarchicalClusteringIndexParams** When passing an object of this type the index constructed
|
||||
will be a hierarchical tree of clusters, dividing each set of points into n clusters whose centers
|
||||
are picked among the points without further refinement of their position.
|
||||
This algorithm fits both floating, integer and binary vectors. :
|
||||
@code
|
||||
struct HierarchicalClusteringIndexParams : public IndexParams
|
||||
{
|
||||
HierarchicalClusteringIndexParams(
|
||||
int branching = 32,
|
||||
flann_centers_init_t centers_init = CENTERS_RANDOM,
|
||||
int trees = 4,
|
||||
int leaf_size = 100);
|
||||
|
||||
};
|
||||
@endcode
|
||||
- **KMeansIndexParams** When passing an object of this type the index constructed will be a
|
||||
hierarchical k-means tree (one tree by default), dividing each set of points into n clusters
|
||||
whose barycenters are refined iteratively.
|
||||
Note that this algorithm has been extended to the support of binary vectors as an alternative
|
||||
to LSH when knn search speed is the criterium. It will also outperform LSH when processing
|
||||
directly (i.e. without the use of MCA/PCA) datasets whose points share mostly the same values
|
||||
for most of the dimensions. It is recommended to set more than one tree with binary data. :
|
||||
@code
|
||||
struct KMeansIndexParams : public IndexParams
|
||||
{
|
||||
KMeansIndexParams(
|
||||
int branching = 32,
|
||||
int iterations = 11,
|
||||
flann_centers_init_t centers_init = CENTERS_RANDOM,
|
||||
float cb_index = 0.2,
|
||||
int trees = 1);
|
||||
};
|
||||
@endcode
|
||||
- **CompositeIndexParams** When using a parameters object of this type the index created
|
||||
combines the randomized kd-trees and the hierarchical k-means tree. :
|
||||
@code
|
||||
struct CompositeIndexParams : public IndexParams
|
||||
{
|
||||
CompositeIndexParams(
|
||||
int trees = 4,
|
||||
int branching = 32,
|
||||
int iterations = 11,
|
||||
flann_centers_init_t centers_init = CENTERS_RANDOM,
|
||||
float cb_index = 0.2 );
|
||||
};
|
||||
@endcode
|
||||
- **LshIndexParams** When using a parameters object of this type the index created uses
|
||||
multi-probe LSH (by Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity Search
|
||||
by Qin Lv, William Josephson, Zhe Wang, Moses Charikar, Kai Li., Proceedings of the 33rd
|
||||
International Conference on Very Large Data Bases (VLDB). Vienna, Austria. September 2007).
|
||||
This algorithm is designed for binary vectors. :
|
||||
@code
|
||||
struct LshIndexParams : public IndexParams
|
||||
{
|
||||
LshIndexParams(
|
||||
int table_number,
|
||||
int key_size,
|
||||
int multi_probe_level );
|
||||
};
|
||||
@endcode
|
||||
- **AutotunedIndexParams** When passing an object of this type the index created is
|
||||
automatically tuned to offer the best performance, by choosing the optimal index type
|
||||
(randomized kd-trees, hierarchical kmeans, linear) and parameters for the dataset provided. :
|
||||
@code
|
||||
struct AutotunedIndexParams : public IndexParams
|
||||
{
|
||||
AutotunedIndexParams(
|
||||
float target_precision = 0.9,
|
||||
float build_weight = 0.01,
|
||||
float memory_weight = 0,
|
||||
float sample_fraction = 0.1 );
|
||||
};
|
||||
@endcode
|
||||
- **SavedIndexParams** This object type is used for loading a previously saved index from the
|
||||
disk. :
|
||||
@code
|
||||
struct SavedIndexParams : public IndexParams
|
||||
{
|
||||
SavedIndexParams( String filename );
|
||||
};
|
||||
@endcode
|
||||
*/
|
||||
GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());
|
||||
|
||||
~GenericIndex();
|
||||
|
||||
/** @brief Performs a K-nearest neighbor search for a given query point using the index.
|
||||
|
||||
@param query The query point
|
||||
@param indices Vector that will contain the indices of the K-nearest neighbors found. It must have
|
||||
at least knn size.
|
||||
@param dists Vector that will contain the distances to the K-nearest neighbors found. It must have
|
||||
at least knn size.
|
||||
@param knn Number of nearest neighbors to search for.
|
||||
@param params SearchParams
|
||||
*/
|
||||
void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
|
||||
std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
|
||||
void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);
|
||||
|
||||
/** @brief Performs a radius nearest neighbor search for a given query point using the index.
|
||||
|
||||
@param query The query point.
|
||||
@param indices Vector that will contain the indices of the nearest neighbors found.
|
||||
@param dists Vector that will contain the distances to the nearest neighbors found. It has the same
|
||||
number of elements as indices.
|
||||
@param radius The search radius.
|
||||
@param params SearchParams
|
||||
|
||||
This function returns the number of nearest neighbors found.
|
||||
*/
|
||||
int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices,
|
||||
std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
|
||||
int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
|
||||
DistanceType radius, const ::cvflann::SearchParams& params);
|
||||
|
||||
void save(String filename) { nnIndex->save(filename); }
|
||||
|
||||
int veclen() const { return nnIndex->veclen(); }
|
||||
|
||||
int size() const { return (int)nnIndex->size(); }
|
||||
|
||||
::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }
|
||||
|
||||
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }
|
||||
|
||||
private:
|
||||
::cvflann::Index<Distance>* nnIndex;
|
||||
Mat _dataset;
|
||||
};
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#define FLANN_DISTANCE_CHECK \
|
||||
if ( ::cvflann::flann_distance_type() != cvflann::FLANN_DIST_L2) { \
|
||||
printf("[WARNING] You are using cv::flann::Index (or cv::flann::GenericIndex) and have also changed "\
|
||||
"the distance using cvflann::set_distance_type. This is no longer working as expected "\
|
||||
"(cv::flann::Index always uses L2). You should create the index templated on the distance, "\
|
||||
"for example for L1 distance use: GenericIndex< L1<float> > \n"); \
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
GenericIndex<Distance>::GenericIndex(const Mat& dataset, const ::cvflann::IndexParams& params, Distance distance)
|
||||
: _dataset(dataset)
|
||||
{
|
||||
CV_Assert(dataset.type() == CvType<ElementType>::type());
|
||||
CV_Assert(dataset.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_dataset((ElementType*)_dataset.ptr<ElementType>(0), _dataset.rows, _dataset.cols);
|
||||
|
||||
nnIndex = new ::cvflann::Index<Distance>(m_dataset, params, distance);
|
||||
|
||||
FLANN_DISTANCE_CHECK
|
||||
|
||||
nnIndex->buildIndex();
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
GenericIndex<Distance>::~GenericIndex()
|
||||
{
|
||||
delete nnIndex;
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
void GenericIndex<Distance>::knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
||||
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
||||
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
||||
|
||||
FLANN_DISTANCE_CHECK
|
||||
|
||||
nnIndex->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
void GenericIndex<Distance>::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
CV_Assert(queries.type() == CvType<ElementType>::type());
|
||||
CV_Assert(queries.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
|
||||
|
||||
CV_Assert(indices.type() == CV_32S);
|
||||
CV_Assert(indices.isContinuous());
|
||||
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
||||
|
||||
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
||||
CV_Assert(dists.isContinuous());
|
||||
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
||||
|
||||
FLANN_DISTANCE_CHECK
|
||||
|
||||
nnIndex->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
int GenericIndex<Distance>::radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
||||
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
||||
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
||||
|
||||
FLANN_DISTANCE_CHECK
|
||||
|
||||
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
int GenericIndex<Distance>::radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
CV_Assert(query.type() == CvType<ElementType>::type());
|
||||
CV_Assert(query.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
|
||||
|
||||
CV_Assert(indices.type() == CV_32S);
|
||||
CV_Assert(indices.isContinuous());
|
||||
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
||||
|
||||
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
||||
CV_Assert(dists.isContinuous());
|
||||
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
||||
|
||||
FLANN_DISTANCE_CHECK
|
||||
|
||||
return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use GenericIndex class instead
|
||||
*/
|
||||
template <typename T>
|
||||
class Index_
|
||||
{
|
||||
public:
|
||||
typedef typename L2<T>::ElementType ElementType;
|
||||
typedef typename L2<T>::ResultType DistanceType;
|
||||
|
||||
CV_DEPRECATED Index_(const Mat& dataset, const ::cvflann::IndexParams& params)
|
||||
{
|
||||
printf("[WARNING] The cv::flann::Index_<T> class is deperecated, use cv::flann::GenericIndex<Distance> instead\n");
|
||||
|
||||
CV_Assert(dataset.type() == CvType<ElementType>::type());
|
||||
CV_Assert(dataset.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_dataset((ElementType*)dataset.ptr<ElementType>(0), dataset.rows, dataset.cols);
|
||||
|
||||
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
|
||||
nnIndex_L1 = NULL;
|
||||
nnIndex_L2 = new ::cvflann::Index< L2<ElementType> >(m_dataset, params);
|
||||
}
|
||||
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
|
||||
nnIndex_L1 = new ::cvflann::Index< L1<ElementType> >(m_dataset, params);
|
||||
nnIndex_L2 = NULL;
|
||||
}
|
||||
else {
|
||||
printf("[ERROR] cv::flann::Index_<T> only provides backwards compatibility for the L1 and L2 distances. "
|
||||
"For other distance types you must use cv::flann::GenericIndex<Distance>\n");
|
||||
CV_Assert(0);
|
||||
}
|
||||
if (nnIndex_L1) nnIndex_L1->buildIndex();
|
||||
if (nnIndex_L2) nnIndex_L2->buildIndex();
|
||||
}
|
||||
CV_DEPRECATED ~Index_()
|
||||
{
|
||||
if (nnIndex_L1) delete nnIndex_L1;
|
||||
if (nnIndex_L2) delete nnIndex_L2;
|
||||
}
|
||||
|
||||
CV_DEPRECATED void knnSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
||||
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
||||
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
||||
|
||||
if (nnIndex_L1) nnIndex_L1->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
||||
if (nnIndex_L2) nnIndex_L2->knnSearch(m_query,m_indices,m_dists,knn,searchParams);
|
||||
}
|
||||
CV_DEPRECATED void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
CV_Assert(queries.type() == CvType<ElementType>::type());
|
||||
CV_Assert(queries.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_queries((ElementType*)queries.ptr<ElementType>(0), queries.rows, queries.cols);
|
||||
|
||||
CV_Assert(indices.type() == CV_32S);
|
||||
CV_Assert(indices.isContinuous());
|
||||
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
||||
|
||||
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
||||
CV_Assert(dists.isContinuous());
|
||||
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
||||
|
||||
if (nnIndex_L1) nnIndex_L1->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
||||
if (nnIndex_L2) nnIndex_L2->knnSearch(m_queries,m_indices,m_dists,knn, searchParams);
|
||||
}
|
||||
|
||||
CV_DEPRECATED int radiusSearch(const std::vector<ElementType>& query, std::vector<int>& indices, std::vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)&query[0], 1, query.size());
|
||||
::cvflann::Matrix<int> m_indices(&indices[0], 1, indices.size());
|
||||
::cvflann::Matrix<DistanceType> m_dists(&dists[0], 1, dists.size());
|
||||
|
||||
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
}
|
||||
|
||||
CV_DEPRECATED int radiusSearch(const Mat& query, Mat& indices, Mat& dists, DistanceType radius, const ::cvflann::SearchParams& searchParams)
|
||||
{
|
||||
CV_Assert(query.type() == CvType<ElementType>::type());
|
||||
CV_Assert(query.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_query((ElementType*)query.ptr<ElementType>(0), query.rows, query.cols);
|
||||
|
||||
CV_Assert(indices.type() == CV_32S);
|
||||
CV_Assert(indices.isContinuous());
|
||||
::cvflann::Matrix<int> m_indices((int*)indices.ptr<int>(0), indices.rows, indices.cols);
|
||||
|
||||
CV_Assert(dists.type() == CvType<DistanceType>::type());
|
||||
CV_Assert(dists.isContinuous());
|
||||
::cvflann::Matrix<DistanceType> m_dists((DistanceType*)dists.ptr<DistanceType>(0), dists.rows, dists.cols);
|
||||
|
||||
if (nnIndex_L1) return nnIndex_L1->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
if (nnIndex_L2) return nnIndex_L2->radiusSearch(m_query,m_indices,m_dists,radius,searchParams);
|
||||
}
|
||||
|
||||
CV_DEPRECATED void save(String filename)
|
||||
{
|
||||
if (nnIndex_L1) nnIndex_L1->save(filename);
|
||||
if (nnIndex_L2) nnIndex_L2->save(filename);
|
||||
}
|
||||
|
||||
CV_DEPRECATED int veclen() const
|
||||
{
|
||||
if (nnIndex_L1) return nnIndex_L1->veclen();
|
||||
if (nnIndex_L2) return nnIndex_L2->veclen();
|
||||
}
|
||||
|
||||
CV_DEPRECATED int size() const
|
||||
{
|
||||
if (nnIndex_L1) return nnIndex_L1->size();
|
||||
if (nnIndex_L2) return nnIndex_L2->size();
|
||||
}
|
||||
|
||||
CV_DEPRECATED ::cvflann::IndexParams getParameters()
|
||||
{
|
||||
if (nnIndex_L1) return nnIndex_L1->getParameters();
|
||||
if (nnIndex_L2) return nnIndex_L2->getParameters();
|
||||
|
||||
}
|
||||
|
||||
CV_DEPRECATED const ::cvflann::IndexParams* getIndexParameters()
|
||||
{
|
||||
if (nnIndex_L1) return nnIndex_L1->getIndexParameters();
|
||||
if (nnIndex_L2) return nnIndex_L2->getIndexParameters();
|
||||
}
|
||||
|
||||
private:
|
||||
// providing backwards compatibility for L2 and L1 distances (most common)
|
||||
::cvflann::Index< L2<ElementType> >* nnIndex_L2;
|
||||
::cvflann::Index< L1<ElementType> >* nnIndex_L1;
|
||||
};
|
||||
|
||||
//! @endcond
|
||||
|
||||
/** @brief Clusters features using hierarchical k-means algorithm.
|
||||
|
||||
@param features The points to be clustered. The matrix must have elements of type
|
||||
Distance::ElementType.
|
||||
@param centers The centers of the clusters obtained. The matrix must have type
|
||||
Distance::CentersType. The number of rows in this matrix represents the number of clusters desired,
|
||||
however, because of the way the cut in the hierarchical tree is chosen, the number of clusters
|
||||
computed will be the highest number of the form (branching-1)\*k+1 that's lower than the number of
|
||||
clusters desired, where branching is the tree's branching factor (see description of the
|
||||
KMeansIndexParams).
|
||||
@param params Parameters used in the construction of the hierarchical k-means tree.
|
||||
@param d Distance to be used for clustering.
|
||||
|
||||
The method clusters the given feature vectors by constructing a hierarchical k-means tree and
|
||||
choosing a cut in the tree that minimizes the cluster's variance. It returns the number of clusters
|
||||
found.
|
||||
*/
|
||||
template <typename Distance>
|
||||
int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params,
|
||||
Distance d = Distance())
|
||||
{
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::CentersType CentersType;
|
||||
|
||||
CV_Assert(features.type() == CvType<ElementType>::type());
|
||||
CV_Assert(features.isContinuous());
|
||||
::cvflann::Matrix<ElementType> m_features((ElementType*)features.ptr<ElementType>(0), features.rows, features.cols);
|
||||
|
||||
CV_Assert(centers.type() == CvType<CentersType>::type());
|
||||
CV_Assert(centers.isContinuous());
|
||||
::cvflann::Matrix<CentersType> m_centers((CentersType*)centers.ptr<CentersType>(0), centers.rows, centers.cols);
|
||||
|
||||
return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);
|
||||
}
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
template <typename ELEM_TYPE, typename DIST_TYPE>
|
||||
CV_DEPRECATED int hierarchicalClustering(const Mat& features, Mat& centers, const ::cvflann::KMeansIndexParams& params)
|
||||
{
|
||||
printf("[WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use "
|
||||
"cv::flann::hierarchicalClustering<Distance> instead\n");
|
||||
|
||||
if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L2 ) {
|
||||
return hierarchicalClustering< L2<ELEM_TYPE> >(features, centers, params);
|
||||
}
|
||||
else if ( ::cvflann::flann_distance_type() == cvflann::FLANN_DIST_L1 ) {
|
||||
return hierarchicalClustering< L1<ELEM_TYPE> >(features, centers, params);
|
||||
}
|
||||
else {
|
||||
printf("[ERROR] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> only provides backwards "
|
||||
"compatibility for the L1 and L2 distances. "
|
||||
"For other distance types you must use cv::flann::hierarchicalClustering<Distance>\n");
|
||||
CV_Assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
//! @} flann
|
||||
|
||||
} } // namespace cv::flann
|
||||
|
||||
#endif
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_ALL_INDICES_H_
|
||||
#define OPENCV_FLANN_ALL_INDICES_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "general.h"
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "kdtree_index.h"
|
||||
#include "kdtree_single_index.h"
|
||||
#include "kmeans_index.h"
|
||||
#include "composite_index.h"
|
||||
#include "linear_index.h"
|
||||
#include "hierarchical_clustering_index.h"
|
||||
#include "lsh_index.h"
|
||||
#include "autotuned_index.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
template<typename KDTreeCapability, typename VectorSpace, typename Distance>
|
||||
struct index_creator
|
||||
{
|
||||
static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
|
||||
{
|
||||
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
|
||||
|
||||
NNIndex<Distance>* nnIndex;
|
||||
switch (index_type) {
|
||||
case FLANN_INDEX_LINEAR:
|
||||
nnIndex = new LinearIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_KDTREE_SINGLE:
|
||||
nnIndex = new KDTreeSingleIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_KDTREE:
|
||||
nnIndex = new KDTreeIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_KMEANS:
|
||||
nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_COMPOSITE:
|
||||
nnIndex = new CompositeIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_AUTOTUNED:
|
||||
nnIndex = new AutotunedIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_HIERARCHICAL:
|
||||
nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_LSH:
|
||||
nnIndex = new LshIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
default:
|
||||
FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
|
||||
}
|
||||
|
||||
return nnIndex;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename VectorSpace, typename Distance>
|
||||
struct index_creator<False,VectorSpace,Distance>
|
||||
{
|
||||
static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
|
||||
{
|
||||
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
|
||||
|
||||
NNIndex<Distance>* nnIndex;
|
||||
switch (index_type) {
|
||||
case FLANN_INDEX_LINEAR:
|
||||
nnIndex = new LinearIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_KMEANS:
|
||||
nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_HIERARCHICAL:
|
||||
nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_LSH:
|
||||
nnIndex = new LshIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
default:
|
||||
FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
|
||||
}
|
||||
|
||||
return nnIndex;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Distance>
|
||||
struct index_creator<False,False,Distance>
|
||||
{
|
||||
static NNIndex<Distance>* create(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
|
||||
{
|
||||
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params, "algorithm");
|
||||
|
||||
NNIndex<Distance>* nnIndex;
|
||||
switch (index_type) {
|
||||
case FLANN_INDEX_LINEAR:
|
||||
nnIndex = new LinearIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_KMEANS:
|
||||
nnIndex = new KMeansIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_HIERARCHICAL:
|
||||
nnIndex = new HierarchicalClusteringIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
case FLANN_INDEX_LSH:
|
||||
nnIndex = new LshIndex<Distance>(dataset, params, distance);
|
||||
break;
|
||||
default:
|
||||
FLANN_THROW(cv::Error::StsBadArg, "Unknown index type");
|
||||
}
|
||||
|
||||
return nnIndex;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Distance>
|
||||
NNIndex<Distance>* create_index_by_type(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance)
|
||||
{
|
||||
return index_creator<typename Distance::is_kdtree_distance,
|
||||
typename Distance::is_vector_space_distance,
|
||||
Distance>::create(dataset, params,distance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_ALL_INDICES_H_ */
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_ALLOCATOR_H_
|
||||
#define OPENCV_FLANN_ALLOCATOR_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
* Allocates (using C's malloc) a generic type T.
|
||||
*
|
||||
* Params:
|
||||
* count = number of instances to allocate.
|
||||
* Returns: pointer (of type T*) to memory buffer
|
||||
*/
|
||||
template <typename T>
|
||||
T* allocate(size_t count = 1)
|
||||
{
|
||||
T* mem = (T*) ::malloc(sizeof(T)*count);
|
||||
return mem;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pooled storage allocator
|
||||
*
|
||||
* The following routines allow for the efficient allocation of storage in
|
||||
* small chunks from a specified pool. Rather than allowing each structure
|
||||
* to be freed individually, an entire pool of storage is freed at once.
|
||||
* This method has two advantages over just using malloc() and free(). First,
|
||||
* it is far more efficient for allocating small objects, as there is
|
||||
* no overhead for remembering all the information needed to free each
|
||||
* object or consolidating fragmented memory. Second, the decision about
|
||||
* how long to keep an object is made at the time of allocation, and there
|
||||
* is no need to track down all the objects to free them.
|
||||
*
|
||||
*/
|
||||
|
||||
const size_t WORDSIZE=16;
|
||||
const size_t BLOCKSIZE=8192;
|
||||
|
||||
class PooledAllocator
|
||||
{
|
||||
/* We maintain memory alignment to word boundaries by requiring that all
|
||||
allocations be in multiples of the machine wordsize. */
|
||||
/* Size of machine word in bytes. Must be power of 2. */
|
||||
/* Minimum number of bytes requested at a time from the system. Must be multiple of WORDSIZE. */
|
||||
|
||||
|
||||
int remaining; /* Number of bytes left in current block of storage. */
|
||||
void* base; /* Pointer to base of current block of storage. */
|
||||
void* loc; /* Current location in block to next allocate memory. */
|
||||
int blocksize;
|
||||
|
||||
|
||||
public:
|
||||
int usedMemory;
|
||||
int wastedMemory;
|
||||
|
||||
/**
|
||||
Default constructor. Initializes a new pool.
|
||||
*/
|
||||
PooledAllocator(int blockSize = BLOCKSIZE)
|
||||
{
|
||||
blocksize = blockSize;
|
||||
remaining = 0;
|
||||
base = NULL;
|
||||
loc = NULL;
|
||||
|
||||
usedMemory = 0;
|
||||
wastedMemory = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor. Frees all the memory allocated in this pool.
|
||||
*/
|
||||
~PooledAllocator()
|
||||
{
|
||||
void* prev;
|
||||
|
||||
while (base != NULL) {
|
||||
prev = *((void**) base); /* Get pointer to prev block. */
|
||||
::free(base);
|
||||
base = prev;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pointer to a piece of new memory of the given size in bytes
|
||||
* allocated from the pool.
|
||||
*/
|
||||
void* allocateMemory(int size)
|
||||
{
|
||||
int blockSize;
|
||||
|
||||
/* Round size up to a multiple of wordsize. The following expression
|
||||
only works for WORDSIZE that is a power of 2, by masking last bits of
|
||||
incremented size to zero.
|
||||
*/
|
||||
size = (size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);
|
||||
|
||||
/* Check whether a new block must be allocated. Note that the first word
|
||||
of a block is reserved for a pointer to the previous block.
|
||||
*/
|
||||
if (size > remaining) {
|
||||
|
||||
wastedMemory += remaining;
|
||||
|
||||
/* Allocate new storage. */
|
||||
blockSize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?
|
||||
size + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;
|
||||
|
||||
// use the standard C malloc to allocate memory
|
||||
void* m = ::malloc(blockSize);
|
||||
if (!m) {
|
||||
fprintf(stderr,"Failed to allocate memory.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Fill first word of new block with pointer to previous block. */
|
||||
((void**) m)[0] = base;
|
||||
base = m;
|
||||
|
||||
int shift = 0;
|
||||
//int shift = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);
|
||||
|
||||
remaining = blockSize - sizeof(void*) - shift;
|
||||
loc = ((char*)m + sizeof(void*) + shift);
|
||||
}
|
||||
void* rloc = loc;
|
||||
loc = (char*)loc + size;
|
||||
remaining -= size;
|
||||
|
||||
usedMemory += size;
|
||||
|
||||
return rloc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocates (using this pool) a generic type T.
|
||||
*
|
||||
* Params:
|
||||
* count = number of instances to allocate.
|
||||
* Returns: pointer (of type T*) to memory buffer
|
||||
*/
|
||||
template <typename T>
|
||||
T* allocate(size_t count = 1)
|
||||
{
|
||||
T* mem = (T*) this->allocateMemory((int)(sizeof(T)*count));
|
||||
return mem;
|
||||
}
|
||||
|
||||
private:
|
||||
PooledAllocator(const PooledAllocator &); // copy disabled
|
||||
PooledAllocator& operator=(const PooledAllocator &); // assign disabled
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_ALLOCATOR_H_
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
#ifndef OPENCV_FLANN_ANY_H_
|
||||
#define OPENCV_FLANN_ANY_H_
|
||||
/*
|
||||
* (C) Copyright Christopher Diggins 2005-2011
|
||||
* (C) Copyright Pablo Aguilar 2005
|
||||
* (C) Copyright Kevlin Henney 2001
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE_1_0.txt or copy at
|
||||
* http://www.boost.org/LICENSE_1_0.txt
|
||||
*
|
||||
* Adapted for FLANN by Marius Muja
|
||||
*/
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "defines.h"
|
||||
#include <stdexcept>
|
||||
#include <ostream>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
namespace anyimpl
|
||||
{
|
||||
|
||||
struct bad_any_cast
|
||||
{
|
||||
};
|
||||
|
||||
struct empty_any
|
||||
{
|
||||
};
|
||||
|
||||
inline std::ostream& operator <<(std::ostream& out, const empty_any&)
|
||||
{
|
||||
out << "[empty_any]";
|
||||
return out;
|
||||
}
|
||||
|
||||
struct base_any_policy
|
||||
{
|
||||
virtual void static_delete(void** x) = 0;
|
||||
virtual void copy_from_value(void const* src, void** dest) = 0;
|
||||
virtual void clone(void* const* src, void** dest) = 0;
|
||||
virtual void move(void* const* src, void** dest) = 0;
|
||||
virtual void* get_value(void** src) = 0;
|
||||
virtual const void* get_value(void* const * src) = 0;
|
||||
virtual ::size_t get_size() = 0;
|
||||
virtual const std::type_info& type() = 0;
|
||||
virtual void print(std::ostream& out, void* const* src) = 0;
|
||||
virtual ~base_any_policy() {}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct typed_base_any_policy : base_any_policy
|
||||
{
|
||||
virtual ::size_t get_size() CV_OVERRIDE { return sizeof(T); }
|
||||
virtual const std::type_info& type() CV_OVERRIDE { return typeid(T); }
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct small_any_policy CV_FINAL : typed_base_any_policy<T>
|
||||
{
|
||||
virtual void static_delete(void**) CV_OVERRIDE { }
|
||||
virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE
|
||||
{
|
||||
new (dest) T(* reinterpret_cast<T const*>(src));
|
||||
}
|
||||
virtual void clone(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; }
|
||||
virtual void move(void* const* src, void** dest) CV_OVERRIDE { *dest = *src; }
|
||||
virtual void* get_value(void** src) CV_OVERRIDE { return reinterpret_cast<void*>(src); }
|
||||
virtual const void* get_value(void* const * src) CV_OVERRIDE { return reinterpret_cast<const void*>(src); }
|
||||
virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(src); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct big_any_policy CV_FINAL : typed_base_any_policy<T>
|
||||
{
|
||||
virtual void static_delete(void** x) CV_OVERRIDE
|
||||
{
|
||||
if (* x) delete (* reinterpret_cast<T**>(x));
|
||||
*x = NULL;
|
||||
}
|
||||
virtual void copy_from_value(void const* src, void** dest) CV_OVERRIDE
|
||||
{
|
||||
*dest = new T(*reinterpret_cast<T const*>(src));
|
||||
}
|
||||
virtual void clone(void* const* src, void** dest) CV_OVERRIDE
|
||||
{
|
||||
*dest = new T(**reinterpret_cast<T* const*>(src));
|
||||
}
|
||||
virtual void move(void* const* src, void** dest) CV_OVERRIDE
|
||||
{
|
||||
(*reinterpret_cast<T**>(dest))->~T();
|
||||
**reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
|
||||
}
|
||||
virtual void* get_value(void** src) CV_OVERRIDE { return *src; }
|
||||
virtual const void* get_value(void* const * src) CV_OVERRIDE { return *src; }
|
||||
virtual void print(std::ostream& out, void* const* src) CV_OVERRIDE { out << *reinterpret_cast<T const*>(*src); }
|
||||
};
|
||||
|
||||
template<> inline void big_any_policy<flann_centers_init_t>::print(std::ostream& out, void* const* src)
|
||||
{
|
||||
out << int(*reinterpret_cast<flann_centers_init_t const*>(*src));
|
||||
}
|
||||
|
||||
template<> inline void big_any_policy<flann_algorithm_t>::print(std::ostream& out, void* const* src)
|
||||
{
|
||||
out << int(*reinterpret_cast<flann_algorithm_t const*>(*src));
|
||||
}
|
||||
|
||||
template<> inline void big_any_policy<cv::String>::print(std::ostream& out, void* const* src)
|
||||
{
|
||||
out << (*reinterpret_cast<cv::String const*>(*src)).c_str();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct choose_policy
|
||||
{
|
||||
typedef big_any_policy<T> type;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct choose_policy<T*>
|
||||
{
|
||||
typedef small_any_policy<T*> type;
|
||||
};
|
||||
|
||||
struct any;
|
||||
|
||||
/// Choosing the policy for an any type is illegal, but should never happen.
|
||||
/// This is designed to throw a compiler error.
|
||||
template<>
|
||||
struct choose_policy<any>
|
||||
{
|
||||
typedef void type;
|
||||
};
|
||||
|
||||
/// Specializations for small types.
|
||||
#define SMALL_POLICY(TYPE) \
|
||||
template<> \
|
||||
struct choose_policy<TYPE> { typedef small_any_policy<TYPE> type; \
|
||||
}
|
||||
|
||||
SMALL_POLICY(signed char);
|
||||
SMALL_POLICY(unsigned char);
|
||||
SMALL_POLICY(signed short);
|
||||
SMALL_POLICY(unsigned short);
|
||||
SMALL_POLICY(signed int);
|
||||
SMALL_POLICY(unsigned int);
|
||||
SMALL_POLICY(signed long);
|
||||
SMALL_POLICY(unsigned long);
|
||||
SMALL_POLICY(float);
|
||||
SMALL_POLICY(bool);
|
||||
|
||||
#undef SMALL_POLICY
|
||||
|
||||
template <typename T>
|
||||
class SinglePolicy
|
||||
{
|
||||
SinglePolicy();
|
||||
SinglePolicy(const SinglePolicy& other);
|
||||
SinglePolicy& operator=(const SinglePolicy& other);
|
||||
|
||||
public:
|
||||
static base_any_policy* get_policy();
|
||||
};
|
||||
|
||||
/// This function will return a different policy for each type.
|
||||
template <typename T>
|
||||
inline base_any_policy* SinglePolicy<T>::get_policy()
|
||||
{
|
||||
static typename choose_policy<T>::type policy;
|
||||
return &policy;
|
||||
}
|
||||
|
||||
} // namespace anyimpl
|
||||
|
||||
struct any
|
||||
{
|
||||
private:
|
||||
// fields
|
||||
anyimpl::base_any_policy* policy;
|
||||
void* object;
|
||||
|
||||
public:
|
||||
/// Initializing constructor.
|
||||
template <typename T>
|
||||
any(const T& x)
|
||||
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
|
||||
{
|
||||
assign(x);
|
||||
}
|
||||
|
||||
/// Empty constructor.
|
||||
any()
|
||||
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
|
||||
{ }
|
||||
|
||||
/// Special initializing constructor for string literals.
|
||||
any(const char* x)
|
||||
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
|
||||
{
|
||||
assign(x);
|
||||
}
|
||||
|
||||
/// Copy constructor.
|
||||
any(const any& x)
|
||||
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
|
||||
{
|
||||
assign(x);
|
||||
}
|
||||
|
||||
/// Destructor.
|
||||
~any()
|
||||
{
|
||||
policy->static_delete(&object);
|
||||
}
|
||||
|
||||
/// Assignment function from another any.
|
||||
any& assign(const any& x)
|
||||
{
|
||||
reset();
|
||||
policy = x.policy;
|
||||
policy->clone(&x.object, &object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assignment function.
|
||||
template <typename T>
|
||||
any& assign(const T& x)
|
||||
{
|
||||
reset();
|
||||
policy = anyimpl::SinglePolicy<T>::get_policy();
|
||||
policy->copy_from_value(&x, &object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assignment operator.
|
||||
template<typename T>
|
||||
any& operator=(const T& x)
|
||||
{
|
||||
return assign(x);
|
||||
}
|
||||
|
||||
/// Assignment operator. Template-based version above doesn't work as expected. We need regular assignment operator here.
|
||||
any& operator=(const any& x)
|
||||
{
|
||||
return assign(x);
|
||||
}
|
||||
|
||||
/// Assignment operator, specialed for literal strings.
|
||||
/// They have types like const char [6] which don't work as expected.
|
||||
any& operator=(const char* x)
|
||||
{
|
||||
return assign(x);
|
||||
}
|
||||
|
||||
/// Utility functions
|
||||
any& swap(any& x)
|
||||
{
|
||||
std::swap(policy, x.policy);
|
||||
std::swap(object, x.object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Cast operator. You can only cast to the original type.
|
||||
template<typename T>
|
||||
T& cast()
|
||||
{
|
||||
if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
|
||||
T* r = reinterpret_cast<T*>(policy->get_value(&object));
|
||||
return *r;
|
||||
}
|
||||
|
||||
/// Cast operator. You can only cast to the original type.
|
||||
template<typename T>
|
||||
const T& cast() const
|
||||
{
|
||||
if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
|
||||
const T* r = reinterpret_cast<const T*>(policy->get_value(&object));
|
||||
return *r;
|
||||
}
|
||||
|
||||
/// Returns true if the any contains no value.
|
||||
bool empty() const
|
||||
{
|
||||
return policy->type() == typeid(anyimpl::empty_any);
|
||||
}
|
||||
|
||||
/// Frees any allocated memory, and sets the value to NULL.
|
||||
void reset()
|
||||
{
|
||||
policy->static_delete(&object);
|
||||
policy = anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy();
|
||||
}
|
||||
|
||||
/// Returns true if the two types are the same.
|
||||
bool compatible(const any& x) const
|
||||
{
|
||||
return policy->type() == x.policy->type();
|
||||
}
|
||||
|
||||
/// Returns if the type is compatible with the policy
|
||||
template<typename T>
|
||||
bool has_type()
|
||||
{
|
||||
return policy->type() == typeid(T);
|
||||
}
|
||||
|
||||
const std::type_info& type() const
|
||||
{
|
||||
return policy->type();
|
||||
}
|
||||
|
||||
friend std::ostream& operator <<(std::ostream& out, const any& any_val);
|
||||
};
|
||||
|
||||
inline std::ostream& operator <<(std::ostream& out, const any& any_val)
|
||||
{
|
||||
any_val.policy->print(out,&any_val.object);
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif // OPENCV_FLANN_ANY_H_
|
||||
+594
@@ -0,0 +1,594 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
#ifndef OPENCV_FLANN_AUTOTUNED_INDEX_H_
|
||||
#define OPENCV_FLANN_AUTOTUNED_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "ground_truth.h"
|
||||
#include "index_testing.h"
|
||||
#include "sampling.h"
|
||||
#include "kdtree_index.h"
|
||||
#include "kdtree_single_index.h"
|
||||
#include "kmeans_index.h"
|
||||
#include "composite_index.h"
|
||||
#include "linear_index.h"
|
||||
#include "logger.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
template<typename Distance>
|
||||
NNIndex<Distance>* create_index_by_type(const Matrix<typename Distance::ElementType>& dataset, const IndexParams& params, const Distance& distance);
|
||||
|
||||
|
||||
struct AutotunedIndexParams : public IndexParams
|
||||
{
|
||||
AutotunedIndexParams(float target_precision = 0.8, float build_weight = 0.01, float memory_weight = 0, float sample_fraction = 0.1)
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_AUTOTUNED;
|
||||
// precision desired (used for autotuning, -1 otherwise)
|
||||
(*this)["target_precision"] = target_precision;
|
||||
// build tree time weighting factor
|
||||
(*this)["build_weight"] = build_weight;
|
||||
// index memory weighting factor
|
||||
(*this)["memory_weight"] = memory_weight;
|
||||
// what fraction of the dataset to use for autotuning
|
||||
(*this)["sample_fraction"] = sample_fraction;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
class AutotunedIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
AutotunedIndex(const Matrix<ElementType>& inputData, const IndexParams& params = AutotunedIndexParams(), Distance d = Distance()) :
|
||||
dataset_(inputData), distance_(d)
|
||||
{
|
||||
target_precision_ = get_param(params, "target_precision",0.8f);
|
||||
build_weight_ = get_param(params,"build_weight", 0.01f);
|
||||
memory_weight_ = get_param(params, "memory_weight", 0.0f);
|
||||
sample_fraction_ = get_param(params,"sample_fraction", 0.1f);
|
||||
bestIndex_ = NULL;
|
||||
speedup_ = 0;
|
||||
}
|
||||
|
||||
AutotunedIndex(const AutotunedIndex&);
|
||||
AutotunedIndex& operator=(const AutotunedIndex&);
|
||||
|
||||
virtual ~AutotunedIndex()
|
||||
{
|
||||
if (bestIndex_ != NULL) {
|
||||
delete bestIndex_;
|
||||
bestIndex_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method responsible with building the index.
|
||||
*/
|
||||
virtual void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
std::ostringstream stream;
|
||||
bestParams_ = estimateBuildParams();
|
||||
print_params(bestParams_, stream);
|
||||
Logger::info("----------------------------------------------------\n");
|
||||
Logger::info("Autotuned parameters:\n");
|
||||
Logger::info("%s", stream.str().c_str());
|
||||
Logger::info("----------------------------------------------------\n");
|
||||
|
||||
bestIndex_ = create_index_by_type(dataset_, bestParams_, distance_);
|
||||
bestIndex_->buildIndex();
|
||||
speedup_ = estimateSearchParams(bestSearchParams_);
|
||||
stream.str(std::string());
|
||||
print_params(bestSearchParams_, stream);
|
||||
Logger::info("----------------------------------------------------\n");
|
||||
Logger::info("Search parameters:\n");
|
||||
Logger::info("%s", stream.str().c_str());
|
||||
Logger::info("----------------------------------------------------\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the index to a stream
|
||||
*/
|
||||
virtual void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
save_value(stream, (int)bestIndex_->getType());
|
||||
bestIndex_->saveIndex(stream);
|
||||
save_value(stream, get_param<int>(bestSearchParams_, "checks"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the index from a stream
|
||||
*/
|
||||
virtual void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
int index_type;
|
||||
|
||||
load_value(stream, index_type);
|
||||
IndexParams params;
|
||||
params["algorithm"] = (flann_algorithm_t)index_type;
|
||||
bestIndex_ = create_index_by_type<Distance>(dataset_, params, distance_);
|
||||
bestIndex_->loadIndex(stream);
|
||||
int checks;
|
||||
load_value(stream, checks);
|
||||
bestSearchParams_["checks"] = checks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that searches for nearest-neighbors
|
||||
*/
|
||||
virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
int checks = get_param<int>(searchParams,"checks",FLANN_CHECKS_AUTOTUNED);
|
||||
if (checks == FLANN_CHECKS_AUTOTUNED) {
|
||||
bestIndex_->findNeighbors(result, vec, bestSearchParams_);
|
||||
}
|
||||
else {
|
||||
bestIndex_->findNeighbors(result, vec, searchParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return bestIndex_->getParameters();
|
||||
}
|
||||
|
||||
SearchParams getSearchParameters() const
|
||||
{
|
||||
return bestSearchParams_;
|
||||
}
|
||||
|
||||
float getSpeedup() const
|
||||
{
|
||||
return speedup_;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Number of features in this index.
|
||||
*/
|
||||
virtual size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return bestIndex_->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* The length of each vector in this index.
|
||||
*/
|
||||
virtual size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return bestIndex_->veclen();
|
||||
}
|
||||
|
||||
/**
|
||||
* The amount of memory (in bytes) this index uses.
|
||||
*/
|
||||
virtual int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return bestIndex_->usedMemory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Algorithm name
|
||||
*/
|
||||
virtual flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_AUTOTUNED;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct CostData
|
||||
{
|
||||
float searchTimeCost;
|
||||
float buildTimeCost;
|
||||
float memoryCost;
|
||||
float totalCost;
|
||||
IndexParams params;
|
||||
};
|
||||
|
||||
void evaluate_kmeans(CostData& cost)
|
||||
{
|
||||
StartStopTimer t;
|
||||
int checks;
|
||||
const int nn = 1;
|
||||
|
||||
Logger::info("KMeansTree using params: max_iterations=%d, branching=%d\n",
|
||||
get_param<int>(cost.params,"iterations"),
|
||||
get_param<int>(cost.params,"branching"));
|
||||
KMeansIndex<Distance> kmeans(sampledDataset_, cost.params, distance_);
|
||||
// measure index build time
|
||||
t.start();
|
||||
kmeans.buildIndex();
|
||||
t.stop();
|
||||
float buildTime = (float)t.value;
|
||||
|
||||
// measure search time
|
||||
float searchTime = test_index_precision(kmeans, sampledDataset_, testDataset_, gt_matches_, target_precision_, checks, distance_, nn);
|
||||
|
||||
float datasetMemory = float(sampledDataset_.rows * sampledDataset_.cols * sizeof(float));
|
||||
cost.memoryCost = (kmeans.usedMemory() + datasetMemory) / datasetMemory;
|
||||
cost.searchTimeCost = searchTime;
|
||||
cost.buildTimeCost = buildTime;
|
||||
Logger::info("KMeansTree buildTime=%g, searchTime=%g, build_weight=%g\n", buildTime, searchTime, build_weight_);
|
||||
}
|
||||
|
||||
|
||||
void evaluate_kdtree(CostData& cost)
|
||||
{
|
||||
StartStopTimer t;
|
||||
int checks;
|
||||
const int nn = 1;
|
||||
|
||||
Logger::info("KDTree using params: trees=%d\n", get_param<int>(cost.params,"trees"));
|
||||
KDTreeIndex<Distance> kdtree(sampledDataset_, cost.params, distance_);
|
||||
|
||||
t.start();
|
||||
kdtree.buildIndex();
|
||||
t.stop();
|
||||
float buildTime = (float)t.value;
|
||||
|
||||
//measure search time
|
||||
float searchTime = test_index_precision(kdtree, sampledDataset_, testDataset_, gt_matches_, target_precision_, checks, distance_, nn);
|
||||
|
||||
float datasetMemory = float(sampledDataset_.rows * sampledDataset_.cols * sizeof(float));
|
||||
cost.memoryCost = (kdtree.usedMemory() + datasetMemory) / datasetMemory;
|
||||
cost.searchTimeCost = searchTime;
|
||||
cost.buildTimeCost = buildTime;
|
||||
Logger::info("KDTree buildTime=%g, searchTime=%g\n", buildTime, searchTime);
|
||||
}
|
||||
|
||||
|
||||
// struct KMeansSimpleDownhillFunctor {
|
||||
//
|
||||
// Autotune& autotuner;
|
||||
// KMeansSimpleDownhillFunctor(Autotune& autotuner_) : autotuner(autotuner_) {}
|
||||
//
|
||||
// float operator()(int* params) {
|
||||
//
|
||||
// float maxFloat = numeric_limits<float>::max();
|
||||
//
|
||||
// if (params[0]<2) return maxFloat;
|
||||
// if (params[1]<0) return maxFloat;
|
||||
//
|
||||
// CostData c;
|
||||
// c.params["algorithm"] = KMEANS;
|
||||
// c.params["centers-init"] = CENTERS_RANDOM;
|
||||
// c.params["branching"] = params[0];
|
||||
// c.params["max-iterations"] = params[1];
|
||||
//
|
||||
// autotuner.evaluate_kmeans(c);
|
||||
//
|
||||
// return c.timeCost;
|
||||
//
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// struct KDTreeSimpleDownhillFunctor {
|
||||
//
|
||||
// Autotune& autotuner;
|
||||
// KDTreeSimpleDownhillFunctor(Autotune& autotuner_) : autotuner(autotuner_) {}
|
||||
//
|
||||
// float operator()(int* params) {
|
||||
// float maxFloat = numeric_limits<float>::max();
|
||||
//
|
||||
// if (params[0]<1) return maxFloat;
|
||||
//
|
||||
// CostData c;
|
||||
// c.params["algorithm"] = KDTREE;
|
||||
// c.params["trees"] = params[0];
|
||||
//
|
||||
// autotuner.evaluate_kdtree(c);
|
||||
//
|
||||
// return c.timeCost;
|
||||
//
|
||||
// }
|
||||
// };
|
||||
|
||||
|
||||
|
||||
void optimizeKMeans(std::vector<CostData>& costs)
|
||||
{
|
||||
Logger::info("KMEANS, Step 1: Exploring parameter space\n");
|
||||
|
||||
// explore kmeans parameters space using combinations of the parameters below
|
||||
int maxIterations[] = { 1, 5, 10, 15 };
|
||||
int branchingFactors[] = { 16, 32, 64, 128, 256 };
|
||||
|
||||
int kmeansParamSpaceSize = FLANN_ARRAY_LEN(maxIterations) * FLANN_ARRAY_LEN(branchingFactors);
|
||||
costs.reserve(costs.size() + kmeansParamSpaceSize);
|
||||
|
||||
// evaluate kmeans for all parameter combinations
|
||||
for (size_t i = 0; i < FLANN_ARRAY_LEN(maxIterations); ++i) {
|
||||
for (size_t j = 0; j < FLANN_ARRAY_LEN(branchingFactors); ++j) {
|
||||
CostData cost;
|
||||
cost.params["algorithm"] = FLANN_INDEX_KMEANS;
|
||||
cost.params["centers_init"] = FLANN_CENTERS_RANDOM;
|
||||
cost.params["iterations"] = maxIterations[i];
|
||||
cost.params["branching"] = branchingFactors[j];
|
||||
|
||||
evaluate_kmeans(cost);
|
||||
costs.push_back(cost);
|
||||
}
|
||||
}
|
||||
|
||||
// Logger::info("KMEANS, Step 2: simplex-downhill optimization\n");
|
||||
//
|
||||
// const int n = 2;
|
||||
// // choose initial simplex points as the best parameters so far
|
||||
// int kmeansNMPoints[n*(n+1)];
|
||||
// float kmeansVals[n+1];
|
||||
// for (int i=0;i<n+1;++i) {
|
||||
// kmeansNMPoints[i*n] = (int)kmeansCosts[i].params["branching"];
|
||||
// kmeansNMPoints[i*n+1] = (int)kmeansCosts[i].params["max-iterations"];
|
||||
// kmeansVals[i] = kmeansCosts[i].timeCost;
|
||||
// }
|
||||
// KMeansSimpleDownhillFunctor kmeans_cost_func(*this);
|
||||
// // run optimization
|
||||
// optimizeSimplexDownhill(kmeansNMPoints,n,kmeans_cost_func,kmeansVals);
|
||||
// // store results
|
||||
// for (int i=0;i<n+1;++i) {
|
||||
// kmeansCosts[i].params["branching"] = kmeansNMPoints[i*2];
|
||||
// kmeansCosts[i].params["max-iterations"] = kmeansNMPoints[i*2+1];
|
||||
// kmeansCosts[i].timeCost = kmeansVals[i];
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
void optimizeKDTree(std::vector<CostData>& costs)
|
||||
{
|
||||
Logger::info("KD-TREE, Step 1: Exploring parameter space\n");
|
||||
|
||||
// explore kd-tree parameters space using the parameters below
|
||||
int testTrees[] = { 1, 4, 8, 16, 32 };
|
||||
|
||||
// evaluate kdtree for all parameter combinations
|
||||
for (size_t i = 0; i < FLANN_ARRAY_LEN(testTrees); ++i) {
|
||||
CostData cost;
|
||||
cost.params["algorithm"] = FLANN_INDEX_KDTREE;
|
||||
cost.params["trees"] = testTrees[i];
|
||||
|
||||
evaluate_kdtree(cost);
|
||||
costs.push_back(cost);
|
||||
}
|
||||
|
||||
// Logger::info("KD-TREE, Step 2: simplex-downhill optimization\n");
|
||||
//
|
||||
// const int n = 1;
|
||||
// // choose initial simplex points as the best parameters so far
|
||||
// int kdtreeNMPoints[n*(n+1)];
|
||||
// float kdtreeVals[n+1];
|
||||
// for (int i=0;i<n+1;++i) {
|
||||
// kdtreeNMPoints[i] = (int)kdtreeCosts[i].params["trees"];
|
||||
// kdtreeVals[i] = kdtreeCosts[i].timeCost;
|
||||
// }
|
||||
// KDTreeSimpleDownhillFunctor kdtree_cost_func(*this);
|
||||
// // run optimization
|
||||
// optimizeSimplexDownhill(kdtreeNMPoints,n,kdtree_cost_func,kdtreeVals);
|
||||
// // store results
|
||||
// for (int i=0;i<n+1;++i) {
|
||||
// kdtreeCosts[i].params["trees"] = kdtreeNMPoints[i];
|
||||
// kdtreeCosts[i].timeCost = kdtreeVals[i];
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* Chooses the best nearest-neighbor algorithm and estimates the optimal
|
||||
* parameters to use when building the index (for a given precision).
|
||||
* Returns a dictionary with the optimal parameters.
|
||||
*/
|
||||
IndexParams estimateBuildParams()
|
||||
{
|
||||
std::vector<CostData> costs;
|
||||
|
||||
int sampleSize = int(sample_fraction_ * dataset_.rows);
|
||||
int testSampleSize = std::min(sampleSize / 10, 1000);
|
||||
|
||||
Logger::info("Entering autotuning, dataset size: %d, sampleSize: %d, testSampleSize: %d, target precision: %g\n", dataset_.rows, sampleSize, testSampleSize, target_precision_);
|
||||
|
||||
// For a very small dataset, it makes no sense to build any fancy index, just
|
||||
// use linear search
|
||||
if (testSampleSize < 10) {
|
||||
Logger::info("Choosing linear, dataset too small\n");
|
||||
return LinearIndexParams();
|
||||
}
|
||||
|
||||
// We use a fraction of the original dataset to speedup the autotune algorithm
|
||||
sampledDataset_ = random_sample(dataset_, sampleSize);
|
||||
// We use a cross-validation approach, first we sample a testset from the dataset
|
||||
testDataset_ = random_sample(sampledDataset_, testSampleSize, true);
|
||||
|
||||
// We compute the ground truth using linear search
|
||||
Logger::info("Computing ground truth... \n");
|
||||
gt_matches_ = Matrix<int>(new int[testDataset_.rows], testDataset_.rows, 1);
|
||||
StartStopTimer t;
|
||||
t.start();
|
||||
compute_ground_truth<Distance>(sampledDataset_, testDataset_, gt_matches_, 0, distance_);
|
||||
t.stop();
|
||||
|
||||
CostData linear_cost;
|
||||
linear_cost.searchTimeCost = (float)t.value;
|
||||
linear_cost.buildTimeCost = 0;
|
||||
linear_cost.memoryCost = 0;
|
||||
linear_cost.params["algorithm"] = FLANN_INDEX_LINEAR;
|
||||
|
||||
costs.push_back(linear_cost);
|
||||
|
||||
// Start parameter autotune process
|
||||
Logger::info("Autotuning parameters...\n");
|
||||
|
||||
optimizeKMeans(costs);
|
||||
optimizeKDTree(costs);
|
||||
|
||||
float bestTimeCost = costs[0].searchTimeCost;
|
||||
for (size_t i = 0; i < costs.size(); ++i) {
|
||||
float timeCost = costs[i].buildTimeCost * build_weight_ + costs[i].searchTimeCost;
|
||||
if (timeCost < bestTimeCost) {
|
||||
bestTimeCost = timeCost;
|
||||
}
|
||||
}
|
||||
|
||||
float bestCost = costs[0].searchTimeCost / bestTimeCost;
|
||||
IndexParams bestParams = costs[0].params;
|
||||
if (bestTimeCost > 0) {
|
||||
for (size_t i = 0; i < costs.size(); ++i) {
|
||||
float crtCost = (costs[i].buildTimeCost * build_weight_ + costs[i].searchTimeCost) / bestTimeCost +
|
||||
memory_weight_ * costs[i].memoryCost;
|
||||
if (crtCost < bestCost) {
|
||||
bestCost = crtCost;
|
||||
bestParams = costs[i].params;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete[] gt_matches_.data;
|
||||
delete[] testDataset_.data;
|
||||
delete[] sampledDataset_.data;
|
||||
|
||||
return bestParams;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Estimates the search time parameters needed to get the desired precision.
|
||||
* Precondition: the index is built
|
||||
* Postcondition: the searchParams will have the optimum params set, also the speedup obtained over linear search.
|
||||
*/
|
||||
float estimateSearchParams(SearchParams& searchParams)
|
||||
{
|
||||
const int nn = 1;
|
||||
const size_t SAMPLE_COUNT = 1000;
|
||||
|
||||
CV_Assert(bestIndex_ != NULL && "Requires a valid index"); // must have a valid index
|
||||
|
||||
float speedup = 0;
|
||||
|
||||
int samples = (int)std::min(dataset_.rows / 10, SAMPLE_COUNT);
|
||||
if (samples > 0) {
|
||||
Matrix<ElementType> testDataset = random_sample(dataset_, samples);
|
||||
|
||||
Logger::info("Computing ground truth\n");
|
||||
|
||||
// we need to compute the ground truth first
|
||||
Matrix<int> gt_matches(new int[testDataset.rows], testDataset.rows, 1);
|
||||
StartStopTimer t;
|
||||
t.start();
|
||||
compute_ground_truth<Distance>(dataset_, testDataset, gt_matches, 1, distance_);
|
||||
t.stop();
|
||||
float linear = (float)t.value;
|
||||
|
||||
int checks;
|
||||
Logger::info("Estimating number of checks\n");
|
||||
|
||||
float searchTime;
|
||||
float cb_index;
|
||||
if (bestIndex_->getType() == FLANN_INDEX_KMEANS) {
|
||||
Logger::info("KMeans algorithm, estimating cluster border factor\n");
|
||||
KMeansIndex<Distance>* kmeans = (KMeansIndex<Distance>*)bestIndex_;
|
||||
float bestSearchTime = -1;
|
||||
float best_cb_index = -1;
|
||||
int best_checks = -1;
|
||||
for (cb_index = 0; cb_index < 1.1f; cb_index += 0.2f) {
|
||||
kmeans->set_cb_index(cb_index);
|
||||
searchTime = test_index_precision(*kmeans, dataset_, testDataset, gt_matches, target_precision_, checks, distance_, nn, 1);
|
||||
if ((searchTime < bestSearchTime) || (bestSearchTime == -1)) {
|
||||
bestSearchTime = searchTime;
|
||||
best_cb_index = cb_index;
|
||||
best_checks = checks;
|
||||
}
|
||||
}
|
||||
searchTime = bestSearchTime;
|
||||
cb_index = best_cb_index;
|
||||
checks = best_checks;
|
||||
|
||||
kmeans->set_cb_index(best_cb_index);
|
||||
Logger::info("Optimum cb_index: %g\n", cb_index);
|
||||
bestParams_["cb_index"] = cb_index;
|
||||
}
|
||||
else {
|
||||
searchTime = test_index_precision(*bestIndex_, dataset_, testDataset, gt_matches, target_precision_, checks, distance_, nn, 1);
|
||||
}
|
||||
|
||||
Logger::info("Required number of checks: %d \n", checks);
|
||||
searchParams["checks"] = checks;
|
||||
|
||||
speedup = linear / searchTime;
|
||||
|
||||
delete[] gt_matches.data;
|
||||
delete[] testDataset.data;
|
||||
}
|
||||
|
||||
return speedup;
|
||||
}
|
||||
|
||||
private:
|
||||
NNIndex<Distance>* bestIndex_;
|
||||
|
||||
IndexParams bestParams_;
|
||||
SearchParams bestSearchParams_;
|
||||
|
||||
Matrix<ElementType> sampledDataset_;
|
||||
Matrix<ElementType> testDataset_;
|
||||
Matrix<int> gt_matches_;
|
||||
|
||||
float speedup_;
|
||||
|
||||
/**
|
||||
* The dataset used by this index
|
||||
*/
|
||||
const Matrix<ElementType> dataset_;
|
||||
|
||||
/**
|
||||
* Index parameters
|
||||
*/
|
||||
float target_precision_;
|
||||
float build_weight_;
|
||||
float memory_weight_;
|
||||
float sample_fraction_;
|
||||
|
||||
Distance distance_;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_AUTOTUNED_INDEX_H_ */
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
|
||||
#define OPENCV_FLANN_COMPOSITE_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "kdtree_index.h"
|
||||
#include "kmeans_index.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
* Index parameters for the CompositeIndex.
|
||||
*/
|
||||
struct CompositeIndexParams : public IndexParams
|
||||
{
|
||||
CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
|
||||
flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_KMEANS;
|
||||
// number of randomized trees to use (for kdtree)
|
||||
(*this)["trees"] = trees;
|
||||
// branching factor
|
||||
(*this)["branching"] = branching;
|
||||
// max iterations to perform in one kmeans clustering (kmeans tree)
|
||||
(*this)["iterations"] = iterations;
|
||||
// algorithm used for picking the initial cluster centers for kmeans tree
|
||||
(*this)["centers_init"] = centers_init;
|
||||
// cluster boundary index. Used when searching the kmeans tree
|
||||
(*this)["cb_index"] = cb_index;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* This index builds a kd-tree index and a k-means index and performs nearest
|
||||
* neighbour search both indexes. This gives a slight boost in search performance
|
||||
* as some of the neighbours that are missed by one index are found by the other.
|
||||
*/
|
||||
template <typename Distance>
|
||||
class CompositeIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
/**
|
||||
* Index constructor
|
||||
* @param inputData dataset containing the points to index
|
||||
* @param params Index parameters
|
||||
* @param d Distance functor
|
||||
* @return
|
||||
*/
|
||||
CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
|
||||
Distance d = Distance()) : index_params_(params)
|
||||
{
|
||||
kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
|
||||
kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
|
||||
|
||||
}
|
||||
|
||||
CompositeIndex(const CompositeIndex&);
|
||||
CompositeIndex& operator=(const CompositeIndex&);
|
||||
|
||||
virtual ~CompositeIndex()
|
||||
{
|
||||
delete kdtree_index_;
|
||||
delete kmeans_index_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The index type
|
||||
*/
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_COMPOSITE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Size of the index
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return kdtree_index_->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The dimensionality of the features in this index.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return kdtree_index_->veclen();
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The amount of memory (in bytes) used by the index.
|
||||
*/
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Builds the index
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
Logger::info("Building kmeans tree...\n");
|
||||
kmeans_index_->buildIndex();
|
||||
Logger::info("Building kdtree tree...\n");
|
||||
kdtree_index_->buildIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Saves the index to a stream
|
||||
* \param stream The stream to save the index to
|
||||
*/
|
||||
void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
kmeans_index_->saveIndex(stream);
|
||||
kdtree_index_->saveIndex(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Loads the index from a stream
|
||||
* \param stream The stream from which the index is loaded
|
||||
*/
|
||||
void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
kmeans_index_->loadIndex(stream);
|
||||
kdtree_index_->loadIndex(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The index parameters
|
||||
*/
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return index_params_;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Method that searches for nearest-neighbours
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
kmeans_index_->findNeighbors(result, vec, searchParams);
|
||||
kdtree_index_->findNeighbors(result, vec, searchParams);
|
||||
}
|
||||
|
||||
private:
|
||||
/** The k-means index */
|
||||
KMeansIndex<Distance>* kmeans_index_;
|
||||
|
||||
/** The kd-tree index */
|
||||
KDTreeIndex<Distance>* kdtree_index_;
|
||||
|
||||
/** The index parameters */
|
||||
const IndexParams index_params_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_COMPOSITE_INDEX_H_
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2011 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2011 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_CONFIG_H_
|
||||
#define OPENCV_FLANN_CONFIG_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#ifdef FLANN_VERSION_
|
||||
#undef FLANN_VERSION_
|
||||
#endif
|
||||
#define FLANN_VERSION_ "1.6.10"
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_CONFIG_H_ */
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2011 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2011 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_DEFINES_H_
|
||||
#define OPENCV_FLANN_DEFINES_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef FLANN_EXPORT
|
||||
#undef FLANN_EXPORT
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
/* win32 dll export/import directives */
|
||||
#ifdef FLANN_EXPORTS
|
||||
#define FLANN_EXPORT __declspec(dllexport)
|
||||
#elif defined(FLANN_STATIC)
|
||||
#define FLANN_EXPORT
|
||||
#else
|
||||
#define FLANN_EXPORT __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
/* unix needs nothing */
|
||||
#define FLANN_EXPORT
|
||||
#endif
|
||||
|
||||
|
||||
#undef FLANN_PLATFORM_32_BIT
|
||||
#undef FLANN_PLATFORM_64_BIT
|
||||
#if defined __amd64__ || defined __x86_64__ || defined _WIN64 || defined _M_X64
|
||||
#define FLANN_PLATFORM_64_BIT
|
||||
#else
|
||||
#define FLANN_PLATFORM_32_BIT
|
||||
#endif
|
||||
|
||||
|
||||
#undef FLANN_ARRAY_LEN
|
||||
#define FLANN_ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
|
||||
|
||||
namespace cvflann {
|
||||
|
||||
/* Nearest neighbour index algorithms */
|
||||
enum flann_algorithm_t
|
||||
{
|
||||
FLANN_INDEX_LINEAR = 0,
|
||||
FLANN_INDEX_KDTREE = 1,
|
||||
FLANN_INDEX_KMEANS = 2,
|
||||
FLANN_INDEX_COMPOSITE = 3,
|
||||
FLANN_INDEX_KDTREE_SINGLE = 4,
|
||||
FLANN_INDEX_HIERARCHICAL = 5,
|
||||
FLANN_INDEX_LSH = 6,
|
||||
FLANN_INDEX_SAVED = 254,
|
||||
FLANN_INDEX_AUTOTUNED = 255,
|
||||
|
||||
// deprecated constants, should use the FLANN_INDEX_* ones instead
|
||||
LINEAR = 0,
|
||||
KDTREE = 1,
|
||||
KMEANS = 2,
|
||||
COMPOSITE = 3,
|
||||
KDTREE_SINGLE = 4,
|
||||
SAVED = 254,
|
||||
AUTOTUNED = 255
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum flann_centers_init_t
|
||||
{
|
||||
FLANN_CENTERS_RANDOM = 0,
|
||||
FLANN_CENTERS_GONZALES = 1,
|
||||
FLANN_CENTERS_KMEANSPP = 2,
|
||||
FLANN_CENTERS_GROUPWISE = 3,
|
||||
|
||||
// deprecated constants, should use the FLANN_CENTERS_* ones instead
|
||||
CENTERS_RANDOM = 0,
|
||||
CENTERS_GONZALES = 1,
|
||||
CENTERS_KMEANSPP = 2
|
||||
};
|
||||
|
||||
enum flann_log_level_t
|
||||
{
|
||||
FLANN_LOG_NONE = 0,
|
||||
FLANN_LOG_FATAL = 1,
|
||||
FLANN_LOG_ERROR = 2,
|
||||
FLANN_LOG_WARN = 3,
|
||||
FLANN_LOG_INFO = 4
|
||||
};
|
||||
|
||||
enum flann_distance_t
|
||||
{
|
||||
FLANN_DIST_EUCLIDEAN = 1,
|
||||
FLANN_DIST_L2 = 1,
|
||||
FLANN_DIST_MANHATTAN = 2,
|
||||
FLANN_DIST_L1 = 2,
|
||||
FLANN_DIST_MINKOWSKI = 3,
|
||||
FLANN_DIST_MAX = 4,
|
||||
FLANN_DIST_HIST_INTERSECT = 5,
|
||||
FLANN_DIST_HELLINGER = 6,
|
||||
FLANN_DIST_CHI_SQUARE = 7,
|
||||
FLANN_DIST_CS = 7,
|
||||
FLANN_DIST_KULLBACK_LEIBLER = 8,
|
||||
FLANN_DIST_KL = 8,
|
||||
FLANN_DIST_HAMMING = 9,
|
||||
FLANN_DIST_DNAMMING = 10,
|
||||
|
||||
// deprecated constants, should use the FLANN_DIST_* ones instead
|
||||
EUCLIDEAN = 1,
|
||||
MANHATTAN = 2,
|
||||
MINKOWSKI = 3,
|
||||
MAX_DIST = 4,
|
||||
HIST_INTERSECT = 5,
|
||||
HELLINGER = 6,
|
||||
CS = 7,
|
||||
KL = 8,
|
||||
KULLBACK_LEIBLER = 8
|
||||
};
|
||||
|
||||
enum flann_datatype_t
|
||||
{
|
||||
FLANN_INT8 = 0,
|
||||
FLANN_INT16 = 1,
|
||||
FLANN_INT32 = 2,
|
||||
FLANN_INT64 = 3,
|
||||
FLANN_UINT8 = 4,
|
||||
FLANN_UINT16 = 5,
|
||||
FLANN_UINT32 = 6,
|
||||
FLANN_UINT64 = 7,
|
||||
FLANN_FLOAT32 = 8,
|
||||
FLANN_FLOAT64 = 9
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
FLANN_CHECKS_UNLIMITED = -1,
|
||||
FLANN_CHECKS_AUTOTUNED = -2
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_DEFINES_H_ */
|
||||
+1292
File diff suppressed because it is too large
Load Diff
+16
@@ -0,0 +1,16 @@
|
||||
|
||||
#ifndef OPENCV_FLANN_DUMMY_H_
|
||||
#define OPENCV_FLANN_DUMMY_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
CV_DEPRECATED inline void dummyfunc() {}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_DUMMY_H_ */
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
/***********************************************************************
|
||||
* Author: Vincent Rabaud
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_DYNAMIC_BITSET_H_
|
||||
#define OPENCV_FLANN_DYNAMIC_BITSET_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#ifndef FLANN_USE_BOOST
|
||||
# define FLANN_USE_BOOST 0
|
||||
#endif
|
||||
//#define FLANN_USE_BOOST 1
|
||||
#if FLANN_USE_BOOST
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
typedef boost::dynamic_bitset<> DynamicBitset;
|
||||
#else
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "dist.h"
|
||||
|
||||
namespace cvflann {
|
||||
|
||||
/** Class re-implementing the boost version of it
|
||||
* This helps not depending on boost, it also does not do the bound checks
|
||||
* and has a way to reset a block for speed
|
||||
*/
|
||||
class DynamicBitset
|
||||
{
|
||||
public:
|
||||
/** default constructor
|
||||
*/
|
||||
DynamicBitset() : size_(0)
|
||||
{
|
||||
}
|
||||
|
||||
/** only constructor we use in our code
|
||||
* @param sz the size of the bitset (in bits)
|
||||
*/
|
||||
DynamicBitset(size_t sz)
|
||||
{
|
||||
resize(sz);
|
||||
reset();
|
||||
}
|
||||
|
||||
/** Sets all the bits to 0
|
||||
*/
|
||||
void clear()
|
||||
{
|
||||
std::fill(bitset_.begin(), bitset_.end(), 0);
|
||||
}
|
||||
|
||||
/** @brief checks if the bitset is empty
|
||||
* @return true if the bitset is empty
|
||||
*/
|
||||
bool empty() const
|
||||
{
|
||||
return bitset_.empty();
|
||||
}
|
||||
|
||||
/** set all the bits to 0
|
||||
*/
|
||||
void reset()
|
||||
{
|
||||
std::fill(bitset_.begin(), bitset_.end(), 0);
|
||||
}
|
||||
|
||||
/** @brief set one bit to 0
|
||||
* @param index
|
||||
*/
|
||||
void reset(size_t index)
|
||||
{
|
||||
bitset_[index / cell_bit_size_] &= ~(size_t(1) << (index % cell_bit_size_));
|
||||
}
|
||||
|
||||
/** @brief sets a specific bit to 0, and more bits too
|
||||
* This function is useful when resetting a given set of bits so that the
|
||||
* whole bitset ends up being 0: if that's the case, we don't care about setting
|
||||
* other bits to 0
|
||||
* @param index
|
||||
*/
|
||||
void reset_block(size_t index)
|
||||
{
|
||||
bitset_[index / cell_bit_size_] = 0;
|
||||
}
|
||||
|
||||
/** resize the bitset so that it contains at least sz bits
|
||||
* @param sz
|
||||
*/
|
||||
void resize(size_t sz)
|
||||
{
|
||||
size_ = sz;
|
||||
bitset_.resize(sz / cell_bit_size_ + 1);
|
||||
}
|
||||
|
||||
/** set a bit to true
|
||||
* @param index the index of the bit to set to 1
|
||||
*/
|
||||
void set(size_t index)
|
||||
{
|
||||
bitset_[index / cell_bit_size_] |= size_t(1) << (index % cell_bit_size_);
|
||||
}
|
||||
|
||||
/** gives the number of contained bits
|
||||
*/
|
||||
size_t size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/** check if a bit is set
|
||||
* @param index the index of the bit to check
|
||||
* @return true if the bit is set
|
||||
*/
|
||||
bool test(size_t index) const
|
||||
{
|
||||
return (bitset_[index / cell_bit_size_] & (size_t(1) << (index % cell_bit_size_))) != 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<size_t> bitset_;
|
||||
size_t size_;
|
||||
static const unsigned int cell_bit_size_ = CHAR_BIT * sizeof(size_t);
|
||||
};
|
||||
|
||||
} // namespace cvflann
|
||||
|
||||
#endif
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif // OPENCV_FLANN_DYNAMIC_BITSET_H_
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*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.
|
||||
// Copyright (C) 2013, OpenCV Foundation, 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*/
|
||||
|
||||
#ifdef __OPENCV_BUILD
|
||||
#error this is a compatibility header which should not be used inside the OpenCV library
|
||||
#endif
|
||||
|
||||
#include "opencv2/flann.hpp"
|
||||
+312
@@ -0,0 +1,312 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_BASE_HPP_
|
||||
#define OPENCV_FLANN_BASE_HPP_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
|
||||
#include "general.h"
|
||||
#include "matrix.h"
|
||||
#include "params.h"
|
||||
#include "saving.h"
|
||||
|
||||
#include "all_indices.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
class FILEScopeGuard {
|
||||
|
||||
public:
|
||||
explicit FILEScopeGuard(FILE* file) {
|
||||
file_ = file;
|
||||
};
|
||||
|
||||
~FILEScopeGuard() {
|
||||
fclose(file_);
|
||||
};
|
||||
|
||||
private:
|
||||
FILE* file_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Sets the log level used for all flann functions
|
||||
* @param level Verbosity level
|
||||
*/
|
||||
inline void log_verbosity(int level)
|
||||
{
|
||||
if (level >= 0) {
|
||||
Logger::setLevel(level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (Deprecated) Index parameters for creating a saved index.
|
||||
*/
|
||||
struct SavedIndexParams : public IndexParams
|
||||
{
|
||||
SavedIndexParams(cv::String filename)
|
||||
{
|
||||
(* this)["algorithm"] = FLANN_INDEX_SAVED;
|
||||
(*this)["filename"] = filename;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Distance>
|
||||
NNIndex<Distance>* load_saved_index(const Matrix<typename Distance::ElementType>& dataset, const cv::String& filename, Distance distance)
|
||||
{
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
|
||||
FILE* fin = fopen(filename.c_str(), "rb");
|
||||
if (fin == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
FILEScopeGuard fscgd(fin);
|
||||
|
||||
IndexHeader header = load_header(fin);
|
||||
if (header.data_type != Datatype<ElementType>::type()) {
|
||||
FLANN_THROW(cv::Error::StsError, "Datatype of saved index is different than of the one to be created.");
|
||||
}
|
||||
if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) {
|
||||
FLANN_THROW(cv::Error::StsError, "The index saved belongs to a different dataset");
|
||||
}
|
||||
|
||||
IndexParams params;
|
||||
params["algorithm"] = header.index_type;
|
||||
NNIndex<Distance>* nnIndex = create_index_by_type<Distance>(dataset, params, distance);
|
||||
nnIndex->loadIndex(fin);
|
||||
|
||||
return nnIndex;
|
||||
}
|
||||
|
||||
|
||||
template<typename Distance>
|
||||
class Index : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
Index(const Matrix<ElementType>& features, const IndexParams& params, Distance distance = Distance() )
|
||||
:index_params_(params)
|
||||
{
|
||||
flann_algorithm_t index_type = get_param<flann_algorithm_t>(params,"algorithm");
|
||||
loaded_ = false;
|
||||
|
||||
if (index_type == FLANN_INDEX_SAVED) {
|
||||
nnIndex_ = load_saved_index<Distance>(features, get_param<cv::String>(params,"filename"), distance);
|
||||
loaded_ = true;
|
||||
}
|
||||
else {
|
||||
nnIndex_ = create_index_by_type<Distance>(features, params, distance);
|
||||
}
|
||||
}
|
||||
|
||||
~Index()
|
||||
{
|
||||
delete nnIndex_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the index.
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
if (!loaded_) {
|
||||
nnIndex_->buildIndex();
|
||||
}
|
||||
}
|
||||
|
||||
void save(cv::String filename)
|
||||
{
|
||||
FILE* fout = fopen(filename.c_str(), "wb");
|
||||
if (fout == NULL) {
|
||||
FLANN_THROW(cv::Error::StsError, "Cannot open file");
|
||||
}
|
||||
save_header(fout, *nnIndex_);
|
||||
saveIndex(fout);
|
||||
fclose(fout);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Saves the index to a stream
|
||||
* \param stream The stream to save the index to
|
||||
*/
|
||||
virtual void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
nnIndex_->saveIndex(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Loads the index from a stream
|
||||
* \param stream The stream from which the index is loaded
|
||||
*/
|
||||
virtual void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
nnIndex_->loadIndex(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns number of features in this index.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->veclen();
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The dimensionality of the features in this index.
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The index type (kdtree, kmeans,...)
|
||||
*/
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->getType();
|
||||
}
|
||||
|
||||
/**
|
||||
* \returns The amount of memory (in bytes) used by the index.
|
||||
*/
|
||||
virtual int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->usedMemory();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \returns The index parameters
|
||||
*/
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->getParameters();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Perform k-nearest neighbor search
|
||||
* \param[in] queries The query points for which to find the nearest neighbors
|
||||
* \param[out] indices The indices of the nearest neighbors found
|
||||
* \param[out] dists Distances to the nearest neighbors found
|
||||
* \param[in] knn Number of nearest neighbors to return
|
||||
* \param[in] params Search parameters
|
||||
*/
|
||||
void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
|
||||
{
|
||||
nnIndex_->knnSearch(queries, indices, dists, knn, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Perform radius search
|
||||
* \param[in] query The query point
|
||||
* \param[out] indices The indinces of the neighbors found within the given radius
|
||||
* \param[out] dists The distances to the nearest neighbors found
|
||||
* \param[in] radius The radius used for search
|
||||
* \param[in] params Search parameters
|
||||
* \returns Number of neighbors found
|
||||
*/
|
||||
int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params) CV_OVERRIDE
|
||||
{
|
||||
return nnIndex_->radiusSearch(query, indices, dists, radius, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Method that searches for nearest-neighbours
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
nnIndex_->findNeighbors(result, vec, searchParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns actual index
|
||||
*/
|
||||
CV_DEPRECATED NNIndex<Distance>* getIndex()
|
||||
{
|
||||
return nnIndex_;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns index parameters.
|
||||
* \deprecated use getParameters() instead.
|
||||
*/
|
||||
CV_DEPRECATED const IndexParams* getIndexParameters()
|
||||
{
|
||||
return &index_params_;
|
||||
}
|
||||
|
||||
private:
|
||||
/** Pointer to actual index class */
|
||||
NNIndex<Distance>* nnIndex_;
|
||||
/** Indices if the index was loaded from a file */
|
||||
bool loaded_;
|
||||
/** Parameters passed to the index */
|
||||
IndexParams index_params_;
|
||||
|
||||
Index(const Index &); // copy disabled
|
||||
Index& operator=(const Index &); // assign disabled
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs a hierarchical clustering of the points passed as argument and then takes a cut in the
|
||||
* the clustering tree to return a flat clustering.
|
||||
* @param[in] points Points to be clustered
|
||||
* @param centers The computed cluster centres. Matrix should be preallocated and centers.rows is the
|
||||
* number of clusters requested.
|
||||
* @param params Clustering parameters (The same as for cvflann::KMeansIndex)
|
||||
* @param d Distance to be used for clustering (eg: cvflann::L2)
|
||||
* @return number of clusters computed (can be different than clusters.rows and is the highest number
|
||||
* of the form (branching-1)*K+1 smaller than clusters.rows).
|
||||
*/
|
||||
template <typename Distance>
|
||||
int hierarchicalClustering(const Matrix<typename Distance::ElementType>& points, Matrix<typename Distance::CentersType>& centers,
|
||||
const KMeansIndexParams& params, Distance d = Distance())
|
||||
{
|
||||
KMeansIndex<Distance> kmeans(points, params, d);
|
||||
kmeans.buildIndex();
|
||||
|
||||
int clusterNum = kmeans.getClusterCenters(centers);
|
||||
return clusterNum;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_BASE_HPP_ */
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_GENERAL_H_
|
||||
#define OPENCV_FLANN_GENERAL_H_
|
||||
|
||||
#if CV_VERSION_MAJOR <= 4
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
class FLANNException : public cv::Exception
|
||||
{
|
||||
public:
|
||||
FLANNException(const char* message) : cv::Exception(0, message, "", __FILE__, __LINE__) { }
|
||||
|
||||
FLANNException(const cv::String& message) : cv::Exception(0, message, "", __FILE__, __LINE__) { }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define FLANN_THROW(TYPE, STR) throw FLANNException(STR)
|
||||
|
||||
#else
|
||||
|
||||
#define FLANN_THROW(TYPE, STR) CV_Error(TYPE, STR)
|
||||
|
||||
#endif
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_GENERAL_H_ */
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_GROUND_TRUTH_H_
|
||||
#define OPENCV_FLANN_GROUND_TRUTH_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "dist.h"
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
template <typename Distance>
|
||||
void find_nearest(const Matrix<typename Distance::ElementType>& dataset, typename Distance::ElementType* query, int* matches, int nn,
|
||||
int skip = 0, Distance distance = Distance())
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
int n = nn + skip;
|
||||
|
||||
std::vector<int> match(n);
|
||||
std::vector<DistanceType> dists(n);
|
||||
|
||||
dists[0] = distance(dataset[0], query, dataset.cols);
|
||||
match[0] = 0;
|
||||
int dcnt = 1;
|
||||
|
||||
for (size_t i=1; i<dataset.rows; ++i) {
|
||||
DistanceType tmp = distance(dataset[i], query, dataset.cols);
|
||||
|
||||
if (dcnt<n) {
|
||||
match[dcnt] = (int)i;
|
||||
dists[dcnt++] = tmp;
|
||||
}
|
||||
else if (tmp < dists[dcnt-1]) {
|
||||
dists[dcnt-1] = tmp;
|
||||
match[dcnt-1] = (int)i;
|
||||
}
|
||||
|
||||
int j = dcnt-1;
|
||||
// bubble up
|
||||
while (j>=1 && dists[j]<dists[j-1]) {
|
||||
std::swap(dists[j],dists[j-1]);
|
||||
std::swap(match[j],match[j-1]);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; i<nn; ++i) {
|
||||
matches[i] = match[i+skip];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
void compute_ground_truth(const Matrix<typename Distance::ElementType>& dataset, const Matrix<typename Distance::ElementType>& testset, Matrix<int>& matches,
|
||||
int skip=0, Distance d = Distance())
|
||||
{
|
||||
for (size_t i=0; i<testset.rows; ++i) {
|
||||
find_nearest<Distance>(dataset, testset[i], matches[i], (int)matches.cols, skip, d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_GROUND_TRUTH_H_
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_HDF5_H_
|
||||
#define OPENCV_FLANN_HDF5_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <hdf5.h>
|
||||
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
hid_t get_hdf5_type()
|
||||
{
|
||||
throw FLANNException("Unsupported type for IO operations");
|
||||
}
|
||||
|
||||
template<>
|
||||
hid_t get_hdf5_type<char>() { return H5T_NATIVE_CHAR; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<unsigned char>() { return H5T_NATIVE_UCHAR; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<short int>() { return H5T_NATIVE_SHORT; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<unsigned short int>() { return H5T_NATIVE_USHORT; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<int>() { return H5T_NATIVE_INT; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<unsigned int>() { return H5T_NATIVE_UINT; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<long>() { return H5T_NATIVE_LONG; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<unsigned long>() { return H5T_NATIVE_ULONG; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<float>() { return H5T_NATIVE_FLOAT; }
|
||||
template<>
|
||||
hid_t get_hdf5_type<double>() { return H5T_NATIVE_DOUBLE; }
|
||||
}
|
||||
|
||||
|
||||
#define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
|
||||
|
||||
template<typename T>
|
||||
void save_to_file(const cvflann::Matrix<T>& dataset, const String& filename, const String& name)
|
||||
{
|
||||
|
||||
#if H5Eset_auto_vers == 2
|
||||
H5Eset_auto( H5E_DEFAULT, NULL, NULL );
|
||||
#else
|
||||
H5Eset_auto( NULL, NULL );
|
||||
#endif
|
||||
|
||||
herr_t status;
|
||||
hid_t file_id;
|
||||
file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
|
||||
if (file_id < 0) {
|
||||
file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
|
||||
}
|
||||
CHECK_ERROR(file_id,"Error creating hdf5 file.");
|
||||
|
||||
hsize_t dimsf[2]; // dataset dimensions
|
||||
dimsf[0] = dataset.rows;
|
||||
dimsf[1] = dataset.cols;
|
||||
|
||||
hid_t space_id = H5Screate_simple(2, dimsf, NULL);
|
||||
hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
|
||||
|
||||
hid_t dataset_id;
|
||||
#if H5Dcreate_vers == 2
|
||||
dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
#else
|
||||
dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
|
||||
#endif
|
||||
|
||||
if (dataset_id<0) {
|
||||
#if H5Dopen_vers == 2
|
||||
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
|
||||
#else
|
||||
dataset_id = H5Dopen(file_id, name.c_str());
|
||||
#endif
|
||||
}
|
||||
CHECK_ERROR(dataset_id,"Error creating or opening dataset in file.");
|
||||
|
||||
status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.data );
|
||||
CHECK_ERROR(status, "Error writing to dataset");
|
||||
|
||||
H5Sclose(memspace_id);
|
||||
H5Sclose(space_id);
|
||||
H5Dclose(dataset_id);
|
||||
H5Fclose(file_id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
|
||||
{
|
||||
herr_t status;
|
||||
hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
|
||||
CHECK_ERROR(file_id,"Error opening hdf5 file.");
|
||||
|
||||
hid_t dataset_id;
|
||||
#if H5Dopen_vers == 2
|
||||
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
|
||||
#else
|
||||
dataset_id = H5Dopen(file_id, name.c_str());
|
||||
#endif
|
||||
CHECK_ERROR(dataset_id,"Error opening dataset in file.");
|
||||
|
||||
hid_t space_id = H5Dget_space(dataset_id);
|
||||
|
||||
hsize_t dims_out[2];
|
||||
H5Sget_simple_extent_dims(space_id, dims_out, NULL);
|
||||
|
||||
dataset = cvflann::Matrix<T>(new T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
|
||||
|
||||
status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
|
||||
CHECK_ERROR(status, "Error reading dataset");
|
||||
|
||||
H5Sclose(space_id);
|
||||
H5Dclose(dataset_id);
|
||||
H5Fclose(file_id);
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_MPI
|
||||
|
||||
namespace mpi
|
||||
{
|
||||
/**
|
||||
* Loads a the hyperslice corresponding to this processor from a hdf5 file.
|
||||
* @param flann_dataset Dataset where the data is loaded
|
||||
* @param filename HDF5 file name
|
||||
* @param name Name of dataset inside file
|
||||
*/
|
||||
template<typename T>
|
||||
void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
|
||||
{
|
||||
MPI_Comm comm = MPI_COMM_WORLD;
|
||||
MPI_Info info = MPI_INFO_NULL;
|
||||
|
||||
int mpi_size, mpi_rank;
|
||||
MPI_Comm_size(comm, &mpi_size);
|
||||
MPI_Comm_rank(comm, &mpi_rank);
|
||||
|
||||
herr_t status;
|
||||
|
||||
hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
|
||||
H5Pset_fapl_mpio(plist_id, comm, info);
|
||||
hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
|
||||
CHECK_ERROR(file_id,"Error opening hdf5 file.");
|
||||
H5Pclose(plist_id);
|
||||
hid_t dataset_id;
|
||||
#if H5Dopen_vers == 2
|
||||
dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
|
||||
#else
|
||||
dataset_id = H5Dopen(file_id, name.c_str());
|
||||
#endif
|
||||
CHECK_ERROR(dataset_id,"Error opening dataset in file.");
|
||||
|
||||
hid_t space_id = H5Dget_space(dataset_id);
|
||||
hsize_t dims[2];
|
||||
H5Sget_simple_extent_dims(space_id, dims, NULL);
|
||||
|
||||
hsize_t count[2];
|
||||
hsize_t offset[2];
|
||||
|
||||
hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
|
||||
hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
|
||||
|
||||
count[0] = cnt;
|
||||
count[1] = dims[1];
|
||||
offset[0] = mpi_rank*item_cnt;
|
||||
offset[1] = 0;
|
||||
|
||||
hid_t memspace_id = H5Screate_simple(2,count,NULL);
|
||||
|
||||
H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
|
||||
|
||||
dataset.rows = count[0];
|
||||
dataset.cols = count[1];
|
||||
dataset.data = new T[dataset.rows*dataset.cols];
|
||||
|
||||
plist_id = H5Pcreate(H5P_DATASET_XFER);
|
||||
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
|
||||
status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset.data);
|
||||
CHECK_ERROR(status, "Error reading dataset");
|
||||
|
||||
H5Pclose(plist_id);
|
||||
H5Sclose(space_id);
|
||||
H5Sclose(memspace_id);
|
||||
H5Dclose(dataset_id);
|
||||
H5Fclose(file_id);
|
||||
}
|
||||
}
|
||||
#endif // HAVE_MPI
|
||||
} // namespace cvflann::mpi
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_HDF5_H_ */
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_HEAP_H_
|
||||
#define OPENCV_FLANN_HEAP_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
// TODO: Define x > y operator and use std::greater<T> instead
|
||||
template <typename T>
|
||||
struct greater
|
||||
{
|
||||
bool operator()(const T& x, const T& y) const
|
||||
{
|
||||
return y < x;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Priority Queue Implementation
|
||||
*
|
||||
* The priority queue is implemented with a heap. A heap is a complete
|
||||
* (full) binary tree in which each parent is less than both of its
|
||||
* children, but the order of the children is unspecified.
|
||||
*/
|
||||
template <typename T>
|
||||
class Heap
|
||||
{
|
||||
/**
|
||||
* Storage array for the heap.
|
||||
* Type T must be comparable.
|
||||
*/
|
||||
std::vector<T> heap;
|
||||
public:
|
||||
/**
|
||||
* \brief Constructs a heap with a pre-allocated capacity
|
||||
*
|
||||
* \param capacity heap maximum capacity
|
||||
*/
|
||||
Heap(const int capacity)
|
||||
{
|
||||
reserve(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Move-constructs a heap from an external vector
|
||||
*
|
||||
* \param vec external vector
|
||||
*/
|
||||
Heap(std::vector<T>&& vec)
|
||||
: heap(std::move(vec))
|
||||
{
|
||||
std::make_heap(heap.begin(), heap.end(), greater<T>());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* \returns heap size
|
||||
*/
|
||||
int size() const
|
||||
{
|
||||
return (int)heap.size();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* \returns heap capacity
|
||||
*/
|
||||
int capacity() const
|
||||
{
|
||||
return (int)heap.capacity();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Tests if the heap is empty
|
||||
*
|
||||
* \returns true is heap empty, false otherwise
|
||||
*/
|
||||
bool empty()
|
||||
{
|
||||
return heap.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Clears the heap.
|
||||
*/
|
||||
void clear()
|
||||
{
|
||||
heap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets the heap maximum capacity.
|
||||
*
|
||||
* \param capacity heap maximum capacity
|
||||
*/
|
||||
void reserve(const int capacity)
|
||||
{
|
||||
heap.reserve(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Inserts a new element in the heap.
|
||||
*
|
||||
* We select the next empty leaf node, and then keep moving any larger
|
||||
* parents down until the right location is found to store this element.
|
||||
*
|
||||
* \param value the new element to be inserted in the heap
|
||||
*/
|
||||
void insert(T value)
|
||||
{
|
||||
/* If heap is full, then return without adding this element. */
|
||||
if (size() == capacity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
heap.push_back(value);
|
||||
std::push_heap(heap.begin(), heap.end(), greater<T>());
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns the node of minimum value from the heap (top of the heap).
|
||||
*
|
||||
* \param[out] value parameter used to return the min element
|
||||
* \returns false if heap empty
|
||||
*/
|
||||
bool popMin(T& value)
|
||||
{
|
||||
if (empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
value = heap[0];
|
||||
std::pop_heap(heap.begin(), heap.end(), greater<T>());
|
||||
heap.pop_back();
|
||||
|
||||
return true; /* Return old last node. */
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns a shared heap for the given memory pool ID.
|
||||
*
|
||||
* It constructs the heap if it does not already exists.
|
||||
*
|
||||
* \param poolId a user-chosen hashable ID for identifying the heap.
|
||||
* For thread-safe operations, using current thread ID is a good choice.
|
||||
* \param capacity heap maximum capacity
|
||||
* \param iterThreshold remove heaps that were not reused for more than specified iterations count
|
||||
* if iterThreshold value is less 2, it will be internally adjusted to twice the number of CPU threads
|
||||
* \returns pointer to the heap
|
||||
*/
|
||||
template <typename HashableT>
|
||||
static cv::Ptr<Heap<T>> getPooledInstance(
|
||||
const HashableT& poolId, const int capacity, int iterThreshold = 0)
|
||||
{
|
||||
static cv::Mutex mutex;
|
||||
const cv::AutoLock lock(mutex);
|
||||
|
||||
struct HeapMapValueType {
|
||||
cv::Ptr<Heap<T>> heapPtr;
|
||||
int iterCounter;
|
||||
};
|
||||
typedef std::unordered_map<HashableT, HeapMapValueType> HeapMapType;
|
||||
|
||||
static HeapMapType heapsPool;
|
||||
typename HeapMapType::iterator heapIt = heapsPool.find(poolId);
|
||||
|
||||
if (heapIt == heapsPool.end())
|
||||
{
|
||||
// Construct the heap as it does not already exists
|
||||
HeapMapValueType heapAndTimePair = {cv::makePtr<Heap<T>>(capacity), 0};
|
||||
const std::pair<typename HeapMapType::iterator, bool>& emplaceResult = heapsPool.emplace(poolId, std::move(heapAndTimePair));
|
||||
CV_CheckEQ(static_cast<int>(emplaceResult.second), 1, "Failed to insert the heap into its memory pool");
|
||||
heapIt = emplaceResult.first;
|
||||
}
|
||||
else
|
||||
{
|
||||
CV_CheckEQ(heapIt->second.heapPtr.use_count(), 1, "Cannot modify a heap that is currently accessed by another caller");
|
||||
heapIt->second.heapPtr->clear();
|
||||
heapIt->second.heapPtr->reserve(capacity);
|
||||
heapIt->second.iterCounter = 0;
|
||||
}
|
||||
|
||||
if (iterThreshold <= 1) {
|
||||
iterThreshold = 2 * cv::getNumThreads();
|
||||
}
|
||||
|
||||
// Remove heaps that were not reused for more than given iterThreshold
|
||||
typename HeapMapType::iterator cleanupIt = heapsPool.begin();
|
||||
while (cleanupIt != heapsPool.end())
|
||||
{
|
||||
if (cleanupIt->second.iterCounter++ > iterThreshold)
|
||||
{
|
||||
CV_Assert(cleanupIt != heapIt);
|
||||
cleanupIt = heapsPool.erase(cleanupIt);
|
||||
continue;
|
||||
}
|
||||
++cleanupIt;
|
||||
}
|
||||
|
||||
return heapIt->second.heapPtr;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_HEAP_H_
|
||||
+846
@@ -0,0 +1,846 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2011 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2011 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
|
||||
#define OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <limits>
|
||||
#include <cmath>
|
||||
|
||||
#include "general.h"
|
||||
#include "nn_index.h"
|
||||
#include "dist.h"
|
||||
#include "matrix.h"
|
||||
#include "result_set.h"
|
||||
#include "heap.h"
|
||||
#include "allocator.h"
|
||||
#include "random.h"
|
||||
#include "saving.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
struct HierarchicalClusteringIndexParams : public IndexParams
|
||||
{
|
||||
HierarchicalClusteringIndexParams(int branching = 32,
|
||||
flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM,
|
||||
int trees = 4, int leaf_size = 100)
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_HIERARCHICAL;
|
||||
// The branching factor used in the hierarchical clustering
|
||||
(*this)["branching"] = branching;
|
||||
// Algorithm used for picking the initial cluster centers
|
||||
(*this)["centers_init"] = centers_init;
|
||||
// number of parallel trees to build
|
||||
(*this)["trees"] = trees;
|
||||
// maximum leaf size
|
||||
(*this)["leaf_size"] = leaf_size;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Hierarchical index
|
||||
*
|
||||
* Contains a tree constructed through a hierarchical clustering
|
||||
* and other information for indexing a set of points for nearest-neighbour matching.
|
||||
*/
|
||||
template <typename Distance>
|
||||
class HierarchicalClusteringIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
typedef void (HierarchicalClusteringIndex::* centersAlgFunction)(int, int*, int, int*, int&);
|
||||
|
||||
/**
|
||||
* The function used for choosing the cluster centers.
|
||||
*/
|
||||
centersAlgFunction chooseCenters;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Chooses the initial centers in the k-means clustering in a random manner.
|
||||
*
|
||||
* Params:
|
||||
* k = number of centers
|
||||
* vecs = the dataset of points
|
||||
* indices = indices in the dataset
|
||||
* indices_length = length of indices vector
|
||||
*
|
||||
*/
|
||||
void chooseCentersRandom(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
|
||||
{
|
||||
UniqueRandom r(indices_length);
|
||||
|
||||
int index;
|
||||
for (index=0; index<k; ++index) {
|
||||
bool duplicate = true;
|
||||
int rnd;
|
||||
while (duplicate) {
|
||||
duplicate = false;
|
||||
rnd = r.next();
|
||||
if (rnd<0) {
|
||||
centers_length = index;
|
||||
return;
|
||||
}
|
||||
|
||||
centers[index] = dsindices[rnd];
|
||||
|
||||
for (int j=0; j<index; ++j) {
|
||||
DistanceType sq = distance(dataset[centers[index]], dataset[centers[j]], dataset.cols);
|
||||
if (sq<1e-16) {
|
||||
duplicate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
centers_length = index;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chooses the initial centers in the k-means using Gonzales' algorithm
|
||||
* so that the centers are spaced apart from each other.
|
||||
*
|
||||
* Params:
|
||||
* k = number of centers
|
||||
* vecs = the dataset of points
|
||||
* indices = indices in the dataset
|
||||
* Returns:
|
||||
*/
|
||||
void chooseCentersGonzales(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
|
||||
{
|
||||
int n = indices_length;
|
||||
|
||||
int rnd = rand_int(n);
|
||||
CV_DbgAssert(rnd >=0 && rnd < n);
|
||||
|
||||
centers[0] = dsindices[rnd];
|
||||
|
||||
int index;
|
||||
for (index=1; index<k; ++index) {
|
||||
|
||||
int best_index = -1;
|
||||
DistanceType best_val = 0;
|
||||
for (int j=0; j<n; ++j) {
|
||||
DistanceType dist = distance(dataset[centers[0]],dataset[dsindices[j]],dataset.cols);
|
||||
for (int i=1; i<index; ++i) {
|
||||
DistanceType tmp_dist = distance(dataset[centers[i]],dataset[dsindices[j]],dataset.cols);
|
||||
if (tmp_dist<dist) {
|
||||
dist = tmp_dist;
|
||||
}
|
||||
}
|
||||
if (dist>best_val) {
|
||||
best_val = dist;
|
||||
best_index = j;
|
||||
}
|
||||
}
|
||||
if (best_index!=-1) {
|
||||
centers[index] = dsindices[best_index];
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
centers_length = index;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chooses the initial centers in the k-means using the algorithm
|
||||
* proposed in the KMeans++ paper:
|
||||
* Arthur, David; Vassilvitskii, Sergei - k-means++: The Advantages of Careful Seeding
|
||||
*
|
||||
* Implementation of this function was converted from the one provided in Arthur's code.
|
||||
*
|
||||
* Params:
|
||||
* k = number of centers
|
||||
* vecs = the dataset of points
|
||||
* indices = indices in the dataset
|
||||
* Returns:
|
||||
*/
|
||||
void chooseCentersKMeanspp(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
|
||||
{
|
||||
int n = indices_length;
|
||||
|
||||
double currentPot = 0;
|
||||
DistanceType* closestDistSq = new DistanceType[n];
|
||||
|
||||
// Choose one random center and set the closestDistSq values
|
||||
int index = rand_int(n);
|
||||
CV_DbgAssert(index >=0 && index < n);
|
||||
centers[0] = dsindices[index];
|
||||
|
||||
// Computing distance^2 will have the advantage of even higher probability further to pick new centers
|
||||
// far from previous centers (and this complies to "k-means++: the advantages of careful seeding" article)
|
||||
for (int i = 0; i < n; i++) {
|
||||
closestDistSq[i] = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
|
||||
closestDistSq[i] = ensureSquareDistance<Distance>( closestDistSq[i] );
|
||||
currentPot += closestDistSq[i];
|
||||
}
|
||||
|
||||
|
||||
const int numLocalTries = 1;
|
||||
|
||||
// Choose each center
|
||||
int centerCount;
|
||||
for (centerCount = 1; centerCount < k; centerCount++) {
|
||||
|
||||
// Repeat several trials
|
||||
double bestNewPot = -1;
|
||||
int bestNewIndex = 0;
|
||||
for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
|
||||
|
||||
// Choose our center - have to be slightly careful to return a valid answer even accounting
|
||||
// for possible rounding errors
|
||||
double randVal = rand_double(currentPot);
|
||||
for (index = 0; index < n-1; index++) {
|
||||
if (randVal <= closestDistSq[index]) break;
|
||||
else randVal -= closestDistSq[index];
|
||||
}
|
||||
|
||||
// Compute the new potential
|
||||
double newPot = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
|
||||
newPot += std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
|
||||
}
|
||||
|
||||
// Store the best result
|
||||
if ((bestNewPot < 0)||(newPot < bestNewPot)) {
|
||||
bestNewPot = newPot;
|
||||
bestNewIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the appropriate center
|
||||
centers[centerCount] = dsindices[bestNewIndex];
|
||||
currentPot = bestNewPot;
|
||||
for (int i = 0; i < n; i++) {
|
||||
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[bestNewIndex]], dataset.cols);
|
||||
closestDistSq[i] = std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
|
||||
}
|
||||
}
|
||||
|
||||
centers_length = centerCount;
|
||||
|
||||
delete[] closestDistSq;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chooses the initial centers in a way inspired by Gonzales (by Pierre-Emmanuel Viel):
|
||||
* select the first point of the list as a candidate, then parse the points list. If another
|
||||
* point is further than current candidate from the other centers, test if it is a good center
|
||||
* of a local aggregation. If it is, replace current candidate by this point. And so on...
|
||||
*
|
||||
* Used with KMeansIndex that computes centers coordinates by averaging positions of clusters points,
|
||||
* this doesn't make a real difference with previous methods. But used with HierarchicalClusteringIndex
|
||||
* class that pick centers among existing points instead of computing the barycenters, there is a real
|
||||
* improvement.
|
||||
*
|
||||
* Params:
|
||||
* k = number of centers
|
||||
* vecs = the dataset of points
|
||||
* indices = indices in the dataset
|
||||
* Returns:
|
||||
*/
|
||||
void GroupWiseCenterChooser(int k, int* dsindices, int indices_length, int* centers, int& centers_length)
|
||||
{
|
||||
const float kSpeedUpFactor = 1.3f;
|
||||
|
||||
int n = indices_length;
|
||||
|
||||
DistanceType* closestDistSq = new DistanceType[n];
|
||||
|
||||
// Choose one random center and set the closestDistSq values
|
||||
int index = rand_int(n);
|
||||
CV_DbgAssert(index >=0 && index < n);
|
||||
centers[0] = dsindices[index];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
closestDistSq[i] = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
|
||||
}
|
||||
|
||||
|
||||
// Choose each center
|
||||
int centerCount;
|
||||
for (centerCount = 1; centerCount < k; centerCount++) {
|
||||
|
||||
// Repeat several trials
|
||||
double bestNewPot = -1;
|
||||
int bestNewIndex = 0;
|
||||
DistanceType furthest = 0;
|
||||
for (index = 0; index < n; index++) {
|
||||
|
||||
// We will test only the potential of the points further than current candidate
|
||||
if( closestDistSq[index] > kSpeedUpFactor * (float)furthest ) {
|
||||
|
||||
// Compute the new potential
|
||||
double newPot = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
newPot += std::min( distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols)
|
||||
, closestDistSq[i] );
|
||||
}
|
||||
|
||||
// Store the best result
|
||||
if ((bestNewPot < 0)||(newPot <= bestNewPot)) {
|
||||
bestNewPot = newPot;
|
||||
bestNewIndex = index;
|
||||
furthest = closestDistSq[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the appropriate center
|
||||
centers[centerCount] = dsindices[bestNewIndex];
|
||||
for (int i = 0; i < n; i++) {
|
||||
closestDistSq[i] = std::min( distance(dataset[dsindices[i]], dataset[dsindices[bestNewIndex]], dataset.cols)
|
||||
, closestDistSq[i] );
|
||||
}
|
||||
}
|
||||
|
||||
centers_length = centerCount;
|
||||
|
||||
delete[] closestDistSq;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Index constructor
|
||||
*
|
||||
* Params:
|
||||
* inputData = dataset with the input features
|
||||
* params = parameters passed to the hierarchical k-means algorithm
|
||||
*/
|
||||
HierarchicalClusteringIndex(const Matrix<ElementType>& inputData, const IndexParams& index_params = HierarchicalClusteringIndexParams(),
|
||||
Distance d = Distance())
|
||||
: dataset(inputData), params(index_params), root(NULL), indices(NULL), distance(d)
|
||||
{
|
||||
memoryCounter = 0;
|
||||
|
||||
size_ = dataset.rows;
|
||||
veclen_ = dataset.cols;
|
||||
|
||||
branching_ = get_param(params,"branching",32);
|
||||
centers_init_ = get_param(params,"centers_init", FLANN_CENTERS_RANDOM);
|
||||
trees_ = get_param(params,"trees",4);
|
||||
leaf_size_ = get_param(params,"leaf_size",100);
|
||||
|
||||
if (centers_init_==FLANN_CENTERS_RANDOM) {
|
||||
chooseCenters = &HierarchicalClusteringIndex::chooseCentersRandom;
|
||||
}
|
||||
else if (centers_init_==FLANN_CENTERS_GONZALES) {
|
||||
chooseCenters = &HierarchicalClusteringIndex::chooseCentersGonzales;
|
||||
}
|
||||
else if (centers_init_==FLANN_CENTERS_KMEANSPP) {
|
||||
chooseCenters = &HierarchicalClusteringIndex::chooseCentersKMeanspp;
|
||||
}
|
||||
else if (centers_init_==FLANN_CENTERS_GROUPWISE) {
|
||||
chooseCenters = &HierarchicalClusteringIndex::GroupWiseCenterChooser;
|
||||
}
|
||||
else {
|
||||
FLANN_THROW(cv::Error::StsError, "Unknown algorithm for choosing initial centers.");
|
||||
}
|
||||
|
||||
root = new NodePtr[trees_];
|
||||
indices = new int*[trees_];
|
||||
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
root[i] = NULL;
|
||||
indices[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
HierarchicalClusteringIndex(const HierarchicalClusteringIndex&);
|
||||
HierarchicalClusteringIndex& operator=(const HierarchicalClusteringIndex&);
|
||||
|
||||
/**
|
||||
* Index destructor.
|
||||
*
|
||||
* Release the memory used by the index.
|
||||
*/
|
||||
virtual ~HierarchicalClusteringIndex()
|
||||
{
|
||||
if (root!=NULL) {
|
||||
delete[] root;
|
||||
}
|
||||
|
||||
if (indices!=NULL) {
|
||||
free_indices();
|
||||
delete[] indices;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns size of index.
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of an index feature.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return veclen_;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Computes the inde memory usage
|
||||
* Returns: memory used by the index
|
||||
*/
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return pool.usedMemory+pool.wastedMemory+memoryCounter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the index
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
if (branching_<2) {
|
||||
FLANN_THROW(cv::Error::StsError, "Branching factor must be at least 2");
|
||||
}
|
||||
|
||||
free_indices();
|
||||
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
indices[i] = new int[size_];
|
||||
for (size_t j=0; j<size_; ++j) {
|
||||
indices[i][j] = (int)j;
|
||||
}
|
||||
root[i] = pool.allocate<Node>();
|
||||
computeClustering(root[i], indices[i], (int)size_, branching_,0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_HIERARCHICAL;
|
||||
}
|
||||
|
||||
|
||||
void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
save_value(stream, branching_);
|
||||
save_value(stream, trees_);
|
||||
save_value(stream, centers_init_);
|
||||
save_value(stream, leaf_size_);
|
||||
save_value(stream, memoryCounter);
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
save_value(stream, *indices[i], size_);
|
||||
save_tree(stream, root[i], i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
if (root!=NULL) {
|
||||
delete[] root;
|
||||
}
|
||||
|
||||
if (indices!=NULL) {
|
||||
free_indices();
|
||||
delete[] indices;
|
||||
}
|
||||
|
||||
load_value(stream, branching_);
|
||||
load_value(stream, trees_);
|
||||
load_value(stream, centers_init_);
|
||||
load_value(stream, leaf_size_);
|
||||
load_value(stream, memoryCounter);
|
||||
|
||||
indices = new int*[trees_];
|
||||
root = new NodePtr[trees_];
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
indices[i] = new int[size_];
|
||||
load_value(stream, *indices[i], size_);
|
||||
load_tree(stream, root[i], i);
|
||||
}
|
||||
|
||||
params["algorithm"] = getType();
|
||||
params["branching"] = branching_;
|
||||
params["trees"] = trees_;
|
||||
params["centers_init"] = centers_init_;
|
||||
params["leaf_size"] = leaf_size_;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find set of nearest neighbors to vec. Their indices are stored inside
|
||||
* the result object.
|
||||
*
|
||||
* Params:
|
||||
* result = the result object in which the indices of the nearest-neighbors are stored
|
||||
* vec = the vector for which to search the nearest neighbors
|
||||
* searchParams = parameters that influence the search algorithm (checks)
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
|
||||
const int maxChecks = get_param(searchParams,"checks",32);
|
||||
const bool explore_all_trees = get_param(searchParams,"explore_all_trees",false);
|
||||
|
||||
// Priority queue storing intermediate branches in the best-bin-first search
|
||||
const cv::Ptr<Heap<BranchSt>>& heap = Heap<BranchSt>::getPooledInstance(cv::utils::getThreadID(), (int)size_);
|
||||
|
||||
std::vector<bool> checked(size_,false);
|
||||
int checks = 0;
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
findNN(root[i], result, vec, checks, maxChecks, heap, checked, explore_all_trees);
|
||||
if (!explore_all_trees && (checks >= maxChecks) && result.full())
|
||||
break;
|
||||
}
|
||||
|
||||
BranchSt branch;
|
||||
while (heap->popMin(branch) && (checks<maxChecks || !result.full())) {
|
||||
NodePtr node = branch.node;
|
||||
findNN(node, result, vec, checks, maxChecks, heap, checked, false);
|
||||
}
|
||||
|
||||
CV_Assert(result.full());
|
||||
}
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Structure representing a node in the hierarchical k-means tree.
|
||||
*/
|
||||
struct Node
|
||||
{
|
||||
/**
|
||||
* The cluster center index
|
||||
*/
|
||||
int pivot;
|
||||
/**
|
||||
* The cluster size (number of points in the cluster)
|
||||
*/
|
||||
int size;
|
||||
/**
|
||||
* Child nodes (only for non-terminal nodes)
|
||||
*/
|
||||
Node** childs;
|
||||
/**
|
||||
* Node points (only for terminal nodes)
|
||||
*/
|
||||
int* indices;
|
||||
/**
|
||||
* Level
|
||||
*/
|
||||
int level;
|
||||
};
|
||||
typedef Node* NodePtr;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Alias definition for a nicer syntax.
|
||||
*/
|
||||
typedef BranchStruct<NodePtr, DistanceType> BranchSt;
|
||||
|
||||
|
||||
|
||||
void save_tree(FILE* stream, NodePtr node, int num)
|
||||
{
|
||||
save_value(stream, *node);
|
||||
if (node->childs==NULL) {
|
||||
int indices_offset = (int)(node->indices - indices[num]);
|
||||
save_value(stream, indices_offset);
|
||||
}
|
||||
else {
|
||||
for(int i=0; i<branching_; ++i) {
|
||||
save_tree(stream, node->childs[i], num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void load_tree(FILE* stream, NodePtr& node, int num)
|
||||
{
|
||||
node = pool.allocate<Node>();
|
||||
load_value(stream, *node);
|
||||
if (node->childs==NULL) {
|
||||
int indices_offset;
|
||||
load_value(stream, indices_offset);
|
||||
node->indices = indices[num] + indices_offset;
|
||||
}
|
||||
else {
|
||||
node->childs = pool.allocate<NodePtr>(branching_);
|
||||
for(int i=0; i<branching_; ++i) {
|
||||
load_tree(stream, node->childs[i], num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Release the inner elements of indices[]
|
||||
*/
|
||||
void free_indices()
|
||||
{
|
||||
if (indices!=NULL) {
|
||||
for(int i=0; i<trees_; ++i) {
|
||||
if (indices[i]!=NULL) {
|
||||
delete[] indices[i];
|
||||
indices[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void computeLabels(int* dsindices, int indices_length, int* centers, int centers_length, int* labels, DistanceType& cost)
|
||||
{
|
||||
cost = 0;
|
||||
for (int i=0; i<indices_length; ++i) {
|
||||
ElementType* point = dataset[dsindices[i]];
|
||||
DistanceType dist = distance(point, dataset[centers[0]], veclen_);
|
||||
labels[i] = 0;
|
||||
for (int j=1; j<centers_length; ++j) {
|
||||
DistanceType new_dist = distance(point, dataset[centers[j]], veclen_);
|
||||
if (dist>new_dist) {
|
||||
labels[i] = j;
|
||||
dist = new_dist;
|
||||
}
|
||||
}
|
||||
cost += dist;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The method responsible with actually doing the recursive hierarchical
|
||||
* clustering
|
||||
*
|
||||
* Params:
|
||||
* node = the node to cluster
|
||||
* indices = indices of the points belonging to the current node
|
||||
* branching = the branching factor to use in the clustering
|
||||
*
|
||||
* TODO: for 1-sized clusters don't store a cluster center (it's the same as the single cluster point)
|
||||
*/
|
||||
void computeClustering(NodePtr node, int* dsindices, int indices_length, int branching, int level)
|
||||
{
|
||||
node->size = indices_length;
|
||||
node->level = level;
|
||||
|
||||
if (indices_length < leaf_size_) { // leaf node
|
||||
node->indices = dsindices;
|
||||
std::sort(node->indices,node->indices+indices_length);
|
||||
node->childs = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<int> centers(branching);
|
||||
std::vector<int> labels(indices_length);
|
||||
|
||||
int centers_length;
|
||||
(this->*chooseCenters)(branching, dsindices, indices_length, ¢ers[0], centers_length);
|
||||
|
||||
if (centers_length<branching) {
|
||||
node->indices = dsindices;
|
||||
std::sort(node->indices,node->indices+indices_length);
|
||||
node->childs = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// assign points to clusters
|
||||
DistanceType cost;
|
||||
computeLabels(dsindices, indices_length, ¢ers[0], centers_length, &labels[0], cost);
|
||||
|
||||
node->childs = pool.allocate<NodePtr>(branching);
|
||||
int start = 0;
|
||||
int end = start;
|
||||
for (int i=0; i<branching; ++i) {
|
||||
for (int j=0; j<indices_length; ++j) {
|
||||
if (labels[j]==i) {
|
||||
std::swap(dsindices[j],dsindices[end]);
|
||||
std::swap(labels[j],labels[end]);
|
||||
end++;
|
||||
}
|
||||
}
|
||||
|
||||
node->childs[i] = pool.allocate<Node>();
|
||||
node->childs[i]->pivot = centers[i];
|
||||
node->childs[i]->indices = NULL;
|
||||
computeClustering(node->childs[i],dsindices+start, end-start, branching, level+1);
|
||||
start=end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Performs one descent in the hierarchical k-means tree. The branches not
|
||||
* visited are stored in a priority queue.
|
||||
*
|
||||
* Params:
|
||||
* node = node to explore
|
||||
* result = container for the k-nearest neighbors found
|
||||
* vec = query points
|
||||
* checks = how many points in the dataset have been checked so far
|
||||
* maxChecks = maximum dataset points to checks
|
||||
*/
|
||||
|
||||
|
||||
void findNN(NodePtr node, ResultSet<DistanceType>& result, const ElementType* vec, int& checks, int maxChecks,
|
||||
const cv::Ptr<Heap<BranchSt>>& heap, std::vector<bool>& checked, bool explore_all_trees = false)
|
||||
{
|
||||
if (node->childs==NULL) {
|
||||
if (!explore_all_trees && (checks>=maxChecks) && result.full()) {
|
||||
return;
|
||||
}
|
||||
for (int i=0; i<node->size; ++i) {
|
||||
int index = node->indices[i];
|
||||
if (!checked[index]) {
|
||||
DistanceType dist = distance(dataset[index], vec, veclen_);
|
||||
result.addPoint(dist, index);
|
||||
checked[index] = true;
|
||||
++checks;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
DistanceType* domain_distances = new DistanceType[branching_];
|
||||
int best_index = 0;
|
||||
domain_distances[best_index] = distance(vec, dataset[node->childs[best_index]->pivot], veclen_);
|
||||
for (int i=1; i<branching_; ++i) {
|
||||
domain_distances[i] = distance(vec, dataset[node->childs[i]->pivot], veclen_);
|
||||
if (domain_distances[i]<domain_distances[best_index]) {
|
||||
best_index = i;
|
||||
}
|
||||
}
|
||||
for (int i=0; i<branching_; ++i) {
|
||||
if (i!=best_index) {
|
||||
heap->insert(BranchSt(node->childs[i],domain_distances[i]));
|
||||
}
|
||||
}
|
||||
delete[] domain_distances;
|
||||
findNN(node->childs[best_index],result,vec, checks, maxChecks, heap, checked, explore_all_trees);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/**
|
||||
* The dataset used by this index
|
||||
*/
|
||||
const Matrix<ElementType> dataset;
|
||||
|
||||
/**
|
||||
* Parameters used by this index
|
||||
*/
|
||||
IndexParams params;
|
||||
|
||||
|
||||
/**
|
||||
* Number of features in the dataset.
|
||||
*/
|
||||
size_t size_;
|
||||
|
||||
/**
|
||||
* Length of each feature.
|
||||
*/
|
||||
size_t veclen_;
|
||||
|
||||
/**
|
||||
* The root node in the tree.
|
||||
*/
|
||||
NodePtr* root;
|
||||
|
||||
/**
|
||||
* Array of indices to vectors in the dataset.
|
||||
*/
|
||||
int** indices;
|
||||
|
||||
|
||||
/**
|
||||
* The distance
|
||||
*/
|
||||
Distance distance;
|
||||
|
||||
/**
|
||||
* Pooled memory allocator.
|
||||
*
|
||||
* Using a pooled memory allocator is more efficient
|
||||
* than allocating memory directly when there is a large
|
||||
* number small of memory allocations.
|
||||
*/
|
||||
PooledAllocator pool;
|
||||
|
||||
/**
|
||||
* Memory occupied by the index.
|
||||
*/
|
||||
int memoryCounter;
|
||||
|
||||
/** index parameters */
|
||||
int branching_;
|
||||
int trees_;
|
||||
flann_centers_init_t centers_init_;
|
||||
int leaf_size_;
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_HIERARCHICAL_CLUSTERING_INDEX_H_ */
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_INDEX_TESTING_H_
|
||||
#define OPENCV_FLANN_INDEX_TESTING_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
|
||||
#include "matrix.h"
|
||||
#include "nn_index.h"
|
||||
#include "result_set.h"
|
||||
#include "logger.h"
|
||||
#include "timer.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
inline int countCorrectMatches(int* neighbors, int* groundTruth, int n)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i=0; i<n; ++i) {
|
||||
for (int k=0; k<n; ++k) {
|
||||
if (neighbors[i]==groundTruth[k]) {
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
typename Distance::ResultType computeDistanceRaport(const Matrix<typename Distance::ElementType>& inputData, typename Distance::ElementType* target,
|
||||
int* neighbors, int* groundTruth, int veclen, int n, const Distance& distance)
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
DistanceType ret = 0;
|
||||
for (int i=0; i<n; ++i) {
|
||||
DistanceType den = distance(inputData[groundTruth[i]], target, veclen);
|
||||
DistanceType num = distance(inputData[neighbors[i]], target, veclen);
|
||||
|
||||
if ((den==0)&&(num==0)) {
|
||||
ret += 1;
|
||||
}
|
||||
else {
|
||||
ret += num/den;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
float search_with_ground_truth(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
|
||||
const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches, int nn, int checks,
|
||||
float& time, typename Distance::ResultType& dist, const Distance& distance, int skipMatches)
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
if (matches.cols<size_t(nn)) {
|
||||
Logger::info("matches.cols=%d, nn=%d\n",matches.cols,nn);
|
||||
|
||||
FLANN_THROW(cv::Error::StsError, "Ground truth is not computed for as many neighbors as requested");
|
||||
}
|
||||
|
||||
KNNResultSet<DistanceType> resultSet(nn+skipMatches);
|
||||
SearchParams searchParams(checks);
|
||||
|
||||
std::vector<int> indices(nn+skipMatches);
|
||||
std::vector<DistanceType> dists(nn+skipMatches);
|
||||
int* neighbors = &indices[skipMatches];
|
||||
|
||||
int correct = 0;
|
||||
DistanceType distR = 0;
|
||||
StartStopTimer t;
|
||||
int repeats = 0;
|
||||
while (t.value<0.2) {
|
||||
repeats++;
|
||||
t.start();
|
||||
correct = 0;
|
||||
distR = 0;
|
||||
for (size_t i = 0; i < testData.rows; i++) {
|
||||
resultSet.init(&indices[0], &dists[0]);
|
||||
index.findNeighbors(resultSet, testData[i], searchParams);
|
||||
|
||||
correct += countCorrectMatches(neighbors,matches[i], nn);
|
||||
distR += computeDistanceRaport<Distance>(inputData, testData[i], neighbors, matches[i], (int)testData.cols, nn, distance);
|
||||
}
|
||||
t.stop();
|
||||
}
|
||||
time = float(t.value/repeats);
|
||||
|
||||
float precicion = (float)correct/(nn*testData.rows);
|
||||
|
||||
dist = distR/(testData.rows*nn);
|
||||
|
||||
Logger::info("%8d %10.4g %10.5g %10.5g %10.5g\n",
|
||||
checks, precicion, time, 1000.0 * time / testData.rows, dist);
|
||||
|
||||
return precicion;
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
float test_index_checks(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
|
||||
const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
|
||||
int checks, float& precision, const Distance& distance, int nn = 1, int skipMatches = 0)
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
|
||||
Logger::info("---------------------------------------------------------\n");
|
||||
|
||||
float time = 0;
|
||||
DistanceType dist = 0;
|
||||
precision = search_with_ground_truth(index, inputData, testData, matches, nn, checks, time, dist, distance, skipMatches);
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
template <typename Distance>
|
||||
float test_index_precision(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
|
||||
const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
|
||||
float precision, int& checks, const Distance& distance, int nn = 1, int skipMatches = 0)
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
const float SEARCH_EPS = 0.001f;
|
||||
|
||||
Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
|
||||
Logger::info("---------------------------------------------------------\n");
|
||||
|
||||
int c2 = 1;
|
||||
float p2;
|
||||
int c1 = 1;
|
||||
//float p1;
|
||||
float time;
|
||||
DistanceType dist;
|
||||
|
||||
p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
|
||||
|
||||
if (p2>precision) {
|
||||
Logger::info("Got as close as I can\n");
|
||||
checks = c2;
|
||||
return time;
|
||||
}
|
||||
|
||||
while (p2<precision) {
|
||||
c1 = c2;
|
||||
//p1 = p2;
|
||||
c2 *=2;
|
||||
p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
|
||||
}
|
||||
|
||||
int cx;
|
||||
float realPrecision;
|
||||
if (fabs(p2-precision)>SEARCH_EPS) {
|
||||
Logger::info("Start linear estimation\n");
|
||||
// after we got to values in the vecinity of the desired precision
|
||||
// use linear approximation get a better estimation
|
||||
|
||||
cx = (c1+c2)/2;
|
||||
realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
|
||||
while (fabs(realPrecision-precision)>SEARCH_EPS) {
|
||||
|
||||
if (realPrecision<precision) {
|
||||
c1 = cx;
|
||||
}
|
||||
else {
|
||||
c2 = cx;
|
||||
}
|
||||
cx = (c1+c2)/2;
|
||||
if (cx==c1) {
|
||||
Logger::info("Got as close as I can\n");
|
||||
break;
|
||||
}
|
||||
realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
|
||||
}
|
||||
|
||||
c2 = cx;
|
||||
p2 = realPrecision;
|
||||
|
||||
}
|
||||
else {
|
||||
Logger::info("No need for linear estimation\n");
|
||||
cx = c2;
|
||||
realPrecision = p2;
|
||||
}
|
||||
|
||||
checks = cx;
|
||||
return time;
|
||||
}
|
||||
|
||||
|
||||
template <typename Distance>
|
||||
void test_index_precisions(NNIndex<Distance>& index, const Matrix<typename Distance::ElementType>& inputData,
|
||||
const Matrix<typename Distance::ElementType>& testData, const Matrix<int>& matches,
|
||||
float* precisions, int precisions_length, const Distance& distance, int nn = 1, int skipMatches = 0, float maxTime = 0)
|
||||
{
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
const float SEARCH_EPS = 0.001;
|
||||
|
||||
// make sure precisions array is sorted
|
||||
std::sort(precisions, precisions+precisions_length);
|
||||
|
||||
int pindex = 0;
|
||||
float precision = precisions[pindex];
|
||||
|
||||
Logger::info(" Nodes Precision(%) Time(s) Time/vec(ms) Mean dist\n");
|
||||
Logger::info("---------------------------------------------------------\n");
|
||||
|
||||
int c2 = 1;
|
||||
float p2;
|
||||
|
||||
int c1 = 1;
|
||||
|
||||
float time;
|
||||
DistanceType dist;
|
||||
|
||||
p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
|
||||
|
||||
// if precision for 1 run down the tree is already
|
||||
// better then some of the requested precisions, then
|
||||
// skip those
|
||||
while (precisions[pindex]<p2 && pindex<precisions_length) {
|
||||
pindex++;
|
||||
}
|
||||
|
||||
if (pindex==precisions_length) {
|
||||
Logger::info("Got as close as I can\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i=pindex; i<precisions_length; ++i) {
|
||||
|
||||
precision = precisions[i];
|
||||
while (p2<precision) {
|
||||
c1 = c2;
|
||||
c2 *=2;
|
||||
p2 = search_with_ground_truth(index, inputData, testData, matches, nn, c2, time, dist, distance, skipMatches);
|
||||
if ((maxTime> 0)&&(time > maxTime)&&(p2<precision)) return;
|
||||
}
|
||||
|
||||
int cx;
|
||||
float realPrecision;
|
||||
if (fabs(p2-precision)>SEARCH_EPS) {
|
||||
Logger::info("Start linear estimation\n");
|
||||
// after we got to values in the vecinity of the desired precision
|
||||
// use linear approximation get a better estimation
|
||||
|
||||
cx = (c1+c2)/2;
|
||||
realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
|
||||
while (fabs(realPrecision-precision)>SEARCH_EPS) {
|
||||
|
||||
if (realPrecision<precision) {
|
||||
c1 = cx;
|
||||
}
|
||||
else {
|
||||
c2 = cx;
|
||||
}
|
||||
cx = (c1+c2)/2;
|
||||
if (cx==c1) {
|
||||
Logger::info("Got as close as I can\n");
|
||||
break;
|
||||
}
|
||||
realPrecision = search_with_ground_truth(index, inputData, testData, matches, nn, cx, time, dist, distance, skipMatches);
|
||||
}
|
||||
|
||||
c2 = cx;
|
||||
p2 = realPrecision;
|
||||
|
||||
}
|
||||
else {
|
||||
Logger::info("No need for linear estimation\n");
|
||||
cx = c2;
|
||||
realPrecision = p2;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_INDEX_TESTING_H_
|
||||
+636
@@ -0,0 +1,636 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_KDTREE_INDEX_H_
|
||||
#define OPENCV_FLANN_KDTREE_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "dynamic_bitset.h"
|
||||
#include "matrix.h"
|
||||
#include "result_set.h"
|
||||
#include "heap.h"
|
||||
#include "allocator.h"
|
||||
#include "random.h"
|
||||
#include "saving.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
struct KDTreeIndexParams : public IndexParams
|
||||
{
|
||||
KDTreeIndexParams(int trees = 4)
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_KDTREE;
|
||||
(*this)["trees"] = trees;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Randomized kd-tree index
|
||||
*
|
||||
* Contains the k-d trees and other information for indexing a set of points
|
||||
* for nearest-neighbor matching.
|
||||
*/
|
||||
template <typename Distance>
|
||||
class KDTreeIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
|
||||
/**
|
||||
* KDTree constructor
|
||||
*
|
||||
* Params:
|
||||
* inputData = dataset with the input features
|
||||
* params = parameters passed to the kdtree algorithm
|
||||
*/
|
||||
KDTreeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = KDTreeIndexParams(),
|
||||
Distance d = Distance() ) :
|
||||
dataset_(inputData), index_params_(params), distance_(d)
|
||||
{
|
||||
size_ = dataset_.rows;
|
||||
veclen_ = dataset_.cols;
|
||||
|
||||
trees_ = get_param(index_params_,"trees",4);
|
||||
tree_roots_ = new NodePtr[trees_];
|
||||
|
||||
// Create a permutable array of indices to the input vectors.
|
||||
vind_.resize(size_);
|
||||
for (size_t i = 0; i < size_; ++i) {
|
||||
vind_[i] = int(i);
|
||||
}
|
||||
|
||||
mean_ = new DistanceType[veclen_];
|
||||
var_ = new DistanceType[veclen_];
|
||||
}
|
||||
|
||||
|
||||
KDTreeIndex(const KDTreeIndex&);
|
||||
KDTreeIndex& operator=(const KDTreeIndex&);
|
||||
|
||||
/**
|
||||
* Standard destructor
|
||||
*/
|
||||
~KDTreeIndex()
|
||||
{
|
||||
if (tree_roots_!=NULL) {
|
||||
delete[] tree_roots_;
|
||||
}
|
||||
delete[] mean_;
|
||||
delete[] var_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the index
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
/* Construct the randomized trees. */
|
||||
for (int i = 0; i < trees_; i++) {
|
||||
/* Randomize the order of vectors to allow for unbiased sampling. */
|
||||
#ifndef OPENCV_FLANN_USE_STD_RAND
|
||||
cv::randShuffle(vind_);
|
||||
#else
|
||||
std::random_shuffle(vind_.begin(), vind_.end());
|
||||
#endif
|
||||
|
||||
tree_roots_[i] = divideTree(&vind_[0], int(size_) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_KDTREE;
|
||||
}
|
||||
|
||||
|
||||
void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
save_value(stream, trees_);
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
save_tree(stream, tree_roots_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
load_value(stream, trees_);
|
||||
if (tree_roots_!=NULL) {
|
||||
delete[] tree_roots_;
|
||||
}
|
||||
tree_roots_ = new NodePtr[trees_];
|
||||
for (int i=0; i<trees_; ++i) {
|
||||
load_tree(stream,tree_roots_[i]);
|
||||
}
|
||||
|
||||
index_params_["algorithm"] = getType();
|
||||
index_params_["trees"] = tree_roots_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns size of index.
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of an index feature.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return veclen_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the inde memory usage
|
||||
* Returns: memory used by the index
|
||||
*/
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return int(pool_.usedMemory+pool_.wastedMemory+dataset_.rows*sizeof(int)); // pool memory and vind array memory
|
||||
}
|
||||
|
||||
/**
|
||||
* Find set of nearest neighbors to vec. Their indices are stored inside
|
||||
* the result object.
|
||||
*
|
||||
* Params:
|
||||
* result = the result object in which the indices of the nearest-neighbors are stored
|
||||
* vec = the vector for which to search the nearest neighbors
|
||||
* maxCheck = the maximum number of restarts (in a best-bin-first manner)
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
const int maxChecks = get_param(searchParams,"checks", 32);
|
||||
const float epsError = 1+get_param(searchParams,"eps",0.0f);
|
||||
const bool explore_all_trees = get_param(searchParams,"explore_all_trees",false);
|
||||
|
||||
if (maxChecks==FLANN_CHECKS_UNLIMITED) {
|
||||
getExactNeighbors(result, vec, epsError);
|
||||
}
|
||||
else {
|
||||
getNeighbors(result, vec, maxChecks, epsError, explore_all_trees);
|
||||
}
|
||||
}
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return index_params_;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/*--------------------- Internal Data Structures --------------------------*/
|
||||
struct Node
|
||||
{
|
||||
/**
|
||||
* Dimension used for subdivision.
|
||||
*/
|
||||
int divfeat;
|
||||
/**
|
||||
* The values used for subdivision.
|
||||
*/
|
||||
DistanceType divval;
|
||||
/**
|
||||
* The child nodes.
|
||||
*/
|
||||
Node* child1, * child2;
|
||||
};
|
||||
typedef Node* NodePtr;
|
||||
typedef BranchStruct<NodePtr, DistanceType> BranchSt;
|
||||
typedef BranchSt* Branch;
|
||||
|
||||
|
||||
|
||||
void save_tree(FILE* stream, NodePtr tree)
|
||||
{
|
||||
save_value(stream, *tree);
|
||||
if (tree->child1!=NULL) {
|
||||
save_tree(stream, tree->child1);
|
||||
}
|
||||
if (tree->child2!=NULL) {
|
||||
save_tree(stream, tree->child2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void load_tree(FILE* stream, NodePtr& tree)
|
||||
{
|
||||
tree = pool_.allocate<Node>();
|
||||
load_value(stream, *tree);
|
||||
if (tree->child1!=NULL) {
|
||||
load_tree(stream, tree->child1);
|
||||
}
|
||||
if (tree->child2!=NULL) {
|
||||
load_tree(stream, tree->child2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a tree node that subdivides the list of vecs from vind[first]
|
||||
* to vind[last]. The routine is called recursively on each sublist.
|
||||
* Place a pointer to this new tree node in the location pTree.
|
||||
*
|
||||
* Params: pTree = the new node to create
|
||||
* first = index of the first vector
|
||||
* last = index of the last vector
|
||||
*/
|
||||
NodePtr divideTree(int* ind, int count)
|
||||
{
|
||||
NodePtr node = pool_.allocate<Node>(); // allocate memory
|
||||
|
||||
/* If too few exemplars remain, then make this a leaf node. */
|
||||
if ( count == 1) {
|
||||
node->child1 = node->child2 = NULL; /* Mark as leaf node. */
|
||||
node->divfeat = *ind; /* Store index of this vec. */
|
||||
}
|
||||
else {
|
||||
int idx;
|
||||
int cutfeat;
|
||||
DistanceType cutval;
|
||||
meanSplit(ind, count, idx, cutfeat, cutval);
|
||||
|
||||
node->divfeat = cutfeat;
|
||||
node->divval = cutval;
|
||||
node->child1 = divideTree(ind, idx);
|
||||
node->child2 = divideTree(ind+idx, count-idx);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Choose which feature to use in order to subdivide this set of vectors.
|
||||
* Make a random choice among those with the highest variance, and use
|
||||
* its variance as the threshold value.
|
||||
*/
|
||||
void meanSplit(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval)
|
||||
{
|
||||
memset(mean_,0,veclen_*sizeof(DistanceType));
|
||||
memset(var_,0,veclen_*sizeof(DistanceType));
|
||||
|
||||
/* Compute mean values. Only the first SAMPLE_MEAN values need to be
|
||||
sampled to get a good estimate.
|
||||
*/
|
||||
int cnt = std::min((int)SAMPLE_MEAN+1, count);
|
||||
for (int j = 0; j < cnt; ++j) {
|
||||
ElementType* v = dataset_[ind[j]];
|
||||
for (size_t k=0; k<veclen_; ++k) {
|
||||
mean_[k] += v[k];
|
||||
}
|
||||
}
|
||||
for (size_t k=0; k<veclen_; ++k) {
|
||||
mean_[k] /= cnt;
|
||||
}
|
||||
|
||||
/* Compute variances (no need to divide by count). */
|
||||
for (int j = 0; j < cnt; ++j) {
|
||||
ElementType* v = dataset_[ind[j]];
|
||||
for (size_t k=0; k<veclen_; ++k) {
|
||||
DistanceType dist = v[k] - mean_[k];
|
||||
var_[k] += dist * dist;
|
||||
}
|
||||
}
|
||||
/* Select one of the highest variance indices at random. */
|
||||
cutfeat = selectDivision(var_);
|
||||
cutval = mean_[cutfeat];
|
||||
|
||||
int lim1, lim2;
|
||||
planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
|
||||
|
||||
if (lim1>count/2) index = lim1;
|
||||
else if (lim2<count/2) index = lim2;
|
||||
else index = count/2;
|
||||
|
||||
/* If either list is empty, it means that all remaining features
|
||||
* are identical. Split in the middle to maintain a balanced tree.
|
||||
*/
|
||||
if ((lim1==count)||(lim2==0)) index = count/2;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Select the top RAND_DIM largest values from v and return the index of
|
||||
* one of these selected at random.
|
||||
*/
|
||||
int selectDivision(DistanceType* v)
|
||||
{
|
||||
int num = 0;
|
||||
size_t topind[RAND_DIM];
|
||||
|
||||
/* Create a list of the indices of the top RAND_DIM values. */
|
||||
for (size_t i = 0; i < veclen_; ++i) {
|
||||
if ((num < RAND_DIM)||(v[i] > v[topind[num-1]])) {
|
||||
/* Put this element at end of topind. */
|
||||
if (num < RAND_DIM) {
|
||||
topind[num++] = i; /* Add to list. */
|
||||
}
|
||||
else {
|
||||
topind[num-1] = i; /* Replace last element. */
|
||||
}
|
||||
/* Bubble end value down to right location by repeated swapping. */
|
||||
int j = num - 1;
|
||||
while (j > 0 && v[topind[j]] > v[topind[j-1]]) {
|
||||
std::swap(topind[j], topind[j-1]);
|
||||
--j;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Select a random integer in range [0,num-1], and return that index. */
|
||||
int rnd = rand_int(num);
|
||||
return (int)topind[rnd];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subdivide the list of points by a plane perpendicular on axe corresponding
|
||||
* to the 'cutfeat' dimension at 'cutval' position.
|
||||
*
|
||||
* On return:
|
||||
* dataset[ind[0..lim1-1]][cutfeat]<cutval
|
||||
* dataset[ind[lim1..lim2-1]][cutfeat]==cutval
|
||||
* dataset[ind[lim2..count]][cutfeat]>cutval
|
||||
*/
|
||||
void planeSplit(int* ind, int count, int cutfeat, DistanceType cutval, int& lim1, int& lim2)
|
||||
{
|
||||
/* Move vector indices for left subtree to front of list. */
|
||||
int left = 0;
|
||||
int right = count-1;
|
||||
for (;; ) {
|
||||
while (left<=right && dataset_[ind[left]][cutfeat]<cutval) ++left;
|
||||
while (left<=right && dataset_[ind[right]][cutfeat]>=cutval) --right;
|
||||
if (left>right) break;
|
||||
std::swap(ind[left], ind[right]); ++left; --right;
|
||||
}
|
||||
lim1 = left;
|
||||
right = count-1;
|
||||
for (;; ) {
|
||||
while (left<=right && dataset_[ind[left]][cutfeat]<=cutval) ++left;
|
||||
while (left<=right && dataset_[ind[right]][cutfeat]>cutval) --right;
|
||||
if (left>right) break;
|
||||
std::swap(ind[left], ind[right]); ++left; --right;
|
||||
}
|
||||
lim2 = left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an exact nearest neighbor search. The exact search performs a full
|
||||
* traversal of the tree.
|
||||
*/
|
||||
void getExactNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, float epsError)
|
||||
{
|
||||
// checkID -= 1; /* Set a different unique ID for each search. */
|
||||
|
||||
if (trees_ > 1) {
|
||||
fprintf(stderr,"It doesn't make any sense to use more than one tree for exact search");
|
||||
}
|
||||
if (trees_>0) {
|
||||
searchLevelExact(result, vec, tree_roots_[0], 0.0, epsError);
|
||||
}
|
||||
CV_Assert(result.full());
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the approximate nearest-neighbor search. The search is approximate
|
||||
* because the tree traversal is abandoned after a given number of descends in
|
||||
* the tree.
|
||||
*/
|
||||
void getNeighbors(ResultSet<DistanceType>& result, const ElementType* vec,
|
||||
int maxCheck, float epsError, bool explore_all_trees = false)
|
||||
{
|
||||
int i;
|
||||
BranchSt branch;
|
||||
int checkCount = 0;
|
||||
DynamicBitset checked(size_);
|
||||
|
||||
// Priority queue storing intermediate branches in the best-bin-first search
|
||||
const cv::Ptr<Heap<BranchSt>>& heap = Heap<BranchSt>::getPooledInstance(cv::utils::getThreadID(), (int)size_);
|
||||
|
||||
/* Search once through each tree down to root. */
|
||||
for (i = 0; i < trees_; ++i) {
|
||||
searchLevel(result, vec, tree_roots_[i], 0, checkCount, maxCheck,
|
||||
epsError, heap, checked, explore_all_trees);
|
||||
if (!explore_all_trees && (checkCount >= maxCheck) && result.full())
|
||||
break;
|
||||
}
|
||||
|
||||
/* Keep searching other branches from heap until finished. */
|
||||
while ( heap->popMin(branch) && (checkCount < maxCheck || !result.full() )) {
|
||||
searchLevel(result, vec, branch.node, branch.mindist, checkCount, maxCheck,
|
||||
epsError, heap, checked, false);
|
||||
}
|
||||
|
||||
CV_Assert(result.full());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Search starting from a given node of the tree. Based on any mismatches at
|
||||
* higher levels, all exemplars below this level must have a distance of
|
||||
* at least "mindistsq".
|
||||
*/
|
||||
void searchLevel(ResultSet<DistanceType>& result_set, const ElementType* vec, NodePtr node, DistanceType mindist, int& checkCount, int maxCheck,
|
||||
float epsError, const cv::Ptr<Heap<BranchSt>>& heap, DynamicBitset& checked, bool explore_all_trees = false)
|
||||
{
|
||||
if (result_set.worstDist()<mindist) {
|
||||
// printf("Ignoring branch, too far\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* If this is a leaf node, then do check and return. */
|
||||
if ((node->child1 == NULL)&&(node->child2 == NULL)) {
|
||||
/* Do not check same node more than once when searching multiple trees.
|
||||
Once a vector is checked, we set its location in vind to the
|
||||
current checkID.
|
||||
*/
|
||||
int index = node->divfeat;
|
||||
if ( checked.test(index) ||
|
||||
(!explore_all_trees && (checkCount>=maxCheck) && result_set.full()) ) {
|
||||
return;
|
||||
}
|
||||
checked.set(index);
|
||||
checkCount++;
|
||||
|
||||
DistanceType dist = distance_(dataset_[index], vec, veclen_);
|
||||
result_set.addPoint(dist,index);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* Which child branch should be taken first? */
|
||||
ElementType val = vec[node->divfeat];
|
||||
DistanceType diff = val - node->divval;
|
||||
NodePtr bestChild = (diff < 0) ? node->child1 : node->child2;
|
||||
NodePtr otherChild = (diff < 0) ? node->child2 : node->child1;
|
||||
|
||||
/* Create a branch record for the branch not taken. Add distance
|
||||
of this feature boundary (we don't attempt to correct for any
|
||||
use of this feature in a parent node, which is unlikely to
|
||||
happen and would have only a small effect). Don't bother
|
||||
adding more branches to heap after halfway point, as cost of
|
||||
adding exceeds their value.
|
||||
*/
|
||||
|
||||
DistanceType new_distsq = mindist + distance_.accum_dist(val, node->divval, node->divfeat);
|
||||
// if (2 * checkCount < maxCheck || !result.full()) {
|
||||
if ((new_distsq*epsError < result_set.worstDist())|| !result_set.full()) {
|
||||
heap->insert( BranchSt(otherChild, new_distsq) );
|
||||
}
|
||||
|
||||
/* Call recursively to search next level down. */
|
||||
searchLevel(result_set, vec, bestChild, mindist, checkCount, maxCheck, epsError, heap, checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an exact search in the tree starting from a node.
|
||||
*/
|
||||
void searchLevelExact(ResultSet<DistanceType>& result_set, const ElementType* vec, const NodePtr node, DistanceType mindist, const float epsError)
|
||||
{
|
||||
/* If this is a leaf node, then do check and return. */
|
||||
if ((node->child1 == NULL)&&(node->child2 == NULL)) {
|
||||
int index = node->divfeat;
|
||||
DistanceType dist = distance_(dataset_[index], vec, veclen_);
|
||||
result_set.addPoint(dist,index);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Which child branch should be taken first? */
|
||||
ElementType val = vec[node->divfeat];
|
||||
DistanceType diff = val - node->divval;
|
||||
NodePtr bestChild = (diff < 0) ? node->child1 : node->child2;
|
||||
NodePtr otherChild = (diff < 0) ? node->child2 : node->child1;
|
||||
|
||||
/* Create a branch record for the branch not taken. Add distance
|
||||
of this feature boundary (we don't attempt to correct for any
|
||||
use of this feature in a parent node, which is unlikely to
|
||||
happen and would have only a small effect). Don't bother
|
||||
adding more branches to heap after halfway point, as cost of
|
||||
adding exceeds their value.
|
||||
*/
|
||||
|
||||
DistanceType new_distsq = mindist + distance_.accum_dist(val, node->divval, node->divfeat);
|
||||
|
||||
/* Call recursively to search next level down. */
|
||||
searchLevelExact(result_set, vec, bestChild, mindist, epsError);
|
||||
|
||||
if (new_distsq*epsError<=result_set.worstDist()) {
|
||||
searchLevelExact(result_set, vec, otherChild, new_distsq, epsError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
enum
|
||||
{
|
||||
/**
|
||||
* To improve efficiency, only SAMPLE_MEAN random values are used to
|
||||
* compute the mean and variance at each level when building a tree.
|
||||
* A value of 100 seems to perform as well as using all values.
|
||||
*/
|
||||
SAMPLE_MEAN = 100,
|
||||
/**
|
||||
* Top random dimensions to consider
|
||||
*
|
||||
* When creating random trees, the dimension on which to subdivide is
|
||||
* selected at random from among the top RAND_DIM dimensions with the
|
||||
* highest variance. A value of 5 works well.
|
||||
*/
|
||||
RAND_DIM=5
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Number of randomized trees that are used
|
||||
*/
|
||||
int trees_;
|
||||
|
||||
/**
|
||||
* Array of indices to vectors in the dataset.
|
||||
*/
|
||||
std::vector<int> vind_;
|
||||
|
||||
/**
|
||||
* The dataset used by this index
|
||||
*/
|
||||
const Matrix<ElementType> dataset_;
|
||||
|
||||
IndexParams index_params_;
|
||||
|
||||
size_t size_;
|
||||
size_t veclen_;
|
||||
|
||||
|
||||
DistanceType* mean_;
|
||||
DistanceType* var_;
|
||||
|
||||
|
||||
/**
|
||||
* Array of k-d trees used to find neighbours.
|
||||
*/
|
||||
NodePtr* tree_roots_;
|
||||
|
||||
/**
|
||||
* Pooled memory allocator.
|
||||
*
|
||||
* Using a pooled memory allocator is more efficient
|
||||
* than allocating memory directly when there is a large
|
||||
* number small of memory allocations.
|
||||
*/
|
||||
PooledAllocator pool_;
|
||||
|
||||
Distance distance_;
|
||||
|
||||
|
||||
}; // class KDTreeForest
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_KDTREE_INDEX_H_
|
||||
Vendored
+645
@@ -0,0 +1,645 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_KDTREE_SINGLE_INDEX_H_
|
||||
#define OPENCV_FLANN_KDTREE_SINGLE_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "matrix.h"
|
||||
#include "result_set.h"
|
||||
#include "heap.h"
|
||||
#include "allocator.h"
|
||||
#include "random.h"
|
||||
#include "saving.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
struct KDTreeSingleIndexParams : public IndexParams
|
||||
{
|
||||
KDTreeSingleIndexParams(int leaf_max_size = 10, bool reorder = true, int dim = -1)
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_KDTREE_SINGLE;
|
||||
(*this)["leaf_max_size"] = leaf_max_size;
|
||||
(*this)["reorder"] = reorder;
|
||||
(*this)["dim"] = dim;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Randomized kd-tree index
|
||||
*
|
||||
* Contains the k-d trees and other information for indexing a set of points
|
||||
* for nearest-neighbor matching.
|
||||
*/
|
||||
template <typename Distance>
|
||||
class KDTreeSingleIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
|
||||
/**
|
||||
* KDTree constructor
|
||||
*
|
||||
* Params:
|
||||
* inputData = dataset with the input features
|
||||
* params = parameters passed to the kdtree algorithm
|
||||
*/
|
||||
KDTreeSingleIndex(const Matrix<ElementType>& inputData, const IndexParams& params = KDTreeSingleIndexParams(),
|
||||
Distance d = Distance() ) :
|
||||
dataset_(inputData), index_params_(params), distance_(d)
|
||||
{
|
||||
size_ = dataset_.rows;
|
||||
dim_ = dataset_.cols;
|
||||
root_node_ = 0;
|
||||
int dim_param = get_param(params,"dim",-1);
|
||||
if (dim_param>0) dim_ = dim_param;
|
||||
leaf_max_size_ = get_param(params,"leaf_max_size",10);
|
||||
reorder_ = get_param(params,"reorder",true);
|
||||
|
||||
// Create a permutable array of indices to the input vectors.
|
||||
vind_.resize(size_);
|
||||
for (size_t i = 0; i < size_; i++) {
|
||||
vind_[i] = (int)i;
|
||||
}
|
||||
}
|
||||
|
||||
KDTreeSingleIndex(const KDTreeSingleIndex&);
|
||||
KDTreeSingleIndex& operator=(const KDTreeSingleIndex&);
|
||||
|
||||
/**
|
||||
* Standard destructor
|
||||
*/
|
||||
~KDTreeSingleIndex()
|
||||
{
|
||||
if (reorder_) delete[] data_.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the index
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
computeBoundingBox(root_bbox_);
|
||||
root_node_ = divideTree(0, (int)size_, root_bbox_ ); // construct the tree
|
||||
|
||||
if (reorder_) {
|
||||
delete[] data_.data;
|
||||
data_ = cvflann::Matrix<ElementType>(new ElementType[size_*dim_], size_, dim_);
|
||||
for (size_t i=0; i<size_; ++i) {
|
||||
for (size_t j=0; j<dim_; ++j) {
|
||||
data_[i][j] = dataset_[vind_[i]][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
data_ = dataset_;
|
||||
}
|
||||
}
|
||||
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_KDTREE_SINGLE;
|
||||
}
|
||||
|
||||
|
||||
void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
save_value(stream, size_);
|
||||
save_value(stream, dim_);
|
||||
save_value(stream, root_bbox_);
|
||||
save_value(stream, reorder_);
|
||||
save_value(stream, leaf_max_size_);
|
||||
save_value(stream, vind_);
|
||||
if (reorder_) {
|
||||
save_value(stream, data_);
|
||||
}
|
||||
save_tree(stream, root_node_);
|
||||
}
|
||||
|
||||
|
||||
void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
load_value(stream, size_);
|
||||
load_value(stream, dim_);
|
||||
load_value(stream, root_bbox_);
|
||||
load_value(stream, reorder_);
|
||||
load_value(stream, leaf_max_size_);
|
||||
load_value(stream, vind_);
|
||||
if (reorder_) {
|
||||
load_value(stream, data_);
|
||||
}
|
||||
else {
|
||||
data_ = dataset_;
|
||||
}
|
||||
load_tree(stream, root_node_);
|
||||
|
||||
|
||||
index_params_["algorithm"] = getType();
|
||||
index_params_["leaf_max_size"] = leaf_max_size_;
|
||||
index_params_["reorder"] = reorder_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns size of index.
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of an index feature.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return dim_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the inde memory usage
|
||||
* Returns: memory used by the index
|
||||
*/
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return (int)(pool_.usedMemory+pool_.wastedMemory+dataset_.rows*sizeof(int)); // pool memory and vind array memory
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \brief Perform k-nearest neighbor search
|
||||
* \param[in] queries The query points for which to find the nearest neighbors
|
||||
* \param[out] indices The indices of the nearest neighbors found
|
||||
* \param[out] dists Distances to the nearest neighbors found
|
||||
* \param[in] knn Number of nearest neighbors to return
|
||||
* \param[in] params Search parameters
|
||||
*/
|
||||
void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
|
||||
{
|
||||
CV_Assert(queries.cols == veclen());
|
||||
CV_Assert(indices.rows >= queries.rows);
|
||||
CV_Assert(dists.rows >= queries.rows);
|
||||
CV_Assert(int(indices.cols) >= knn);
|
||||
CV_Assert(int(dists.cols) >= knn);
|
||||
|
||||
KNNSimpleResultSet<DistanceType> resultSet(knn);
|
||||
for (size_t i = 0; i < queries.rows; i++) {
|
||||
resultSet.init(indices[i], dists[i]);
|
||||
findNeighbors(resultSet, queries[i], params);
|
||||
}
|
||||
}
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return index_params_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find set of nearest neighbors to vec. Their indices are stored inside
|
||||
* the result object.
|
||||
*
|
||||
* Params:
|
||||
* result = the result object in which the indices of the nearest-neighbors are stored
|
||||
* vec = the vector for which to search the nearest neighbors
|
||||
* maxCheck = the maximum number of restarts (in a best-bin-first manner)
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
|
||||
{
|
||||
float epsError = 1+get_param(searchParams,"eps",0.0f);
|
||||
|
||||
std::vector<DistanceType> dists(dim_,0);
|
||||
DistanceType distsq = computeInitialDistances(vec, dists);
|
||||
searchLevel(result, vec, root_node_, distsq, dists, epsError);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/*--------------------- Internal Data Structures --------------------------*/
|
||||
struct Node
|
||||
{
|
||||
/**
|
||||
* Indices of points in leaf node
|
||||
*/
|
||||
int left, right;
|
||||
/**
|
||||
* Dimension used for subdivision.
|
||||
*/
|
||||
int divfeat;
|
||||
/**
|
||||
* The values used for subdivision.
|
||||
*/
|
||||
DistanceType divlow, divhigh;
|
||||
/**
|
||||
* The child nodes.
|
||||
*/
|
||||
Node* child1, * child2;
|
||||
};
|
||||
typedef Node* NodePtr;
|
||||
|
||||
|
||||
struct Interval
|
||||
{
|
||||
DistanceType low, high;
|
||||
};
|
||||
|
||||
typedef std::vector<Interval> BoundingBox;
|
||||
|
||||
typedef BranchStruct<NodePtr, DistanceType> BranchSt;
|
||||
typedef BranchSt* Branch;
|
||||
|
||||
|
||||
|
||||
|
||||
void save_tree(FILE* stream, NodePtr tree)
|
||||
{
|
||||
save_value(stream, *tree);
|
||||
if (tree->child1!=NULL) {
|
||||
save_tree(stream, tree->child1);
|
||||
}
|
||||
if (tree->child2!=NULL) {
|
||||
save_tree(stream, tree->child2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void load_tree(FILE* stream, NodePtr& tree)
|
||||
{
|
||||
tree = pool_.allocate<Node>();
|
||||
load_value(stream, *tree);
|
||||
if (tree->child1!=NULL) {
|
||||
load_tree(stream, tree->child1);
|
||||
}
|
||||
if (tree->child2!=NULL) {
|
||||
load_tree(stream, tree->child2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void computeBoundingBox(BoundingBox& bbox)
|
||||
{
|
||||
bbox.resize(dim_);
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
bbox[i].low = (DistanceType)dataset_[0][i];
|
||||
bbox[i].high = (DistanceType)dataset_[0][i];
|
||||
}
|
||||
for (size_t k=1; k<dataset_.rows; ++k) {
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
if (dataset_[k][i]<bbox[i].low) bbox[i].low = (DistanceType)dataset_[k][i];
|
||||
if (dataset_[k][i]>bbox[i].high) bbox[i].high = (DistanceType)dataset_[k][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a tree node that subdivides the list of vecs from vind[first]
|
||||
* to vind[last]. The routine is called recursively on each sublist.
|
||||
* Place a pointer to this new tree node in the location pTree.
|
||||
*
|
||||
* Params: pTree = the new node to create
|
||||
* first = index of the first vector
|
||||
* last = index of the last vector
|
||||
*/
|
||||
NodePtr divideTree(int left, int right, BoundingBox& bbox)
|
||||
{
|
||||
NodePtr node = pool_.allocate<Node>(); // allocate memory
|
||||
|
||||
/* If too few exemplars remain, then make this a leaf node. */
|
||||
if ( (right-left) <= leaf_max_size_) {
|
||||
node->child1 = node->child2 = NULL; /* Mark as leaf node. */
|
||||
node->left = left;
|
||||
node->right = right;
|
||||
|
||||
// compute bounding-box of leaf points
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
bbox[i].low = (DistanceType)dataset_[vind_[left]][i];
|
||||
bbox[i].high = (DistanceType)dataset_[vind_[left]][i];
|
||||
}
|
||||
for (int k=left+1; k<right; ++k) {
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
if (bbox[i].low>dataset_[vind_[k]][i]) bbox[i].low=(DistanceType)dataset_[vind_[k]][i];
|
||||
if (bbox[i].high<dataset_[vind_[k]][i]) bbox[i].high=(DistanceType)dataset_[vind_[k]][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
int idx;
|
||||
int cutfeat;
|
||||
DistanceType cutval;
|
||||
middleSplit_(&vind_[0]+left, right-left, idx, cutfeat, cutval, bbox);
|
||||
|
||||
node->divfeat = cutfeat;
|
||||
|
||||
BoundingBox left_bbox(bbox);
|
||||
left_bbox[cutfeat].high = cutval;
|
||||
node->child1 = divideTree(left, left+idx, left_bbox);
|
||||
|
||||
BoundingBox right_bbox(bbox);
|
||||
right_bbox[cutfeat].low = cutval;
|
||||
node->child2 = divideTree(left+idx, right, right_bbox);
|
||||
|
||||
node->divlow = left_bbox[cutfeat].high;
|
||||
node->divhigh = right_bbox[cutfeat].low;
|
||||
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low);
|
||||
bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high);
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void computeMinMax(int* ind, int count, int dim, ElementType& min_elem, ElementType& max_elem)
|
||||
{
|
||||
min_elem = dataset_[ind[0]][dim];
|
||||
max_elem = dataset_[ind[0]][dim];
|
||||
for (int i=1; i<count; ++i) {
|
||||
ElementType val = dataset_[ind[i]][dim];
|
||||
if (val<min_elem) min_elem = val;
|
||||
if (val>max_elem) max_elem = val;
|
||||
}
|
||||
}
|
||||
|
||||
void middleSplit(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)
|
||||
{
|
||||
// find the largest span from the approximate bounding box
|
||||
ElementType max_span = bbox[0].high-bbox[0].low;
|
||||
cutfeat = 0;
|
||||
cutval = (bbox[0].high+bbox[0].low)/2;
|
||||
for (size_t i=1; i<dim_; ++i) {
|
||||
ElementType span = bbox[i].high-bbox[i].low;
|
||||
if (span>max_span) {
|
||||
max_span = span;
|
||||
cutfeat = i;
|
||||
cutval = (bbox[i].high+bbox[i].low)/2;
|
||||
}
|
||||
}
|
||||
|
||||
// compute exact span on the found dimension
|
||||
ElementType min_elem, max_elem;
|
||||
computeMinMax(ind, count, cutfeat, min_elem, max_elem);
|
||||
cutval = (min_elem+max_elem)/2;
|
||||
max_span = max_elem - min_elem;
|
||||
|
||||
// check if a dimension of a largest span exists
|
||||
size_t k = cutfeat;
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
if (i==k) continue;
|
||||
ElementType span = bbox[i].high-bbox[i].low;
|
||||
if (span>max_span) {
|
||||
computeMinMax(ind, count, i, min_elem, max_elem);
|
||||
span = max_elem - min_elem;
|
||||
if (span>max_span) {
|
||||
max_span = span;
|
||||
cutfeat = i;
|
||||
cutval = (min_elem+max_elem)/2;
|
||||
}
|
||||
}
|
||||
}
|
||||
int lim1, lim2;
|
||||
planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
|
||||
|
||||
if (lim1>count/2) index = lim1;
|
||||
else if (lim2<count/2) index = lim2;
|
||||
else index = count/2;
|
||||
}
|
||||
|
||||
|
||||
void middleSplit_(int* ind, int count, int& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)
|
||||
{
|
||||
const float EPS=0.00001f;
|
||||
DistanceType max_span = bbox[0].high-bbox[0].low;
|
||||
for (size_t i=1; i<dim_; ++i) {
|
||||
DistanceType span = bbox[i].high-bbox[i].low;
|
||||
if (span>max_span) {
|
||||
max_span = span;
|
||||
}
|
||||
}
|
||||
DistanceType max_spread = -1;
|
||||
cutfeat = 0;
|
||||
for (size_t i=0; i<dim_; ++i) {
|
||||
DistanceType span = bbox[i].high-bbox[i].low;
|
||||
if (span>(DistanceType)((1-EPS)*max_span)) {
|
||||
ElementType min_elem, max_elem;
|
||||
computeMinMax(ind, count, (int)i, min_elem, max_elem);
|
||||
DistanceType spread = (DistanceType)(max_elem-min_elem);
|
||||
if (spread>max_spread) {
|
||||
cutfeat = (int)i;
|
||||
max_spread = spread;
|
||||
}
|
||||
}
|
||||
}
|
||||
// split in the middle
|
||||
DistanceType split_val = (bbox[cutfeat].low+bbox[cutfeat].high)/2;
|
||||
ElementType min_elem, max_elem;
|
||||
computeMinMax(ind, count, cutfeat, min_elem, max_elem);
|
||||
|
||||
if (split_val<min_elem) cutval = (DistanceType)min_elem;
|
||||
else if (split_val>max_elem) cutval = (DistanceType)max_elem;
|
||||
else cutval = split_val;
|
||||
|
||||
int lim1, lim2;
|
||||
planeSplit(ind, count, cutfeat, cutval, lim1, lim2);
|
||||
|
||||
if (lim1>count/2) index = lim1;
|
||||
else if (lim2<count/2) index = lim2;
|
||||
else index = count/2;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subdivide the list of points by a plane perpendicular on axe corresponding
|
||||
* to the 'cutfeat' dimension at 'cutval' position.
|
||||
*
|
||||
* On return:
|
||||
* dataset[ind[0..lim1-1]][cutfeat]<cutval
|
||||
* dataset[ind[lim1..lim2-1]][cutfeat]==cutval
|
||||
* dataset[ind[lim2..count]][cutfeat]>cutval
|
||||
*/
|
||||
void planeSplit(int* ind, int count, int cutfeat, DistanceType cutval, int& lim1, int& lim2)
|
||||
{
|
||||
/* Move vector indices for left subtree to front of list. */
|
||||
int left = 0;
|
||||
int right = count-1;
|
||||
for (;; ) {
|
||||
while (left<=right && dataset_[ind[left]][cutfeat]<cutval) ++left;
|
||||
while (left<=right && dataset_[ind[right]][cutfeat]>=cutval) --right;
|
||||
if (left>right) break;
|
||||
std::swap(ind[left], ind[right]); ++left; --right;
|
||||
}
|
||||
/* If either list is empty, it means that all remaining features
|
||||
* are identical. Split in the middle to maintain a balanced tree.
|
||||
*/
|
||||
lim1 = left;
|
||||
right = count-1;
|
||||
for (;; ) {
|
||||
while (left<=right && dataset_[ind[left]][cutfeat]<=cutval) ++left;
|
||||
while (left<=right && dataset_[ind[right]][cutfeat]>cutval) --right;
|
||||
if (left>right) break;
|
||||
std::swap(ind[left], ind[right]); ++left; --right;
|
||||
}
|
||||
lim2 = left;
|
||||
}
|
||||
|
||||
DistanceType computeInitialDistances(const ElementType* vec, std::vector<DistanceType>& dists)
|
||||
{
|
||||
DistanceType distsq = 0.0;
|
||||
|
||||
for (size_t i = 0; i < dim_; ++i) {
|
||||
if (vec[i] < root_bbox_[i].low) {
|
||||
dists[i] = distance_.accum_dist(vec[i], root_bbox_[i].low, (int)i);
|
||||
distsq += dists[i];
|
||||
}
|
||||
if (vec[i] > root_bbox_[i].high) {
|
||||
dists[i] = distance_.accum_dist(vec[i], root_bbox_[i].high, (int)i);
|
||||
distsq += dists[i];
|
||||
}
|
||||
}
|
||||
|
||||
return distsq;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an exact search in the tree starting from a node.
|
||||
*/
|
||||
void searchLevel(ResultSet<DistanceType>& result_set, const ElementType* vec, const NodePtr node, DistanceType mindistsq,
|
||||
std::vector<DistanceType>& dists, const float epsError)
|
||||
{
|
||||
/* If this is a leaf node, then do check and return. */
|
||||
if ((node->child1 == NULL)&&(node->child2 == NULL)) {
|
||||
DistanceType worst_dist = result_set.worstDist();
|
||||
if (reorder_) {
|
||||
for (int i=node->left; i<node->right; ++i) {
|
||||
DistanceType dist = distance_(vec, data_[i], dim_, worst_dist);
|
||||
if (dist<worst_dist) {
|
||||
result_set.addPoint(dist,vind_[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i=node->left; i<node->right; ++i) {
|
||||
DistanceType dist = distance_(vec, data_[vind_[i]], dim_, worst_dist);
|
||||
if (dist<worst_dist) {
|
||||
result_set.addPoint(dist,vind_[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* Which child branch should be taken first? */
|
||||
int idx = node->divfeat;
|
||||
ElementType val = vec[idx];
|
||||
DistanceType diff1 = val - node->divlow;
|
||||
DistanceType diff2 = val - node->divhigh;
|
||||
|
||||
NodePtr bestChild;
|
||||
NodePtr otherChild;
|
||||
DistanceType cut_dist;
|
||||
if ((diff1+diff2)<0) {
|
||||
bestChild = node->child1;
|
||||
otherChild = node->child2;
|
||||
cut_dist = distance_.accum_dist(val, node->divhigh, idx);
|
||||
}
|
||||
else {
|
||||
bestChild = node->child2;
|
||||
otherChild = node->child1;
|
||||
cut_dist = distance_.accum_dist( val, node->divlow, idx);
|
||||
}
|
||||
|
||||
/* Call recursively to search next level down. */
|
||||
searchLevel(result_set, vec, bestChild, mindistsq, dists, epsError);
|
||||
|
||||
DistanceType dst = dists[idx];
|
||||
mindistsq = mindistsq + cut_dist - dst;
|
||||
dists[idx] = cut_dist;
|
||||
if (mindistsq*epsError<=result_set.worstDist()) {
|
||||
searchLevel(result_set, vec, otherChild, mindistsq, dists, epsError);
|
||||
}
|
||||
dists[idx] = dst;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* The dataset used by this index
|
||||
*/
|
||||
const Matrix<ElementType> dataset_;
|
||||
|
||||
IndexParams index_params_;
|
||||
|
||||
int leaf_max_size_;
|
||||
bool reorder_;
|
||||
|
||||
|
||||
/**
|
||||
* Array of indices to vectors in the dataset.
|
||||
*/
|
||||
std::vector<int> vind_;
|
||||
|
||||
Matrix<ElementType> data_;
|
||||
|
||||
size_t size_;
|
||||
size_t dim_;
|
||||
|
||||
/**
|
||||
* Array of k-d trees used to find neighbours.
|
||||
*/
|
||||
NodePtr root_node_;
|
||||
|
||||
BoundingBox root_bbox_;
|
||||
|
||||
/**
|
||||
* Pooled memory allocator.
|
||||
*
|
||||
* Using a pooled memory allocator is more efficient
|
||||
* than allocating memory directly when there is a large
|
||||
* number small of memory allocations.
|
||||
*/
|
||||
PooledAllocator pool_;
|
||||
|
||||
Distance distance_;
|
||||
}; // class KDTree
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_KDTREE_SINGLE_INDEX_H_
|
||||
+1819
File diff suppressed because it is too large
Load Diff
+135
@@ -0,0 +1,135 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_LINEAR_INDEX_H_
|
||||
#define OPENCV_FLANN_LINEAR_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "nn_index.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
struct LinearIndexParams : public IndexParams
|
||||
{
|
||||
LinearIndexParams()
|
||||
{
|
||||
(* this)["algorithm"] = FLANN_INDEX_LINEAR;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Distance>
|
||||
class LinearIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
|
||||
LinearIndex(const Matrix<ElementType>& inputData, const IndexParams& params = LinearIndexParams(),
|
||||
Distance d = Distance()) :
|
||||
dataset_(inputData), index_params_(params), distance_(d)
|
||||
{
|
||||
}
|
||||
|
||||
LinearIndex(const LinearIndex&);
|
||||
LinearIndex& operator=(const LinearIndex&);
|
||||
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_LINEAR;
|
||||
}
|
||||
|
||||
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return dataset_.rows;
|
||||
}
|
||||
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return dataset_.cols;
|
||||
}
|
||||
|
||||
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
/* nothing to do here for linear search */
|
||||
}
|
||||
|
||||
void saveIndex(FILE*) CV_OVERRIDE
|
||||
{
|
||||
/* nothing to do here for linear search */
|
||||
}
|
||||
|
||||
|
||||
void loadIndex(FILE*) CV_OVERRIDE
|
||||
{
|
||||
/* nothing to do here for linear search */
|
||||
|
||||
index_params_["algorithm"] = getType();
|
||||
}
|
||||
|
||||
void findNeighbors(ResultSet<DistanceType>& resultSet, const ElementType* vec, const SearchParams& /*searchParams*/) CV_OVERRIDE
|
||||
{
|
||||
ElementType* data = dataset_.data;
|
||||
for (size_t i = 0; i < dataset_.rows; ++i, data += dataset_.cols) {
|
||||
DistanceType dist = distance_(data, vec, dataset_.cols);
|
||||
resultSet.addPoint(dist, (int)i);
|
||||
}
|
||||
}
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return index_params_;
|
||||
}
|
||||
|
||||
private:
|
||||
/** The dataset */
|
||||
const Matrix<ElementType> dataset_;
|
||||
/** Index parameters */
|
||||
IndexParams index_params_;
|
||||
/** Index distance */
|
||||
Distance distance_;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif // OPENCV_FLANN_LINEAR_INDEX_H_
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_LOGGER_H
|
||||
#define OPENCV_FLANN_LOGGER_H
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
class Logger
|
||||
{
|
||||
Logger() : stream(stdout), logLevel(FLANN_LOG_WARN) {}
|
||||
|
||||
~Logger()
|
||||
{
|
||||
if ((stream!=NULL)&&(stream!=stdout)) {
|
||||
fclose(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static Logger& instance()
|
||||
{
|
||||
static Logger logger;
|
||||
return logger;
|
||||
}
|
||||
|
||||
void _setDestination(const char* name)
|
||||
{
|
||||
if (name==NULL) {
|
||||
stream = stdout;
|
||||
}
|
||||
else {
|
||||
#ifdef _MSC_VER
|
||||
if (fopen_s(&stream, name, "w") != 0)
|
||||
stream = NULL;
|
||||
#else
|
||||
stream = fopen(name,"w");
|
||||
#endif
|
||||
if (stream == NULL) {
|
||||
stream = stdout;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int _log(int level, const char* fmt, va_list arglist)
|
||||
{
|
||||
if (level > logLevel ) return -1;
|
||||
int ret = vfprintf(stream, fmt, arglist);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the logging level. All messages with lower priority will be ignored.
|
||||
* @param level Logging level
|
||||
*/
|
||||
static void setLevel(int level) { instance().logLevel = level; }
|
||||
|
||||
/**
|
||||
* Sets the logging destination
|
||||
* @param name Filename or NULL for console
|
||||
*/
|
||||
static void setDestination(const char* name) { instance()._setDestination(name); }
|
||||
|
||||
/**
|
||||
* Print log message
|
||||
* @param level Log level
|
||||
* @param fmt Message format
|
||||
* @return
|
||||
*/
|
||||
static int log(int level, const char* fmt, ...)
|
||||
{
|
||||
va_list arglist;
|
||||
va_start(arglist, fmt);
|
||||
int ret = instance()._log(level,fmt,arglist);
|
||||
va_end(arglist);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define LOG_METHOD(NAME,LEVEL) \
|
||||
static int NAME(const char* fmt, ...) \
|
||||
{ \
|
||||
va_list ap; \
|
||||
va_start(ap, fmt); \
|
||||
int ret = instance()._log(LEVEL, fmt, ap); \
|
||||
va_end(ap); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
LOG_METHOD(fatal, FLANN_LOG_FATAL)
|
||||
LOG_METHOD(error, FLANN_LOG_ERROR)
|
||||
LOG_METHOD(warn, FLANN_LOG_WARN)
|
||||
LOG_METHOD(info, FLANN_LOG_INFO)
|
||||
|
||||
private:
|
||||
FILE* stream;
|
||||
int logLevel;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_LOGGER_H
|
||||
+403
@@ -0,0 +1,403 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
/***********************************************************************
|
||||
* Author: Vincent Rabaud
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_LSH_INDEX_H_
|
||||
#define OPENCV_FLANN_LSH_INDEX_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "nn_index.h"
|
||||
#include "matrix.h"
|
||||
#include "result_set.h"
|
||||
#include "heap.h"
|
||||
#include "lsh_table.h"
|
||||
#include "allocator.h"
|
||||
#include "random.h"
|
||||
#include "saving.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4702) //disable unreachable code
|
||||
#endif
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
struct LshIndexParams : public IndexParams
|
||||
{
|
||||
LshIndexParams(int table_number = 12, int key_size = 20, int multi_probe_level = 2)
|
||||
{
|
||||
(*this)["algorithm"] = FLANN_INDEX_LSH;
|
||||
// The number of hash tables to use
|
||||
(*this)["table_number"] = table_number;
|
||||
// The length of the key in the hash tables
|
||||
(*this)["key_size"] = key_size;
|
||||
// Number of levels to use in multi-probe (0 for standard LSH)
|
||||
(*this)["multi_probe_level"] = multi_probe_level;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Locality-sensitive hashing index
|
||||
*
|
||||
* Contains the tables and other information for indexing a set of points
|
||||
* for nearest-neighbor matching.
|
||||
*/
|
||||
template<typename Distance>
|
||||
class LshIndex : public NNIndex<Distance>
|
||||
{
|
||||
public:
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
/** Constructor
|
||||
* @param input_data dataset with the input features
|
||||
* @param params parameters passed to the LSH algorithm
|
||||
* @param d the distance used
|
||||
*/
|
||||
LshIndex(const Matrix<ElementType>& input_data, const IndexParams& params = LshIndexParams(),
|
||||
Distance d = Distance()) :
|
||||
dataset_(input_data), index_params_(params), distance_(d)
|
||||
{
|
||||
// cv::flann::IndexParams sets integer params as 'int', so it is used with get_param
|
||||
// in place of 'unsigned int'
|
||||
table_number_ = get_param(index_params_,"table_number",12);
|
||||
key_size_ = get_param(index_params_,"key_size",20);
|
||||
multi_probe_level_ = get_param(index_params_,"multi_probe_level",2);
|
||||
|
||||
feature_size_ = (unsigned)dataset_.cols;
|
||||
fill_xor_mask(0, key_size_, multi_probe_level_, xor_masks_);
|
||||
}
|
||||
|
||||
|
||||
LshIndex(const LshIndex&);
|
||||
LshIndex& operator=(const LshIndex&);
|
||||
|
||||
/**
|
||||
* Builds the index
|
||||
*/
|
||||
void buildIndex() CV_OVERRIDE
|
||||
{
|
||||
tables_.resize(table_number_);
|
||||
for (int i = 0; i < table_number_; ++i) {
|
||||
lsh::LshTable<ElementType>& table = tables_[i];
|
||||
table = lsh::LshTable<ElementType>(feature_size_, key_size_);
|
||||
|
||||
// Add the features to the table
|
||||
table.add(dataset_);
|
||||
}
|
||||
}
|
||||
|
||||
flann_algorithm_t getType() const CV_OVERRIDE
|
||||
{
|
||||
return FLANN_INDEX_LSH;
|
||||
}
|
||||
|
||||
|
||||
void saveIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
save_value(stream,table_number_);
|
||||
save_value(stream,key_size_);
|
||||
save_value(stream,multi_probe_level_);
|
||||
save_value(stream, dataset_);
|
||||
}
|
||||
|
||||
void loadIndex(FILE* stream) CV_OVERRIDE
|
||||
{
|
||||
load_value(stream, table_number_);
|
||||
load_value(stream, key_size_);
|
||||
load_value(stream, multi_probe_level_);
|
||||
load_value(stream, dataset_);
|
||||
// Building the index is so fast we can afford not storing it
|
||||
buildIndex();
|
||||
|
||||
index_params_["algorithm"] = getType();
|
||||
index_params_["table_number"] = table_number_;
|
||||
index_params_["key_size"] = key_size_;
|
||||
index_params_["multi_probe_level"] = multi_probe_level_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns size of index.
|
||||
*/
|
||||
size_t size() const CV_OVERRIDE
|
||||
{
|
||||
return dataset_.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of an index feature.
|
||||
*/
|
||||
size_t veclen() const CV_OVERRIDE
|
||||
{
|
||||
return feature_size_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the index memory usage
|
||||
* Returns: memory used by the index
|
||||
*/
|
||||
int usedMemory() const CV_OVERRIDE
|
||||
{
|
||||
return (int)(dataset_.rows * sizeof(int));
|
||||
}
|
||||
|
||||
|
||||
IndexParams getParameters() const CV_OVERRIDE
|
||||
{
|
||||
return index_params_;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Perform k-nearest neighbor search
|
||||
* \param[in] queries The query points for which to find the nearest neighbors
|
||||
* \param[out] indices The indices of the nearest neighbors found
|
||||
* \param[out] dists Distances to the nearest neighbors found
|
||||
* \param[in] knn Number of nearest neighbors to return
|
||||
* \param[in] params Search parameters
|
||||
*/
|
||||
virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params) CV_OVERRIDE
|
||||
{
|
||||
CV_Assert(queries.cols == veclen());
|
||||
CV_Assert(indices.rows >= queries.rows);
|
||||
CV_Assert(dists.rows >= queries.rows);
|
||||
CV_Assert(int(indices.cols) >= knn);
|
||||
CV_Assert(int(dists.cols) >= knn);
|
||||
|
||||
|
||||
KNNUniqueResultSet<DistanceType> resultSet(knn);
|
||||
for (size_t i = 0; i < queries.rows; i++) {
|
||||
resultSet.clear();
|
||||
std::fill_n(indices[i], knn, -1);
|
||||
std::fill_n(dists[i], knn, std::numeric_limits<DistanceType>::max());
|
||||
findNeighbors(resultSet, queries[i], params);
|
||||
if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
|
||||
else resultSet.copy(indices[i], dists[i], knn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find set of nearest neighbors to vec. Their indices are stored inside
|
||||
* the result object.
|
||||
*
|
||||
* Params:
|
||||
* result = the result object in which the indices of the nearest-neighbors are stored
|
||||
* vec = the vector for which to search the nearest neighbors
|
||||
* maxCheck = the maximum number of restarts (in a best-bin-first manner)
|
||||
*/
|
||||
void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& /*searchParams*/) CV_OVERRIDE
|
||||
{
|
||||
getNeighbors(vec, result);
|
||||
}
|
||||
|
||||
private:
|
||||
/** Defines the comparator on score and index
|
||||
*/
|
||||
typedef std::pair<float, unsigned int> ScoreIndexPair;
|
||||
struct SortScoreIndexPairOnSecond
|
||||
{
|
||||
bool operator()(const ScoreIndexPair& left, const ScoreIndexPair& right) const
|
||||
{
|
||||
return left.second < right.second;
|
||||
}
|
||||
};
|
||||
|
||||
/** Fills the different xor masks to use when getting the neighbors in multi-probe LSH
|
||||
* @param key the key we build neighbors from
|
||||
* @param lowest_index the lowest index of the bit set
|
||||
* @param level the multi-probe level we are at
|
||||
* @param xor_masks all the xor mask
|
||||
*/
|
||||
void fill_xor_mask(lsh::BucketKey key, int lowest_index, unsigned int level,
|
||||
std::vector<lsh::BucketKey>& xor_masks)
|
||||
{
|
||||
xor_masks.push_back(key);
|
||||
if (level == 0) return;
|
||||
for (int index = lowest_index - 1; index >= 0; --index) {
|
||||
// Create a new key
|
||||
lsh::BucketKey new_key = key | (1 << index);
|
||||
fill_xor_mask(new_key, index, level - 1, xor_masks);
|
||||
}
|
||||
}
|
||||
|
||||
/** Performs the approximate nearest-neighbor search.
|
||||
* @param vec the feature to analyze
|
||||
* @param do_radius flag indicating if we check the radius too
|
||||
* @param radius the radius if it is a radius search
|
||||
* @param do_k flag indicating if we limit the number of nn
|
||||
* @param k_nn the number of nearest neighbors
|
||||
* @param checked_average used for debugging
|
||||
*/
|
||||
void getNeighbors(const ElementType* vec, bool /*do_radius*/, float radius, bool do_k, unsigned int k_nn,
|
||||
float& /*checked_average*/)
|
||||
{
|
||||
static std::vector<ScoreIndexPair> score_index_heap;
|
||||
|
||||
if (do_k) {
|
||||
unsigned int worst_score = std::numeric_limits<unsigned int>::max();
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
|
||||
for (; table != table_end; ++table) {
|
||||
size_t key = table->getKey(vec);
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
|
||||
for (; xor_mask != xor_mask_end; ++xor_mask) {
|
||||
size_t sub_key = key ^ (*xor_mask);
|
||||
const lsh::Bucket* bucket = table->getBucketFromKey(sub_key);
|
||||
if (bucket == 0) continue;
|
||||
|
||||
// Go over each descriptor index
|
||||
std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
|
||||
std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
|
||||
DistanceType hamming_distance;
|
||||
|
||||
// Process the rest of the candidates
|
||||
for (; training_index < last_training_index; ++training_index) {
|
||||
hamming_distance = distance_(vec, dataset_[*training_index], dataset_.cols);
|
||||
|
||||
if (hamming_distance < worst_score) {
|
||||
// Insert the new element
|
||||
score_index_heap.push_back(ScoreIndexPair(hamming_distance, training_index));
|
||||
std::push_heap(score_index_heap.begin(), score_index_heap.end());
|
||||
|
||||
if (score_index_heap.size() > (unsigned int)k_nn) {
|
||||
// Remove the highest distance value as we have too many elements
|
||||
std::pop_heap(score_index_heap.begin(), score_index_heap.end());
|
||||
score_index_heap.pop_back();
|
||||
// Keep track of the worst score
|
||||
worst_score = score_index_heap.front().first;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
|
||||
for (; table != table_end; ++table) {
|
||||
size_t key = table->getKey(vec);
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
|
||||
for (; xor_mask != xor_mask_end; ++xor_mask) {
|
||||
size_t sub_key = key ^ (*xor_mask);
|
||||
const lsh::Bucket* bucket = table->getBucketFromKey(sub_key);
|
||||
if (bucket == 0) continue;
|
||||
|
||||
// Go over each descriptor index
|
||||
std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
|
||||
std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
|
||||
DistanceType hamming_distance;
|
||||
|
||||
// Process the rest of the candidates
|
||||
for (; training_index < last_training_index; ++training_index) {
|
||||
// Compute the Hamming distance
|
||||
hamming_distance = distance_(vec, dataset_[*training_index], dataset_.cols);
|
||||
if (hamming_distance < radius) score_index_heap.push_back(ScoreIndexPair(hamming_distance, training_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Performs the approximate nearest-neighbor search.
|
||||
* This is a slower version than the above as it uses the ResultSet
|
||||
* @param vec the feature to analyze
|
||||
*/
|
||||
void getNeighbors(const ElementType* vec, ResultSet<DistanceType>& result)
|
||||
{
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table = tables_.begin();
|
||||
typename std::vector<lsh::LshTable<ElementType> >::const_iterator table_end = tables_.end();
|
||||
for (; table != table_end; ++table) {
|
||||
size_t key = table->getKey(vec);
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask = xor_masks_.begin();
|
||||
std::vector<lsh::BucketKey>::const_iterator xor_mask_end = xor_masks_.end();
|
||||
for (; xor_mask != xor_mask_end; ++xor_mask) {
|
||||
size_t sub_key = key ^ (*xor_mask);
|
||||
const lsh::Bucket* bucket = table->getBucketFromKey((lsh::BucketKey)sub_key);
|
||||
if (bucket == 0) continue;
|
||||
|
||||
// Go over each descriptor index
|
||||
std::vector<lsh::FeatureIndex>::const_iterator training_index = bucket->begin();
|
||||
std::vector<lsh::FeatureIndex>::const_iterator last_training_index = bucket->end();
|
||||
DistanceType hamming_distance;
|
||||
|
||||
// Process the rest of the candidates
|
||||
for (; training_index < last_training_index; ++training_index) {
|
||||
// Compute the Hamming distance
|
||||
hamming_distance = distance_(vec, dataset_[*training_index], (int)dataset_.cols);
|
||||
result.addPoint(hamming_distance, *training_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The different hash tables */
|
||||
std::vector<lsh::LshTable<ElementType> > tables_;
|
||||
|
||||
/** The data the LSH tables where built from */
|
||||
Matrix<ElementType> dataset_;
|
||||
|
||||
/** The size of the features (as ElementType[]) */
|
||||
unsigned int feature_size_;
|
||||
|
||||
IndexParams index_params_;
|
||||
|
||||
/** table number */
|
||||
int table_number_;
|
||||
/** key size */
|
||||
int key_size_;
|
||||
/** How far should we look for neighbors in multi-probe LSH */
|
||||
int multi_probe_level_;
|
||||
|
||||
/** The XOR masks to apply to a key to get the neighboring buckets */
|
||||
std::vector<lsh::BucketKey> xor_masks_;
|
||||
|
||||
Distance distance_;
|
||||
};
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_LSH_INDEX_H_
|
||||
+525
@@ -0,0 +1,525 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
/***********************************************************************
|
||||
* Author: Vincent Rabaud
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_LSH_TABLE_H_
|
||||
#define OPENCV_FLANN_LSH_TABLE_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <limits.h>
|
||||
// TODO as soon as we use C++0x, use the code in USE_UNORDERED_MAP
|
||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||
# define USE_UNORDERED_MAP 1
|
||||
#else
|
||||
# define USE_UNORDERED_MAP 0
|
||||
#endif
|
||||
#if USE_UNORDERED_MAP
|
||||
#include <unordered_map>
|
||||
#else
|
||||
#include <map>
|
||||
#endif
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "dynamic_bitset.h"
|
||||
#include "matrix.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4702) //disable unreachable code
|
||||
#endif
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
namespace lsh
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** What is stored in an LSH bucket
|
||||
*/
|
||||
typedef uint32_t FeatureIndex;
|
||||
/** The id from which we can get a bucket back in an LSH table
|
||||
*/
|
||||
typedef unsigned int BucketKey;
|
||||
|
||||
/** A bucket in an LSH table
|
||||
*/
|
||||
typedef std::vector<FeatureIndex> Bucket;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** POD for stats about an LSH table
|
||||
*/
|
||||
struct LshStats
|
||||
{
|
||||
std::vector<unsigned int> bucket_sizes_;
|
||||
size_t n_buckets_;
|
||||
size_t bucket_size_mean_;
|
||||
size_t bucket_size_median_;
|
||||
size_t bucket_size_min_;
|
||||
size_t bucket_size_max_;
|
||||
size_t bucket_size_std_dev;
|
||||
/** Each contained vector contains three value: beginning/end for interval, number of elements in the bin
|
||||
*/
|
||||
std::vector<std::vector<unsigned int> > size_histogram_;
|
||||
};
|
||||
|
||||
/** Overload the << operator for LshStats
|
||||
* @param out the streams
|
||||
* @param stats the stats to display
|
||||
* @return the streams
|
||||
*/
|
||||
inline std::ostream& operator <<(std::ostream& out, const LshStats& stats)
|
||||
{
|
||||
int w = 20;
|
||||
out << "Lsh Table Stats:\n" << std::setw(w) << std::setiosflags(std::ios::right) << "N buckets : "
|
||||
<< stats.n_buckets_ << "\n" << std::setw(w) << std::setiosflags(std::ios::right) << "mean size : "
|
||||
<< std::setiosflags(std::ios::left) << stats.bucket_size_mean_ << "\n" << std::setw(w)
|
||||
<< std::setiosflags(std::ios::right) << "median size : " << stats.bucket_size_median_ << "\n" << std::setw(w)
|
||||
<< std::setiosflags(std::ios::right) << "min size : " << std::setiosflags(std::ios::left)
|
||||
<< stats.bucket_size_min_ << "\n" << std::setw(w) << std::setiosflags(std::ios::right) << "max size : "
|
||||
<< std::setiosflags(std::ios::left) << stats.bucket_size_max_;
|
||||
|
||||
// Display the histogram
|
||||
out << std::endl << std::setw(w) << std::setiosflags(std::ios::right) << "histogram : "
|
||||
<< std::setiosflags(std::ios::left);
|
||||
for (std::vector<std::vector<unsigned int> >::const_iterator iterator = stats.size_histogram_.begin(), end =
|
||||
stats.size_histogram_.end(); iterator != end; ++iterator) out << (*iterator)[0] << "-" << (*iterator)[1] << ": " << (*iterator)[2] << ", ";
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** Lsh hash table. As its key is a sub-feature, and as usually
|
||||
* the size of it is pretty small, we keep it as a continuous memory array.
|
||||
* The value is an index in the corpus of features (we keep it as an unsigned
|
||||
* int for pure memory reasons, it could be a size_t)
|
||||
*/
|
||||
template<typename ElementType>
|
||||
class LshTable
|
||||
{
|
||||
public:
|
||||
/** A container of all the feature indices. Optimized for space
|
||||
*/
|
||||
#if USE_UNORDERED_MAP
|
||||
typedef std::unordered_map<BucketKey, Bucket> BucketsSpace;
|
||||
#else
|
||||
typedef std::map<BucketKey, Bucket> BucketsSpace;
|
||||
#endif
|
||||
|
||||
/** A container of all the feature indices. Optimized for speed
|
||||
*/
|
||||
typedef std::vector<Bucket> BucketsSpeed;
|
||||
|
||||
/** Default constructor
|
||||
*/
|
||||
LshTable()
|
||||
{
|
||||
key_size_ = 0;
|
||||
feature_size_ = 0;
|
||||
speed_level_ = kArray;
|
||||
}
|
||||
|
||||
/** Default constructor
|
||||
* Create the mask and allocate the memory
|
||||
* @param feature_size is the size of the feature (considered as a ElementType[])
|
||||
* @param key_size is the number of bits that are turned on in the feature
|
||||
*/
|
||||
LshTable(unsigned int feature_size, unsigned int key_size)
|
||||
{
|
||||
feature_size_ = feature_size;
|
||||
CV_UNUSED(key_size);
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "LSH is not implemented for that type" );
|
||||
}
|
||||
|
||||
/** Add a feature to the table
|
||||
* @param value the value to store for that feature
|
||||
* @param feature the feature itself
|
||||
*/
|
||||
void add(unsigned int value, const ElementType* feature)
|
||||
{
|
||||
// Add the value to the corresponding bucket
|
||||
BucketKey key = (lsh::BucketKey)getKey(feature);
|
||||
|
||||
switch (speed_level_) {
|
||||
case kArray:
|
||||
// That means we get the buckets from an array
|
||||
buckets_speed_[key].push_back(value);
|
||||
break;
|
||||
case kBitsetHash:
|
||||
// That means we can check the bitset for the presence of a key
|
||||
key_bitset_.set(key);
|
||||
buckets_space_[key].push_back(value);
|
||||
break;
|
||||
case kHash:
|
||||
{
|
||||
// That means we have to check for the hash table for the presence of a key
|
||||
buckets_space_[key].push_back(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Add a set of features to the table
|
||||
* @param dataset the values to store
|
||||
*/
|
||||
void add(Matrix<ElementType> dataset)
|
||||
{
|
||||
#if USE_UNORDERED_MAP
|
||||
buckets_space_.rehash((buckets_space_.size() + dataset.rows) * 1.2);
|
||||
#endif
|
||||
// Add the features to the table
|
||||
for (unsigned int i = 0; i < dataset.rows; ++i) add(i, dataset[i]);
|
||||
// Now that the table is full, optimize it for speed/space
|
||||
optimize();
|
||||
}
|
||||
|
||||
/** Get a bucket given the key
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
inline const Bucket* getBucketFromKey(BucketKey key) const
|
||||
{
|
||||
// Generate other buckets
|
||||
switch (speed_level_) {
|
||||
case kArray:
|
||||
// That means we get the buckets from an array
|
||||
return &buckets_speed_[key];
|
||||
break;
|
||||
case kBitsetHash:
|
||||
// That means we can check the bitset for the presence of a key
|
||||
if (key_bitset_.test(key)) return &buckets_space_.find(key)->second;
|
||||
else return 0;
|
||||
break;
|
||||
case kHash:
|
||||
{
|
||||
// That means we have to check for the hash table for the presence of a key
|
||||
BucketsSpace::const_iterator bucket_it, bucket_end = buckets_space_.end();
|
||||
bucket_it = buckets_space_.find(key);
|
||||
// Stop here if that bucket does not exist
|
||||
if (bucket_it == bucket_end) return 0;
|
||||
else return &bucket_it->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Compute the sub-signature of a feature
|
||||
*/
|
||||
size_t getKey(const ElementType* /*feature*/) const
|
||||
{
|
||||
CV_Error(cv::Error::StsUnsupportedFormat, "LSH is not implemented for that type" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Get statistics about the table
|
||||
* @return
|
||||
*/
|
||||
LshStats getStats() const;
|
||||
|
||||
private:
|
||||
/** defines the speed fo the implementation
|
||||
* kArray uses a vector for storing data
|
||||
* kBitsetHash uses a hash map but checks for the validity of a key with a bitset
|
||||
* kHash uses a hash map only
|
||||
*/
|
||||
enum SpeedLevel
|
||||
{
|
||||
kArray, kBitsetHash, kHash
|
||||
};
|
||||
|
||||
/** Initialize some variables
|
||||
*/
|
||||
void initialize(size_t key_size)
|
||||
{
|
||||
const size_t key_size_lower_bound = 1;
|
||||
//a value (size_t(1) << key_size) must fit the size_t type so key_size has to be strictly less than size of size_t
|
||||
const size_t key_size_upper_bound = (std::min)(sizeof(BucketKey) * CHAR_BIT + 1, sizeof(size_t) * CHAR_BIT);
|
||||
if (key_size < key_size_lower_bound || key_size >= key_size_upper_bound)
|
||||
{
|
||||
CV_Error(cv::Error::StsBadArg, cv::format("Invalid key_size (=%d). Valid values for your system are %d <= key_size < %d.", (int)key_size, (int)key_size_lower_bound, (int)key_size_upper_bound));
|
||||
}
|
||||
|
||||
speed_level_ = kHash;
|
||||
key_size_ = (unsigned)key_size;
|
||||
}
|
||||
|
||||
/** Optimize the table for speed/space
|
||||
*/
|
||||
void optimize()
|
||||
{
|
||||
// If we are already using the fast storage, no need to do anything
|
||||
if (speed_level_ == kArray) return;
|
||||
|
||||
// Use an array if it will be more than half full
|
||||
if (buckets_space_.size() > ((size_t(1) << key_size_) / 2)) {
|
||||
speed_level_ = kArray;
|
||||
// Fill the array version of it
|
||||
buckets_speed_.resize(size_t(1) << key_size_);
|
||||
for (BucketsSpace::const_iterator key_bucket = buckets_space_.begin(); key_bucket != buckets_space_.end(); ++key_bucket) buckets_speed_[key_bucket->first] = key_bucket->second;
|
||||
|
||||
// Empty the hash table
|
||||
buckets_space_.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// If the bitset is going to use less than 10% of the RAM of the hash map (at least 1 size_t for the key and two
|
||||
// for the vector) or less than 512MB (key_size_ <= 30)
|
||||
if (((std::max(buckets_space_.size(), buckets_speed_.size()) * CHAR_BIT * 3 * sizeof(BucketKey)) / 10
|
||||
>= (size_t(1) << key_size_)) || (key_size_ <= 32)) {
|
||||
speed_level_ = kBitsetHash;
|
||||
key_bitset_.resize(size_t(1) << key_size_);
|
||||
key_bitset_.reset();
|
||||
// Try with the BucketsSpace
|
||||
for (BucketsSpace::const_iterator key_bucket = buckets_space_.begin(); key_bucket != buckets_space_.end(); ++key_bucket) key_bitset_.set(key_bucket->first);
|
||||
}
|
||||
else {
|
||||
speed_level_ = kHash;
|
||||
key_bitset_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/** The vector of all the buckets if they are held for speed
|
||||
*/
|
||||
BucketsSpeed buckets_speed_;
|
||||
|
||||
/** The hash table of all the buckets in case we cannot use the speed version
|
||||
*/
|
||||
BucketsSpace buckets_space_;
|
||||
|
||||
/** What is used to store the data */
|
||||
SpeedLevel speed_level_;
|
||||
|
||||
/** If the subkey is small enough, it will keep track of which subkeys are set through that bitset
|
||||
* That is just a speedup so that we don't look in the hash table (which can be mush slower that checking a bitset)
|
||||
*/
|
||||
DynamicBitset key_bitset_;
|
||||
|
||||
/** The size of the sub-signature in bits
|
||||
*/
|
||||
unsigned int key_size_;
|
||||
|
||||
unsigned int feature_size_;
|
||||
|
||||
// Members only used for the unsigned char specialization
|
||||
/** The mask to apply to a feature to get the hash key
|
||||
* Only used in the unsigned char case
|
||||
*/
|
||||
std::vector<size_t> mask_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Specialization for unsigned char
|
||||
|
||||
template<>
|
||||
inline LshTable<unsigned char>::LshTable(unsigned int feature_size, unsigned int subsignature_size)
|
||||
{
|
||||
feature_size_ = feature_size;
|
||||
initialize(subsignature_size);
|
||||
// Allocate the mask
|
||||
mask_ = std::vector<size_t>((feature_size * sizeof(char) + sizeof(size_t) - 1) / sizeof(size_t), 0);
|
||||
|
||||
// A bit brutal but fast to code
|
||||
std::vector<int> indices(feature_size * CHAR_BIT);
|
||||
for (size_t i = 0; i < feature_size * CHAR_BIT; ++i) indices[i] = (int)i;
|
||||
#ifndef OPENCV_FLANN_USE_STD_RAND
|
||||
cv::randShuffle(indices);
|
||||
#else
|
||||
std::random_shuffle(indices.begin(), indices.end());
|
||||
#endif
|
||||
|
||||
// Generate a random set of order of subsignature_size_ bits
|
||||
for (unsigned int i = 0; i < key_size_; ++i) {
|
||||
size_t index = indices[i];
|
||||
|
||||
// Set that bit in the mask
|
||||
size_t divisor = CHAR_BIT * sizeof(size_t);
|
||||
size_t idx = index / divisor; //pick the right size_t index
|
||||
mask_[idx] |= size_t(1) << (index % divisor); //use modulo to find the bit offset
|
||||
}
|
||||
|
||||
// Set to 1 if you want to display the mask for debug
|
||||
#if 0
|
||||
{
|
||||
size_t bcount = 0;
|
||||
BOOST_FOREACH(size_t mask_block, mask_){
|
||||
out << std::setw(sizeof(size_t) * CHAR_BIT / 4) << std::setfill('0') << std::hex << mask_block
|
||||
<< std::endl;
|
||||
bcount += __builtin_popcountll(mask_block);
|
||||
}
|
||||
out << "bit count : " << std::dec << bcount << std::endl;
|
||||
out << "mask size : " << mask_.size() << std::endl;
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Return the Subsignature of a feature
|
||||
* @param feature the feature to analyze
|
||||
*/
|
||||
template<>
|
||||
inline size_t LshTable<unsigned char>::getKey(const unsigned char* feature) const
|
||||
{
|
||||
// no need to check if T is dividable by sizeof(size_t) like in the Hamming
|
||||
// distance computation as we have a mask
|
||||
// FIXIT: This is bad assumption, because we reading tail bytes after of the allocated features buffer
|
||||
const size_t* feature_block_ptr = reinterpret_cast<const size_t*> ((const void*)feature);
|
||||
|
||||
// Figure out the subsignature of the feature
|
||||
// Given the feature ABCDEF, and the mask 001011, the output will be
|
||||
// 000CEF
|
||||
size_t subsignature = 0;
|
||||
size_t bit_index = 1;
|
||||
|
||||
for (unsigned i = 0; i < feature_size_; i += sizeof(size_t)) {
|
||||
// get the mask and signature blocks
|
||||
size_t feature_block;
|
||||
if (i <= feature_size_ - sizeof(size_t))
|
||||
{
|
||||
feature_block = *feature_block_ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t tmp = 0;
|
||||
memcpy(&tmp, feature_block_ptr, feature_size_ - i); // preserve bytes order
|
||||
feature_block = tmp;
|
||||
}
|
||||
size_t mask_block = mask_[i / sizeof(size_t)];
|
||||
while (mask_block) {
|
||||
// Get the lowest set bit in the mask block
|
||||
size_t lowest_bit = mask_block & (-(ptrdiff_t)mask_block);
|
||||
// Add it to the current subsignature if necessary
|
||||
subsignature += (feature_block & lowest_bit) ? bit_index : 0;
|
||||
// Reset the bit in the mask block
|
||||
mask_block ^= lowest_bit;
|
||||
// increment the bit index for the subsignature
|
||||
bit_index <<= 1;
|
||||
}
|
||||
// Check the next feature block
|
||||
++feature_block_ptr;
|
||||
}
|
||||
return subsignature;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline LshStats LshTable<unsigned char>::getStats() const
|
||||
{
|
||||
LshStats stats;
|
||||
stats.bucket_size_mean_ = 0;
|
||||
if ((buckets_speed_.empty()) && (buckets_space_.empty())) {
|
||||
stats.n_buckets_ = 0;
|
||||
stats.bucket_size_median_ = 0;
|
||||
stats.bucket_size_min_ = 0;
|
||||
stats.bucket_size_max_ = 0;
|
||||
return stats;
|
||||
}
|
||||
|
||||
if (!buckets_speed_.empty()) {
|
||||
for (BucketsSpeed::const_iterator pbucket = buckets_speed_.begin(); pbucket != buckets_speed_.end(); ++pbucket) {
|
||||
stats.bucket_sizes_.push_back((lsh::FeatureIndex)pbucket->size());
|
||||
stats.bucket_size_mean_ += pbucket->size();
|
||||
}
|
||||
stats.bucket_size_mean_ /= buckets_speed_.size();
|
||||
stats.n_buckets_ = buckets_speed_.size();
|
||||
}
|
||||
else {
|
||||
for (BucketsSpace::const_iterator x = buckets_space_.begin(); x != buckets_space_.end(); ++x) {
|
||||
stats.bucket_sizes_.push_back((lsh::FeatureIndex)x->second.size());
|
||||
stats.bucket_size_mean_ += x->second.size();
|
||||
}
|
||||
stats.bucket_size_mean_ /= buckets_space_.size();
|
||||
stats.n_buckets_ = buckets_space_.size();
|
||||
}
|
||||
|
||||
std::sort(stats.bucket_sizes_.begin(), stats.bucket_sizes_.end());
|
||||
|
||||
// BOOST_FOREACH(int size, stats.bucket_sizes_)
|
||||
// std::cout << size << " ";
|
||||
// std::cout << std::endl;
|
||||
stats.bucket_size_median_ = stats.bucket_sizes_[stats.bucket_sizes_.size() / 2];
|
||||
stats.bucket_size_min_ = stats.bucket_sizes_.front();
|
||||
stats.bucket_size_max_ = stats.bucket_sizes_.back();
|
||||
|
||||
// TODO compute mean and std
|
||||
/*float mean, stddev;
|
||||
stats.bucket_size_mean_ = mean;
|
||||
stats.bucket_size_std_dev = stddev;*/
|
||||
|
||||
// Include a histogram of the buckets
|
||||
unsigned int bin_start = 0;
|
||||
unsigned int bin_end = 20;
|
||||
bool is_new_bin = true;
|
||||
for (std::vector<unsigned int>::iterator iterator = stats.bucket_sizes_.begin(), end = stats.bucket_sizes_.end(); iterator
|
||||
!= end; )
|
||||
if (*iterator < bin_end) {
|
||||
if (is_new_bin) {
|
||||
stats.size_histogram_.push_back(std::vector<unsigned int>(3, 0));
|
||||
stats.size_histogram_.back()[0] = bin_start;
|
||||
stats.size_histogram_.back()[1] = bin_end - 1;
|
||||
is_new_bin = false;
|
||||
}
|
||||
++stats.size_histogram_.back()[2];
|
||||
++iterator;
|
||||
}
|
||||
else {
|
||||
bin_start += 20;
|
||||
bin_end += 20;
|
||||
is_new_bin = true;
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
// End the two namespaces
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_LSH_TABLE_H_ */
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_DATASET_H_
|
||||
#define OPENCV_FLANN_DATASET_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
* Class that implements a simple rectangular matrix stored in a memory buffer and
|
||||
* provides convenient matrix-like access using the [] operators.
|
||||
*/
|
||||
template <typename T>
|
||||
class Matrix
|
||||
{
|
||||
public:
|
||||
typedef T type;
|
||||
|
||||
size_t rows;
|
||||
size_t cols;
|
||||
size_t stride;
|
||||
T* data;
|
||||
|
||||
Matrix() : rows(0), cols(0), stride(0), data(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
Matrix(T* data_, size_t rows_, size_t cols_, size_t stride_ = 0) :
|
||||
rows(rows_), cols(cols_), stride(stride_), data(data_)
|
||||
{
|
||||
if (stride==0) stride = cols;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function for deallocating the storage data.
|
||||
*/
|
||||
CV_DEPRECATED void free()
|
||||
{
|
||||
fprintf(stderr, "The cvflann::Matrix<T>::free() method is deprecated "
|
||||
"and it does not do any memory deallocation any more. You are"
|
||||
"responsible for deallocating the matrix memory (by doing"
|
||||
"'delete[] matrix.data' for example)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Operator that return a (pointer to a) row of the data.
|
||||
*/
|
||||
T* operator[](size_t index) const
|
||||
{
|
||||
return data+index*stride;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class UntypedMatrix
|
||||
{
|
||||
public:
|
||||
size_t rows;
|
||||
size_t cols;
|
||||
void* data;
|
||||
flann_datatype_t type;
|
||||
|
||||
UntypedMatrix(void* data_, long rows_, long cols_) :
|
||||
rows(rows_), cols(cols_), data(data_)
|
||||
{
|
||||
}
|
||||
|
||||
~UntypedMatrix()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
Matrix<T> as()
|
||||
{
|
||||
return Matrix<T>((T*)data, rows, cols);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_DATASET_H_
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
/*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*/
|
||||
|
||||
#ifndef OPENCV_MINIFLANN_HPP
|
||||
#define OPENCV_MINIFLANN_HPP
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/flann/defines.h"
|
||||
|
||||
namespace cv
|
||||
{
|
||||
|
||||
namespace flann
|
||||
{
|
||||
|
||||
enum FlannIndexType {
|
||||
FLANN_INDEX_TYPE_8U = CV_8U,
|
||||
FLANN_INDEX_TYPE_8S = CV_8S,
|
||||
FLANN_INDEX_TYPE_16U = CV_16U,
|
||||
FLANN_INDEX_TYPE_16S = CV_16S,
|
||||
FLANN_INDEX_TYPE_32S = CV_32S,
|
||||
FLANN_INDEX_TYPE_32F = CV_32F,
|
||||
FLANN_INDEX_TYPE_64F = CV_64F,
|
||||
FLANN_INDEX_TYPE_STRING,
|
||||
FLANN_INDEX_TYPE_BOOL,
|
||||
FLANN_INDEX_TYPE_ALGORITHM,
|
||||
LAST_VALUE_FLANN_INDEX_TYPE = FLANN_INDEX_TYPE_ALGORITHM
|
||||
};
|
||||
|
||||
struct CV_EXPORTS IndexParams
|
||||
{
|
||||
IndexParams();
|
||||
~IndexParams();
|
||||
|
||||
String getString(const String& key, const String& defaultVal=String()) const;
|
||||
int getInt(const String& key, int defaultVal=-1) const;
|
||||
double getDouble(const String& key, double defaultVal=-1) const;
|
||||
|
||||
void setString(const String& key, const String& value);
|
||||
void setInt(const String& key, int value);
|
||||
void setDouble(const String& key, double value);
|
||||
void setFloat(const String& key, float value);
|
||||
void setBool(const String& key, bool value);
|
||||
void setAlgorithm(int value);
|
||||
|
||||
// FIXIT: replace by void write(FileStorage& fs) const + read()
|
||||
void getAll(std::vector<String>& names,
|
||||
std::vector<FlannIndexType>& types,
|
||||
std::vector<String>& strValues,
|
||||
std::vector<double>& numValues) const;
|
||||
|
||||
void* params;
|
||||
|
||||
private:
|
||||
IndexParams(const IndexParams &); // copy disabled
|
||||
IndexParams& operator=(const IndexParams &); // assign disabled
|
||||
};
|
||||
|
||||
struct CV_EXPORTS KDTreeIndexParams : public IndexParams
|
||||
{
|
||||
KDTreeIndexParams(int trees=4);
|
||||
};
|
||||
|
||||
struct CV_EXPORTS LinearIndexParams : public IndexParams
|
||||
{
|
||||
LinearIndexParams();
|
||||
};
|
||||
|
||||
struct CV_EXPORTS CompositeIndexParams : public IndexParams
|
||||
{
|
||||
CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
|
||||
cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
|
||||
};
|
||||
|
||||
struct CV_EXPORTS AutotunedIndexParams : public IndexParams
|
||||
{
|
||||
AutotunedIndexParams(float target_precision = 0.8f, float build_weight = 0.01f,
|
||||
float memory_weight = 0, float sample_fraction = 0.1f);
|
||||
};
|
||||
|
||||
struct CV_EXPORTS HierarchicalClusteringIndexParams : public IndexParams
|
||||
{
|
||||
HierarchicalClusteringIndexParams(int branching = 32,
|
||||
cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, int trees = 4, int leaf_size = 100 );
|
||||
};
|
||||
|
||||
struct CV_EXPORTS KMeansIndexParams : public IndexParams
|
||||
{
|
||||
KMeansIndexParams(int branching = 32, int iterations = 11,
|
||||
cvflann::flann_centers_init_t centers_init = cvflann::FLANN_CENTERS_RANDOM, float cb_index = 0.2f );
|
||||
};
|
||||
|
||||
struct CV_EXPORTS LshIndexParams : public IndexParams
|
||||
{
|
||||
LshIndexParams(int table_number, int key_size, int multi_probe_level);
|
||||
};
|
||||
|
||||
struct CV_EXPORTS SavedIndexParams : public IndexParams
|
||||
{
|
||||
SavedIndexParams(const String& filename);
|
||||
};
|
||||
|
||||
struct CV_EXPORTS SearchParams : public IndexParams
|
||||
{
|
||||
SearchParams( int checks, float eps, bool sorted, bool explore_all_trees );
|
||||
SearchParams( int checks = 32, float eps = 0, bool sorted = true );
|
||||
};
|
||||
|
||||
class CV_EXPORTS_W Index
|
||||
{
|
||||
public:
|
||||
CV_WRAP Index();
|
||||
CV_WRAP Index(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
|
||||
virtual ~Index();
|
||||
|
||||
CV_WRAP virtual void build(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
|
||||
CV_WRAP virtual void knnSearch(InputArray query, OutputArray indices,
|
||||
OutputArray dists, int knn, const SearchParams& params=SearchParams());
|
||||
|
||||
CV_WRAP virtual int radiusSearch(InputArray query, OutputArray indices,
|
||||
OutputArray dists, double radius, int maxResults,
|
||||
const SearchParams& params=SearchParams());
|
||||
|
||||
CV_WRAP virtual void save(const String& filename) const;
|
||||
CV_WRAP virtual bool load(InputArray features, const String& filename);
|
||||
CV_WRAP virtual void release();
|
||||
CV_WRAP cvflann::flann_distance_t getDistance() const;
|
||||
CV_WRAP cvflann::flann_algorithm_t getAlgorithm() const;
|
||||
|
||||
protected:
|
||||
bool load_(const String& filename);
|
||||
|
||||
cvflann::flann_distance_t distType;
|
||||
cvflann::flann_algorithm_t algo;
|
||||
int featureType;
|
||||
void* index;
|
||||
Mat features_clone; // index may store features pointer internally for searching, so avoid dangling pointers: https://github.com/opencv/opencv/issues/17553
|
||||
};
|
||||
|
||||
} } // namespace cv::flann
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_NNINDEX_H
|
||||
#define OPENCV_FLANN_NNINDEX_H
|
||||
|
||||
#include "matrix.h"
|
||||
#include "result_set.h"
|
||||
#include "params.h"
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
* Nearest-neighbour index base class
|
||||
*/
|
||||
template <typename Distance>
|
||||
class NNIndex
|
||||
{
|
||||
typedef typename Distance::ElementType ElementType;
|
||||
typedef typename Distance::ResultType DistanceType;
|
||||
|
||||
public:
|
||||
|
||||
virtual ~NNIndex() {}
|
||||
|
||||
/**
|
||||
* \brief Builds the index
|
||||
*/
|
||||
virtual void buildIndex() = 0;
|
||||
|
||||
/**
|
||||
* \brief Perform k-nearest neighbor search
|
||||
* \param[in] queries The query points for which to find the nearest neighbors
|
||||
* \param[out] indices The indices of the nearest neighbors found
|
||||
* \param[out] dists Distances to the nearest neighbors found
|
||||
* \param[in] knn Number of nearest neighbors to return
|
||||
* \param[in] params Search parameters
|
||||
*/
|
||||
virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
|
||||
{
|
||||
CV_Assert(queries.cols == veclen());
|
||||
CV_Assert(indices.rows >= queries.rows);
|
||||
CV_Assert(dists.rows >= queries.rows);
|
||||
CV_Assert(int(indices.cols) >= knn);
|
||||
CV_Assert(int(dists.cols) >= knn);
|
||||
|
||||
#if 0
|
||||
KNNResultSet<DistanceType> resultSet(knn);
|
||||
for (size_t i = 0; i < queries.rows; i++) {
|
||||
resultSet.init(indices[i], dists[i]);
|
||||
findNeighbors(resultSet, queries[i], params);
|
||||
}
|
||||
#else
|
||||
KNNUniqueResultSet<DistanceType> resultSet(knn);
|
||||
for (size_t i = 0; i < queries.rows; i++) {
|
||||
resultSet.clear();
|
||||
findNeighbors(resultSet, queries[i], params);
|
||||
if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
|
||||
else resultSet.copy(indices[i], dists[i], knn);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Perform radius search
|
||||
* \param[in] query The query point
|
||||
* \param[out] indices The indinces of the neighbors found within the given radius
|
||||
* \param[out] dists The distances to the nearest neighbors found
|
||||
* \param[in] radius The radius used for search
|
||||
* \param[in] params Search parameters
|
||||
* \returns Number of neighbors found
|
||||
*/
|
||||
virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
|
||||
{
|
||||
if (query.rows != 1) {
|
||||
fprintf(stderr, "I can only search one feature at a time for range search\n");
|
||||
return -1;
|
||||
}
|
||||
CV_Assert(query.cols == veclen());
|
||||
CV_Assert(indices.cols == dists.cols);
|
||||
|
||||
int n = 0;
|
||||
int* indices_ptr = NULL;
|
||||
DistanceType* dists_ptr = NULL;
|
||||
if (indices.cols > 0) {
|
||||
n = (int)indices.cols;
|
||||
indices_ptr = indices[0];
|
||||
dists_ptr = dists[0];
|
||||
}
|
||||
|
||||
RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
|
||||
resultSet.clear();
|
||||
findNeighbors(resultSet, query[0], params);
|
||||
if (n>0) {
|
||||
if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
|
||||
else resultSet.copy(indices_ptr, dists_ptr, n);
|
||||
}
|
||||
|
||||
return (int)resultSet.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Saves the index to a stream
|
||||
* \param stream The stream to save the index to
|
||||
*/
|
||||
virtual void saveIndex(FILE* stream) = 0;
|
||||
|
||||
/**
|
||||
* \brief Loads the index from a stream
|
||||
* \param stream The stream from which the index is loaded
|
||||
*/
|
||||
virtual void loadIndex(FILE* stream) = 0;
|
||||
|
||||
/**
|
||||
* \returns number of features in this index.
|
||||
*/
|
||||
virtual size_t size() const = 0;
|
||||
|
||||
/**
|
||||
* \returns The dimensionality of the features in this index.
|
||||
*/
|
||||
virtual size_t veclen() const = 0;
|
||||
|
||||
/**
|
||||
* \returns The amount of memory (in bytes) used by the index.
|
||||
*/
|
||||
virtual int usedMemory() const = 0;
|
||||
|
||||
/**
|
||||
* \returns The index type (kdtree, kmeans,...)
|
||||
*/
|
||||
virtual flann_algorithm_t getType() const = 0;
|
||||
|
||||
/**
|
||||
* \returns The index parameters
|
||||
*/
|
||||
virtual IndexParams getParameters() const = 0;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Method that searches for nearest-neighbours
|
||||
*/
|
||||
virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_NNINDEX_H
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_OBJECT_FACTORY_H_
|
||||
#define OPENCV_FLANN_OBJECT_FACTORY_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
class CreatorNotFound
|
||||
{
|
||||
};
|
||||
|
||||
template<typename BaseClass,
|
||||
typename UniqueIdType,
|
||||
typename ObjectCreator = BaseClass* (*)()>
|
||||
class ObjectFactory
|
||||
{
|
||||
typedef ObjectFactory<BaseClass,UniqueIdType,ObjectCreator> ThisClass;
|
||||
typedef std::map<UniqueIdType, ObjectCreator> ObjectRegistry;
|
||||
|
||||
// singleton class, private constructor
|
||||
ObjectFactory() {}
|
||||
|
||||
public:
|
||||
|
||||
bool subscribe(UniqueIdType id, ObjectCreator creator)
|
||||
{
|
||||
if (object_registry.find(id) != object_registry.end()) return false;
|
||||
|
||||
object_registry[id] = creator;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool unregister(UniqueIdType id)
|
||||
{
|
||||
return object_registry.erase(id) == 1;
|
||||
}
|
||||
|
||||
ObjectCreator create(UniqueIdType id)
|
||||
{
|
||||
typename ObjectRegistry::const_iterator iter = object_registry.find(id);
|
||||
|
||||
if (iter == object_registry.end()) {
|
||||
throw CreatorNotFound();
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
static ThisClass& instance()
|
||||
{
|
||||
static ThisClass the_factory;
|
||||
return the_factory;
|
||||
}
|
||||
private:
|
||||
ObjectRegistry object_registry;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_OBJECT_FACTORY_H_ */
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2011 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2011 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_PARAMS_H_
|
||||
#define OPENCV_FLANN_PARAMS_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "any.h"
|
||||
#include "general.h"
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
typedef std::map<cv::String, any> IndexParams;
|
||||
|
||||
struct SearchParams : public IndexParams
|
||||
{
|
||||
SearchParams(int checks = 32, float eps = 0, bool sorted = true )
|
||||
{
|
||||
init(checks, eps, sorted, false);
|
||||
}
|
||||
|
||||
SearchParams(int checks, float eps, bool sorted, bool explore_all_trees )
|
||||
{
|
||||
init(checks, eps, sorted, explore_all_trees);
|
||||
}
|
||||
|
||||
void init(int checks = 32, float eps = 0, bool sorted = true, bool explore_all_trees = false )
|
||||
{
|
||||
// how many leafs to visit when searching for neighbours (-1 for unlimited)
|
||||
(*this)["checks"] = checks;
|
||||
// search for eps-approximate neighbours (default: 0)
|
||||
(*this)["eps"] = eps;
|
||||
// only for radius search, require neighbours sorted by distance (default: true)
|
||||
(*this)["sorted"] = sorted;
|
||||
// if false, search stops at the tree reaching the number of max checks (original behavior).
|
||||
// When true, we do a descent in each tree and. Like before the alternative paths
|
||||
// stored in the heap are not be processed further when max checks is reached.
|
||||
(*this)["explore_all_trees"] = explore_all_trees;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
T get_param(const IndexParams& params, cv::String name, const T& default_value)
|
||||
{
|
||||
IndexParams::const_iterator it = params.find(name);
|
||||
if (it != params.end()) {
|
||||
return it->second.cast<T>();
|
||||
}
|
||||
else {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get_param(const IndexParams& params, cv::String name)
|
||||
{
|
||||
IndexParams::const_iterator it = params.find(name);
|
||||
if (it != params.end()) {
|
||||
return it->second.cast<T>();
|
||||
}
|
||||
else {
|
||||
FLANN_THROW(cv::Error::StsBadArg, cv::String("Missing parameter '")+name+cv::String("' in the parameters given"));
|
||||
}
|
||||
}
|
||||
|
||||
inline void print_params(const IndexParams& params, std::ostream& stream)
|
||||
{
|
||||
IndexParams::const_iterator it;
|
||||
|
||||
for(it=params.begin(); it!=params.end(); ++it) {
|
||||
stream << it->first << " : " << it->second << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
inline void print_params(const IndexParams& params)
|
||||
{
|
||||
print_params(params, std::cout);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_PARAMS_H_ */
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_RANDOM_H
|
||||
#define OPENCV_FLANN_RANDOM_H
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
inline int rand()
|
||||
{
|
||||
#ifndef OPENCV_FLANN_USE_STD_RAND
|
||||
# if INT_MAX == RAND_MAX
|
||||
int v = cv::theRNG().next() & INT_MAX;
|
||||
# else
|
||||
int v = cv::theRNG().uniform(0, RAND_MAX + 1);
|
||||
# endif
|
||||
#else
|
||||
int v = std::rand();
|
||||
#endif // OPENCV_FLANN_USE_STD_RAND
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the random number generator
|
||||
* @param seed Random seed
|
||||
*/
|
||||
inline void seed_random(unsigned int seed)
|
||||
{
|
||||
#ifndef OPENCV_FLANN_USE_STD_RAND
|
||||
cv::theRNG() = cv::RNG(seed);
|
||||
#else
|
||||
std::srand(seed);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Generates a random double value.
|
||||
*/
|
||||
/**
|
||||
* Generates a random double value.
|
||||
* @param high Upper limit
|
||||
* @param low Lower limit
|
||||
* @return Random double value
|
||||
*/
|
||||
inline double rand_double(double high = 1.0, double low = 0)
|
||||
{
|
||||
return low + ((high-low) * (rand() / (RAND_MAX + 1.0)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random integer value.
|
||||
* @param high Upper limit
|
||||
* @param low Lower limit
|
||||
* @return Random integer value
|
||||
*/
|
||||
inline int rand_int(int high = RAND_MAX, int low = 0)
|
||||
{
|
||||
return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Random number generator that returns a distinct number from
|
||||
* the [0,n) interval each time.
|
||||
*/
|
||||
class UniqueRandom
|
||||
{
|
||||
std::vector<int> vals_;
|
||||
int size_;
|
||||
int counter_;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* @param n Size of the interval from which to generate
|
||||
* @return
|
||||
*/
|
||||
UniqueRandom(int n)
|
||||
{
|
||||
init(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the number generator.
|
||||
* @param n the size of the interval from which to generate random numbers.
|
||||
*/
|
||||
void init(int n)
|
||||
{
|
||||
// create and initialize an array of size n
|
||||
vals_.resize(n);
|
||||
size_ = n;
|
||||
for (int i = 0; i < size_; ++i) vals_[i] = i;
|
||||
|
||||
// shuffle the elements in the array
|
||||
#ifndef OPENCV_FLANN_USE_STD_RAND
|
||||
cv::randShuffle(vals_);
|
||||
#else
|
||||
std::random_shuffle(vals_.begin(), vals_.end());
|
||||
#endif
|
||||
|
||||
counter_ = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a distinct random integer in greater or equal to 0 and less
|
||||
* than 'n' on each call. It should be called maximum 'n' times.
|
||||
* Returns: a random integer
|
||||
*/
|
||||
int next()
|
||||
{
|
||||
if (counter_ == size_) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return vals_[counter_++];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_RANDOM_H
|
||||
+548
@@ -0,0 +1,548 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_RESULTSET_H
|
||||
#define OPENCV_FLANN_RESULTSET_H
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/* This record represents a branch point when finding neighbors in
|
||||
the tree. It contains a record of the minimum distance to the query
|
||||
point, as well as the node at which the search resumes.
|
||||
*/
|
||||
|
||||
template <typename T, typename DistanceType>
|
||||
struct BranchStruct
|
||||
{
|
||||
T node; /* Tree node at which search resumes */
|
||||
DistanceType mindist; /* Minimum distance to query for all nodes below. */
|
||||
|
||||
BranchStruct() {}
|
||||
BranchStruct(const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}
|
||||
|
||||
bool operator<(const BranchStruct<T, DistanceType>& rhs) const
|
||||
{
|
||||
return mindist<rhs.mindist;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename DistanceType>
|
||||
class ResultSet
|
||||
{
|
||||
public:
|
||||
virtual ~ResultSet() {}
|
||||
|
||||
virtual bool full() const = 0;
|
||||
|
||||
virtual void addPoint(DistanceType dist, int index) = 0;
|
||||
|
||||
virtual DistanceType worstDist() const = 0;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* KNNSimpleResultSet does not ensure that the element it holds are unique.
|
||||
* Is used in those cases where the nearest neighbour algorithm used does not
|
||||
* attempt to insert the same element multiple times.
|
||||
*/
|
||||
template <typename DistanceType>
|
||||
class KNNSimpleResultSet : public ResultSet<DistanceType>
|
||||
{
|
||||
int* indices;
|
||||
DistanceType* dists;
|
||||
int capacity;
|
||||
int count;
|
||||
DistanceType worst_distance_;
|
||||
|
||||
public:
|
||||
KNNSimpleResultSet(int capacity_) : capacity(capacity_), count(0)
|
||||
{
|
||||
}
|
||||
|
||||
void init(int* indices_, DistanceType* dists_)
|
||||
{
|
||||
indices = indices_;
|
||||
dists = dists_;
|
||||
count = 0;
|
||||
worst_distance_ = (std::numeric_limits<DistanceType>::max)();
|
||||
dists[capacity-1] = worst_distance_;
|
||||
}
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
bool full() const CV_OVERRIDE
|
||||
{
|
||||
return count == capacity;
|
||||
}
|
||||
|
||||
|
||||
void addPoint(DistanceType dist, int index) CV_OVERRIDE
|
||||
{
|
||||
if (dist >= worst_distance_) return;
|
||||
int i;
|
||||
for (i=count; i>0; --i) {
|
||||
#ifdef FLANN_FIRST_MATCH
|
||||
if ( (dists[i-1]>dist) || ((dist==dists[i-1])&&(indices[i-1]>index)) )
|
||||
#else
|
||||
if (dists[i-1]>dist)
|
||||
#endif
|
||||
{
|
||||
if (i<capacity) {
|
||||
dists[i] = dists[i-1];
|
||||
indices[i] = indices[i-1];
|
||||
}
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if (count < capacity) ++count;
|
||||
dists[i] = dist;
|
||||
indices[i] = index;
|
||||
worst_distance_ = dists[capacity-1];
|
||||
}
|
||||
|
||||
DistanceType worstDist() const CV_OVERRIDE
|
||||
{
|
||||
return worst_distance_;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* K-Nearest neighbour result set. Ensures that the elements inserted are unique
|
||||
*/
|
||||
template <typename DistanceType>
|
||||
class KNNResultSet : public ResultSet<DistanceType>
|
||||
{
|
||||
int* indices;
|
||||
DistanceType* dists;
|
||||
int capacity;
|
||||
int count;
|
||||
DistanceType worst_distance_;
|
||||
|
||||
public:
|
||||
KNNResultSet(int capacity_)
|
||||
: indices(NULL), dists(NULL), capacity(capacity_), count(0), worst_distance_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void init(int* indices_, DistanceType* dists_)
|
||||
{
|
||||
indices = indices_;
|
||||
dists = dists_;
|
||||
count = 0;
|
||||
worst_distance_ = (std::numeric_limits<DistanceType>::max)();
|
||||
dists[capacity-1] = worst_distance_;
|
||||
}
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
bool full() const CV_OVERRIDE
|
||||
{
|
||||
return count == capacity;
|
||||
}
|
||||
|
||||
|
||||
void addPoint(DistanceType dist, int index) CV_OVERRIDE
|
||||
{
|
||||
CV_DbgAssert(indices);
|
||||
CV_DbgAssert(dists);
|
||||
if (dist >= worst_distance_) return;
|
||||
int i;
|
||||
for (i = count; i > 0; --i) {
|
||||
#ifdef FLANN_FIRST_MATCH
|
||||
if ( (dists[i-1]<=dist) && ((dist!=dists[i-1])||(indices[i-1]<=index)) )
|
||||
#else
|
||||
if (dists[i-1]<=dist)
|
||||
#endif
|
||||
{
|
||||
// Check for duplicate indices
|
||||
for (int j = i; dists[j] == dist && j--;) {
|
||||
if (indices[j] == index) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count < capacity) ++count;
|
||||
for (int j = count-1; j > i; --j) {
|
||||
dists[j] = dists[j-1];
|
||||
indices[j] = indices[j-1];
|
||||
}
|
||||
dists[i] = dist;
|
||||
indices[i] = index;
|
||||
worst_distance_ = dists[capacity-1];
|
||||
}
|
||||
|
||||
DistanceType worstDist() const CV_OVERRIDE
|
||||
{
|
||||
return worst_distance_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A result-set class used when performing a radius based search.
|
||||
*/
|
||||
template <typename DistanceType>
|
||||
class RadiusResultSet : public ResultSet<DistanceType>
|
||||
{
|
||||
DistanceType radius;
|
||||
int* indices;
|
||||
DistanceType* dists;
|
||||
size_t capacity;
|
||||
size_t count;
|
||||
|
||||
public:
|
||||
RadiusResultSet(DistanceType radius_, int* indices_, DistanceType* dists_, int capacity_) :
|
||||
radius(radius_), indices(indices_), dists(dists_), capacity(capacity_)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
~RadiusResultSet()
|
||||
{
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
bool full() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void addPoint(DistanceType dist, int index)
|
||||
{
|
||||
if (dist<radius) {
|
||||
if ((capacity>0)&&(count < capacity)) {
|
||||
dists[count] = dist;
|
||||
indices[count] = index;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
DistanceType worstDist() const
|
||||
{
|
||||
return radius;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** Class that holds the k NN neighbors
|
||||
* Faster than KNNResultSet as it uses a binary heap and does not maintain two arrays
|
||||
*/
|
||||
template<typename DistanceType>
|
||||
class UniqueResultSet : public ResultSet<DistanceType>
|
||||
{
|
||||
public:
|
||||
struct DistIndex
|
||||
{
|
||||
DistIndex(DistanceType dist, unsigned int index) :
|
||||
dist_(dist), index_(index)
|
||||
{
|
||||
}
|
||||
bool operator<(const DistIndex dist_index) const
|
||||
{
|
||||
return (dist_ < dist_index.dist_) || ((dist_ == dist_index.dist_) && index_ < dist_index.index_);
|
||||
}
|
||||
DistanceType dist_;
|
||||
unsigned int index_;
|
||||
};
|
||||
|
||||
/** Default constructor */
|
||||
UniqueResultSet() :
|
||||
is_full_(false), worst_distance_(std::numeric_limits<DistanceType>::max())
|
||||
{
|
||||
}
|
||||
|
||||
/** Check the status of the set
|
||||
* @return true if we have k NN
|
||||
*/
|
||||
inline bool full() const CV_OVERRIDE
|
||||
{
|
||||
return is_full_;
|
||||
}
|
||||
|
||||
/** Remove all elements in the set
|
||||
*/
|
||||
virtual void clear() = 0;
|
||||
|
||||
/** Copy the set to two C arrays
|
||||
* @param indices pointer to a C array of indices
|
||||
* @param dist pointer to a C array of distances
|
||||
* @param n_neighbors the number of neighbors to copy
|
||||
*/
|
||||
virtual void copy(int* indices, DistanceType* dist, int n_neighbors = -1) const
|
||||
{
|
||||
if (n_neighbors < 0) {
|
||||
for (typename std::set<DistIndex>::const_iterator dist_index = dist_indices_.begin(), dist_index_end =
|
||||
dist_indices_.end(); dist_index != dist_index_end; ++dist_index, ++indices, ++dist) {
|
||||
*indices = dist_index->index_;
|
||||
*dist = dist_index->dist_;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int i = 0;
|
||||
for (typename std::set<DistIndex>::const_iterator dist_index = dist_indices_.begin(), dist_index_end =
|
||||
dist_indices_.end(); (dist_index != dist_index_end) && (i < n_neighbors); ++dist_index, ++indices, ++dist, ++i) {
|
||||
*indices = dist_index->index_;
|
||||
*dist = dist_index->dist_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Copy the set to two C arrays but sort it according to the distance first
|
||||
* @param indices pointer to a C array of indices
|
||||
* @param dist pointer to a C array of distances
|
||||
* @param n_neighbors the number of neighbors to copy
|
||||
*/
|
||||
virtual void sortAndCopy(int* indices, DistanceType* dist, int n_neighbors = -1) const
|
||||
{
|
||||
copy(indices, dist, n_neighbors);
|
||||
}
|
||||
|
||||
/** The number of neighbors in the set
|
||||
* @return
|
||||
*/
|
||||
size_t size() const
|
||||
{
|
||||
return dist_indices_.size();
|
||||
}
|
||||
|
||||
/** The distance of the furthest neighbor
|
||||
* If we don't have enough neighbors, it returns the max possible value
|
||||
* @return
|
||||
*/
|
||||
inline DistanceType worstDist() const CV_OVERRIDE
|
||||
{
|
||||
return worst_distance_;
|
||||
}
|
||||
protected:
|
||||
/** Flag to say if the set is full */
|
||||
bool is_full_;
|
||||
|
||||
/** The worst distance found so far */
|
||||
DistanceType worst_distance_;
|
||||
|
||||
/** The best candidates so far */
|
||||
std::set<DistIndex> dist_indices_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** Class that holds the k NN neighbors
|
||||
* Faster than KNNResultSet as it uses a binary heap and does not maintain two arrays
|
||||
*/
|
||||
template<typename DistanceType>
|
||||
class KNNUniqueResultSet : public UniqueResultSet<DistanceType>
|
||||
{
|
||||
public:
|
||||
/** Constructor
|
||||
* @param capacity the number of neighbors to store at max
|
||||
*/
|
||||
KNNUniqueResultSet(unsigned int capacity) : capacity_(capacity)
|
||||
{
|
||||
this->is_full_ = false;
|
||||
this->clear();
|
||||
}
|
||||
|
||||
/** Add a possible candidate to the best neighbors
|
||||
* @param dist distance for that neighbor
|
||||
* @param index index of that neighbor
|
||||
*/
|
||||
inline void addPoint(DistanceType dist, int index) CV_OVERRIDE
|
||||
{
|
||||
// Don't do anything if we are worse than the worst
|
||||
if (dist >= worst_distance_) return;
|
||||
dist_indices_.insert(DistIndex(dist, index));
|
||||
|
||||
if (is_full_) {
|
||||
if (dist_indices_.size() > capacity_) {
|
||||
dist_indices_.erase(*dist_indices_.rbegin());
|
||||
worst_distance_ = dist_indices_.rbegin()->dist_;
|
||||
}
|
||||
}
|
||||
else if (dist_indices_.size() == capacity_) {
|
||||
is_full_ = true;
|
||||
worst_distance_ = dist_indices_.rbegin()->dist_;
|
||||
}
|
||||
}
|
||||
|
||||
/** Remove all elements in the set
|
||||
*/
|
||||
void clear() CV_OVERRIDE
|
||||
{
|
||||
dist_indices_.clear();
|
||||
worst_distance_ = std::numeric_limits<DistanceType>::max();
|
||||
is_full_ = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
|
||||
using UniqueResultSet<DistanceType>::is_full_;
|
||||
using UniqueResultSet<DistanceType>::worst_distance_;
|
||||
using UniqueResultSet<DistanceType>::dist_indices_;
|
||||
|
||||
/** The number of neighbors to keep */
|
||||
unsigned int capacity_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** Class that holds the radius nearest neighbors
|
||||
* It is more accurate than RadiusResult as it is not limited in the number of neighbors
|
||||
*/
|
||||
template<typename DistanceType>
|
||||
class RadiusUniqueResultSet : public UniqueResultSet<DistanceType>
|
||||
{
|
||||
public:
|
||||
/** Constructor
|
||||
* @param radius the maximum distance of a neighbor
|
||||
*/
|
||||
RadiusUniqueResultSet(DistanceType radius) :
|
||||
radius_(radius)
|
||||
{
|
||||
is_full_ = true;
|
||||
}
|
||||
|
||||
/** Add a possible candidate to the best neighbors
|
||||
* @param dist distance for that neighbor
|
||||
* @param index index of that neighbor
|
||||
*/
|
||||
void addPoint(DistanceType dist, int index) CV_OVERRIDE
|
||||
{
|
||||
if (dist <= radius_) dist_indices_.insert(DistIndex(dist, index));
|
||||
}
|
||||
|
||||
/** Remove all elements in the set
|
||||
*/
|
||||
inline void clear() CV_OVERRIDE
|
||||
{
|
||||
dist_indices_.clear();
|
||||
}
|
||||
|
||||
|
||||
/** Check the status of the set
|
||||
* @return alwys false
|
||||
*/
|
||||
inline bool full() const CV_OVERRIDE
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The distance of the furthest neighbor
|
||||
* If we don't have enough neighbors, it returns the max possible value
|
||||
* @return
|
||||
*/
|
||||
inline DistanceType worstDist() const CV_OVERRIDE
|
||||
{
|
||||
return radius_;
|
||||
}
|
||||
private:
|
||||
typedef typename UniqueResultSet<DistanceType>::DistIndex DistIndex;
|
||||
using UniqueResultSet<DistanceType>::dist_indices_;
|
||||
using UniqueResultSet<DistanceType>::is_full_;
|
||||
|
||||
/** The furthest distance a neighbor can be */
|
||||
DistanceType radius_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** Class that holds the k NN neighbors within a radius distance
|
||||
*/
|
||||
template<typename DistanceType>
|
||||
class KNNRadiusUniqueResultSet : public KNNUniqueResultSet<DistanceType>
|
||||
{
|
||||
public:
|
||||
/** Constructor
|
||||
* @param capacity the number of neighbors to store at max
|
||||
* @param radius the maximum distance of a neighbor
|
||||
*/
|
||||
KNNRadiusUniqueResultSet(unsigned int capacity, DistanceType radius)
|
||||
{
|
||||
this->capacity_ = capacity;
|
||||
this->radius_ = radius;
|
||||
this->dist_indices_.reserve(capacity_);
|
||||
this->clear();
|
||||
}
|
||||
|
||||
/** Remove all elements in the set
|
||||
*/
|
||||
void clear()
|
||||
{
|
||||
dist_indices_.clear();
|
||||
worst_distance_ = radius_;
|
||||
is_full_ = false;
|
||||
}
|
||||
private:
|
||||
using KNNUniqueResultSet<DistanceType>::dist_indices_;
|
||||
using KNNUniqueResultSet<DistanceType>::is_full_;
|
||||
using KNNUniqueResultSet<DistanceType>::worst_distance_;
|
||||
|
||||
/** The maximum number of neighbors to consider */
|
||||
unsigned int capacity_;
|
||||
|
||||
/** The maximum distance of a neighbor */
|
||||
DistanceType radius_;
|
||||
};
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_RESULTSET_H
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#ifndef OPENCV_FLANN_SAMPLING_H_
|
||||
#define OPENCV_FLANN_SAMPLING_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include "matrix.h"
|
||||
#include "random.h"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
Matrix<T> random_sample(Matrix<T>& srcMatrix, long size, bool remove = false)
|
||||
{
|
||||
Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols);
|
||||
|
||||
T* src,* dest;
|
||||
for (long i=0; i<size; ++i) {
|
||||
long r = rand_int((int)(srcMatrix.rows-i));
|
||||
dest = newSet[i];
|
||||
src = srcMatrix[r];
|
||||
std::copy(src, src+srcMatrix.cols, dest);
|
||||
if (remove) {
|
||||
src = srcMatrix[srcMatrix.rows-i-1];
|
||||
dest = srcMatrix[r];
|
||||
std::copy(src, src+srcMatrix.cols, dest);
|
||||
}
|
||||
}
|
||||
if (remove) {
|
||||
srcMatrix.rows -= size;
|
||||
}
|
||||
return newSet;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Matrix<T> random_sample(const Matrix<T>& srcMatrix, size_t size)
|
||||
{
|
||||
UniqueRandom rand((int)srcMatrix.rows);
|
||||
Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols);
|
||||
|
||||
T* src,* dest;
|
||||
for (size_t i=0; i<size; ++i) {
|
||||
long r = rand.next();
|
||||
dest = newSet[i];
|
||||
src = srcMatrix[r];
|
||||
std::copy(src, src+srcMatrix.cols, dest);
|
||||
}
|
||||
return newSet;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_SAMPLING_H_ */
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_SAVING_H_
|
||||
#define OPENCV_FLANN_SAVING_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include "general.h"
|
||||
#include "nn_index.h"
|
||||
|
||||
#ifdef FLANN_SIGNATURE_
|
||||
#undef FLANN_SIGNATURE_
|
||||
#endif
|
||||
#define FLANN_SIGNATURE_ "FLANN_INDEX"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
struct Datatype {};
|
||||
template<>
|
||||
struct Datatype<char> { static flann_datatype_t type() { return FLANN_INT8; } };
|
||||
template<>
|
||||
struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } };
|
||||
template<>
|
||||
struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } };
|
||||
template<>
|
||||
struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } };
|
||||
template<>
|
||||
struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } };
|
||||
template<>
|
||||
struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } };
|
||||
template<>
|
||||
struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } };
|
||||
template<>
|
||||
struct Datatype<double> { static flann_datatype_t type() { return FLANN_FLOAT64; } };
|
||||
|
||||
|
||||
/**
|
||||
* Structure representing the index header.
|
||||
*/
|
||||
struct IndexHeader
|
||||
{
|
||||
char signature[16];
|
||||
char version[16];
|
||||
flann_datatype_t data_type;
|
||||
flann_algorithm_t index_type;
|
||||
size_t rows;
|
||||
size_t cols;
|
||||
};
|
||||
|
||||
/**
|
||||
* Saves index header to stream
|
||||
*
|
||||
* @param stream - Stream to save to
|
||||
* @param index - The index to save
|
||||
*/
|
||||
template<typename Distance>
|
||||
void save_header(FILE* stream, const NNIndex<Distance>& index)
|
||||
{
|
||||
IndexHeader header;
|
||||
memset(header.signature, 0, sizeof(header.signature));
|
||||
strcpy(header.signature, FLANN_SIGNATURE_);
|
||||
memset(header.version, 0, sizeof(header.version));
|
||||
strcpy(header.version, FLANN_VERSION_);
|
||||
header.data_type = Datatype<typename Distance::ElementType>::type();
|
||||
header.index_type = index.getType();
|
||||
header.rows = index.size();
|
||||
header.cols = index.veclen();
|
||||
|
||||
std::fwrite(&header, sizeof(header),1,stream);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stream - Stream to load from
|
||||
* @return Index header
|
||||
*/
|
||||
inline IndexHeader load_header(FILE* stream)
|
||||
{
|
||||
IndexHeader header;
|
||||
size_t read_size = fread(&header,sizeof(header),1,stream);
|
||||
|
||||
if (read_size!=(size_t)1) {
|
||||
FLANN_THROW(cv::Error::StsError, "Invalid index file, cannot read");
|
||||
}
|
||||
|
||||
if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
|
||||
FLANN_THROW(cv::Error::StsError, "Invalid index file, wrong signature");
|
||||
}
|
||||
|
||||
return header;
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void save_value(FILE* stream, const T& value, size_t count = 1)
|
||||
{
|
||||
fwrite(&value, sizeof(value),count, stream);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void save_value(FILE* stream, const cvflann::Matrix<T>& value)
|
||||
{
|
||||
fwrite(&value, sizeof(value),1, stream);
|
||||
fwrite(value.data, sizeof(T),value.rows*value.cols, stream);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void save_value(FILE* stream, const std::vector<T>& value)
|
||||
{
|
||||
size_t size = value.size();
|
||||
fwrite(&size, sizeof(size_t), 1, stream);
|
||||
fwrite(&value[0], sizeof(T), size, stream);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void load_value(FILE* stream, T& value, size_t count = 1)
|
||||
{
|
||||
size_t read_cnt = fread(&value, sizeof(value), count, stream);
|
||||
if (read_cnt != count) {
|
||||
FLANN_THROW(cv::Error::StsParseError, "Cannot read from file");
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void load_value(FILE* stream, cvflann::Matrix<T>& value)
|
||||
{
|
||||
size_t read_cnt = fread(&value, sizeof(value), 1, stream);
|
||||
if (read_cnt != 1) {
|
||||
FLANN_THROW(cv::Error::StsParseError, "Cannot read from file");
|
||||
}
|
||||
value.data = new T[value.rows*value.cols];
|
||||
read_cnt = fread(value.data, sizeof(T), value.rows*value.cols, stream);
|
||||
if (read_cnt != (size_t)(value.rows*value.cols)) {
|
||||
FLANN_THROW(cv::Error::StsParseError, "Cannot read from file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void load_value(FILE* stream, std::vector<T>& value)
|
||||
{
|
||||
size_t size;
|
||||
size_t read_cnt = fread(&size, sizeof(size_t), 1, stream);
|
||||
if (read_cnt!=1) {
|
||||
FLANN_THROW(cv::Error::StsError, "Cannot read from file");
|
||||
}
|
||||
value.resize(size);
|
||||
read_cnt = fread(&value[0], sizeof(T), size, stream);
|
||||
if (read_cnt != size) {
|
||||
FLANN_THROW(cv::Error::StsError, "Cannot read from file");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif /* OPENCV_FLANN_SAVING_H_ */
|
||||
Vendored
+190
@@ -0,0 +1,190 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_SIMPLEX_DOWNHILL_H_
|
||||
#define OPENCV_FLANN_SIMPLEX_DOWNHILL_H_
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
Adds val to array vals (and point to array points) and keeping the arrays sorted by vals.
|
||||
*/
|
||||
template <typename T>
|
||||
void addValue(int pos, float val, float* vals, T* point, T* points, int n)
|
||||
{
|
||||
vals[pos] = val;
|
||||
for (int i=0; i<n; ++i) {
|
||||
points[pos*n+i] = point[i];
|
||||
}
|
||||
|
||||
// bubble down
|
||||
int j=pos;
|
||||
while (j>0 && vals[j]<vals[j-1]) {
|
||||
swap(vals[j],vals[j-1]);
|
||||
for (int i=0; i<n; ++i) {
|
||||
swap(points[j*n+i],points[(j-1)*n+i]);
|
||||
}
|
||||
--j;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Simplex downhill optimization function.
|
||||
Preconditions: points is a 2D mattrix of size (n+1) x n
|
||||
func is the cost function taking n an array of n params and returning float
|
||||
vals is the cost function in the n+1 simplex points, if NULL it will be computed
|
||||
|
||||
Postcondition: returns optimum value and points[0..n] are the optimum parameters
|
||||
*/
|
||||
template <typename T, typename F>
|
||||
float optimizeSimplexDownhill(T* points, int n, F func, float* vals = NULL )
|
||||
{
|
||||
const int MAX_ITERATIONS = 10;
|
||||
|
||||
CV_DbgAssert(n>0);
|
||||
|
||||
T* p_o = new T[n];
|
||||
T* p_r = new T[n];
|
||||
T* p_e = new T[n];
|
||||
|
||||
int alpha = 1;
|
||||
|
||||
int iterations = 0;
|
||||
|
||||
bool ownVals = false;
|
||||
if (vals == NULL) {
|
||||
ownVals = true;
|
||||
vals = new float[n+1];
|
||||
for (int i=0; i<n+1; ++i) {
|
||||
float val = func(points+i*n);
|
||||
addValue(i, val, vals, points+i*n, points, n);
|
||||
}
|
||||
}
|
||||
int nn = n*n;
|
||||
|
||||
while (true) {
|
||||
|
||||
if (iterations++ > MAX_ITERATIONS) break;
|
||||
|
||||
// compute average of simplex points (except the highest point)
|
||||
for (int j=0; j<n; ++j) {
|
||||
p_o[j] = 0;
|
||||
for (int i=0; i<n; ++i) {
|
||||
p_o[i] += points[j*n+i];
|
||||
}
|
||||
}
|
||||
for (int i=0; i<n; ++i) {
|
||||
p_o[i] /= n;
|
||||
}
|
||||
|
||||
bool converged = true;
|
||||
for (int i=0; i<n; ++i) {
|
||||
if (p_o[i] != points[nn+i]) {
|
||||
converged = false;
|
||||
}
|
||||
}
|
||||
if (converged) break;
|
||||
|
||||
// trying a reflection
|
||||
for (int i=0; i<n; ++i) {
|
||||
p_r[i] = p_o[i] + alpha*(p_o[i]-points[nn+i]);
|
||||
}
|
||||
float val_r = func(p_r);
|
||||
|
||||
if ((val_r>=vals[0])&&(val_r<vals[n])) {
|
||||
// reflection between second highest and lowest
|
||||
// add it to the simplex
|
||||
Logger::info("Choosing reflection\n");
|
||||
addValue(n, val_r,vals, p_r, points, n);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (val_r<vals[0]) {
|
||||
// value is smaller than smallest in simplex
|
||||
|
||||
// expand some more to see if it drops further
|
||||
for (int i=0; i<n; ++i) {
|
||||
p_e[i] = 2*p_r[i]-p_o[i];
|
||||
}
|
||||
float val_e = func(p_e);
|
||||
|
||||
if (val_e<val_r) {
|
||||
Logger::info("Choosing reflection and expansion\n");
|
||||
addValue(n, val_e,vals,p_e,points,n);
|
||||
}
|
||||
else {
|
||||
Logger::info("Choosing reflection\n");
|
||||
addValue(n, val_r,vals,p_r,points,n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (val_r>=vals[n]) {
|
||||
for (int i=0; i<n; ++i) {
|
||||
p_e[i] = (p_o[i]+points[nn+i])/2;
|
||||
}
|
||||
float val_e = func(p_e);
|
||||
|
||||
if (val_e<vals[n]) {
|
||||
Logger::info("Choosing contraction\n");
|
||||
addValue(n,val_e,vals,p_e,points,n);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
{
|
||||
Logger::info("Full contraction\n");
|
||||
for (int j=1; j<=n; ++j) {
|
||||
for (int i=0; i<n; ++i) {
|
||||
points[j*n+i] = (points[j*n+i]+points[i])/2;
|
||||
}
|
||||
float val = func(points+j*n);
|
||||
addValue(j,val,vals,points+j*n,points,n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float bestVal = vals[0];
|
||||
|
||||
delete[] p_r;
|
||||
delete[] p_o;
|
||||
delete[] p_e;
|
||||
if (ownVals) delete[] vals;
|
||||
|
||||
return bestVal;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif //OPENCV_FLANN_SIMPLEX_DOWNHILL_H_
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
/***********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
|
||||
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
|
||||
*
|
||||
* THE BSD LICENSE
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef OPENCV_FLANN_TIMER_H
|
||||
#define OPENCV_FLANN_TIMER_H
|
||||
|
||||
//! @cond IGNORED
|
||||
|
||||
#include <time.h>
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/core/utility.hpp"
|
||||
|
||||
namespace cvflann
|
||||
{
|
||||
|
||||
/**
|
||||
* A start-stop timer class.
|
||||
*
|
||||
* Can be used to time portions of code.
|
||||
*/
|
||||
class StartStopTimer
|
||||
{
|
||||
int64 startTime;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Value of the timer.
|
||||
*/
|
||||
double value;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
StartStopTimer()
|
||||
: startTime(0)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the timer.
|
||||
*/
|
||||
void start()
|
||||
{
|
||||
startTime = cv::getTickCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the timer and updates timer value.
|
||||
*/
|
||||
void stop()
|
||||
{
|
||||
int64 stopTime = cv::getTickCount();
|
||||
value += ( (double)stopTime - startTime) / cv::getTickFrequency();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the timer value to 0.
|
||||
*/
|
||||
void reset()
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//! @endcond
|
||||
|
||||
#endif // FLANN_TIMER_H
|
||||
@@ -0,0 +1,42 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_HPP
|
||||
#define OPENCV_GAPI_HPP
|
||||
|
||||
#include <memory>
|
||||
|
||||
/** \defgroup gapi G-API framework
|
||||
@{
|
||||
@defgroup gapi_main_classes G-API Main Classes
|
||||
@defgroup gapi_data_objects G-API Data Types
|
||||
@{
|
||||
@defgroup gapi_meta_args G-API Metadata Descriptors
|
||||
@}
|
||||
@defgroup gapi_std_backends G-API Standard Backends
|
||||
@defgroup gapi_compile_args G-API Graph Compilation Arguments
|
||||
@defgroup gapi_serialization G-API Serialization functionality
|
||||
@}
|
||||
*/
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/garray.hpp>
|
||||
#include <opencv2/gapi/gscalar.hpp>
|
||||
#include <opencv2/gapi/gopaque.hpp>
|
||||
#include <opencv2/gapi/gframe.hpp>
|
||||
#include <opencv2/gapi/gcomputation.hpp>
|
||||
#include <opencv2/gapi/gcompiled.hpp>
|
||||
#include <opencv2/gapi/gtyped.hpp>
|
||||
#include <opencv2/gapi/gkernel.hpp>
|
||||
#include <opencv2/gapi/operators.hpp>
|
||||
|
||||
// Include these files here to avoid cyclic dependency between
|
||||
// Desync & GKernel & GComputation & GStreamingCompiled.
|
||||
#include <opencv2/gapi/streaming/desync.hpp>
|
||||
#include <opencv2/gapi/streaming/format.hpp>
|
||||
|
||||
#endif // OPENCV_GAPI_HPP
|
||||
+1911
File diff suppressed because it is too large
Load Diff
+27
@@ -0,0 +1,27 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_CPU_CORE_API_HPP
|
||||
#define OPENCV_GAPI_CPU_CORE_API_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace core {
|
||||
namespace cpu {
|
||||
|
||||
GAPI_EXPORTS_W cv::GKernelPackage kernels();
|
||||
|
||||
} // namespace cpu
|
||||
} // namespace core
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_CPU_CORE_API_HPP
|
||||
+542
@@ -0,0 +1,542 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2022 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCPUKERNEL_HPP
|
||||
#define OPENCV_GAPI_GCPUKERNEL_HPP
|
||||
|
||||
#if defined _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4702) // "Unreachable code" on postprocess(...) call inside OCVCallHelper
|
||||
#endif
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <opencv2/core/mat.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/gkernel.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/gmetaarg.hpp>
|
||||
#include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
|
||||
// FIXME: namespace scheme for backends?
|
||||
namespace cv {
|
||||
|
||||
namespace gimpl
|
||||
{
|
||||
// Forward-declare an internal class
|
||||
class GCPUExecutable;
|
||||
} // namespace gimpl
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
/**
|
||||
* @brief This namespace contains G-API CPU backend functions,
|
||||
* structures, and symbols.
|
||||
*/
|
||||
namespace cpu
|
||||
{
|
||||
/**
|
||||
* \addtogroup gapi_std_backends
|
||||
* @{
|
||||
*
|
||||
* @brief G-API backends available in this OpenCV version
|
||||
*
|
||||
* G-API backends play a corner stone role in G-API execution
|
||||
* stack. Every backend is hardware-oriented and thus can run its
|
||||
* kernels efficiently on the target platform.
|
||||
*
|
||||
* Backends are usually "black boxes" for G-API users -- on the API
|
||||
* side, all backends are represented as different objects of the
|
||||
* same class cv::gapi::GBackend.
|
||||
* User can manipulate with backends by specifying which kernels to use.
|
||||
*
|
||||
* @sa @ref gapi_hld
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Get a reference to CPU (OpenCV) backend.
|
||||
*
|
||||
* This is the default backend in G-API at the moment, providing
|
||||
* broader functional coverage but losing some graph model
|
||||
* advantages. Provided mostly for reference and prototyping
|
||||
* purposes.
|
||||
*
|
||||
* @sa gapi_std_backends
|
||||
*/
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
/** @} */
|
||||
|
||||
class GOCVFunctor;
|
||||
|
||||
//! @cond IGNORED
|
||||
template<typename K, typename Callable>
|
||||
GOCVFunctor ocv_kernel(const Callable& c);
|
||||
|
||||
template<typename K, typename Callable>
|
||||
GOCVFunctor ocv_kernel(Callable& c);
|
||||
//! @endcond
|
||||
|
||||
} // namespace cpu
|
||||
} // namespace gapi
|
||||
|
||||
// Represents arguments which are passed to a wrapped CPU function
|
||||
// FIXME: put into detail?
|
||||
class GAPI_EXPORTS GCPUContext
|
||||
{
|
||||
public:
|
||||
// Generic accessor API
|
||||
template<typename T>
|
||||
const T& inArg(int input) { return m_args.at(input).get<T>(); }
|
||||
|
||||
// Syntax sugar
|
||||
const cv::Mat& inMat(int input);
|
||||
cv::Mat& outMatR(int output); // FIXME: Avoid cv::Mat m = ctx.outMatR()
|
||||
|
||||
const cv::Scalar& inVal(int input);
|
||||
cv::Scalar& outValR(int output); // FIXME: Avoid cv::Scalar s = ctx.outValR()
|
||||
cv::MediaFrame& outFrame(int output);
|
||||
template<typename T> std::vector<T>& outVecR(int output) // FIXME: the same issue
|
||||
{
|
||||
return outVecRef(output).wref<T>();
|
||||
}
|
||||
template<typename T> T& outOpaqueR(int output) // FIXME: the same issue
|
||||
{
|
||||
return outOpaqueRef(output).wref<T>();
|
||||
}
|
||||
|
||||
GArg state()
|
||||
{
|
||||
return m_state;
|
||||
}
|
||||
|
||||
protected:
|
||||
detail::VectorRef& outVecRef(int output);
|
||||
detail::OpaqueRef& outOpaqueRef(int output);
|
||||
|
||||
std::vector<GArg> m_args;
|
||||
GArg m_state;
|
||||
|
||||
//FIXME: avoid conversion of arguments from internal representation to OpenCV one on each call
|
||||
//to OCV kernel. (This can be achieved by a two single time conversions in GCPUExecutable::run,
|
||||
//once on enter for input and output arguments, and once before return for output arguments only
|
||||
std::unordered_map<std::size_t, GRunArgP> m_results;
|
||||
|
||||
friend class gimpl::GCPUExecutable;
|
||||
};
|
||||
|
||||
class GAPI_EXPORTS GCPUKernel
|
||||
{
|
||||
public:
|
||||
// This function is a kernel's execution entry point (does the processing work)
|
||||
using RunF = std::function<void(GCPUContext &)>;
|
||||
// This function is a stateful kernel's setup routine (configures state)
|
||||
using SetupF = std::function<void(const GMetaArgs &, const GArgs &,
|
||||
GArg &, const GCompileArgs &)>;
|
||||
|
||||
GCPUKernel();
|
||||
GCPUKernel(const RunF& runF, const SetupF& setupF = nullptr);
|
||||
|
||||
RunF m_runF = nullptr;
|
||||
SetupF m_setupF = nullptr;
|
||||
|
||||
bool m_isStateful = false;
|
||||
};
|
||||
|
||||
// FIXME: This is an ugly ad-hoc implementation. TODO: refactor
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<class T> struct get_in;
|
||||
template<> struct get_in<cv::GMat>
|
||||
{
|
||||
static cv::Mat get(GCPUContext &ctx, int idx) { return ctx.inMat(idx); }
|
||||
};
|
||||
template<> struct get_in<cv::GMatP>
|
||||
{
|
||||
static cv::Mat get(GCPUContext &ctx, int idx) { return get_in<cv::GMat>::get(ctx, idx); }
|
||||
};
|
||||
template<> struct get_in<cv::GFrame>
|
||||
{
|
||||
static cv::MediaFrame get(GCPUContext &ctx, int idx) { return ctx.inArg<cv::MediaFrame>(idx); }
|
||||
};
|
||||
template<> struct get_in<cv::GScalar>
|
||||
{
|
||||
static cv::Scalar get(GCPUContext &ctx, int idx) { return ctx.inVal(idx); }
|
||||
};
|
||||
template<typename U> struct get_in<cv::GArray<U> >
|
||||
{
|
||||
static const std::vector<U>& get(GCPUContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
|
||||
};
|
||||
template<typename U> struct get_in<cv::GOpaque<U> >
|
||||
{
|
||||
static const U& get(GCPUContext &ctx, int idx) { return ctx.inArg<OpaqueRef>(idx).rref<U>(); }
|
||||
};
|
||||
|
||||
//FIXME(dm): GArray<Mat>/GArray<GMat> conversion should be done more gracefully in the system
|
||||
template<> struct get_in<cv::GArray<cv::GMat> >: public get_in<cv::GArray<cv::Mat> >
|
||||
{
|
||||
};
|
||||
|
||||
//FIXME(dm): GArray<Scalar>/GArray<GScalar> conversion should be done more gracefully in the system
|
||||
template<> struct get_in<cv::GArray<cv::GScalar> >: public get_in<cv::GArray<cv::Scalar> >
|
||||
{
|
||||
};
|
||||
|
||||
// FIXME(dm): GArray<vector<U>>/GArray<GArray<U>> conversion should be done more gracefully in the system
|
||||
template<typename U> struct get_in<cv::GArray<cv::GArray<U>> >: public get_in<cv::GArray<std::vector<U>> >
|
||||
{
|
||||
};
|
||||
|
||||
//FIXME(dm): GOpaque<Mat>/GOpaque<GMat> conversion should be done more gracefully in the system
|
||||
template<> struct get_in<cv::GOpaque<cv::GMat> >: public get_in<cv::GOpaque<cv::Mat> >
|
||||
{
|
||||
};
|
||||
|
||||
//FIXME(dm): GOpaque<Scalar>/GOpaque<GScalar> conversion should be done more gracefully in the system
|
||||
template<> struct get_in<cv::GOpaque<cv::GScalar> >: public get_in<cv::GOpaque<cv::Mat> >
|
||||
{
|
||||
};
|
||||
|
||||
template<class T> struct get_in
|
||||
{
|
||||
static T get(GCPUContext &ctx, int idx) { return ctx.inArg<T>(idx); }
|
||||
};
|
||||
|
||||
struct tracked_cv_mat{
|
||||
tracked_cv_mat(cv::Mat& m) : r{m}, original_data{m.data} {}
|
||||
cv::Mat r;
|
||||
uchar* original_data;
|
||||
|
||||
operator cv::Mat& (){ return r;}
|
||||
void validate() const{
|
||||
if (r.data != original_data)
|
||||
{
|
||||
util::throw_error
|
||||
(std::logic_error
|
||||
("OpenCV kernel output parameter was reallocated. \n"
|
||||
"Incorrect meta data was provided ?"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Outputs>
|
||||
void postprocess(Outputs&... outs)
|
||||
{
|
||||
struct
|
||||
{
|
||||
void operator()(tracked_cv_mat* bm) { bm->validate(); }
|
||||
void operator()(...) { }
|
||||
|
||||
} validate;
|
||||
//dummy array to unfold parameter pack
|
||||
int dummy[] = { 0, (validate(&outs), 0)... };
|
||||
cv::util::suppress_unused_warning(dummy);
|
||||
}
|
||||
|
||||
template<class T> struct get_out;
|
||||
template<> struct get_out<cv::GMat>
|
||||
{
|
||||
static tracked_cv_mat get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
auto& r = ctx.outMatR(idx);
|
||||
return {r};
|
||||
}
|
||||
};
|
||||
template<> struct get_out<cv::GMatP>
|
||||
{
|
||||
static tracked_cv_mat get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
return get_out<cv::GMat>::get(ctx, idx);
|
||||
}
|
||||
};
|
||||
template<> struct get_out<cv::GScalar>
|
||||
{
|
||||
static cv::Scalar& get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
return ctx.outValR(idx);
|
||||
}
|
||||
};
|
||||
template<> struct get_out<cv::GFrame>
|
||||
{
|
||||
static cv::MediaFrame& get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
return ctx.outFrame(idx);
|
||||
}
|
||||
};
|
||||
template<typename U> struct get_out<cv::GArray<U>>
|
||||
{
|
||||
static std::vector<U>& get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
return ctx.outVecR<U>(idx);
|
||||
}
|
||||
};
|
||||
|
||||
//FIXME(dm): GArray<Mat>/GArray<GMat> conversion should be done more gracefully in the system
|
||||
template<> struct get_out<cv::GArray<cv::GMat> >: public get_out<cv::GArray<cv::Mat> >
|
||||
{
|
||||
};
|
||||
|
||||
// FIXME(dm): GArray<vector<U>>/GArray<GArray<U>> conversion should be done more gracefully in the system
|
||||
template<typename U> struct get_out<cv::GArray<cv::GArray<U>> >: public get_out<cv::GArray<std::vector<U>> >
|
||||
{
|
||||
};
|
||||
|
||||
template<typename U> struct get_out<cv::GOpaque<U>>
|
||||
{
|
||||
static U& get(GCPUContext &ctx, int idx)
|
||||
{
|
||||
return ctx.outOpaqueR<U>(idx);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename, typename>
|
||||
struct OCVSetupHelper;
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct OCVSetupHelper<Impl, std::tuple<Ins...>>
|
||||
{
|
||||
// Using 'auto' return type and 'decltype' specifier in both 'setup_impl' versions
|
||||
// to check existence of required 'Impl::setup' functions.
|
||||
// While 'decltype' specifier accepts expression we pass expression with 'comma-operator'
|
||||
// where first operand of comma-operator is call attempt to desired 'Impl::setup' and
|
||||
// the second operand is 'void()' expression.
|
||||
//
|
||||
// SFINAE for 'Impl::setup' which accepts compile arguments.
|
||||
template<int... IIs>
|
||||
static auto setup_impl(const GMetaArgs &metaArgs, const GArgs &args,
|
||||
GArg &state, const GCompileArgs &compileArgs,
|
||||
detail::Seq<IIs...>) ->
|
||||
decltype(Impl::setup(detail::get_in_meta<Ins>(metaArgs, args, IIs)...,
|
||||
std::declval<typename std::add_lvalue_reference<
|
||||
std::shared_ptr<typename Impl::State>
|
||||
>::type
|
||||
>(),
|
||||
compileArgs)
|
||||
, void())
|
||||
{
|
||||
// TODO: unique_ptr <-> shared_ptr conversion ?
|
||||
// To check: Conversion is possible only if the state which should be passed to
|
||||
// 'setup' user callback isn't required to have previous value
|
||||
std::shared_ptr<typename Impl::State> stPtr;
|
||||
Impl::setup(detail::get_in_meta<Ins>(metaArgs, args, IIs)..., stPtr, compileArgs);
|
||||
state = GArg(stPtr);
|
||||
}
|
||||
|
||||
// SFINAE for 'Impl::setup' which doesn't accept compile arguments.
|
||||
template<int... IIs>
|
||||
static auto setup_impl(const GMetaArgs &metaArgs, const GArgs &args,
|
||||
GArg &state, const GCompileArgs &/* compileArgs */,
|
||||
detail::Seq<IIs...>) ->
|
||||
decltype(Impl::setup(detail::get_in_meta<Ins>(metaArgs, args, IIs)...,
|
||||
std::declval<typename std::add_lvalue_reference<
|
||||
std::shared_ptr<typename Impl::State>
|
||||
>::type
|
||||
>()
|
||||
)
|
||||
, void())
|
||||
{
|
||||
// The same comment as in 'setup' above.
|
||||
std::shared_ptr<typename Impl::State> stPtr;
|
||||
Impl::setup(detail::get_in_meta<Ins>(metaArgs, args, IIs)..., stPtr);
|
||||
state = GArg(stPtr);
|
||||
}
|
||||
|
||||
static void setup(const GMetaArgs &metaArgs, const GArgs &args,
|
||||
GArg& state, const GCompileArgs &compileArgs)
|
||||
{
|
||||
setup_impl(metaArgs, args, state, compileArgs,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
// OCVCallHelper is a helper class to call stateless OCV kernels and OCV kernel functors.
|
||||
template<typename, typename, typename>
|
||||
struct OCVCallHelper;
|
||||
|
||||
// FIXME: probably can be simplified with std::apply or analogue.
|
||||
template<typename Impl, typename... Ins, typename... Outs>
|
||||
struct OCVCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...>>
|
||||
{
|
||||
template<typename... Inputs>
|
||||
struct call_and_postprocess
|
||||
{
|
||||
template<typename... Outputs>
|
||||
static void call(Inputs&&... ins, Outputs&&... outs)
|
||||
{
|
||||
//not using a std::forward on outs is deliberate in order to
|
||||
//cause compilation error, by trying to bind rvalue references to lvalue references
|
||||
Impl::run(std::forward<Inputs>(ins)..., outs...);
|
||||
postprocess(outs...);
|
||||
}
|
||||
|
||||
template<typename... Outputs>
|
||||
static void call(Impl& impl, Inputs&&... ins, Outputs&&... outs)
|
||||
{
|
||||
impl(std::forward<Inputs>(ins)..., outs...);
|
||||
}
|
||||
};
|
||||
|
||||
template<int... IIs, int... OIs>
|
||||
static void call_impl(GCPUContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
|
||||
{
|
||||
//Make sure that OpenCV kernels do not reallocate memory for output parameters
|
||||
//by comparing it's state (data ptr) before and after the call.
|
||||
//This is done by converting each output Mat into tracked_cv_mat object, and binding
|
||||
//them to parameters of ad-hoc function
|
||||
call_and_postprocess<decltype(get_in<Ins>::get(ctx, IIs))...>
|
||||
::call(get_in<Ins>::get(ctx, IIs)..., get_out<Outs>::get(ctx, OIs)...);
|
||||
}
|
||||
|
||||
template<int... IIs, int... OIs>
|
||||
static void call_impl(cv::GCPUContext &ctx, Impl& impl,
|
||||
detail::Seq<IIs...>, detail::Seq<OIs...>)
|
||||
{
|
||||
call_and_postprocess<decltype(get_in<Ins>::get(ctx, IIs))...>
|
||||
::call(impl, get_in<Ins>::get(ctx, IIs)..., get_out<Outs>::get(ctx, OIs)...);
|
||||
}
|
||||
|
||||
static void call(GCPUContext &ctx)
|
||||
{
|
||||
call_impl(ctx,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<sizeof...(Outs)>::type());
|
||||
}
|
||||
|
||||
// NB: Same as call but calling the object
|
||||
// This necessary for kernel implementations that have a state
|
||||
// and are represented as an object
|
||||
static void callFunctor(cv::GCPUContext &ctx, Impl& impl)
|
||||
{
|
||||
call_impl(ctx, impl,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<sizeof...(Outs)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
// OCVStCallHelper is a helper class to call stateful OCV kernels.
|
||||
template<typename, typename, typename>
|
||||
struct OCVStCallHelper;
|
||||
|
||||
template<typename Impl, typename... Ins, typename... Outs>
|
||||
struct OCVStCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...>> :
|
||||
OCVCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...>>
|
||||
{
|
||||
template<typename... Inputs>
|
||||
struct call_and_postprocess
|
||||
{
|
||||
template<typename... Outputs>
|
||||
static void call(typename Impl::State& st, Inputs&&... ins, Outputs&&... outs)
|
||||
{
|
||||
Impl::run(std::forward<Inputs>(ins)..., outs..., st);
|
||||
postprocess(outs...);
|
||||
}
|
||||
};
|
||||
|
||||
template<int... IIs, int... OIs>
|
||||
static void call_impl(GCPUContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
|
||||
{
|
||||
auto& st = *ctx.state().get<std::shared_ptr<typename Impl::State>>();
|
||||
call_and_postprocess<decltype(get_in<Ins>::get(ctx, IIs))...>
|
||||
::call(st, get_in<Ins>::get(ctx, IIs)..., get_out<Outs>::get(ctx, OIs)...);
|
||||
}
|
||||
|
||||
static void call(GCPUContext &ctx)
|
||||
{
|
||||
call_impl(ctx,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<sizeof...(Outs)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class Impl, class K>
|
||||
class GCPUKernelImpl: public cv::detail::KernelTag
|
||||
{
|
||||
using CallHelper = cv::detail::OCVCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
|
||||
|
||||
public:
|
||||
using API = K;
|
||||
|
||||
static cv::gapi::GBackend backend() { return cv::gapi::cpu::backend(); }
|
||||
static cv::GCPUKernel kernel() { return GCPUKernel(&CallHelper::call); }
|
||||
};
|
||||
|
||||
template<class Impl, class K, class S>
|
||||
class GCPUStKernelImpl: public cv::detail::KernelTag
|
||||
{
|
||||
using StSetupHelper = detail::OCVSetupHelper<Impl, typename K::InArgs>;
|
||||
using StCallHelper = detail::OCVStCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
|
||||
|
||||
public:
|
||||
using API = K;
|
||||
using State = S;
|
||||
|
||||
static cv::gapi::GBackend backend() { return cv::gapi::cpu::backend(); }
|
||||
static cv::GCPUKernel kernel() { return GCPUKernel(&StCallHelper::call,
|
||||
&StSetupHelper::setup); }
|
||||
};
|
||||
|
||||
#define GAPI_OCV_KERNEL(Name, API) struct Name: public cv::GCPUKernelImpl<Name, API>
|
||||
|
||||
// TODO: Reuse Anatoliy's logic for support of types with commas in macro.
|
||||
// Retrieve the common part from Anatoliy's logic to the separate place.
|
||||
#define GAPI_OCV_KERNEL_ST(Name, API, State) \
|
||||
struct Name: public cv::GCPUStKernelImpl<Name, API, State> \
|
||||
|
||||
/// @private
|
||||
class gapi::cpu::GOCVFunctor : public gapi::GFunctor
|
||||
{
|
||||
public:
|
||||
using Impl = std::function<void(GCPUContext &)>;
|
||||
using Meta = cv::GKernel::M;
|
||||
|
||||
GOCVFunctor(const char* id, const Meta &meta, const Impl& impl)
|
||||
: gapi::GFunctor(id), impl_{GCPUKernel(impl), meta}
|
||||
{
|
||||
}
|
||||
|
||||
GKernelImpl impl() const override { return impl_; }
|
||||
gapi::GBackend backend() const override { return gapi::cpu::backend(); }
|
||||
|
||||
private:
|
||||
GKernelImpl impl_;
|
||||
};
|
||||
|
||||
//! @cond IGNORED
|
||||
template<typename K, typename Callable>
|
||||
gapi::cpu::GOCVFunctor gapi::cpu::ocv_kernel(Callable& c)
|
||||
{
|
||||
using P = cv::detail::OCVCallHelper<Callable, typename K::InArgs, typename K::OutArgs>;
|
||||
return GOCVFunctor{ K::id()
|
||||
, &K::getOutMeta
|
||||
, std::bind(&P::callFunctor, std::placeholders::_1, std::ref(c))
|
||||
};
|
||||
}
|
||||
|
||||
template<typename K, typename Callable>
|
||||
gapi::cpu::GOCVFunctor gapi::cpu::ocv_kernel(const Callable& c)
|
||||
{
|
||||
using P = cv::detail::OCVCallHelper<Callable, typename K::InArgs, typename K::OutArgs>;
|
||||
return GOCVFunctor{ K::id()
|
||||
, &K::getOutMeta
|
||||
, std::bind(&P::callFunctor, std::placeholders::_1, c)
|
||||
};
|
||||
}
|
||||
//! @endcond
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#if defined _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif // OPENCV_GAPI_GCPUKERNEL_HPP
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_CPU_IMGPROC_API_HPP
|
||||
#define OPENCV_GAPI_CPU_IMGPROC_API_HPP
|
||||
|
||||
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace imgproc {
|
||||
namespace cpu {
|
||||
|
||||
GAPI_EXPORTS GKernelPackage kernels();
|
||||
|
||||
} // namespace cpu
|
||||
} // namespace imgproc
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_CPU_IMGPROC_API_HPP
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2021 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_CPU_STEREO_API_HPP
|
||||
#define OPENCV_GAPI_CPU_STEREO_API_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace calib3d {
|
||||
namespace cpu {
|
||||
|
||||
GAPI_EXPORTS GKernelPackage kernels();
|
||||
|
||||
/** @brief Structure for the Stereo operation initialization parameters.*/
|
||||
struct GAPI_EXPORTS StereoInitParam {
|
||||
StereoInitParam(int nD, int bS, double bL, double f):
|
||||
numDisparities(nD), blockSize(bS), baseline(bL), focus(f) {}
|
||||
|
||||
StereoInitParam() = default;
|
||||
|
||||
int numDisparities = 0;
|
||||
int blockSize = 21;
|
||||
double baseline = 63.5;
|
||||
double focus = 3.6;
|
||||
};
|
||||
|
||||
} // namespace cpu
|
||||
} // namespace calib3d
|
||||
} // namespace gapi
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<> struct CompileArgTag<cv::gapi::calib3d::cpu::StereoInitParam> {
|
||||
static const char* tag() {
|
||||
return "org.opencv.stereoInit";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_CPU_STEREO_API_HPP
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_CPU_VIDEO_API_HPP
|
||||
#define OPENCV_GAPI_CPU_VIDEO_API_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace video {
|
||||
namespace cpu {
|
||||
|
||||
GAPI_EXPORTS GKernelPackage kernels();
|
||||
|
||||
} // namespace cpu
|
||||
} // namespace video
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_CPU_VIDEO_API_HPP
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_FLUID_CORE_HPP
|
||||
#define OPENCV_GAPI_FLUID_CORE_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
|
||||
|
||||
namespace cv { namespace gapi { namespace core { namespace fluid {
|
||||
|
||||
GAPI_EXPORTS_W cv::GKernelPackage kernels();
|
||||
|
||||
}}}}
|
||||
|
||||
#endif // OPENCV_GAPI_FLUID_CORE_HPP
|
||||
Vendored
+154
@@ -0,0 +1,154 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_FLUID_BUFFER_HPP
|
||||
#define OPENCV_GAPI_FLUID_BUFFER_HPP
|
||||
|
||||
#include <list>
|
||||
#include <numeric> // accumulate
|
||||
#include <ostream> // ostream
|
||||
#include <cstdint> // uint8_t
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
|
||||
#include <opencv2/gapi/util/optional.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace fluid {
|
||||
|
||||
struct Border
|
||||
{
|
||||
// This constructor is required to support existing kernels which are part of G-API
|
||||
Border(int _type, cv::Scalar _val) : type(_type), value(_val) {}
|
||||
|
||||
int type;
|
||||
cv::Scalar value;
|
||||
};
|
||||
|
||||
using BorderOpt = util::optional<Border>;
|
||||
|
||||
bool operator == (const Border& b1, const Border& b2);
|
||||
|
||||
class GAPI_EXPORTS Buffer;
|
||||
|
||||
class GAPI_EXPORTS View
|
||||
{
|
||||
public:
|
||||
struct Cache
|
||||
{
|
||||
std::vector<const uint8_t*> m_linePtrs;
|
||||
GMatDesc m_desc;
|
||||
int m_border_size = 0;
|
||||
|
||||
inline const uint8_t* linePtr(int index) const
|
||||
{
|
||||
// "out_of_window" check:
|
||||
// user must not request the lines which are outside of specified kernel window
|
||||
GAPI_DbgAssert(index >= -m_border_size
|
||||
&& index < -m_border_size + static_cast<int>(m_linePtrs.size()));
|
||||
return m_linePtrs[index + m_border_size];
|
||||
}
|
||||
};
|
||||
|
||||
const inline uint8_t* InLineB(int index) const // -(w-1)/2...0...+(w-1)/2 for Filters
|
||||
{
|
||||
return m_cache->linePtr(index);
|
||||
}
|
||||
|
||||
template<typename T> const inline T* InLine(int i) const
|
||||
{
|
||||
const uint8_t* ptr = this->InLineB(i);
|
||||
return reinterpret_cast<const T*>(ptr);
|
||||
}
|
||||
|
||||
inline operator bool() const { return m_priv != nullptr; }
|
||||
bool ready() const;
|
||||
inline int length() const { return m_cache->m_desc.size.width; }
|
||||
int y() const;
|
||||
|
||||
inline const GMatDesc& meta() const { return m_cache->m_desc; }
|
||||
|
||||
class GAPI_EXPORTS Priv; // internal use only
|
||||
Priv& priv(); // internal use only
|
||||
const Priv& priv() const; // internal use only
|
||||
|
||||
View();
|
||||
View(std::unique_ptr<Priv>&& p);
|
||||
View(View&& v);
|
||||
View& operator=(View&& v);
|
||||
~View();
|
||||
|
||||
private:
|
||||
std::unique_ptr<Priv> m_priv;
|
||||
const Cache* m_cache = nullptr;
|
||||
};
|
||||
|
||||
class GAPI_EXPORTS Buffer
|
||||
{
|
||||
public:
|
||||
struct Cache
|
||||
{
|
||||
std::vector<uint8_t*> m_linePtrs;
|
||||
GMatDesc m_desc;
|
||||
};
|
||||
|
||||
// Default constructor (executable creation stage,
|
||||
// all following initialization performed in Priv::init())
|
||||
Buffer();
|
||||
// Scratch constructor (user kernels)
|
||||
Buffer(const cv::GMatDesc &desc);
|
||||
|
||||
// Constructor for intermediate buffers (for tests)
|
||||
Buffer(const cv::GMatDesc &desc,
|
||||
int max_line_consumption, int border_size,
|
||||
int skew,
|
||||
int wlpi,
|
||||
BorderOpt border);
|
||||
// Constructor for in/out buffers (for tests)
|
||||
Buffer(const cv::Mat &data, bool is_input);
|
||||
~Buffer();
|
||||
Buffer& operator=(Buffer&&);
|
||||
|
||||
inline uint8_t* OutLineB(int index = 0)
|
||||
{
|
||||
return m_cache->m_linePtrs[index];
|
||||
}
|
||||
|
||||
template<typename T> inline T* OutLine(int index = 0)
|
||||
{
|
||||
uint8_t* ptr = this->OutLineB(index);
|
||||
return reinterpret_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
int y() const;
|
||||
|
||||
int linesReady() const;
|
||||
void debug(std::ostream &os) const;
|
||||
inline int length() const { return m_cache->m_desc.size.width; }
|
||||
int lpi() const; // LPI for WRITER
|
||||
|
||||
inline const GMatDesc& meta() const { return m_cache->m_desc; }
|
||||
|
||||
View mkView(int borderSize, bool ownStorage);
|
||||
void addView(const View* v);
|
||||
|
||||
class GAPI_EXPORTS Priv; // internal use only
|
||||
Priv& priv(); // internal use only
|
||||
const Priv& priv() const; // internal use only
|
||||
|
||||
private:
|
||||
std::unique_ptr<Priv> m_priv;
|
||||
const Cache* m_cache;
|
||||
};
|
||||
|
||||
} // namespace cv::gapi::fluid
|
||||
} // namespace cv::gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_FLUID_BUFFER_HPP
|
||||
Vendored
+442
@@ -0,0 +1,442 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2019 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_FLUID_KERNEL_HPP
|
||||
#define OPENCV_GAPI_FLUID_KERNEL_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/gkernel.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
|
||||
#include <opencv2/gapi/fluid/gfluidbuffer.hpp>
|
||||
|
||||
// FIXME: namespace scheme for backends?
|
||||
namespace cv {
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
/**
|
||||
* @brief This namespace contains G-API Fluid backend functions, structures, and symbols.
|
||||
*/
|
||||
namespace fluid
|
||||
{
|
||||
/**
|
||||
* \addtogroup gapi_std_backends G-API Standard Backends
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief Get a reference to Fluid backend.
|
||||
*
|
||||
* @sa gapi_std_backends
|
||||
*/
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
/** @} */
|
||||
} // namespace fluid
|
||||
} // namespace gapi
|
||||
|
||||
|
||||
class GAPI_EXPORTS GFluidKernel
|
||||
{
|
||||
public:
|
||||
enum class Kind
|
||||
{
|
||||
Filter,
|
||||
Resize,
|
||||
YUV420toRGB //Color conversion of 4:2:0 chroma sub-sampling formats (NV12, I420 ..etc) to RGB
|
||||
};
|
||||
|
||||
// This function is a generic "doWork" callback
|
||||
using F = std::function<void(const cv::GArgs&, const std::vector<gapi::fluid::Buffer*> &)>;
|
||||
|
||||
// This function is a generic "initScratch" callback
|
||||
using IS = std::function<void(const cv::GMetaArgs &, const cv::GArgs&, gapi::fluid::Buffer &)>;
|
||||
|
||||
// This function is a generic "resetScratch" callback
|
||||
using RS = std::function<void(gapi::fluid::Buffer &)>;
|
||||
|
||||
// This function describes kernel metadata inference rule.
|
||||
using M = std::function<GMetaArgs(const GMetaArgs &, const GArgs &)>;
|
||||
|
||||
// This function is a generic "getBorder" callback (extracts border-related data from kernel's input parameters)
|
||||
using B = std::function<gapi::fluid::BorderOpt(const GMetaArgs&, const GArgs&)>;
|
||||
|
||||
// This function is a generic "getWindow" callback (extracts window-related data from kernel's input parameters)
|
||||
using GW = std::function<int(const GMetaArgs&, const GArgs&)>;
|
||||
|
||||
// FIXME: move implementations out of header file
|
||||
GFluidKernel() {}
|
||||
GFluidKernel(Kind k, int l, bool scratch, const F& f, const IS &is, const RS &rs, const B& b, const GW& win)
|
||||
: m_kind(k)
|
||||
, m_lpi(l)
|
||||
, m_scratch(scratch)
|
||||
, m_f(f)
|
||||
, m_is(is)
|
||||
, m_rs(rs)
|
||||
, m_b(b)
|
||||
, m_gw(win) {}
|
||||
|
||||
Kind m_kind;
|
||||
const int m_lpi = -1;
|
||||
const bool m_scratch = false;
|
||||
|
||||
const F m_f;
|
||||
const IS m_is;
|
||||
const RS m_rs;
|
||||
const B m_b;
|
||||
const GW m_gw;
|
||||
};
|
||||
|
||||
// FIXME!!!
|
||||
// This is the temporary and experimental API
|
||||
// which should be replaced by runtime roi-based scheduling
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief This structure allows to control the output image region
|
||||
* which Fluid backend will produce in the graph.
|
||||
*
|
||||
* This feature is useful for external tiling and parallelism, but
|
||||
* will be deprecated in the future releases.
|
||||
*/
|
||||
struct GFluidOutputRois
|
||||
{
|
||||
std::vector<cv::Rect> rois;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This structure forces Fluid backend to generate multiple
|
||||
* parallel output regions in the graph. These regions execute in parallel.
|
||||
*
|
||||
* This feature may be deprecated in the future releases.
|
||||
*/
|
||||
struct GFluidParallelOutputRois
|
||||
{
|
||||
std::vector<GFluidOutputRois> parallel_rois;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This structure allows to customize the way how Fluid executes
|
||||
* parallel regions.
|
||||
*
|
||||
* For example, user can utilize his own threading runtime via this parameter.
|
||||
* The `parallel_for` member functor is called by the Fluid runtime with the
|
||||
* following arguments:
|
||||
*
|
||||
* @param size Size of the parallel range to process
|
||||
* @param f A function which should be called for every integer index
|
||||
* in this range by the specified parallel_for implementation.
|
||||
*
|
||||
* This feature may be deprecated in the future releases.
|
||||
*/
|
||||
struct GFluidParallelFor
|
||||
{
|
||||
//this function accepts:
|
||||
// - size of the "parallel" range as the first argument
|
||||
// - and a function to be called on the range items, designated by item index
|
||||
std::function<void(std::size_t size, std::function<void(std::size_t index)>)> parallel_for;
|
||||
};
|
||||
/** @} gapi_compile_args */
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<> struct CompileArgTag<GFluidOutputRois>
|
||||
{
|
||||
static const char* tag() { return "gapi.fluid.outputRois"; }
|
||||
};
|
||||
|
||||
template<> struct CompileArgTag<GFluidParallelFor>
|
||||
{
|
||||
static const char* tag() { return "gapi.fluid.parallelFor"; }
|
||||
};
|
||||
|
||||
template<> struct CompileArgTag<GFluidParallelOutputRois>
|
||||
{
|
||||
static const char* tag() { return "gapi.fluid.parallelOutputRois"; }
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<class T> struct fluid_get_in;
|
||||
template<> struct fluid_get_in<cv::GMat>
|
||||
{
|
||||
static const cv::gapi::fluid::View& get(const cv::GArgs &in_args, int idx)
|
||||
{
|
||||
return *in_args[idx].unsafe_get<cv::gapi::fluid::View*>();
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct fluid_get_in<cv::GScalar>
|
||||
{
|
||||
// FIXME: change to return by reference when moved to own::Scalar
|
||||
static cv::Scalar get(const cv::GArgs &in_args, int idx)
|
||||
{
|
||||
return in_args[idx].unsafe_get<cv::Scalar>();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename U> struct fluid_get_in<cv::GArray<U>>
|
||||
{
|
||||
static const std::vector<U>& get(const cv::GArgs &in_args, int idx)
|
||||
{
|
||||
return in_args.at(idx).unsafe_get<cv::detail::VectorRef>().rref<U>();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename U> struct fluid_get_in<cv::GOpaque<U>>
|
||||
{
|
||||
static const U& get(const cv::GArgs &in_args, int idx)
|
||||
{
|
||||
return in_args.at(idx).unsafe_get<cv::detail::OpaqueRef>().rref<U>();
|
||||
}
|
||||
};
|
||||
|
||||
template<class T> struct fluid_get_in
|
||||
{
|
||||
static const T& get(const cv::GArgs &in_args, int idx)
|
||||
{
|
||||
return in_args[idx].unsafe_get<T>();
|
||||
}
|
||||
};
|
||||
|
||||
template<bool, typename Impl, typename... Ins>
|
||||
struct scratch_helper;
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct scratch_helper<true, Impl, Ins...>
|
||||
{
|
||||
// Init
|
||||
template<int... IIs>
|
||||
static void help_init_impl(const cv::GMetaArgs &metas,
|
||||
const cv::GArgs &in_args,
|
||||
gapi::fluid::Buffer &scratch_buf,
|
||||
detail::Seq<IIs...>)
|
||||
{
|
||||
Impl::initScratch(get_in_meta<Ins>(metas, in_args, IIs)..., scratch_buf);
|
||||
}
|
||||
|
||||
static void help_init(const cv::GMetaArgs &metas,
|
||||
const cv::GArgs &in_args,
|
||||
gapi::fluid::Buffer &b)
|
||||
{
|
||||
help_init_impl(metas, in_args, b, typename detail::MkSeq<sizeof...(Ins)>::type());
|
||||
}
|
||||
|
||||
// Reset
|
||||
static void help_reset(gapi::fluid::Buffer &b)
|
||||
{
|
||||
Impl::resetScratch(b);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct scratch_helper<false, Impl, Ins...>
|
||||
{
|
||||
static void help_init(const cv::GMetaArgs &,
|
||||
const cv::GArgs &,
|
||||
gapi::fluid::Buffer &)
|
||||
{
|
||||
GAPI_Error("InternalError");
|
||||
}
|
||||
static void help_reset(gapi::fluid::Buffer &)
|
||||
{
|
||||
GAPI_Error("InternalError");
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> struct is_gmat_type
|
||||
{
|
||||
static const constexpr bool value = std::is_same<cv::GMat, T>::value;
|
||||
};
|
||||
|
||||
template<bool CallCustomGetBorder, typename Impl, typename... Ins>
|
||||
struct get_border_helper;
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct get_border_helper<true, Impl, Ins...>
|
||||
{
|
||||
template<int... IIs>
|
||||
static gapi::fluid::BorderOpt get_border_impl(const GMetaArgs &metas,
|
||||
const cv::GArgs &in_args,
|
||||
cv::detail::Seq<IIs...>)
|
||||
{
|
||||
return util::make_optional(Impl::getBorder(cv::detail::get_in_meta<Ins>(metas, in_args, IIs)...));
|
||||
}
|
||||
|
||||
static gapi::fluid::BorderOpt help(const GMetaArgs &metas,
|
||||
const cv::GArgs &in_args)
|
||||
{
|
||||
return get_border_impl(metas, in_args, typename detail::MkSeq<sizeof...(Ins)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct get_border_helper<false, Impl, Ins...>
|
||||
{
|
||||
static gapi::fluid::BorderOpt help(const cv::GMetaArgs &,
|
||||
const cv::GArgs &)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
template<bool CallCustomGetWindow, typename, typename... Ins>
|
||||
struct get_window_helper;
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct get_window_helper<true, Impl, Ins...>
|
||||
{
|
||||
template<int... IIs>
|
||||
static int get_window_impl(const GMetaArgs &metas,
|
||||
const cv::GArgs &in_args,
|
||||
cv::detail::Seq<IIs...>)
|
||||
{
|
||||
return Impl::getWindow(cv::detail::get_in_meta<Ins>(metas, in_args, IIs)...);
|
||||
}
|
||||
|
||||
static int help(const GMetaArgs &metas, const cv::GArgs &in_args)
|
||||
{
|
||||
return get_window_impl(metas, in_args, typename detail::MkSeq<sizeof...(Ins)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Impl, typename... Ins>
|
||||
struct get_window_helper<false, Impl, Ins...>
|
||||
{
|
||||
static int help(const cv::GMetaArgs &,
|
||||
const cv::GArgs &)
|
||||
{
|
||||
return Impl::Window;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C, typename T>
|
||||
struct has_Window
|
||||
{
|
||||
private:
|
||||
template<class U>
|
||||
static constexpr auto Check(U*) -> typename std::is_same<decltype(U::Window), T>::type;
|
||||
|
||||
template<typename>
|
||||
static constexpr std::false_type Check(...);
|
||||
|
||||
typedef decltype(Check<C>(0)) Result;
|
||||
|
||||
public:
|
||||
static constexpr bool value = Result::value;
|
||||
};
|
||||
|
||||
template<bool hasWindow, typename Impl>
|
||||
struct callCustomGetBorder;
|
||||
|
||||
template<typename Impl>
|
||||
struct callCustomGetBorder<true, Impl>
|
||||
{
|
||||
static constexpr bool value = (Impl::Window != 1);
|
||||
};
|
||||
|
||||
template<typename Impl>
|
||||
struct callCustomGetBorder<false, Impl>
|
||||
{
|
||||
static constexpr bool value = true;
|
||||
};
|
||||
|
||||
template<typename, typename, typename, bool UseScratch>
|
||||
struct FluidCallHelper;
|
||||
|
||||
template<typename Impl, typename... Ins, typename... Outs, bool UseScratch>
|
||||
struct FluidCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...>, UseScratch>
|
||||
{
|
||||
static_assert(all_satisfy<is_gmat_type, Outs...>::value, "return type must be GMat");
|
||||
static_assert(contains<GMat, Ins...>::value, "input must contain at least one GMat");
|
||||
|
||||
// Execution dispatcher ////////////////////////////////////////////////////
|
||||
template<int... IIs, int... OIs>
|
||||
static void call_impl(const cv::GArgs &in_args,
|
||||
const std::vector<gapi::fluid::Buffer*> &out_bufs,
|
||||
detail::Seq<IIs...>,
|
||||
detail::Seq<OIs...>)
|
||||
{
|
||||
Impl::run(fluid_get_in<Ins>::get(in_args, IIs)..., *out_bufs[OIs]...);
|
||||
}
|
||||
|
||||
static void call(const cv::GArgs &in_args,
|
||||
const std::vector<gapi::fluid::Buffer*> &out_bufs)
|
||||
{
|
||||
constexpr int numOuts = (sizeof...(Outs)) + (UseScratch ? 1 : 0);
|
||||
call_impl(in_args, out_bufs,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<numOuts>::type());
|
||||
}
|
||||
|
||||
// Scratch buffer initialization dispatcher ////////////////////////////////
|
||||
static void init_scratch(const GMetaArgs &metas,
|
||||
const cv::GArgs &in_args,
|
||||
gapi::fluid::Buffer &b)
|
||||
{
|
||||
scratch_helper<UseScratch, Impl, Ins...>::help_init(metas, in_args, b);
|
||||
}
|
||||
|
||||
// Scratch buffer reset dispatcher /////////////////////////////////////////
|
||||
static void reset_scratch(gapi::fluid::Buffer &scratch_buf)
|
||||
{
|
||||
scratch_helper<UseScratch, Impl, Ins...>::help_reset(scratch_buf);
|
||||
}
|
||||
|
||||
static gapi::fluid::BorderOpt getBorder(const GMetaArgs &metas, const cv::GArgs &in_args)
|
||||
{
|
||||
constexpr bool hasWindow = has_Window<Impl, const int>::value;
|
||||
|
||||
// User must provide "init" callback if Window != 1
|
||||
// TODO: move to constexpr if when we enable C++17
|
||||
return get_border_helper<callCustomGetBorder<hasWindow, Impl>::value, Impl, Ins...>::help(metas, in_args);
|
||||
}
|
||||
|
||||
static int getWindow(const GMetaArgs &metas, const cv::GArgs &in_args)
|
||||
{
|
||||
constexpr bool callCustomGetWindow = !(has_Window<Impl, const int>::value);
|
||||
return get_window_helper<callCustomGetWindow, Impl, Ins...>::help(metas, in_args);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
|
||||
template<class Impl, class K, bool UseScratch>
|
||||
class GFluidKernelImpl : public cv::detail::KernelTag
|
||||
{
|
||||
static const int LPI = 1;
|
||||
static const auto Kind = GFluidKernel::Kind::Filter;
|
||||
using P = detail::FluidCallHelper<Impl, typename K::InArgs, typename K::OutArgs, UseScratch>;
|
||||
|
||||
public:
|
||||
using API = K;
|
||||
|
||||
static GFluidKernel kernel()
|
||||
{
|
||||
// FIXME: call() and getOutMeta() needs to be renamed so it is clear these
|
||||
// functions are internal wrappers, not user API
|
||||
return GFluidKernel(Impl::Kind, Impl::LPI,
|
||||
UseScratch,
|
||||
&P::call, &P::init_scratch, &P::reset_scratch, &P::getBorder, &P::getWindow);
|
||||
}
|
||||
|
||||
static cv::gapi::GBackend backend() { return cv::gapi::fluid::backend(); }
|
||||
};
|
||||
|
||||
#define GAPI_FLUID_KERNEL(Name, API, Scratch) struct Name: public cv::GFluidKernelImpl<Name, API, Scratch>
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GCPUKERNEL_HPP
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_FLUID_IMGPROC_HPP
|
||||
#define OPENCV_GAPI_FLUID_IMGPROC_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
|
||||
|
||||
namespace cv { namespace gapi { namespace imgproc { namespace fluid {
|
||||
|
||||
GAPI_EXPORTS_W GKernelPackage kernels();
|
||||
|
||||
}}}}
|
||||
|
||||
#endif // OPENCV_GAPI_FLUID_IMGPROC_HPP
|
||||
+309
@@ -0,0 +1,309 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GARG_HPP
|
||||
#define OPENCV_GAPI_GARG_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <type_traits>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/own/mat.hpp>
|
||||
#include <opencv2/gapi/media.hpp>
|
||||
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include <opencv2/gapi/util/variant.hpp>
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/gscalar.hpp>
|
||||
#include <opencv2/gapi/garray.hpp>
|
||||
#include <opencv2/gapi/gopaque.hpp>
|
||||
#include <opencv2/gapi/gframe.hpp>
|
||||
#include <opencv2/gapi/gtype_traits.hpp>
|
||||
#include <opencv2/gapi/gmetaarg.hpp>
|
||||
#include <opencv2/gapi/streaming/source.hpp>
|
||||
#include <opencv2/gapi/rmat.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
class GArg;
|
||||
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
using is_garg = std::is_same<GArg, typename std::decay<T>::type>;
|
||||
}
|
||||
|
||||
// Parameter holder class for a node
|
||||
// Depending on platform capabilities, can either support arbitrary types
|
||||
// (as `boost::any`) or a limited number of types (as `boot::variant`).
|
||||
// FIXME: put into "details" as a user shouldn't use it in his code
|
||||
class GAPI_EXPORTS GArg
|
||||
{
|
||||
public:
|
||||
GArg() {}
|
||||
|
||||
template<typename T, typename std::enable_if<!detail::is_garg<T>::value, int>::type = 0>
|
||||
explicit GArg(const T &t)
|
||||
: kind(detail::GTypeTraits<T>::kind)
|
||||
, opaque_kind(detail::GOpaqueTraits<T>::kind)
|
||||
, value(detail::wrap_gapi_helper<T>::wrap(t))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, typename std::enable_if<!detail::is_garg<T>::value, int>::type = 0>
|
||||
explicit GArg(T &&t)
|
||||
: kind(detail::GTypeTraits<typename std::decay<T>::type>::kind)
|
||||
, opaque_kind(detail::GOpaqueTraits<typename std::decay<T>::type>::kind)
|
||||
, value(detail::wrap_gapi_helper<T>::wrap(t))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T> inline T& get()
|
||||
{
|
||||
return util::any_cast<typename std::remove_reference<T>::type>(value);
|
||||
}
|
||||
|
||||
template<typename T> inline const T& get() const
|
||||
{
|
||||
return util::any_cast<typename std::remove_reference<T>::type>(value);
|
||||
}
|
||||
|
||||
template<typename T> inline T& unsafe_get()
|
||||
{
|
||||
return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
|
||||
}
|
||||
|
||||
template<typename T> inline const T& unsafe_get() const
|
||||
{
|
||||
return util::unsafe_any_cast<typename std::remove_reference<T>::type>(value);
|
||||
}
|
||||
|
||||
detail::ArgKind kind = detail::ArgKind::OPAQUE_VAL;
|
||||
detail::OpaqueKind opaque_kind = detail::OpaqueKind::CV_UNKNOWN;
|
||||
|
||||
protected:
|
||||
util::any value;
|
||||
};
|
||||
|
||||
using GArgs = std::vector<GArg>;
|
||||
|
||||
// FIXME: Express as M<GProtoArg...>::type
|
||||
// FIXME: Move to a separate file!
|
||||
using GRunArgBase = util::variant<
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
cv::UMat,
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
cv::RMat,
|
||||
cv::gapi::wip::IStreamSource::Ptr,
|
||||
cv::Mat,
|
||||
cv::Scalar,
|
||||
cv::detail::VectorRef,
|
||||
cv::detail::OpaqueRef,
|
||||
cv::MediaFrame
|
||||
>;
|
||||
|
||||
namespace detail {
|
||||
template<typename,typename>
|
||||
struct in_variant;
|
||||
|
||||
template<typename T, typename... Types>
|
||||
struct in_variant<T, util::variant<Types...> >
|
||||
: std::integral_constant<bool, cv::detail::contains<T, Types...>::value > {
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
struct GAPI_EXPORTS GRunArg: public GRunArgBase
|
||||
{
|
||||
// Metadata information here
|
||||
using Meta = std::unordered_map<std::string, util::any>;
|
||||
Meta meta;
|
||||
|
||||
// Mimic the old GRunArg semantics here, old of the times when
|
||||
// GRunArg was an alias to variant<>
|
||||
GRunArg();
|
||||
GRunArg(const cv::GRunArg &arg);
|
||||
GRunArg(cv::GRunArg &&arg);
|
||||
|
||||
GRunArg& operator= (const GRunArg &arg);
|
||||
GRunArg& operator= (GRunArg &&arg);
|
||||
|
||||
template <typename T>
|
||||
GRunArg(const T &t,
|
||||
const Meta &m = Meta{},
|
||||
typename std::enable_if< detail::in_variant<T, GRunArgBase>::value, int>::type = 0)
|
||||
: GRunArgBase(t)
|
||||
, meta(m)
|
||||
{
|
||||
}
|
||||
template <typename T>
|
||||
GRunArg(T &&t,
|
||||
const Meta &m = Meta{},
|
||||
typename std::enable_if< detail::in_variant<T, GRunArgBase>::value, int>::type = 0)
|
||||
: GRunArgBase(std::move(t))
|
||||
, meta(m)
|
||||
{
|
||||
}
|
||||
template <typename T> auto operator= (const T &t)
|
||||
-> typename std::enable_if< detail::in_variant<T, GRunArgBase>::value, cv::GRunArg>::type&
|
||||
{
|
||||
GRunArgBase::operator=(t);
|
||||
return *this;
|
||||
}
|
||||
template <typename T> auto operator= (T&& t)
|
||||
-> typename std::enable_if< detail::in_variant<T, GRunArgBase>::value, cv::GRunArg>::type&
|
||||
{
|
||||
GRunArgBase::operator=(std::move(t));
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
using GRunArgs = std::vector<GRunArg>;
|
||||
|
||||
// TODO: Think about the addition operator
|
||||
/**
|
||||
* @brief This operator allows to complement the input vector at runtime.
|
||||
*
|
||||
* It's an ordinary overload of addition assignment operator.
|
||||
*
|
||||
* Example of usage:
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/dynamic_graph_snippets.cpp GRunArgs usage
|
||||
*
|
||||
*/
|
||||
inline GRunArgs& operator += (GRunArgs &lhs, const GRunArgs &rhs)
|
||||
{
|
||||
lhs.reserve(lhs.size() + rhs.size());
|
||||
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
namespace wip
|
||||
{
|
||||
/**
|
||||
* @brief This aggregate type represents all types which G-API can
|
||||
* handle (via variant).
|
||||
*
|
||||
* It only exists to overcome C++ language limitations (where a
|
||||
* `using`-defined class can't be forward-declared).
|
||||
*/
|
||||
struct GAPI_EXPORTS Data: public GRunArg
|
||||
{
|
||||
using GRunArg::GRunArg;
|
||||
template <typename T>
|
||||
Data& operator= (const T& t) { GRunArg::operator=(t); return *this; }
|
||||
template <typename T>
|
||||
Data& operator= (T&& t) { GRunArg::operator=(std::move(t)); return *this; }
|
||||
};
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
|
||||
using GRunArgP = util::variant<
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
cv::UMat*,
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
cv::Mat*,
|
||||
cv::RMat*,
|
||||
cv::Scalar*,
|
||||
cv::MediaFrame*,
|
||||
cv::detail::VectorRef,
|
||||
cv::detail::OpaqueRef
|
||||
>;
|
||||
using GRunArgsP = std::vector<GRunArgP>;
|
||||
|
||||
// TODO: Think about the addition operator
|
||||
/**
|
||||
* @brief This operator allows to complement the output vector at runtime.
|
||||
*
|
||||
* It's an ordinary overload of addition assignment operator.
|
||||
*
|
||||
* Example of usage:
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/dynamic_graph_snippets.cpp GRunArgsP usage
|
||||
*
|
||||
*/
|
||||
inline GRunArgsP& operator += (GRunArgsP &lhs, const GRunArgsP &rhs)
|
||||
{
|
||||
lhs.reserve(lhs.size() + rhs.size());
|
||||
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
/**
|
||||
* \addtogroup gapi_serialization
|
||||
* @{
|
||||
*
|
||||
* @brief G-API functions and classes for serialization and deserialization.
|
||||
*/
|
||||
/** @brief Wraps deserialized output GRunArgs to GRunArgsP which can be used by GCompiled.
|
||||
*
|
||||
* Since it's impossible to get modifiable output arguments from deserialization
|
||||
* it needs to be wrapped by this function.
|
||||
*
|
||||
* Example of usage:
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp bind after deserialization
|
||||
*
|
||||
* @param out_args deserialized GRunArgs.
|
||||
* @return the same GRunArgs wrapped in GRunArgsP.
|
||||
* @see deserialize
|
||||
*/
|
||||
GAPI_EXPORTS cv::GRunArgsP bind(cv::GRunArgs &out_args);
|
||||
/** @brief Wraps output GRunArgsP available during graph execution to GRunArgs which can be serialized.
|
||||
*
|
||||
* GRunArgsP is pointer-to-value, so to be serialized they need to be binded to real values
|
||||
* which this function does.
|
||||
*
|
||||
* Example of usage:
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp bind before serialization
|
||||
*
|
||||
* @param out output GRunArgsP available during graph execution.
|
||||
* @return the same GRunArgsP wrapped in serializable GRunArgs.
|
||||
* @see serialize
|
||||
*/
|
||||
GAPI_EXPORTS cv::GRunArg bind(cv::GRunArgP &out); // FIXME: think more about it
|
||||
/** @} */
|
||||
}
|
||||
|
||||
template<typename... Ts> inline GRunArgs gin(const Ts&... args)
|
||||
{
|
||||
return GRunArgs{ GRunArg(detail::wrap_host_helper<Ts>::wrap_in(args))... };
|
||||
}
|
||||
|
||||
template<typename... Ts> inline GRunArgsP gout(Ts&... args)
|
||||
{
|
||||
return GRunArgsP{ GRunArgP(detail::wrap_host_helper<Ts>::wrap_out(args))... };
|
||||
}
|
||||
|
||||
struct GTypeInfo;
|
||||
using GTypesInfo = std::vector<GTypeInfo>;
|
||||
|
||||
// FIXME: Needed for python bridge, must be moved to more appropriate header
|
||||
namespace detail {
|
||||
struct ExtractArgsCallback
|
||||
{
|
||||
cv::GRunArgs operator()(const cv::GTypesInfo& info) const { return c(info); }
|
||||
using CallBackT = std::function<cv::GRunArgs(const cv::GTypesInfo& info)>;
|
||||
CallBackT c;
|
||||
};
|
||||
|
||||
struct ExtractMetaCallback
|
||||
{
|
||||
cv::GMetaArgs operator()(const cv::GTypesInfo& info) const { return c(info); }
|
||||
using CallBackT = std::function<cv::GMetaArgs(const cv::GTypesInfo& info)>;
|
||||
CallBackT c;
|
||||
};
|
||||
|
||||
void constructGraphOutputs(const cv::GTypesInfo &out_info,
|
||||
cv::GRunArgs &args,
|
||||
cv::GRunArgsP &outs);
|
||||
} // namespace detail
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GARG_HPP
|
||||
+440
@@ -0,0 +1,440 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GARRAY_HPP
|
||||
#define OPENCV_GAPI_GARRAY_HPP
|
||||
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
|
||||
#include <opencv2/gapi/util/variant.hpp>
|
||||
#include <opencv2/gapi/util/throw.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp> // flatten_g only!
|
||||
#include <opencv2/gapi/gscalar.hpp> // flatten_g only!
|
||||
|
||||
namespace cv
|
||||
{
|
||||
// Forward declaration; GNode and GOrigin are an internal
|
||||
// (user-inaccessible) classes.
|
||||
class GNode;
|
||||
struct GOrigin;
|
||||
template<typename T> class GArray;
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_meta_args
|
||||
* @{
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE GArrayDesc
|
||||
{
|
||||
// FIXME: Body
|
||||
// FIXME: Also implement proper operator== then
|
||||
bool operator== (const GArrayDesc&) const { return true; }
|
||||
};
|
||||
template<typename U> GArrayDesc descr_of(const std::vector<U> &) { return {};}
|
||||
GAPI_EXPORTS_W inline GArrayDesc empty_array_desc() {return {}; }
|
||||
/** @} */
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const cv::GArrayDesc &desc);
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// ConstructVec is a callback which stores information about T and is used by
|
||||
// G-API runtime to construct arrays in host memory (T remains opaque for G-API).
|
||||
// ConstructVec is carried into G-API internals by GArrayU.
|
||||
// Currently it is suitable for Host (CPU) plugins only, real offload may require
|
||||
// more information for manual memory allocation on-device.
|
||||
class VectorRef;
|
||||
using ConstructVec = std::function<void(VectorRef&)>;
|
||||
|
||||
// This is the base struct for GArrayU type holder
|
||||
struct TypeHintBase{virtual ~TypeHintBase() = default;};
|
||||
|
||||
// This class holds type of initial GArray to be checked from GArrayU
|
||||
template <typename T>
|
||||
struct TypeHint final : public TypeHintBase{};
|
||||
|
||||
// This class strips type information from GArray<T> and makes it usable
|
||||
// in the G-API graph compiler (expression unrolling, graph generation, etc).
|
||||
// Part of GProtoArg.
|
||||
class GAPI_EXPORTS GArrayU
|
||||
{
|
||||
public:
|
||||
GArrayU(const GNode &n, std::size_t out); // Operation result constructor
|
||||
|
||||
template <typename T>
|
||||
bool holds() const; // Check if was created from GArray<T>
|
||||
|
||||
GOrigin& priv(); // Internal use only
|
||||
const GOrigin& priv() const; // Internal use only
|
||||
|
||||
protected:
|
||||
GArrayU(); // Default constructor
|
||||
GArrayU(const detail::VectorRef& vref); // Constant value constructor
|
||||
template<class> friend class cv::GArray; // (available to GArray<T> only)
|
||||
|
||||
void setConstructFcn(ConstructVec &&cv); // Store T-aware constructor
|
||||
|
||||
template <typename T>
|
||||
void specifyType(); // Store type of initial GArray<T>
|
||||
|
||||
template <typename T>
|
||||
void storeKind();
|
||||
|
||||
void setKind(cv::detail::OpaqueKind);
|
||||
|
||||
std::shared_ptr<GOrigin> m_priv;
|
||||
std::shared_ptr<TypeHintBase> m_hint;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool GArrayU::holds() const{
|
||||
GAPI_Assert(m_hint != nullptr);
|
||||
using U = typename std::decay<T>::type;
|
||||
return dynamic_cast<TypeHint<U>*>(m_hint.get()) != nullptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void GArrayU::specifyType(){
|
||||
m_hint.reset(new TypeHint<typename std::decay<T>::type>);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void GArrayU::storeKind(){
|
||||
setKind(cv::detail::GOpaqueTraits<T>::kind);
|
||||
};
|
||||
|
||||
// This class represents a typed STL vector reference.
|
||||
// Depending on origins, this reference may be either "just a" reference to
|
||||
// an object created externally, OR actually own the underlying object
|
||||
// (be value holder).
|
||||
class BasicVectorRef
|
||||
{
|
||||
public:
|
||||
// These fields are set by the derived class(es)
|
||||
std::size_t m_elemSize = 0ul;
|
||||
cv::GArrayDesc m_desc;
|
||||
virtual ~BasicVectorRef() {}
|
||||
|
||||
virtual void mov(BasicVectorRef &ref) = 0;
|
||||
virtual const void* ptr() const = 0;
|
||||
virtual std::size_t size() const = 0;
|
||||
};
|
||||
|
||||
template<typename T> class VectorRefT final: public BasicVectorRef
|
||||
{
|
||||
using empty_t = util::monostate;
|
||||
using ro_ext_t = const std::vector<T> *;
|
||||
using rw_ext_t = std::vector<T> *;
|
||||
using rw_own_t = std::vector<T> ;
|
||||
util::variant<empty_t, ro_ext_t, rw_ext_t, rw_own_t> m_ref;
|
||||
|
||||
inline bool isEmpty() const { return util::holds_alternative<empty_t>(m_ref); }
|
||||
inline bool isROExt() const { return util::holds_alternative<ro_ext_t>(m_ref); }
|
||||
inline bool isRWExt() const { return util::holds_alternative<rw_ext_t>(m_ref); }
|
||||
inline bool isRWOwn() const { return util::holds_alternative<rw_own_t>(m_ref); }
|
||||
|
||||
void init(const std::vector<T>* vec = nullptr)
|
||||
{
|
||||
m_elemSize = sizeof(T);
|
||||
if (vec) m_desc = cv::descr_of(*vec);
|
||||
}
|
||||
|
||||
public:
|
||||
VectorRefT() { init(); }
|
||||
virtual ~VectorRefT() {}
|
||||
|
||||
explicit VectorRefT(const std::vector<T>& vec) : m_ref(&vec) { init(&vec); }
|
||||
explicit VectorRefT(std::vector<T>& vec) : m_ref(&vec) { init(&vec); }
|
||||
explicit VectorRefT(std::vector<T>&& vec) : m_ref(std::move(vec)) { init(&vec); }
|
||||
|
||||
// Reset a VectorRefT. Called only for objects instantiated
|
||||
// internally in G-API (e.g. temporary GArray<T>'s within a
|
||||
// computation). Reset here means both initialization
|
||||
// (creating an object) and reset (discarding its existing
|
||||
// content before the next execution). Must never be called
|
||||
// for external VectorRefTs.
|
||||
void reset()
|
||||
{
|
||||
if (isEmpty())
|
||||
{
|
||||
std::vector<T> empty_vector;
|
||||
m_desc = cv::descr_of(empty_vector);
|
||||
m_ref = std::move(empty_vector);
|
||||
GAPI_Assert(isRWOwn());
|
||||
}
|
||||
else if (isRWOwn())
|
||||
{
|
||||
util::get<rw_own_t>(m_ref).clear();
|
||||
}
|
||||
else GAPI_Error("InternalError"); // shouldn't be called in *EXT modes
|
||||
}
|
||||
|
||||
// Obtain a WRITE reference to underlying object
|
||||
// Used by CPU kernel API wrappers when a kernel execution frame
|
||||
// is created
|
||||
std::vector<T>& wref()
|
||||
{
|
||||
GAPI_Assert(isRWExt() || isRWOwn());
|
||||
if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
|
||||
if (isRWOwn()) return util::get<rw_own_t>(m_ref);
|
||||
util::throw_error(std::logic_error("Impossible happened"));
|
||||
}
|
||||
|
||||
// Obtain a READ reference to underlying object
|
||||
// Used by CPU kernel API wrappers when a kernel execution frame
|
||||
// is created
|
||||
const std::vector<T>& rref() const
|
||||
{
|
||||
// ANY vector can be accessed for reading, even if it declared for
|
||||
// output. Example -- a GComputation from [in] to [out1,out2]
|
||||
// where [out2] is a result of operation applied to [out1]:
|
||||
//
|
||||
// GComputation boundary
|
||||
// . . . . . . .
|
||||
// . .
|
||||
// [in] ----> foo() ----> [out1]
|
||||
// . . :
|
||||
// . . . .:. . .
|
||||
// . V .
|
||||
// . bar() ---> [out2]
|
||||
// . . . . . . . . . . . .
|
||||
//
|
||||
if (isROExt()) return *util::get<ro_ext_t>(m_ref);
|
||||
if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
|
||||
if (isRWOwn()) return util::get<rw_own_t>(m_ref);
|
||||
util::throw_error(std::logic_error("Impossible happened"));
|
||||
}
|
||||
|
||||
virtual void mov(BasicVectorRef &v) override {
|
||||
VectorRefT<T> *tv = dynamic_cast<VectorRefT<T>*>(&v);
|
||||
GAPI_Assert(tv != nullptr);
|
||||
wref() = std::move(tv->wref());
|
||||
}
|
||||
|
||||
virtual const void* ptr() const override { return &rref(); }
|
||||
virtual std::size_t size() const override { return rref().size(); }
|
||||
};
|
||||
|
||||
// This class strips type information from VectorRefT<> and makes it usable
|
||||
// in the G-API executables (carrying run-time data/information to kernels).
|
||||
// Part of GRunArg.
|
||||
// Its methods are typed proxies to VectorRefT<T>.
|
||||
// VectorRef maintains "reference" semantics so two copies of VectoRef refer
|
||||
// to the same underlying object.
|
||||
// FIXME: Put a good explanation on why cv::OutputArray doesn't fit this role
|
||||
class VectorRef
|
||||
{
|
||||
std::shared_ptr<BasicVectorRef> m_ref;
|
||||
cv::detail::OpaqueKind m_kind = cv::detail::OpaqueKind::CV_UNKNOWN;
|
||||
|
||||
template<typename T> inline void check() const
|
||||
{
|
||||
GAPI_DbgAssert(dynamic_cast<VectorRefT<T>*>(m_ref.get()) != nullptr);
|
||||
GAPI_Assert(sizeof(T) == m_ref->m_elemSize);
|
||||
}
|
||||
|
||||
public:
|
||||
VectorRef() = default;
|
||||
template<typename T> explicit VectorRef(const std::vector<T>& vec)
|
||||
: m_ref(new VectorRefT<T>(vec))
|
||||
, m_kind(GOpaqueTraits<T>::kind)
|
||||
{}
|
||||
template<typename T> explicit VectorRef(std::vector<T>& vec)
|
||||
: m_ref(new VectorRefT<T>(vec))
|
||||
, m_kind(GOpaqueTraits<T>::kind)
|
||||
{}
|
||||
template<typename T> explicit VectorRef(std::vector<T>&& vec)
|
||||
: m_ref(new VectorRefT<T>(std::move(vec)))
|
||||
, m_kind(GOpaqueTraits<T>::kind)
|
||||
{}
|
||||
|
||||
cv::detail::OpaqueKind getKind() const
|
||||
{
|
||||
return m_kind;
|
||||
}
|
||||
|
||||
template<typename T> void reset()
|
||||
{
|
||||
if (!m_ref) m_ref.reset(new VectorRefT<T>());
|
||||
check<T>();
|
||||
storeKind<T>();
|
||||
static_cast<VectorRefT<T>&>(*m_ref).reset();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void storeKind()
|
||||
{
|
||||
m_kind = cv::detail::GOpaqueTraits<T>::kind;
|
||||
}
|
||||
|
||||
template<typename T> std::vector<T>& wref()
|
||||
{
|
||||
check<T>();
|
||||
return static_cast<VectorRefT<T>&>(*m_ref).wref();
|
||||
}
|
||||
|
||||
template<typename T> const std::vector<T>& rref() const
|
||||
{
|
||||
check<T>();
|
||||
return static_cast<VectorRefT<T>&>(*m_ref).rref();
|
||||
}
|
||||
|
||||
// Check if was created for/from std::vector<T>
|
||||
template <typename T> bool holds() const
|
||||
{
|
||||
if (!m_ref) return false;
|
||||
using U = typename std::decay<T>::type;
|
||||
return dynamic_cast<VectorRefT<U>*>(m_ref.get()) != nullptr;
|
||||
}
|
||||
|
||||
void mov(VectorRef &v)
|
||||
{
|
||||
m_ref->mov(*v.m_ref);
|
||||
}
|
||||
|
||||
cv::GArrayDesc descr_of() const
|
||||
{
|
||||
return m_ref->m_desc;
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return m_ref->size();
|
||||
}
|
||||
|
||||
// May be used to uniquely identify this object internally
|
||||
const void *ptr() const { return m_ref->ptr(); }
|
||||
};
|
||||
|
||||
// Helper (FIXME: work-around?)
|
||||
// stripping G types to their host types
|
||||
// like cv::GArray<GMat> would still map to std::vector<cv::Mat>
|
||||
// but not to std::vector<cv::GMat>
|
||||
#if defined(GAPI_STANDALONE)
|
||||
# define FLATTEN_NS cv::gapi::own
|
||||
#else
|
||||
# define FLATTEN_NS cv
|
||||
#endif
|
||||
template<class T> struct flatten_g;
|
||||
template<> struct flatten_g<cv::GMat> { using type = FLATTEN_NS::Mat; };
|
||||
template<> struct flatten_g<cv::GScalar> { using type = FLATTEN_NS::Scalar; };
|
||||
template<class T> struct flatten_g<GArray<T>> { using type = std::vector<T>; };
|
||||
template<class T> struct flatten_g { using type = T; };
|
||||
#undef FLATTEN_NS
|
||||
// FIXME: the above mainly duplicates "ProtoToParam" thing from gtyped.hpp
|
||||
// but I decided not to include gtyped here - probably worth moving that stuff
|
||||
// to some common place? (DM)
|
||||
} // namespace detail
|
||||
|
||||
/** \addtogroup gapi_data_objects
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief `cv::GArray<T>` template class represents a list of objects
|
||||
* of class `T` in the graph.
|
||||
*
|
||||
* `cv::GArray<T>` describes a functional relationship between
|
||||
* operations consuming and producing arrays of objects of class
|
||||
* `T`. The primary purpose of `cv::GArray<T>` is to represent a
|
||||
* dynamic list of objects -- where the size of the list is not known
|
||||
* at the graph construction or compile time. Examples include: corner
|
||||
* and feature detectors (`cv::GArray<cv::Point>`), object detection
|
||||
* and tracking results (`cv::GArray<cv::Rect>`). Programmers can use
|
||||
* their own types with `cv::GArray<T>` in the custom operations.
|
||||
*
|
||||
* Similar to `cv::GScalar`, `cv::GArray<T>` may be value-initialized
|
||||
* -- in this case a graph-constant value is associated with the object.
|
||||
*
|
||||
* `GArray<T>` is a virtual counterpart of `std::vector<T>`, which is
|
||||
* usually used to represent the `GArray<T>` data in G-API during the
|
||||
* execution.
|
||||
*
|
||||
* @sa `cv::GOpaque<T>`
|
||||
*/
|
||||
template<typename T> class GArray
|
||||
{
|
||||
public:
|
||||
// Host type (or Flat type) - the type this GArray is actually
|
||||
// specified to.
|
||||
/// @private
|
||||
using HT = typename detail::flatten_g<typename std::decay<T>::type>::type;
|
||||
|
||||
/**
|
||||
* @brief Constructs a value-initialized `cv::GArray<T>`
|
||||
*
|
||||
* `cv::GArray<T>` objects may have their values
|
||||
* be associated at graph construction time. It is useful when
|
||||
* some operation has a `cv::GArray<T>` input which doesn't change during
|
||||
* the program execution, and is set only once. In this case,
|
||||
* there is no need to declare such `cv::GArray<T>` as a graph input.
|
||||
*
|
||||
* @note The value of `cv::GArray<T>` may be overwritten by assigning some
|
||||
* other `cv::GArray<T>` to the object using `operator=` -- on the
|
||||
* assignment, the old association or value is discarded.
|
||||
*
|
||||
* @param v a std::vector<T> to associate with this
|
||||
* `cv::GArray<T>` object. Vector data is copied into the
|
||||
* `cv::GArray<T>` (no reference to the passed data is held).
|
||||
*/
|
||||
explicit GArray(const std::vector<HT>& v) // Constant value constructor
|
||||
: m_ref(detail::GArrayU(detail::VectorRef(v))) { putDetails(); }
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @brief Constructs a value-initialized `cv::GArray<T>`
|
||||
*
|
||||
* @param v a std::vector<T> to associate with this
|
||||
* `cv::GArray<T>` object. Vector data is moved into the `cv::GArray<T>`.
|
||||
*/
|
||||
explicit GArray(std::vector<HT>&& v) // Move-constructor
|
||||
: m_ref(detail::GArrayU(detail::VectorRef(std::move(v)))) { putDetails(); }
|
||||
|
||||
/**
|
||||
* @brief Constructs an empty `cv::GArray<T>`
|
||||
*
|
||||
* Normally, empty G-API data objects denote a starting point of
|
||||
* the graph. When an empty `cv::GArray<T>` is assigned to a result
|
||||
* of some operation, it obtains a functional link to this
|
||||
* operation (and is not empty anymore).
|
||||
*/
|
||||
GArray() { putDetails(); } // Empty constructor
|
||||
|
||||
/// @private
|
||||
explicit GArray(detail::GArrayU &&ref) // GArrayU-based constructor
|
||||
: m_ref(ref) { putDetails(); } // (used by GCall, not for users)
|
||||
|
||||
/// @private
|
||||
detail::GArrayU strip() const {
|
||||
return m_ref;
|
||||
}
|
||||
/// @private
|
||||
static void VCtor(detail::VectorRef& vref) {
|
||||
vref.reset<HT>();
|
||||
}
|
||||
|
||||
private:
|
||||
void putDetails() {
|
||||
m_ref.setConstructFcn(&VCtor);
|
||||
m_ref.specifyType<HT>(); // FIXME: to unify those 2 to avoid excessive dynamic_cast
|
||||
m_ref.storeKind<HT>(); //
|
||||
}
|
||||
|
||||
detail::GArrayU m_ref;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GARRAY_HPP
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_GASYNC_CONTEXT_HPP
|
||||
#define OPENCV_GAPI_GASYNC_CONTEXT_HPP
|
||||
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
# include <opencv2/core/cvdef.h>
|
||||
#else // Without OpenCV
|
||||
# include <opencv2/gapi/own/cvdefs.hpp>
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi{
|
||||
|
||||
/**
|
||||
* @brief This namespace contains experimental G-API functionality,
|
||||
* functions or structures in this namespace are subjects to change or
|
||||
* removal in the future releases. This namespace also contains
|
||||
* functions which API is not stabilized yet.
|
||||
*/
|
||||
namespace wip {
|
||||
|
||||
/**
|
||||
* @brief A class to group async requests to cancel them in a single shot.
|
||||
*
|
||||
* GAsyncContext is passed as an argument to async() and async_apply() functions
|
||||
*/
|
||||
|
||||
class GAPI_EXPORTS GAsyncContext{
|
||||
std::atomic<bool> cancelation_requested = {false};
|
||||
public:
|
||||
/**
|
||||
* @brief Start cancellation process for an associated request.
|
||||
*
|
||||
* User still has to wait for each individual request (either via callback or according std::future object) to make sure it actually canceled.
|
||||
*
|
||||
* @return true if it was a first request to cancel the context
|
||||
*/
|
||||
bool cancel();
|
||||
|
||||
/**
|
||||
* @brief Returns true if cancellation was requested for this context.
|
||||
*
|
||||
* @return true if cancellation was requested for this context
|
||||
*/
|
||||
bool isCanceled() const;
|
||||
};
|
||||
|
||||
class GAPI_EXPORTS GAsyncCanceled : public std::exception {
|
||||
public:
|
||||
virtual const char* what() const noexcept CV_OVERRIDE;
|
||||
};
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif //OPENCV_GAPI_GASYNC_CONTEXT_HPP
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCALL_HPP
|
||||
#define OPENCV_GAPI_GCALL_HPP
|
||||
|
||||
#include <opencv2/gapi/garg.hpp> // GArg
|
||||
#include <opencv2/gapi/gmat.hpp> // GMat
|
||||
#include <opencv2/gapi/gscalar.hpp> // GScalar
|
||||
#include <opencv2/gapi/gframe.hpp> // GFrame
|
||||
#include <opencv2/gapi/garray.hpp> // GArray<T>
|
||||
#include <opencv2/gapi/gopaque.hpp> // GOpaque<T>
|
||||
|
||||
namespace cv {
|
||||
|
||||
struct GKernel;
|
||||
|
||||
// The whole idea of this class is to represent an operation
|
||||
// which is applied to arguments. This is part of public API,
|
||||
// since it is what users should use to define kernel interfaces.
|
||||
|
||||
class GAPI_EXPORTS GCall final
|
||||
{
|
||||
public:
|
||||
class Priv;
|
||||
|
||||
explicit GCall(const GKernel &k);
|
||||
~GCall();
|
||||
|
||||
template<typename... Ts>
|
||||
GCall& pass(Ts&&... args)
|
||||
{
|
||||
setArgs({cv::GArg(std::move(args))...});
|
||||
return *this;
|
||||
}
|
||||
|
||||
// A generic yield method - obtain a link to operator's particular GMat output
|
||||
GMat yield (int output = 0);
|
||||
GMatP yieldP (int output = 0);
|
||||
GScalar yieldScalar(int output = 0);
|
||||
GFrame yieldFrame (int output = 0);
|
||||
|
||||
template<class T> GArray<T> yieldArray(int output = 0)
|
||||
{
|
||||
return GArray<T>(yieldArray(output));
|
||||
}
|
||||
|
||||
template<class T> GOpaque<T> yieldOpaque(int output = 0)
|
||||
{
|
||||
return GOpaque<T>(yieldOpaque(output));
|
||||
}
|
||||
|
||||
// Internal use only
|
||||
Priv& priv();
|
||||
const Priv& priv() const;
|
||||
|
||||
// GKernel and params can be modified, it's needed for infer<Generic>,
|
||||
// because information about output shapes doesn't exist in compile time
|
||||
GKernel& kernel();
|
||||
cv::util::any& params();
|
||||
|
||||
void setArgs(std::vector<GArg> &&args);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
|
||||
// Public versions return a typed array or opaque, those are implementation details
|
||||
detail::GArrayU yieldArray(int output = 0);
|
||||
detail::GOpaqueU yieldOpaque(int output = 0);
|
||||
};
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GCALL_HPP
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMMON_HPP
|
||||
#define OPENCV_GAPI_GCOMMON_HPP
|
||||
|
||||
#include <functional> // std::hash
|
||||
#include <vector> // std::vector
|
||||
#include <type_traits> // decay
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include <opencv2/gapi/util/optional.hpp>
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
#include <opencv2/gapi/render/render_types.hpp>
|
||||
#include <opencv2/gapi/s11n/base.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
class GMat; // FIXME: forward declaration for GOpaqueTraits
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// This is a trait-like structure to mark backend-specific compile arguments
|
||||
// with tags
|
||||
template<typename T> struct CompileArgTag;
|
||||
|
||||
// These structures are tags which separate kernels and transformations
|
||||
struct KernelTag
|
||||
{};
|
||||
struct TransformTag
|
||||
{};
|
||||
|
||||
// This enum is utilized mostly by GArray and GOpaque to store and recognize their internal data
|
||||
// types (aka Host type). Also it is widely used during serialization routine.
|
||||
enum class OpaqueKind: int
|
||||
{
|
||||
CV_UNKNOWN, // Unknown, generic, opaque-to-GAPI data type unsupported in graph seriallization
|
||||
CV_BOOL, // bool user G-API data
|
||||
CV_INT, // int user G-API data
|
||||
CV_INT64, // int64_t user G-API data
|
||||
CV_DOUBLE, // double user G-API data
|
||||
CV_FLOAT, // float user G-API data
|
||||
CV_UINT64, // uint64_t user G-API data
|
||||
CV_STRING, // std::string user G-API data
|
||||
CV_POINT, // cv::Point user G-API data
|
||||
CV_POINT2F, // cv::Point2f user G-API data
|
||||
CV_POINT3F, // cv::Point3f user G-API data
|
||||
CV_SIZE, // cv::Size user G-API data
|
||||
CV_RECT, // cv::Rect user G-API data
|
||||
CV_SCALAR, // cv::Scalar user G-API data
|
||||
CV_MAT, // cv::Mat user G-API data
|
||||
CV_DRAW_PRIM, // cv::gapi::wip::draw::Prim user G-API data
|
||||
};
|
||||
|
||||
// Type traits helper which simplifies the extraction of kind from type
|
||||
template<typename T> struct GOpaqueTraits;
|
||||
template<typename T> struct GOpaqueTraits { static constexpr const OpaqueKind kind = OpaqueKind::CV_UNKNOWN; };
|
||||
template<> struct GOpaqueTraits<int> { static constexpr const OpaqueKind kind = OpaqueKind::CV_INT; };
|
||||
template<> struct GOpaqueTraits<int64_t> { static constexpr const OpaqueKind kind = OpaqueKind::CV_INT64; };
|
||||
template<> struct GOpaqueTraits<double> { static constexpr const OpaqueKind kind = OpaqueKind::CV_DOUBLE; };
|
||||
template<> struct GOpaqueTraits<float> { static constexpr const OpaqueKind kind = OpaqueKind::CV_FLOAT; };
|
||||
template<> struct GOpaqueTraits<uint64_t> { static constexpr const OpaqueKind kind = OpaqueKind::CV_UINT64; };
|
||||
template<> struct GOpaqueTraits<bool> { static constexpr const OpaqueKind kind = OpaqueKind::CV_BOOL; };
|
||||
template<> struct GOpaqueTraits<std::string> { static constexpr const OpaqueKind kind = OpaqueKind::CV_STRING; };
|
||||
template<> struct GOpaqueTraits<cv::Size> { static constexpr const OpaqueKind kind = OpaqueKind::CV_SIZE; };
|
||||
template<> struct GOpaqueTraits<cv::Scalar> { static constexpr const OpaqueKind kind = OpaqueKind::CV_SCALAR; };
|
||||
template<> struct GOpaqueTraits<cv::Point> { static constexpr const OpaqueKind kind = OpaqueKind::CV_POINT; };
|
||||
template<> struct GOpaqueTraits<cv::Point2f> { static constexpr const OpaqueKind kind = OpaqueKind::CV_POINT2F; };
|
||||
template<> struct GOpaqueTraits<cv::Point3f> { static constexpr const OpaqueKind kind = OpaqueKind::CV_POINT3F; };
|
||||
template<> struct GOpaqueTraits<cv::Mat> { static constexpr const OpaqueKind kind = OpaqueKind::CV_MAT; };
|
||||
template<> struct GOpaqueTraits<cv::Rect> { static constexpr const OpaqueKind kind = OpaqueKind::CV_RECT; };
|
||||
template<> struct GOpaqueTraits<cv::GMat> { static constexpr const OpaqueKind kind = OpaqueKind::CV_MAT; };
|
||||
template<> struct GOpaqueTraits<cv::gapi::wip::draw::Prim>
|
||||
{ static constexpr const OpaqueKind kind = OpaqueKind::CV_DRAW_PRIM; };
|
||||
using GOpaqueTraitsArrayTypes = std::tuple<int, double, float, uint64_t, bool, std::string, cv::Size, cv::Scalar, cv::Point, cv::Point2f,
|
||||
cv::Point3f, cv::Mat, cv::Rect, cv::gapi::wip::draw::Prim>;
|
||||
// GOpaque is not supporting cv::Mat and cv::Scalar since there are GScalar and GMat types
|
||||
using GOpaqueTraitsOpaqueTypes = std::tuple<int, double, float, uint64_t, bool, std::string, cv::Size, cv::Point, cv::Point2f, cv::Point3f,
|
||||
cv::Rect, cv::gapi::wip::draw::Prim>;
|
||||
} // namespace detail
|
||||
|
||||
// This definition is here because it is reused by both public(?) and internal
|
||||
// modules. Keeping it here wouldn't expose public details (e.g., API-level)
|
||||
// to components which are internal and operate on a lower-level entities
|
||||
// (e.g., compiler, backends).
|
||||
// FIXME: merge with ArgKind?
|
||||
// FIXME: replace with variant[format desc]?
|
||||
enum class GShape: int
|
||||
{
|
||||
GMAT,
|
||||
GSCALAR,
|
||||
GARRAY,
|
||||
GOPAQUE,
|
||||
GFRAME,
|
||||
};
|
||||
|
||||
namespace gapi {
|
||||
namespace s11n {
|
||||
namespace detail {
|
||||
template<typename T> struct wrap_serialize;
|
||||
} // namespace detail
|
||||
} // namespace s11n
|
||||
} // namespace gapi
|
||||
|
||||
|
||||
struct GCompileArg;
|
||||
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
using is_compile_arg = std::is_same<GCompileArg, typename std::decay<T>::type>;
|
||||
} // namespace detail
|
||||
|
||||
// CompileArg is an unified interface over backend-specific compilation
|
||||
// information
|
||||
// FIXME: Move to a separate file?
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*
|
||||
* @brief Compilation arguments: data structures controlling the
|
||||
* compilation process
|
||||
*
|
||||
* G-API comes with a number of graph compilation options which can be
|
||||
* passed to cv::GComputation::apply() or
|
||||
* cv::GComputation::compile(). Known compilation options are listed
|
||||
* in this page, while extra backends may introduce their own
|
||||
* compilation options (G-API transparently accepts _everything_ which
|
||||
* can be passed to cv::compile_args(), it depends on underlying
|
||||
* backends if an option would be interpreted or not).
|
||||
*
|
||||
* For example, if an example computation is executed like this:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_decl_apply
|
||||
*
|
||||
* Extra parameter specifying which kernels to compile with can be
|
||||
* passed like this:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp apply_with_param
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Represents an arbitrary compilation argument.
|
||||
*
|
||||
* Any value can be wrapped into cv::GCompileArg, but only known ones
|
||||
* (to G-API or its backends) can be interpreted correctly.
|
||||
*
|
||||
* Normally objects of this class shouldn't be created manually, use
|
||||
* cv::compile_args() function which automatically wraps everything
|
||||
* passed in (a variadic template parameter pack) into a vector of
|
||||
* cv::GCompileArg objects.
|
||||
*/
|
||||
struct GCompileArg
|
||||
{
|
||||
public:
|
||||
// NB: Required for pythnon bindings
|
||||
GCompileArg() = default;
|
||||
|
||||
std::string tag;
|
||||
|
||||
// FIXME: use decay in GArg/other trait-based wrapper before leg is shot!
|
||||
template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
|
||||
explicit GCompileArg(T &&t)
|
||||
: tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
|
||||
, serializeF(cv::gapi::s11n::detail::has_S11N_spec<T>::value ?
|
||||
&cv::gapi::s11n::detail::wrap_serialize<T>::serialize :
|
||||
nullptr)
|
||||
, arg(t)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T> T& get()
|
||||
{
|
||||
return util::any_cast<T>(arg);
|
||||
}
|
||||
|
||||
template<typename T> const T& get() const
|
||||
{
|
||||
return util::any_cast<T>(arg);
|
||||
}
|
||||
|
||||
void serialize(cv::gapi::s11n::IOStream& os) const
|
||||
{
|
||||
if (serializeF)
|
||||
{
|
||||
serializeF(os, *this);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<void(cv::gapi::s11n::IOStream&, const GCompileArg&)> serializeF;
|
||||
util::any arg;
|
||||
};
|
||||
|
||||
using GCompileArgs = std::vector<GCompileArg>;
|
||||
|
||||
inline cv::GCompileArgs& operator += ( cv::GCompileArgs &lhs,
|
||||
const cv::GCompileArgs &rhs)
|
||||
{
|
||||
lhs.reserve(lhs.size() + rhs.size());
|
||||
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wraps a list of arguments (a parameter pack) into a vector of
|
||||
* compilation arguments (cv::GCompileArg).
|
||||
*/
|
||||
template<typename... Ts> GCompileArgs compile_args(Ts&&... args)
|
||||
{
|
||||
return GCompileArgs{ GCompileArg(args)... };
|
||||
}
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
/**
|
||||
* @brief Retrieves particular compilation argument by its type from
|
||||
* cv::GCompileArgs
|
||||
*/
|
||||
template<typename T>
|
||||
inline cv::util::optional<T> getCompileArg(const cv::GCompileArgs &args)
|
||||
{
|
||||
for (auto &compile_arg : args)
|
||||
{
|
||||
if (compile_arg.tag == cv::detail::CompileArgTag<T>::tag())
|
||||
{
|
||||
return cv::util::optional<T>(compile_arg.get<T>());
|
||||
}
|
||||
}
|
||||
return cv::util::optional<T>();
|
||||
}
|
||||
|
||||
namespace s11n {
|
||||
namespace detail {
|
||||
template<typename T> struct wrap_serialize
|
||||
{
|
||||
static void serialize(IOStream& os, const GCompileArg& arg)
|
||||
{
|
||||
using DT = typename std::decay<T>::type;
|
||||
S11N<DT>::serialize(os, arg.get<DT>());
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace s11n
|
||||
} // namespace gapi
|
||||
|
||||
/**
|
||||
* @brief Ask G-API to dump compiled graph in Graphviz format under
|
||||
* the given file name.
|
||||
*
|
||||
* Specifies a graph dump path (path to .dot file to be generated).
|
||||
* G-API will dump a .dot file under specified path during a
|
||||
* compilation process if this flag is passed.
|
||||
*/
|
||||
struct graph_dump_path
|
||||
{
|
||||
std::string m_dump_path;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<> struct CompileArgTag<cv::graph_dump_path>
|
||||
{
|
||||
static const char* tag() { return "gapi.graph_dump_path"; }
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace cv
|
||||
|
||||
// std::hash overload for GShape
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash<cv::GShape>
|
||||
{
|
||||
size_t operator() (cv::GShape sh) const
|
||||
{
|
||||
return std::hash<int>()(static_cast<int>(sh));
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_GCOMMON_HPP
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMPILED_HPP
|
||||
#define OPENCV_GAPI_GCOMPILED_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
// This class represents a compiled computation.
|
||||
// In theory (and ideally), it can be used w/o the rest of APIs.
|
||||
// In theory (and ideally), it can be serialized/deserialized.
|
||||
// It can enable scenarious like deployment to an autonomous devince, FuSa, etc.
|
||||
//
|
||||
// Currently GCompiled assumes all GMats you used to pass data to G-API
|
||||
// are valid and not destroyed while you use a GCompiled object.
|
||||
//
|
||||
// FIXME: In future, there should be a way to name I/O objects and specify it
|
||||
// to GCompiled externally (for example, when it is loaded on the target system).
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_main_classes
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief Represents a compiled computation (graph). Can only be used
|
||||
* with image / data formats & resolutions it was compiled for, with
|
||||
* some exceptions.
|
||||
*
|
||||
* This class represents a product of graph compilation (calling
|
||||
* cv::GComputation::compile()). Objects of this class actually do
|
||||
* data processing, and graph execution is incapsulated into objects
|
||||
* of this class. Execution model itself depends on kernels and
|
||||
* backends which were using during the compilation, see @ref
|
||||
* gapi_compile_args for details.
|
||||
*
|
||||
* In a general case, GCompiled objects can be applied to data only in
|
||||
* that formats/resolutions they were compiled for (see @ref
|
||||
* gapi_meta_args). However, if the underlying backends allow, a
|
||||
* compiled object can be _reshaped_ to handle data (images) of
|
||||
* different resolution, though formats and types must remain the same.
|
||||
*
|
||||
* GCompiled is very similar to `std::function<>` in its semantics --
|
||||
* running it looks like a function call in the user code.
|
||||
*
|
||||
* At the moment, GCompiled objects are not reentrant -- generally,
|
||||
* the objects are stateful since graph execution itself is a stateful
|
||||
* process and this state is now maintained in GCompiled's own memory
|
||||
* (not on the process stack).
|
||||
*
|
||||
* At the same time, two different GCompiled objects produced from the
|
||||
* single cv::GComputation are completely independent and can be used
|
||||
* concurrently.
|
||||
*
|
||||
* @sa GStreamingCompiled
|
||||
*/
|
||||
class GAPI_EXPORTS GCompiled
|
||||
{
|
||||
public:
|
||||
/// @private
|
||||
class GAPI_EXPORTS Priv;
|
||||
|
||||
/**
|
||||
* @brief Constructs an empty object
|
||||
*/
|
||||
GCompiled();
|
||||
|
||||
/**
|
||||
* @brief Run the compiled computation, a generic version.
|
||||
*
|
||||
* @param ins vector of inputs to process.
|
||||
* @param outs vector of outputs to produce.
|
||||
*
|
||||
* Input/output vectors must have the same number of elements as
|
||||
* defined in the cv::GComputation protocol (at the moment of its
|
||||
* construction). Shapes of elements also must conform to protocol
|
||||
* (e.g. cv::Mat needs to be passed where cv::GMat has been
|
||||
* declared as input, and so on). Run-time exception is generated
|
||||
* otherwise.
|
||||
*
|
||||
* Objects in output vector may remain empty (like cv::Mat) --
|
||||
* G-API will automatically initialize output objects to proper formats.
|
||||
*
|
||||
* @note Don't construct GRunArgs/GRunArgsP objects manually, use
|
||||
* cv::gin()/cv::gout() wrappers instead.
|
||||
*/
|
||||
void operator() (GRunArgs &&ins, GRunArgsP &&outs); // Generic arg-to-arg
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
|
||||
/**
|
||||
* @brief Execute an unary computation
|
||||
*
|
||||
* @overload
|
||||
* @param in input cv::Mat for unary computation
|
||||
* @param out output cv::Mat for unary computation
|
||||
* process.
|
||||
*/
|
||||
void operator() (cv::Mat in, cv::Mat &out); // Unary overload
|
||||
|
||||
/**
|
||||
* @brief Execute an unary computation
|
||||
*
|
||||
* @overload
|
||||
* @param in input cv::Mat for unary computation
|
||||
* @param out output cv::Scalar for unary computation
|
||||
* process.
|
||||
*/
|
||||
void operator() (cv::Mat in, cv::Scalar &out); // Unary overload (scalar)
|
||||
|
||||
/**
|
||||
* @brief Execute a binary computation
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input cv::Mat for binary computation
|
||||
* @param in2 second input cv::Mat for binary computation
|
||||
* @param out output cv::Mat for binary computation
|
||||
* process.
|
||||
*/
|
||||
void operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out); // Binary overload
|
||||
|
||||
/**
|
||||
* @brief Execute an binary computation
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input cv::Mat for binary computation
|
||||
* @param in2 second input cv::Mat for binary computation
|
||||
* @param out output cv::Scalar for binary computation
|
||||
* process.
|
||||
*/
|
||||
void operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out); // Binary overload (scalar)
|
||||
|
||||
/**
|
||||
* @brief Execute a computation with arbitrary number of
|
||||
* inputs/outputs.
|
||||
*
|
||||
* @overload
|
||||
* @param ins vector of input cv::Mat objects to process by the
|
||||
* computation.
|
||||
* @param outs vector of output cv::Mat objects to produce by the
|
||||
* computation.
|
||||
*
|
||||
* Numbers of elements in ins/outs vectors must match numbers of
|
||||
* inputs/outputs which were used to define the source GComputation.
|
||||
*/
|
||||
void operator() (const std::vector<cv::Mat> &ins, // Compatibility overload
|
||||
const std::vector<cv::Mat> &outs);
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
/// @private
|
||||
Priv& priv();
|
||||
|
||||
/**
|
||||
* @brief Check if compiled object is valid (non-empty)
|
||||
*
|
||||
* @return true if the object is runnable (valid), false otherwise
|
||||
*/
|
||||
explicit operator bool () const;
|
||||
|
||||
/**
|
||||
* @brief Vector of metadata this graph was compiled for.
|
||||
*
|
||||
* @return Unless _reshape_ is not supported, return value is the
|
||||
* same vector which was passed to cv::GComputation::compile() to
|
||||
* produce this compiled object. Otherwise, it is the latest
|
||||
* metadata vector passed to reshape() (if that call was
|
||||
* successful).
|
||||
*/
|
||||
const GMetaArgs& metas() const; // Meta passed to compile()
|
||||
|
||||
/**
|
||||
* @brief Vector of metadata descriptions of graph outputs
|
||||
*
|
||||
* @return vector with formats/resolutions of graph's output
|
||||
* objects, auto-inferred from input metadata vector by
|
||||
* operations which form this computation.
|
||||
*
|
||||
* @note GCompiled objects produced from the same
|
||||
* cv::GComputiation graph with different input metas may return
|
||||
* different values in this vector.
|
||||
*/
|
||||
const GMetaArgs& outMetas() const;
|
||||
|
||||
/**
|
||||
* @brief Check if the underlying backends support reshape or not.
|
||||
*
|
||||
* @return true if supported, false otherwise.
|
||||
*/
|
||||
bool canReshape() const;
|
||||
|
||||
/**
|
||||
* @brief Reshape a compiled graph to support new image
|
||||
* resolutions.
|
||||
*
|
||||
* Throws an exception if an error occurs.
|
||||
*
|
||||
* @param inMetas new metadata to reshape on. Vector size and
|
||||
* metadata shapes must match the computation's protocol.
|
||||
* @param args compilation arguments to use.
|
||||
*/
|
||||
// FIXME: Why it requires compile args?
|
||||
void reshape(const GMetaArgs& inMetas, const GCompileArgs& args);
|
||||
|
||||
/**
|
||||
* @brief Prepare inner kernels states for a new video-stream.
|
||||
*
|
||||
* GCompiled objects may be used to process video streams frame by frame.
|
||||
* In this case, a GCompiled is called on every image frame individually.
|
||||
* Starting OpenCV 4.4, some kernels in the graph may have their internal
|
||||
* states (see GAPI_OCV_KERNEL_ST for the OpenCV backend).
|
||||
* In this case, if user starts processing another video stream with
|
||||
* this GCompiled, this method needs to be called to let kernels re-initialize
|
||||
* their internal states to a new video stream.
|
||||
*/
|
||||
void prepareForNewStream();
|
||||
|
||||
protected:
|
||||
/// @private
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
}
|
||||
|
||||
#endif // OPENCV_GAPI_GCOMPILED_HPP
|
||||
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMPILED_ASYNC_HPP
|
||||
#define OPENCV_GAPI_GCOMPILED_ASYNC_HPP
|
||||
|
||||
#include <future> //for std::future
|
||||
#include <exception> //for std::exception_ptr
|
||||
#include <functional> //for std::function
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
|
||||
namespace cv {
|
||||
//fwd declaration
|
||||
class GCompiled;
|
||||
|
||||
namespace gapi{
|
||||
namespace wip {
|
||||
class GAsyncContext;
|
||||
/**
|
||||
These functions asynchronously (i.e. probably on a separate thread of execution) call GCompiled::operator() member function of their first argument with copies of rest of arguments (except callback) passed in.
|
||||
The difference between the function is the way to get the completion notification (via callback or a waiting on std::future object)
|
||||
If exception is occurred during execution of apply it is transferred to the callback (via function parameter) or passed to future (and will be thrown on call to std::future::get)
|
||||
|
||||
N.B. :
|
||||
Input arguments are copied on call to async function (actually on call to cv::gin) and thus do not have to outlive the actual completion of asynchronous activity.
|
||||
While output arguments are "captured" by reference(pointer) and therefore _must_ outlive the asynchronous activity
|
||||
(i.e. live at least until callback is called or future is unblocked)
|
||||
|
||||
@param gcmpld Compiled computation (graph) to start asynchronously
|
||||
@param callback Callback to be called when execution of gcmpld is done
|
||||
@param ins Input parameters for gcmpld
|
||||
@param outs Output parameters for gcmpld
|
||||
*/
|
||||
GAPI_EXPORTS void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs);
|
||||
|
||||
/** @overload
|
||||
@param gcmpld Compiled computation (graph) to run asynchronously
|
||||
@param callback Callback to be called when execution of gcmpld is done
|
||||
@param ins Input parameters for gcmpld
|
||||
@param outs Output parameters for gcmpld
|
||||
@param ctx Context this request belongs to
|
||||
@see async GAsyncContext
|
||||
*/
|
||||
GAPI_EXPORTS void async(GCompiled& gcmpld, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GAsyncContext& ctx);
|
||||
|
||||
/** @overload
|
||||
@param gcmpld Compiled computation (graph) to run asynchronously
|
||||
@param ins Input parameters for gcmpld
|
||||
@param outs Output parameters for gcmpld
|
||||
@return std::future<void> object to wait for completion of async operation
|
||||
@see async
|
||||
*/
|
||||
GAPI_EXPORTS std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs);
|
||||
|
||||
/**
|
||||
@param gcmpld Compiled computation (graph) to run asynchronously
|
||||
@param ins Input parameters for gcmpld
|
||||
@param outs Output parameters for gcmpld
|
||||
@param ctx Context this request belongs to
|
||||
@return std::future<void> object to wait for completion of async operation
|
||||
@see async GAsyncContext
|
||||
*/
|
||||
GAPI_EXPORTS std::future<void> async(GCompiled& gcmpld, GRunArgs &&ins, GRunArgsP &&outs, GAsyncContext& ctx);
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GCOMPILED_ASYNC_HPP
|
||||
Vendored
+139
@@ -0,0 +1,139 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2019 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
|
||||
#define OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/gkernel.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi
|
||||
{
|
||||
namespace compound
|
||||
{
|
||||
// FIXME User does not need to know about this function
|
||||
// Needs that user may define compound kernels(as cpu kernels)
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
} // namespace compound
|
||||
} // namespace gapi
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
struct GCompoundContext
|
||||
{
|
||||
explicit GCompoundContext(const GArgs& in_args);
|
||||
template<typename T>
|
||||
const T& inArg(int input) { return m_args.at(input).get<T>(); }
|
||||
|
||||
GArgs m_args;
|
||||
GArgs m_results;
|
||||
};
|
||||
|
||||
class GAPI_EXPORTS GCompoundKernel
|
||||
{
|
||||
// Compound kernel must use all of it's inputs
|
||||
public:
|
||||
using F = std::function<void(GCompoundContext& ctx)>;
|
||||
|
||||
explicit GCompoundKernel(const F& f);
|
||||
void apply(GCompoundContext& ctx);
|
||||
|
||||
protected:
|
||||
F m_f;
|
||||
};
|
||||
|
||||
template<typename T> struct get_compound_in
|
||||
{
|
||||
static T get(GCompoundContext &ctx, int idx) { return ctx.inArg<T>(idx); }
|
||||
};
|
||||
|
||||
template<typename U> struct get_compound_in<cv::GArray<U>>
|
||||
{
|
||||
static cv::GArray<U> get(GCompoundContext &ctx, int idx)
|
||||
{
|
||||
auto array = cv::GArray<U>();
|
||||
ctx.m_args[idx] = GArg(array);
|
||||
return array;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename U> struct get_compound_in<cv::GOpaque<U>>
|
||||
{
|
||||
static cv::GOpaque<U> get(GCompoundContext &ctx, int idx)
|
||||
{
|
||||
auto opaq = cv::GOpaque<U>();
|
||||
ctx.m_args[idx] = GArg(opaq);
|
||||
return opaq;
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct get_compound_in<cv::GMatP>
|
||||
{
|
||||
static cv::GMatP get(GCompoundContext &ctx, int idx)
|
||||
{
|
||||
auto mat = cv::GMatP();
|
||||
ctx.m_args[idx] = GArg(mat);
|
||||
return mat;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename, typename, typename>
|
||||
struct GCompoundCallHelper;
|
||||
|
||||
template<typename Impl, typename... Ins, typename... Outs>
|
||||
struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
|
||||
{
|
||||
template<int... IIs, int... OIs>
|
||||
static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
|
||||
{
|
||||
auto result = Impl::expand(get_compound_in<Ins>::get(ctx, IIs)...);
|
||||
auto tuple_return = tuple_wrap_helper<decltype(result)>::get(std::move(result));
|
||||
ctx.m_results = { cv::GArg(std::get<OIs>(tuple_return))... };
|
||||
}
|
||||
|
||||
static void expand(GCompoundContext &ctx)
|
||||
{
|
||||
expand_impl(ctx,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<sizeof...(Outs)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
template<class Impl, class K>
|
||||
class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
|
||||
public cv::detail::KernelTag
|
||||
{
|
||||
using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
|
||||
|
||||
public:
|
||||
using API = K;
|
||||
|
||||
static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
|
||||
static GCompoundKernel kernel() { return GCompoundKernel(&P::expand); }
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
/**
|
||||
* Declares a new compound kernel. See this
|
||||
* [documentation chapter](@ref gapi_kernel_compound)
|
||||
* on compound kernels for more details.
|
||||
*
|
||||
* @param Name type name for new kernel
|
||||
* @param API the interface this kernel implements
|
||||
*/
|
||||
#define GAPI_COMPOUND_KERNEL(Name, API) \
|
||||
struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
|
||||
+580
@@ -0,0 +1,580 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMPUTATION_HPP
|
||||
#define OPENCV_GAPI_GCOMPUTATION_HPP
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/gproto.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/gcompiled.hpp>
|
||||
#include <opencv2/gapi/gstreaming.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// FIXME: move to algorithm, cover with separate tests
|
||||
// FIXME: replace with O(1) version (both memory and compilation time)
|
||||
template<typename...>
|
||||
struct last_type;
|
||||
|
||||
template<typename T>
|
||||
struct last_type<T> { using type = T;};
|
||||
|
||||
template<typename T, typename... Ts>
|
||||
struct last_type<T, Ts...> { using type = typename last_type<Ts...>::type; };
|
||||
|
||||
template<typename... Ts>
|
||||
using last_type_t = typename last_type<Ts...>::type;
|
||||
}
|
||||
|
||||
// Forward-declare the serialization objects
|
||||
namespace gapi {
|
||||
namespace s11n {
|
||||
struct IIStream;
|
||||
struct IOStream;
|
||||
} // namespace s11n
|
||||
} // namespace gapi
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_main_classes
|
||||
* @{
|
||||
*
|
||||
* @brief G-API classes for constructed and compiled graphs.
|
||||
*/
|
||||
/**
|
||||
* @brief GComputation class represents a captured computation
|
||||
* graph. GComputation objects form boundaries for expression code
|
||||
* user writes with G-API, allowing to compile and execute it.
|
||||
*
|
||||
* G-API computations are defined with input/output data
|
||||
* objects. G-API will track automatically which operations connect
|
||||
* specified outputs to the inputs, forming up a call graph to be
|
||||
* executed. The below example expresses calculation of Sobel operator
|
||||
* for edge detection (\f$G = \sqrt{G_x^2 + G_y^2}\f$):
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_def
|
||||
*
|
||||
* Full pipeline can be now captured with this object declaration:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_cap_full
|
||||
*
|
||||
* Input/output data objects on which a call graph should be
|
||||
* reconstructed are passed using special wrappers cv::GIn and
|
||||
* cv::GOut. G-API will track automatically which operations form a
|
||||
* path from inputs to outputs and build the execution graph appropriately.
|
||||
*
|
||||
* Note that cv::GComputation doesn't take ownership on data objects
|
||||
* it is defined. Moreover, multiple GComputation objects may be
|
||||
* defined on the same expressions, e.g. a smaller pipeline which
|
||||
* expects that image gradients are already pre-calculated may be
|
||||
* defined like this:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_cap_sub
|
||||
*
|
||||
* The resulting graph would expect two inputs and produce one
|
||||
* output. In this case, it doesn't matter if gx/gy data objects are
|
||||
* results of cv::gapi::Sobel operators -- G-API will stop unrolling
|
||||
* expressions and building the underlying graph one reaching this
|
||||
* data objects.
|
||||
*
|
||||
* The way how GComputation is defined is important as its definition
|
||||
* specifies graph _protocol_ -- the way how the graph should be
|
||||
* used. Protocol is defined by number of inputs, number of outputs,
|
||||
* and shapes of inputs and outputs.
|
||||
*
|
||||
* In the above example, sobelEdge expects one Mat on input and
|
||||
* produces one Mat; while sobelEdgeSub expects two Mats on input and
|
||||
* produces one Mat. GComputation's protocol defines how other
|
||||
* computation methods should be used -- cv::GComputation::compile() and
|
||||
* cv::GComputation::apply(). For example, if a graph is defined on
|
||||
* two GMat inputs, two cv::Mat objects have to be passed to apply()
|
||||
* for execution. GComputation checks protocol correctness in runtime
|
||||
* so passing a different number of objects in apply() or passing
|
||||
* cv::Scalar instead of cv::Mat there would compile well as a C++
|
||||
* source but raise an exception in run-time. G-API also comes with a
|
||||
* typed wrapper cv::GComputationT<> which introduces this type-checking in
|
||||
* compile-time.
|
||||
*
|
||||
* cv::GComputation itself is a thin object which just captures what
|
||||
* the graph is. The compiled graph (which actually process data) is
|
||||
* represented by class GCompiled. Use compile() method to generate a
|
||||
* compiled graph with given compile options. cv::GComputation can
|
||||
* also be used to process data with implicit graph compilation
|
||||
* on-the-fly, see apply() for details.
|
||||
*
|
||||
* GComputation is a reference-counted object -- once defined, all its
|
||||
* copies will refer to the same instance.
|
||||
*
|
||||
* @sa GCompiled
|
||||
*/
|
||||
class GAPI_EXPORTS_W GComputation
|
||||
{
|
||||
public:
|
||||
class Priv;
|
||||
typedef std::function<GComputation()> Generator;
|
||||
|
||||
// Various constructors enable different ways to define a computation: /////
|
||||
// 1. Generic constructors
|
||||
/**
|
||||
* @brief Define a computation using a generator function.
|
||||
*
|
||||
* Graph can be defined in-place directly at the moment of its
|
||||
* construction with a lambda:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp graph_gen
|
||||
*
|
||||
* This may be useful since all temporary objects (cv::GMats) and
|
||||
* namespaces can be localized to scope of lambda, without
|
||||
* contaminating the parent scope with probably unnecessary objects
|
||||
* and information.
|
||||
*
|
||||
* @param gen generator function which returns a cv::GComputation,
|
||||
* see Generator.
|
||||
*/
|
||||
GComputation(const Generator& gen); // Generator
|
||||
// overload
|
||||
|
||||
/**
|
||||
* @brief Generic GComputation constructor.
|
||||
*
|
||||
* Constructs a new graph with a given protocol, specified as a
|
||||
* flow of operations connecting input/output objects. Throws if
|
||||
* the passed boundaries are invalid, e.g. if there's no
|
||||
* functional dependency (path) between given outputs and inputs.
|
||||
*
|
||||
* @param ins Input data vector.
|
||||
* @param outs Output data vector.
|
||||
*
|
||||
* @note Don't construct GProtoInputArgs/GProtoOutputArgs objects
|
||||
* directly, use cv::GIn()/cv::GOut() wrapper functions instead.
|
||||
*
|
||||
* @sa @ref gapi_data_objects
|
||||
*/
|
||||
GAPI_WRAP GComputation(GProtoInputArgs &&ins,
|
||||
GProtoOutputArgs &&outs); // Arg-to-arg overload
|
||||
|
||||
// 2. Syntax sugar and compatibility overloads
|
||||
/**
|
||||
* @brief Defines an unary (one input -- one output) computation
|
||||
*
|
||||
* @overload
|
||||
* @param in input GMat of the defined unary computation
|
||||
* @param out output GMat of the defined unary computation
|
||||
*/
|
||||
GAPI_WRAP GComputation(GMat in, GMat out); // Unary overload
|
||||
|
||||
/**
|
||||
* @brief Defines an unary (one input -- one output) computation
|
||||
*
|
||||
* @overload
|
||||
* @param in input GMat of the defined unary computation
|
||||
* @param out output GScalar of the defined unary computation
|
||||
*/
|
||||
GAPI_WRAP GComputation(GMat in, GScalar out); // Unary overload (scalar)
|
||||
|
||||
/**
|
||||
* @brief Defines a binary (two inputs -- one output) computation
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input GMat of the defined binary computation
|
||||
* @param in2 second input GMat of the defined binary computation
|
||||
* @param out output GMat of the defined binary computation
|
||||
*/
|
||||
GAPI_WRAP GComputation(GMat in1, GMat in2, GMat out); // Binary overload
|
||||
|
||||
/**
|
||||
* @brief Defines a binary (two inputs -- one output) computation
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input GMat of the defined binary computation
|
||||
* @param in2 second input GMat of the defined binary computation
|
||||
* @param out output GScalar of the defined binary computation
|
||||
*/
|
||||
GComputation(GMat in1, GMat in2, GScalar out); // Binary
|
||||
// overload
|
||||
// (scalar)
|
||||
|
||||
/**
|
||||
* @brief Defines a computation with arbitrary input/output number.
|
||||
*
|
||||
* @overload
|
||||
* @param ins vector of inputs GMats for this computation
|
||||
* @param outs vector of outputs GMats for this computation
|
||||
*
|
||||
* Use this overload for cases when number of computation
|
||||
* inputs/outputs is not known in compile-time -- e.g. when graph
|
||||
* is programmatically generated to build an image pyramid with
|
||||
* the given number of levels, etc.
|
||||
*/
|
||||
GComputation(const std::vector<GMat> &ins, // Compatibility overload
|
||||
const std::vector<GMat> &outs);
|
||||
|
||||
// Various versions of apply(): ////////////////////////////////////////////
|
||||
// 1. Generic apply()
|
||||
/**
|
||||
* @brief Compile graph on-the-fly and immediately execute it on
|
||||
* the inputs data vectors.
|
||||
*
|
||||
* Number of input/output data objects must match GComputation's
|
||||
* protocol, also types of host data objects (cv::Mat, cv::Scalar)
|
||||
* must match the shapes of data objects from protocol (cv::GMat,
|
||||
* cv::GScalar). If there's a mismatch, a run-time exception will
|
||||
* be generated.
|
||||
*
|
||||
* Internally, a cv::GCompiled object is created for the given
|
||||
* input format configuration, which then is executed on the input
|
||||
* data immediately. cv::GComputation caches compiled objects
|
||||
* produced within apply() -- if this method would be called next
|
||||
* time with the same input parameters (image formats, image
|
||||
* resolution, etc), the underlying compiled graph will be reused
|
||||
* without recompilation. If new metadata doesn't match the cached
|
||||
* one, the underlying compiled graph is regenerated.
|
||||
*
|
||||
* @note compile() always triggers a compilation process and
|
||||
* produces a new GCompiled object regardless if a similar one has
|
||||
* been cached via apply() or not.
|
||||
*
|
||||
* @param ins vector of input data to process. Don't create
|
||||
* GRunArgs object manually, use cv::gin() wrapper instead.
|
||||
* @param outs vector of output data to fill results in. cv::Mat
|
||||
* objects may be empty in this vector, G-API will automatically
|
||||
* initialize it with the required format & dimensions. Don't
|
||||
* create GRunArgsP object manually, use cv::gout() wrapper instead.
|
||||
* @param args a list of compilation arguments to pass to the
|
||||
* underlying compilation process. Don't create GCompileArgs
|
||||
* object manually, use cv::compile_args() wrapper instead.
|
||||
*
|
||||
* @sa @ref gapi_data_objects, @ref gapi_compile_args
|
||||
*/
|
||||
void apply(GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args = {}); // Arg-to-arg overload
|
||||
|
||||
/// @private -- Exclude this function from OpenCV documentation
|
||||
GAPI_WRAP GRunArgs apply(const cv::detail::ExtractArgsCallback &callback,
|
||||
GCompileArgs &&args = {});
|
||||
|
||||
/// @private -- Exclude this function from OpenCV documentation
|
||||
void apply(const std::vector<cv::Mat>& ins, // Compatibility overload
|
||||
const std::vector<cv::Mat>& outs,
|
||||
GCompileArgs &&args = {});
|
||||
|
||||
// 2. Syntax sugar and compatibility overloads
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
/**
|
||||
* @brief Execute an unary computation (with compilation on the fly)
|
||||
*
|
||||
* @overload
|
||||
* @param in input cv::Mat for unary computation
|
||||
* @param out output cv::Mat for unary computation
|
||||
* @param args compilation arguments for underlying compilation
|
||||
* process.
|
||||
*/
|
||||
void apply(cv::Mat in, cv::Mat &out, GCompileArgs &&args = {}); // Unary overload
|
||||
|
||||
/**
|
||||
* @brief Execute an unary computation (with compilation on the fly)
|
||||
*
|
||||
* @overload
|
||||
* @param in input cv::Mat for unary computation
|
||||
* @param out output cv::Scalar for unary computation
|
||||
* @param args compilation arguments for underlying compilation
|
||||
* process.
|
||||
*/
|
||||
void apply(cv::Mat in, cv::Scalar &out, GCompileArgs &&args = {}); // Unary overload (scalar)
|
||||
|
||||
/**
|
||||
* @brief Execute a binary computation (with compilation on the fly)
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input cv::Mat for binary computation
|
||||
* @param in2 second input cv::Mat for binary computation
|
||||
* @param out output cv::Mat for binary computation
|
||||
* @param args compilation arguments for underlying compilation
|
||||
* process.
|
||||
*/
|
||||
void apply(cv::Mat in1, cv::Mat in2, cv::Mat &out, GCompileArgs &&args = {}); // Binary overload
|
||||
|
||||
/**
|
||||
* @brief Execute an binary computation (with compilation on the fly)
|
||||
*
|
||||
* @overload
|
||||
* @param in1 first input cv::Mat for binary computation
|
||||
* @param in2 second input cv::Mat for binary computation
|
||||
* @param out output cv::Scalar for binary computation
|
||||
* @param args compilation arguments for underlying compilation
|
||||
* process.
|
||||
*/
|
||||
void apply(cv::Mat in1, cv::Mat in2, cv::Scalar &out, GCompileArgs &&args = {}); // Binary overload (scalar)
|
||||
|
||||
/**
|
||||
* @brief Execute a computation with arbitrary number of
|
||||
* inputs/outputs (with compilation on-the-fly).
|
||||
*
|
||||
* @overload
|
||||
* @param ins vector of input cv::Mat objects to process by the
|
||||
* computation.
|
||||
* @param outs vector of output cv::Mat objects to produce by the
|
||||
* computation.
|
||||
* @param args compilation arguments for underlying compilation
|
||||
* process.
|
||||
*
|
||||
* Numbers of elements in ins/outs vectors must match numbers of
|
||||
* inputs/outputs which were used to define this GComputation.
|
||||
*/
|
||||
void apply(const std::vector<cv::Mat>& ins, // Compatibility overload
|
||||
std::vector<cv::Mat>& outs,
|
||||
GCompileArgs &&args = {});
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
// Various versions of compile(): //////////////////////////////////////////
|
||||
// 1. Generic compile() - requires metas to be passed as vector
|
||||
/**
|
||||
* @brief Compile the computation for specific input format(s).
|
||||
*
|
||||
* This method triggers compilation process and produces a new
|
||||
* GCompiled object which then can process data of the given
|
||||
* format. Passing data with different format to the compiled
|
||||
* computation will generate a run-time exception.
|
||||
*
|
||||
* @param in_metas vector of input metadata configuration. Grab
|
||||
* metadata from real data objects (like cv::Mat or cv::Scalar)
|
||||
* using cv::descr_of(), or create it on your own.
|
||||
* @param args compilation arguments for this compilation
|
||||
* process. Compilation arguments directly affect what kind of
|
||||
* executable object would be produced, e.g. which kernels (and
|
||||
* thus, devices) would be used to execute computation.
|
||||
*
|
||||
* @return GCompiled, an executable computation compiled
|
||||
* specifically for the given input parameters.
|
||||
*
|
||||
* @sa @ref gapi_compile_args
|
||||
*/
|
||||
GCompiled compile(GMetaArgs &&in_metas, GCompileArgs &&args = {});
|
||||
|
||||
// 2. Syntax sugar - variadic list of metas, no extra compile args
|
||||
// FIXME: SFINAE looks ugly in the generated documentation
|
||||
/**
|
||||
* @overload
|
||||
*
|
||||
* Takes a variadic parameter pack with metadata
|
||||
* descriptors for which a compiled object needs to be produced.
|
||||
*
|
||||
* @return GCompiled, an executable computation compiled
|
||||
* specifically for the given input parameters.
|
||||
*/
|
||||
template<typename... Ts>
|
||||
auto compile(const Ts&... metas) ->
|
||||
typename std::enable_if<detail::are_meta_descrs<Ts...>::value, GCompiled>::type
|
||||
{
|
||||
return compile(GMetaArgs{GMetaArg(metas)...}, GCompileArgs());
|
||||
}
|
||||
|
||||
// 3. Syntax sugar - variadic list of metas, extra compile args
|
||||
// (seems optional parameters don't work well when there's an variadic template
|
||||
// comes first)
|
||||
//
|
||||
// Ideally it should look like:
|
||||
//
|
||||
// template<typename... Ts>
|
||||
// GCompiled compile(const Ts&... metas, GCompileArgs &&args)
|
||||
//
|
||||
// But not all compilers can handle this (and seems they shouldn't be able to).
|
||||
// FIXME: SFINAE looks ugly in the generated documentation
|
||||
/**
|
||||
* @overload
|
||||
*
|
||||
* Takes a variadic parameter pack with metadata
|
||||
* descriptors for which a compiled object needs to be produced,
|
||||
* followed by GCompileArgs object representing compilation
|
||||
* arguments for this process.
|
||||
*
|
||||
* @return GCompiled, an executable computation compiled
|
||||
* specifically for the given input parameters.
|
||||
*/
|
||||
template<typename... Ts>
|
||||
auto compile(const Ts&... meta_and_compile_args) ->
|
||||
typename std::enable_if<detail::are_meta_descrs_but_last<Ts...>::value
|
||||
&& std::is_same<GCompileArgs, detail::last_type_t<Ts...> >::value,
|
||||
GCompiled>::type
|
||||
{
|
||||
//FIXME: wrapping meta_and_compile_args into a tuple to unwrap them inside a helper function is the overkill
|
||||
return compile(std::make_tuple(meta_and_compile_args...),
|
||||
typename detail::MkSeq<sizeof...(Ts)-1>::type());
|
||||
}
|
||||
|
||||
|
||||
// FIXME: Document properly in the Doxygen format
|
||||
// Video-oriented pipeline compilation:
|
||||
// 1. A generic version
|
||||
/**
|
||||
* @brief Compile the computation for streaming mode.
|
||||
*
|
||||
* This method triggers compilation process and produces a new
|
||||
* GStreamingCompiled object which then can process video stream
|
||||
* data of the given format. Passing a stream in a different
|
||||
* format to the compiled computation will generate a run-time
|
||||
* exception.
|
||||
*
|
||||
* @param in_metas vector of input metadata configuration. Grab
|
||||
* metadata from real data objects (like cv::Mat or cv::Scalar)
|
||||
* using cv::descr_of(), or create it on your own.
|
||||
*
|
||||
* @param args compilation arguments for this compilation
|
||||
* process. Compilation arguments directly affect what kind of
|
||||
* executable object would be produced, e.g. which kernels (and
|
||||
* thus, devices) would be used to execute computation.
|
||||
*
|
||||
* @return GStreamingCompiled, a streaming-oriented executable
|
||||
* computation compiled specifically for the given input
|
||||
* parameters.
|
||||
*
|
||||
* @sa @ref gapi_compile_args
|
||||
*/
|
||||
GAPI_WRAP GStreamingCompiled compileStreaming(GMetaArgs &&in_metas, GCompileArgs &&args = {});
|
||||
|
||||
/**
|
||||
* @brief Compile the computation for streaming mode.
|
||||
*
|
||||
* This method triggers compilation process and produces a new
|
||||
* GStreamingCompiled object which then can process video stream
|
||||
* data in any format. Underlying mechanisms will be adjusted to
|
||||
* every new input video stream automatically, but please note that
|
||||
* _not all_ existing backends support this (see reshape()).
|
||||
*
|
||||
* @param args compilation arguments for this compilation
|
||||
* process. Compilation arguments directly affect what kind of
|
||||
* executable object would be produced, e.g. which kernels (and
|
||||
* thus, devices) would be used to execute computation.
|
||||
*
|
||||
* @return GStreamingCompiled, a streaming-oriented executable
|
||||
* computation compiled for any input image format.
|
||||
*
|
||||
* @sa @ref gapi_compile_args
|
||||
*/
|
||||
GAPI_WRAP GStreamingCompiled compileStreaming(GCompileArgs &&args = {});
|
||||
|
||||
/// @private -- Exclude this function from OpenCV documentation
|
||||
GAPI_WRAP GStreamingCompiled compileStreaming(const cv::detail::ExtractMetaCallback &callback,
|
||||
GCompileArgs &&args = {});
|
||||
|
||||
// 2. Direct metadata version
|
||||
/**
|
||||
* @overload
|
||||
*
|
||||
* Takes a variadic parameter pack with metadata
|
||||
* descriptors for which a compiled object needs to be produced.
|
||||
*
|
||||
* @return GStreamingCompiled, a streaming-oriented executable
|
||||
* computation compiled specifically for the given input
|
||||
* parameters.
|
||||
*/
|
||||
template<typename... Ts>
|
||||
auto compileStreaming(const Ts&... metas) ->
|
||||
typename std::enable_if<detail::are_meta_descrs<Ts...>::value, GStreamingCompiled>::type
|
||||
{
|
||||
return compileStreaming(GMetaArgs{GMetaArg(metas)...}, GCompileArgs());
|
||||
}
|
||||
|
||||
// 2. Direct metadata + compile arguments version
|
||||
/**
|
||||
* @overload
|
||||
*
|
||||
* Takes a variadic parameter pack with metadata
|
||||
* descriptors for which a compiled object needs to be produced,
|
||||
* followed by GCompileArgs object representing compilation
|
||||
* arguments for this process.
|
||||
*
|
||||
* @return GStreamingCompiled, a streaming-oriented executable
|
||||
* computation compiled specifically for the given input
|
||||
* parameters.
|
||||
*/
|
||||
template<typename... Ts>
|
||||
auto compileStreaming(const Ts&... meta_and_compile_args) ->
|
||||
typename std::enable_if<detail::are_meta_descrs_but_last<Ts...>::value
|
||||
&& std::is_same<GCompileArgs, detail::last_type_t<Ts...> >::value,
|
||||
GStreamingCompiled>::type
|
||||
{
|
||||
//FIXME: wrapping meta_and_compile_args into a tuple to unwrap them inside a helper function is the overkill
|
||||
return compileStreaming(std::make_tuple(meta_and_compile_args...),
|
||||
typename detail::MkSeq<sizeof...(Ts)-1>::type());
|
||||
}
|
||||
|
||||
// Internal use only
|
||||
/// @private
|
||||
Priv& priv();
|
||||
/// @private
|
||||
const Priv& priv() const;
|
||||
/// @private
|
||||
explicit GComputation(cv::gapi::s11n::IIStream &);
|
||||
/// @private
|
||||
void serialize(cv::gapi::s11n::IOStream &) const;
|
||||
|
||||
protected:
|
||||
|
||||
// 4. Helper methods for (3)
|
||||
/// @private
|
||||
template<typename... Ts, int... IIs>
|
||||
GCompiled compile(const std::tuple<Ts...> &meta_and_compile_args, detail::Seq<IIs...>)
|
||||
{
|
||||
GMetaArgs meta_args = {GMetaArg(std::get<IIs>(meta_and_compile_args))...};
|
||||
GCompileArgs comp_args = std::get<sizeof...(Ts)-1>(meta_and_compile_args);
|
||||
return compile(std::move(meta_args), std::move(comp_args));
|
||||
}
|
||||
template<typename... Ts, int... IIs>
|
||||
GStreamingCompiled compileStreaming(const std::tuple<Ts...> &meta_and_compile_args, detail::Seq<IIs...>)
|
||||
{
|
||||
GMetaArgs meta_args = {GMetaArg(std::get<IIs>(meta_and_compile_args))...};
|
||||
GCompileArgs comp_args = std::get<sizeof...(Ts)-1>(meta_and_compile_args);
|
||||
return compileStreaming(std::move(meta_args), std::move(comp_args));
|
||||
}
|
||||
void recompile(GMetaArgs&& in_metas, GCompileArgs &&args);
|
||||
/// @private
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
namespace gapi
|
||||
{
|
||||
// FIXME: all these standalone functions need to be added to some
|
||||
// common documentation section
|
||||
/**
|
||||
* @brief Define an tagged island (subgraph) within a computation.
|
||||
*
|
||||
* Declare an Island tagged with `name` and defined from `ins` to `outs`
|
||||
* (exclusively, as ins/outs are data objects, and regioning is done on
|
||||
* operations level).
|
||||
* Throws if any operation between `ins` and `outs` are already assigned
|
||||
* to another island.
|
||||
*
|
||||
* Islands allow to partition graph into subgraphs, fine-tuning
|
||||
* the way it is scheduled by the underlying executor.
|
||||
*
|
||||
* @param name name of the Island to create
|
||||
* @param ins vector of input data objects where the subgraph
|
||||
* begins
|
||||
* @param outs vector of output data objects where the subgraph
|
||||
* ends.
|
||||
*
|
||||
* The way how an island is defined is similar to how
|
||||
* cv::GComputation is defined on input/output data objects.
|
||||
* Same rules apply here as well -- if there's no functional
|
||||
* dependency between inputs and outputs or there's not enough
|
||||
* input data objects were specified to properly calculate all
|
||||
* outputs, an exception is thrown.
|
||||
*
|
||||
* Use cv::GIn() / cv::GOut() to specify input/output vectors.
|
||||
*/
|
||||
void GAPI_EXPORTS island(const std::string &name,
|
||||
GProtoInputArgs &&ins,
|
||||
GProtoOutputArgs &&outs);
|
||||
} // namespace gapi
|
||||
|
||||
} // namespace cv
|
||||
#endif // OPENCV_GAPI_GCOMPUTATION_HPP
|
||||
Vendored
+69
@@ -0,0 +1,69 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_GCOMPUTATION_ASYNC_HPP
|
||||
#define OPENCV_GAPI_GCOMPUTATION_ASYNC_HPP
|
||||
|
||||
|
||||
#include <future> //for std::future
|
||||
#include <exception> //for std::exception_ptr
|
||||
#include <functional> //for std::function
|
||||
#include <opencv2/gapi/garg.hpp> //for GRunArgs, GRunArgsP
|
||||
#include <opencv2/gapi/gcommon.hpp> //for GCompileArgs
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
|
||||
|
||||
namespace cv {
|
||||
//fwd declaration
|
||||
class GComputation;
|
||||
namespace gapi {
|
||||
namespace wip {
|
||||
class GAsyncContext;
|
||||
/** In contrast to async() functions, these do call GComputation::apply() member function of the GComputation passed in.
|
||||
|
||||
@param gcomp Computation (graph) to run asynchronously
|
||||
@param callback Callback to be called when execution of gcomp is done
|
||||
@param ins Input parameters for gcomp
|
||||
@param outs Output parameters for gcomp
|
||||
@param args Compile arguments to pass to GComputation::apply()
|
||||
@see async
|
||||
*/
|
||||
GAPI_EXPORTS void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args = {});
|
||||
/** @overload
|
||||
@param gcomp Computation (graph) to run asynchronously
|
||||
@param callback Callback to be called when execution of gcomp is done
|
||||
@param ins Input parameters for gcomp
|
||||
@param outs Output parameters for gcomp
|
||||
@param args Compile arguments to pass to GComputation::apply()
|
||||
@param ctx Context this request belongs to
|
||||
@see async_apply async GAsyncContext
|
||||
*/
|
||||
GAPI_EXPORTS void async_apply(GComputation& gcomp, std::function<void(std::exception_ptr)>&& callback, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args, GAsyncContext& ctx);
|
||||
/** @overload
|
||||
@param gcomp Computation (graph) to run asynchronously
|
||||
@param ins Input parameters for gcomp
|
||||
@param outs Output parameters for gcomp
|
||||
@param args Compile arguments to pass to GComputation::apply()
|
||||
@return std::future<void> object to wait for completion of async operation
|
||||
@see async_apply async
|
||||
*/
|
||||
GAPI_EXPORTS std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args = {});
|
||||
/** @overload
|
||||
@param gcomp Computation (graph) to run asynchronously
|
||||
@param ins Input parameters for gcomp
|
||||
@param outs Output parameters for gcomp
|
||||
@param args Compile arguments to pass to GComputation::apply()
|
||||
@param ctx Context this request belongs to
|
||||
@return std::future<void> object to wait for completion of async operation
|
||||
@see async_apply async GAsyncContext
|
||||
*/
|
||||
GAPI_EXPORTS std::future<void> async_apply(GComputation& gcomp, GRunArgs &&ins, GRunArgsP &&outs, GCompileArgs &&args, GAsyncContext& ctx);
|
||||
} // namespace wip
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif //OPENCV_GAPI_GCOMPUTATION_ASYNC_HPP
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GFRAME_HPP
|
||||
#define OPENCV_GAPI_GFRAME_HPP
|
||||
|
||||
#include <ostream>
|
||||
#include <memory> // std::shared_ptr
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp> // GShape
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
|
||||
// TODO GAPI_EXPORTS or so
|
||||
namespace cv
|
||||
{
|
||||
// Forward declaration; GNode and GOrigin are an internal
|
||||
// (user-inaccessible) classes.
|
||||
class GNode;
|
||||
struct GOrigin;
|
||||
|
||||
/** \addtogroup gapi_data_objects
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief GFrame class represents an image or media frame in the graph.
|
||||
*
|
||||
* GFrame doesn't store any data itself, instead it describes a
|
||||
* functional relationship between operations consuming and producing
|
||||
* GFrame objects.
|
||||
*
|
||||
* GFrame is introduced to handle various media formats (e.g., NV12 or
|
||||
* I420) under the same type. Various image formats may differ in the
|
||||
* number of planes (e.g. two for NV12, three for I420) and the pixel
|
||||
* layout inside. GFrame type allows to handle these media formats in
|
||||
* the graph uniformly -- the graph structure will not change if the
|
||||
* media format changes, e.g. a different camera or decoder is used
|
||||
* with the same graph. G-API provides a number of operations which
|
||||
* operate directly on GFrame, like `infer<>()` or
|
||||
* renderFrame(); these operations are expected to handle different
|
||||
* media formats inside. There is also a number of accessor
|
||||
* operations like BGR(), Y(), UV() -- these operations provide
|
||||
* access to frame's data in the familiar cv::GMat form, which can be
|
||||
* used with the majority of the existing G-API operations. These
|
||||
* accessor functions may perform color space conversion on the fly if
|
||||
* the image format of the GFrame they are applied to differs from the
|
||||
* operation's semantic (e.g. the BGR() accessor is called on an NV12
|
||||
* image frame).
|
||||
*
|
||||
* GFrame is a virtual counterpart of cv::MediaFrame.
|
||||
*
|
||||
* @sa cv::MediaFrame, cv::GFrameDesc, BGR(), Y(), UV(), infer<>().
|
||||
*/
|
||||
class GAPI_EXPORTS_W_SIMPLE GFrame
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs an empty GFrame
|
||||
*
|
||||
* Normally, empty G-API data objects denote a starting point of
|
||||
* the graph. When an empty GFrame is assigned to a result of some
|
||||
* operation, it obtains a functional link to this operation (and
|
||||
* is not empty anymore).
|
||||
*/
|
||||
GAPI_WRAP GFrame(); // Empty constructor
|
||||
|
||||
/// @private
|
||||
GFrame(const GNode &n, std::size_t out); // Operation result constructor
|
||||
/// @private
|
||||
GOrigin& priv(); // Internal use only
|
||||
/// @private
|
||||
const GOrigin& priv() const; // Internal use only
|
||||
|
||||
private:
|
||||
std::shared_ptr<GOrigin> m_priv;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
enum class MediaFormat: int
|
||||
{
|
||||
BGR = 0,
|
||||
NV12,
|
||||
GRAY,
|
||||
};
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_meta_args
|
||||
* @{
|
||||
*/
|
||||
struct GAPI_EXPORTS GFrameDesc
|
||||
{
|
||||
MediaFormat fmt;
|
||||
cv::Size size;
|
||||
|
||||
bool operator== (const GFrameDesc &) const;
|
||||
};
|
||||
static inline GFrameDesc empty_gframe_desc() { return GFrameDesc{}; }
|
||||
/** @} */
|
||||
|
||||
class MediaFrame;
|
||||
GAPI_EXPORTS GFrameDesc descr_of(const MediaFrame &frame);
|
||||
|
||||
GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const cv::GFrameDesc &desc);
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GFRAME_HPP
|
||||
+757
@@ -0,0 +1,757 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GKERNEL_HPP
|
||||
#define OPENCV_GAPI_GKERNEL_HPP
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string> // string
|
||||
#include <type_traits> // false_type, true_type
|
||||
#include <unordered_map> // map (for GKernelPackage)
|
||||
#include <utility> // tuple
|
||||
|
||||
#include <opencv2/gapi/gcommon.hpp> // CompileArgTag
|
||||
#include <opencv2/gapi/util/util.hpp> // Seq
|
||||
#include <opencv2/gapi/gcall.hpp>
|
||||
#include <opencv2/gapi/garg.hpp> // GArg
|
||||
#include <opencv2/gapi/gmetaarg.hpp> // GMetaArg
|
||||
#include <opencv2/gapi/gtype_traits.hpp> // GTypeTraits
|
||||
#include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
|
||||
#include <opencv2/gapi/gtransform.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
struct GTypeInfo
|
||||
{
|
||||
GShape shape;
|
||||
cv::detail::OpaqueKind kind;
|
||||
detail::HostCtor ctor;
|
||||
};
|
||||
|
||||
using GShapes = std::vector<GShape>;
|
||||
using GKinds = std::vector<cv::detail::OpaqueKind>;
|
||||
using GCtors = std::vector<detail::HostCtor>;
|
||||
using GTypesInfo = std::vector<GTypeInfo>;
|
||||
|
||||
// GKernel describes kernel API to the system
|
||||
// FIXME: add attributes of a kernel, (e.g. number and types
|
||||
// of inputs, etc)
|
||||
struct GAPI_EXPORTS GKernel
|
||||
{
|
||||
using M = std::function<GMetaArgs(const GMetaArgs &, const GArgs &)>;
|
||||
|
||||
std::string name; // kernel ID, defined by its API (signature)
|
||||
std::string tag; // some (implementation-specific) tag
|
||||
M outMeta; // generic adaptor to API::outMeta(...)
|
||||
GShapes outShapes; // types (shapes) kernel's outputs
|
||||
GKinds inKinds; // kinds of kernel's inputs (fixme: below)
|
||||
GCtors outCtors; // captured constructors for template output types
|
||||
GKinds outKinds; // kinds of kernel's outputs (fixme: below)
|
||||
};
|
||||
// TODO: It's questionable if inKinds should really be here. Instead,
|
||||
// this information could come from meta.
|
||||
|
||||
// GKernelImpl describes particular kernel implementation to the system
|
||||
struct GAPI_EXPORTS GKernelImpl
|
||||
{
|
||||
util::any opaque; // backend-specific opaque info
|
||||
GKernel::M outMeta; // for deserialized graphs, the outMeta is taken here
|
||||
};
|
||||
|
||||
template<typename, typename> class GKernelTypeM;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// yield() is used in graph construction time as a generic method to obtain
|
||||
// lazy "return value" of G-API operations
|
||||
//
|
||||
template<typename T> struct Yield;
|
||||
template<> struct Yield<cv::GMat>
|
||||
{
|
||||
static inline cv::GMat yield(cv::GCall &call, int i) { return call.yield(i); }
|
||||
};
|
||||
template<> struct Yield<cv::GMatP>
|
||||
{
|
||||
static inline cv::GMatP yield(cv::GCall &call, int i) { return call.yieldP(i); }
|
||||
};
|
||||
template<> struct Yield<cv::GScalar>
|
||||
{
|
||||
static inline cv::GScalar yield(cv::GCall &call, int i) { return call.yieldScalar(i); }
|
||||
};
|
||||
template<typename U> struct Yield<cv::GArray<U> >
|
||||
{
|
||||
static inline cv::GArray<U> yield(cv::GCall &call, int i) { return call.yieldArray<U>(i); }
|
||||
};
|
||||
template<typename U> struct Yield<cv::GOpaque<U> >
|
||||
{
|
||||
static inline cv::GOpaque<U> yield(cv::GCall &call, int i) { return call.yieldOpaque<U>(i); }
|
||||
};
|
||||
template<> struct Yield<GFrame>
|
||||
{
|
||||
static inline cv::GFrame yield(cv::GCall &call, int i) { return call.yieldFrame(i); }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Helper classes which brings outputMeta() marshalling to kernel
|
||||
// implementations
|
||||
//
|
||||
// 1. MetaType establishes G#Type -> G#Meta mapping between G-API dynamic
|
||||
// types and its metadata descriptor types.
|
||||
// This mapping is used to transform types to call outMeta() callback.
|
||||
template<typename T> struct MetaType;
|
||||
template<> struct MetaType<cv::GMat> { using type = GMatDesc; };
|
||||
template<> struct MetaType<cv::GMatP> { using type = GMatDesc; };
|
||||
template<> struct MetaType<cv::GFrame> { using type = GFrameDesc; };
|
||||
template<> struct MetaType<cv::GScalar> { using type = GScalarDesc; };
|
||||
template<typename U> struct MetaType<cv::GArray<U> > { using type = GArrayDesc; };
|
||||
template<typename U> struct MetaType<cv::GOpaque<U> > { using type = GOpaqueDesc; };
|
||||
template<typename T> struct MetaType { using type = T; }; // opaque args passed as-is
|
||||
// FIXME: Move it to type traits?
|
||||
|
||||
// 2. Hacky test based on MetaType to check if we operate on G-* type or not
|
||||
template<typename T> using is_nongapi_type = std::is_same<T, typename MetaType<T>::type>;
|
||||
|
||||
// 3. Two ways to transform input arguments to its meta - for G-* and non-G* types:
|
||||
template<typename T>
|
||||
typename std::enable_if<!is_nongapi_type<T>::value, typename MetaType<T>::type>
|
||||
::type get_in_meta(const GMetaArgs &in_meta, const GArgs &, int idx)
|
||||
{
|
||||
return util::get<typename MetaType<T>::type>(in_meta.at(idx));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<is_nongapi_type<T>::value, T>
|
||||
::type get_in_meta(const GMetaArgs &, const GArgs &in_args, int idx)
|
||||
{
|
||||
return in_args.at(idx).template get<T>();
|
||||
}
|
||||
|
||||
// 4. The MetaHelper itself: an entity which generates outMeta() call
|
||||
// based on kernel signature, with arguments properly substituted.
|
||||
// 4.1 - case for multiple return values
|
||||
// FIXME: probably can be simplified with std::apply or analogue.
|
||||
template<typename, typename, typename>
|
||||
struct MetaHelper;
|
||||
|
||||
template<typename K, typename... Ins, typename... Outs>
|
||||
struct MetaHelper<K, std::tuple<Ins...>, std::tuple<Outs...> >
|
||||
{
|
||||
template<int... IIs, int... OIs>
|
||||
static GMetaArgs getOutMeta_impl(const GMetaArgs &in_meta,
|
||||
const GArgs &in_args,
|
||||
detail::Seq<IIs...>,
|
||||
detail::Seq<OIs...>)
|
||||
{
|
||||
// FIXME: decay?
|
||||
using R = std::tuple<typename MetaType<Outs>::type...>;
|
||||
const R r = K::outMeta( get_in_meta<Ins>(in_meta, in_args, IIs)... );
|
||||
return GMetaArgs{ GMetaArg(std::get<OIs>(r))... };
|
||||
}
|
||||
// FIXME: help users identify how outMeta must look like (via default impl w/static_assert?)
|
||||
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &in_meta,
|
||||
const GArgs &in_args)
|
||||
{
|
||||
return getOutMeta_impl(in_meta,
|
||||
in_args,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type(),
|
||||
typename detail::MkSeq<sizeof...(Outs)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
// 4.1 - case for a single return value
|
||||
// FIXME: How to avoid duplication here?
|
||||
template<typename K, typename... Ins, typename Out>
|
||||
struct MetaHelper<K, std::tuple<Ins...>, Out >
|
||||
{
|
||||
template<int... IIs>
|
||||
static GMetaArgs getOutMeta_impl(const GMetaArgs &in_meta,
|
||||
const GArgs &in_args,
|
||||
detail::Seq<IIs...>)
|
||||
{
|
||||
// FIXME: decay?
|
||||
using R = typename MetaType<Out>::type;
|
||||
const R r = K::outMeta( get_in_meta<Ins>(in_meta, in_args, IIs)... );
|
||||
return GMetaArgs{ GMetaArg(r) };
|
||||
}
|
||||
// FIXME: help users identify how outMeta must look like (via default impl w/static_assert?)
|
||||
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &in_meta,
|
||||
const GArgs &in_args)
|
||||
{
|
||||
return getOutMeta_impl(in_meta,
|
||||
in_args,
|
||||
typename detail::MkSeq<sizeof...(Ins)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Helper class to introduce tags to calls. By default there's no tag
|
||||
struct NoTag {
|
||||
static constexpr const char *tag() { return ""; }
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// GKernelType and GKernelTypeM are base classes which implement typed ::on()
|
||||
// method based on kernel signature. GKernelTypeM stands for multiple-return-value kernels
|
||||
//
|
||||
// G_TYPED_KERNEL and G_TYPED_KERNEL_M macros inherit user classes from GKernelType and
|
||||
// GKernelTypeM respectively.
|
||||
|
||||
template<typename K, typename... R, typename... Args>
|
||||
class GKernelTypeM<K, std::function<std::tuple<R...>(Args...)> >
|
||||
: public detail::MetaHelper<K, std::tuple<Args...>, std::tuple<R...>>
|
||||
, public detail::NoTag
|
||||
{
|
||||
template<int... IIs>
|
||||
static std::tuple<R...> yield(cv::GCall &call, detail::Seq<IIs...>)
|
||||
{
|
||||
return std::make_tuple(detail::Yield<R>::yield(call, IIs)...);
|
||||
}
|
||||
|
||||
public:
|
||||
using InArgs = std::tuple<Args...>;
|
||||
using OutArgs = std::tuple<R...>;
|
||||
|
||||
// TODO: Args&&... here?
|
||||
static std::tuple<R...> on(Args... args)
|
||||
{
|
||||
cv::GCall call(GKernel{ K::id()
|
||||
, K::tag()
|
||||
, &K::getOutMeta
|
||||
, {detail::GTypeTraits<R>::shape...}
|
||||
, {detail::GTypeTraits<Args>::op_kind...}
|
||||
, {detail::GObtainCtor<R>::get()...}
|
||||
, {detail::GTypeTraits<R>::op_kind...}});
|
||||
call.pass(args...); // TODO: std::forward() here?
|
||||
return yield(call, typename detail::MkSeq<sizeof...(R)>::type());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename, typename> class GKernelType;
|
||||
|
||||
template<typename K, typename R, typename... Args>
|
||||
class GKernelType<K, std::function<R(Args...)> >
|
||||
: public detail::MetaHelper<K, std::tuple<Args...>, R>
|
||||
, public detail::NoTag
|
||||
{
|
||||
public:
|
||||
using InArgs = std::tuple<Args...>;
|
||||
using OutArgs = std::tuple<R>;
|
||||
|
||||
static R on(Args... args)
|
||||
{
|
||||
cv::GCall call(GKernel{ K::id()
|
||||
, K::tag()
|
||||
, &K::getOutMeta
|
||||
, {detail::GTypeTraits<R>::shape}
|
||||
, {detail::GTypeTraits<Args>::op_kind...}
|
||||
, {detail::GObtainCtor<R>::get()}
|
||||
, {detail::GTypeTraits<R>::op_kind}});
|
||||
call.pass(args...);
|
||||
return detail::Yield<R>::yield(call, 0);
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
// This tiny class eliminates the semantic difference between
|
||||
// GKernelType and GKernelTypeM.
|
||||
template<typename, typename> class KernelTypeMedium;
|
||||
|
||||
template<typename K, typename... R, typename... Args>
|
||||
class KernelTypeMedium<K, std::function<std::tuple<R...>(Args...)>> :
|
||||
public cv::GKernelTypeM<K, std::function<std::tuple<R...>(Args...)>> {};
|
||||
|
||||
template<typename K, typename R, typename... Args>
|
||||
class KernelTypeMedium<K, std::function<R(Args...)>> :
|
||||
public cv::GKernelType<K, std::function<R(Args...)>> {};
|
||||
} // namespace detail
|
||||
|
||||
} // namespace cv
|
||||
|
||||
|
||||
// FIXME: I don't know a better way so far. Feel free to suggest one
|
||||
// The problem is that every typed kernel should have ::id() but body
|
||||
// of the class is defined by user (with outMeta, other stuff)
|
||||
|
||||
//! @cond IGNORED
|
||||
#define G_ID_HELPER_CLASS(Class) Class##IdHelper
|
||||
|
||||
#define G_ID_HELPER_BODY(Class, Id) \
|
||||
struct G_ID_HELPER_CLASS(Class) \
|
||||
{ \
|
||||
static constexpr const char * id() {return Id;} \
|
||||
}; \
|
||||
//! @endcond
|
||||
|
||||
#define GET_G_TYPED_KERNEL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, NAME, ...) NAME
|
||||
#define COMBINE_SIGNATURE(...) __VA_ARGS__
|
||||
// Ensure correct __VA_ARGS__ expansion on Windows
|
||||
#define __WRAP_VAARGS(x) x
|
||||
|
||||
/**
|
||||
* Helper for G_TYPED_KERNEL declares a new G-API Operation. See [Kernel API](@ref gapi_kernel_api)
|
||||
* for more details.
|
||||
*
|
||||
* @param Class type name for this operation.
|
||||
* @param API an `std::function<>`-like signature for the operation;
|
||||
* return type is a single value or a tuple of multiple values.
|
||||
* @param Id string identifier for the operation. Must be unique.
|
||||
*/
|
||||
#define G_TYPED_KERNEL_HELPER(Class, API, Id) \
|
||||
G_ID_HELPER_BODY(Class, Id) \
|
||||
struct Class final: public cv::detail::KernelTypeMedium<Class, std::function API >, \
|
||||
public G_ID_HELPER_CLASS(Class)
|
||||
// {body} is to be defined by user
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_2(Class, _1, _2, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_3(Class, _1, _2, _3, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_4(Class, _1, _2, _3, _4, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_5(Class, _1, _2, _3, _4, _5, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_6(Class, _1, _2, _3, _4, _5, _6, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5, _6), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_7(Class, _1, _2, _3, _4, _5, _6, _7, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5, _6, _7), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_8(Class, _1, _2, _3, _4, _5, _6, _7, _8, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5, _6, _7, _8), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_9(Class, _1, _2, _3, _4, _5, _6, _7, _8, _9, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5, _6, _7, _8, _9), Id)
|
||||
|
||||
#define G_TYPED_KERNEL_HELPER_10(Class, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, Id) \
|
||||
G_TYPED_KERNEL_HELPER(Class, COMBINE_SIGNATURE(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10), Id)
|
||||
|
||||
/**
|
||||
* Declares a new G-API Operation. See [Kernel API](@ref gapi_kernel_api)
|
||||
* for more details.
|
||||
*
|
||||
* @param Class type name for this operation.
|
||||
*/
|
||||
#define G_TYPED_KERNEL(Class, ...) __WRAP_VAARGS(GET_G_TYPED_KERNEL(__VA_ARGS__, \
|
||||
G_TYPED_KERNEL_HELPER_10, \
|
||||
G_TYPED_KERNEL_HELPER_9, \
|
||||
G_TYPED_KERNEL_HELPER_8, \
|
||||
G_TYPED_KERNEL_HELPER_7, \
|
||||
G_TYPED_KERNEL_HELPER_6, \
|
||||
G_TYPED_KERNEL_HELPER_5, \
|
||||
G_TYPED_KERNEL_HELPER_4, \
|
||||
G_TYPED_KERNEL_HELPER_3, \
|
||||
G_TYPED_KERNEL_HELPER_2, \
|
||||
G_TYPED_KERNEL_HELPER)(Class, __VA_ARGS__)) \
|
||||
|
||||
/**
|
||||
* Declares a new G-API Operation. See [Kernel API](@ref gapi_kernel_api) for more details.
|
||||
*
|
||||
* @deprecated This macro is deprecated in favor of `G_TYPED_KERNEL` that is used for declaring any
|
||||
* G-API Operation.
|
||||
*
|
||||
* @param Class type name for this operation.
|
||||
*/
|
||||
#define G_TYPED_KERNEL_M G_TYPED_KERNEL
|
||||
|
||||
#define G_API_OP G_TYPED_KERNEL
|
||||
#define G_API_OP_M G_API_OP
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace gapi
|
||||
{
|
||||
// Prework: model "Device" API before it gets to G-API headers.
|
||||
// FIXME: Don't mix with internal Backends class!
|
||||
/// @private
|
||||
class GAPI_EXPORTS GBackend
|
||||
{
|
||||
public:
|
||||
class Priv;
|
||||
|
||||
// TODO: make it template (call `new` within??)
|
||||
GBackend();
|
||||
explicit GBackend(std::shared_ptr<Priv> &&p);
|
||||
|
||||
Priv& priv();
|
||||
const Priv& priv() const;
|
||||
std::size_t hash() const;
|
||||
|
||||
bool operator== (const GBackend &rhs) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
|
||||
inline bool operator != (const GBackend &lhs, const GBackend &rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash<cv::gapi::GBackend>
|
||||
{
|
||||
std::size_t operator() (const cv::gapi::GBackend &b) const
|
||||
{
|
||||
return b.hash();
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
namespace cv {
|
||||
class GAPI_EXPORTS_W_SIMPLE GKernelPackage;
|
||||
|
||||
namespace gapi {
|
||||
GAPI_EXPORTS_W cv::GKernelPackage combine(const cv::GKernelPackage &lhs,
|
||||
const cv::GKernelPackage &rhs);
|
||||
|
||||
/// @private
|
||||
class GFunctor
|
||||
{
|
||||
public:
|
||||
virtual cv::GKernelImpl impl() const = 0;
|
||||
virtual cv::gapi::GBackend backend() const = 0;
|
||||
const char* id() const { return m_id; }
|
||||
|
||||
virtual ~GFunctor() = default;
|
||||
protected:
|
||||
GFunctor(const char* id) : m_id(id) { };
|
||||
private:
|
||||
const char* m_id;
|
||||
};
|
||||
} // namespace gapi
|
||||
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*/
|
||||
|
||||
// FIXME: Hide implementation
|
||||
/**
|
||||
* @brief A container class for heterogeneous kernel
|
||||
* implementation collections and graph transformations.
|
||||
*
|
||||
* GKernelPackage is a special container class which stores kernel
|
||||
* _implementations_ and graph _transformations_. Objects of this class
|
||||
* are created and passed to cv::GComputation::compile() to specify
|
||||
* which kernels to use and which transformations to apply in the
|
||||
* compiled graph. GKernelPackage may contain kernels of
|
||||
* different backends, e.g. be heterogeneous.
|
||||
*
|
||||
* The most easy way to create a kernel package is to use function
|
||||
* cv::gapi::kernels(). This template functions takes kernel
|
||||
* implementations in form of type list (variadic template) and
|
||||
* generates a kernel package atop of that.
|
||||
*
|
||||
* Kernel packages can be also generated programmatically, starting
|
||||
* with an empty package (created with the default constructor)
|
||||
* and then by populating it with kernels via call to
|
||||
* GKernelPackage::include(). Note this method is also a template
|
||||
* one since G-API kernel and transformation implementations are _types_,
|
||||
* not objects.
|
||||
*
|
||||
* Finally, two kernel packages can be combined into a new one
|
||||
* with function cv::gapi::combine().
|
||||
*/
|
||||
class GAPI_EXPORTS_W_SIMPLE GKernelPackage
|
||||
{
|
||||
|
||||
/// @private
|
||||
using M = std::unordered_map<std::string, std::pair<cv::gapi::GBackend, cv::GKernelImpl>>;
|
||||
|
||||
/// @private
|
||||
M m_id_kernels;
|
||||
|
||||
/// @private
|
||||
std::vector<GTransform> m_transformations;
|
||||
|
||||
protected:
|
||||
/// @private
|
||||
// Remove ALL implementations of the given API (identified by ID)
|
||||
void removeAPI(const std::string &id);
|
||||
|
||||
/// @private
|
||||
// Partial include() specialization for kernels
|
||||
template <typename KImpl>
|
||||
typename std::enable_if<(std::is_base_of<cv::detail::KernelTag, KImpl>::value), void>::type
|
||||
includeHelper()
|
||||
{
|
||||
auto backend = KImpl::backend();
|
||||
auto kernel_id = KImpl::API::id();
|
||||
auto kernel_impl = GKernelImpl{KImpl::kernel(), &KImpl::API::getOutMeta};
|
||||
removeAPI(kernel_id);
|
||||
|
||||
m_id_kernels[kernel_id] = std::make_pair(backend, kernel_impl);
|
||||
}
|
||||
|
||||
/// @private
|
||||
// Partial include() specialization for transformations
|
||||
template <typename TImpl>
|
||||
typename std::enable_if<(std::is_base_of<cv::detail::TransformTag, TImpl>::value), void>::type
|
||||
includeHelper()
|
||||
{
|
||||
m_transformations.emplace_back(TImpl::transformation());
|
||||
}
|
||||
|
||||
public:
|
||||
void include(const cv::gapi::GFunctor& functor);
|
||||
|
||||
/**
|
||||
* @brief Returns total number of kernels
|
||||
* in the package (across all backends included)
|
||||
*
|
||||
* @return a number of kernels in the package
|
||||
*/
|
||||
GAPI_WRAP std::size_t size() const;
|
||||
|
||||
/**
|
||||
* @brief Returns vector of transformations included in the package
|
||||
*
|
||||
* @return vector of transformations included in the package
|
||||
*/
|
||||
const std::vector<GTransform>& get_transformations() const;
|
||||
|
||||
/**
|
||||
* @brief Returns vector of kernel ids included in the package
|
||||
*
|
||||
* @return vector of kernel ids included in the package
|
||||
*/
|
||||
std::vector<std::string> get_kernel_ids() const;
|
||||
|
||||
/**
|
||||
* @brief Test if a particular kernel _implementation_ KImpl is
|
||||
* included in this kernel package.
|
||||
*
|
||||
* @sa includesAPI()
|
||||
*
|
||||
* @note cannot be applied to transformations
|
||||
*
|
||||
* @return true if there is such kernel, false otherwise.
|
||||
*/
|
||||
template<typename KImpl>
|
||||
bool includes() const
|
||||
{
|
||||
static_assert(std::is_base_of<cv::detail::KernelTag, KImpl>::value,
|
||||
"includes() can be applied to kernels only");
|
||||
|
||||
auto kernel_it = m_id_kernels.find(KImpl::API::id());
|
||||
return kernel_it != m_id_kernels.end() &&
|
||||
kernel_it->second.first == KImpl::backend();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Remove all kernels associated with the given backend
|
||||
* from the package.
|
||||
*
|
||||
* Does nothing if there's no kernels of this backend in the package.
|
||||
*
|
||||
* @param backend backend which kernels to remove
|
||||
*/
|
||||
void remove(const cv::gapi::GBackend& backend);
|
||||
|
||||
/**
|
||||
* @brief Remove all kernels implementing the given API from
|
||||
* the package.
|
||||
*
|
||||
* Does nothing if there's no kernels implementing the given interface.
|
||||
*/
|
||||
template<typename KAPI>
|
||||
void remove()
|
||||
{
|
||||
removeAPI(KAPI::id());
|
||||
}
|
||||
|
||||
// FIXME: Rename to includes() and distinguish API/impl case by
|
||||
// statically?
|
||||
/**
|
||||
* Check if package contains ANY implementation of a kernel API
|
||||
* by API type.
|
||||
*/
|
||||
template<typename KAPI>
|
||||
bool includesAPI() const
|
||||
{
|
||||
return includesAPI(KAPI::id());
|
||||
}
|
||||
|
||||
/// @private
|
||||
bool includesAPI(const std::string &id) const;
|
||||
|
||||
// FIXME: The below comment is wrong, and who needs this function?
|
||||
/**
|
||||
* @brief Find a kernel (by its API)
|
||||
*
|
||||
* Returns implementation corresponding id.
|
||||
* Throws if nothing found.
|
||||
*
|
||||
* @return Backend which hosts matching kernel implementation.
|
||||
*
|
||||
*/
|
||||
template<typename KAPI>
|
||||
cv::gapi::GBackend lookup() const
|
||||
{
|
||||
return lookup(KAPI::id()).first;
|
||||
}
|
||||
|
||||
/// @private
|
||||
std::pair<cv::gapi::GBackend, cv::GKernelImpl>
|
||||
lookup(const std::string &id) const;
|
||||
|
||||
// FIXME: No overwrites allowed?
|
||||
/**
|
||||
* @brief Put a new kernel implementation or a new transformation
|
||||
* KImpl into the package.
|
||||
*/
|
||||
template<typename KImpl>
|
||||
void include()
|
||||
{
|
||||
includeHelper<KImpl>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds a new kernel based on it's backend and id into the kernel package
|
||||
*
|
||||
* @param backend backend associated with the kernel
|
||||
* @param kernel_id a name/id of the kernel
|
||||
*/
|
||||
void include(const cv::gapi::GBackend& backend, const std::string& kernel_id);
|
||||
|
||||
/**
|
||||
* @brief Lists all backends which are included into package
|
||||
*
|
||||
* @return vector of backends
|
||||
*/
|
||||
std::vector<cv::gapi::GBackend> backends() const;
|
||||
|
||||
// TODO: Doxygen bug -- it wants me to place this comment
|
||||
// here, not below.
|
||||
/**
|
||||
* @brief Create a new package based on `lhs` and `rhs`.
|
||||
*
|
||||
* @param lhs "Left-hand-side" package in the process
|
||||
* @param rhs "Right-hand-side" package in the process
|
||||
* @return a new kernel package.
|
||||
*/
|
||||
friend GAPI_EXPORTS GKernelPackage cv::gapi::combine(const GKernelPackage &lhs,
|
||||
const GKernelPackage &rhs);
|
||||
};
|
||||
/** @} */
|
||||
|
||||
namespace gapi {
|
||||
using GKernelPackage = cv::GKernelPackage; // Keep backward compatibility
|
||||
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Create a kernel package object containing kernels
|
||||
* and transformations specified in variadic template argument.
|
||||
*
|
||||
* In G-API, kernel implementations and transformations are _types_.
|
||||
* Every backend has its own kernel API (like GAPI_OCV_KERNEL() and
|
||||
* GAPI_FLUID_KERNEL()) but all of that APIs define a new type for
|
||||
* each kernel implementation.
|
||||
*
|
||||
* Use this function to pass kernel implementations (defined in
|
||||
* either way) and transformations to the system. Example:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp kernels_snippet
|
||||
*
|
||||
* Note that kernels() itself is a function returning object, not
|
||||
* a type, so having `()` at the end is important -- it must be a
|
||||
* function call.
|
||||
*/
|
||||
template<typename... KK> GKernelPackage kernels()
|
||||
{
|
||||
// FIXME: currently there is no check that transformations' signatures are unique
|
||||
// and won't be any intersection in graph compilation stage
|
||||
static_assert(cv::detail::all_unique<typename KK::API...>::value, "Kernels API must be unique");
|
||||
|
||||
GKernelPackage pkg;
|
||||
|
||||
// For those who wonder - below is a trick to call a number of
|
||||
// methods based on parameter pack (zeroes just help hiding these
|
||||
// calls into a sequence which helps to expand this parameter pack).
|
||||
// Just note that `f(),a` always equals to `a` (with f() called!)
|
||||
// and parentheses are used to hide function call in the expanded sequence.
|
||||
// Leading 0 helps to handle case when KK is an empty list (kernels<>()).
|
||||
int unused[] = { 0, (pkg.include<KK>(), 0)... };
|
||||
cv::util::suppress_unused_warning(unused);
|
||||
return pkg;
|
||||
};
|
||||
|
||||
template<typename... FF>
|
||||
GKernelPackage kernels(FF&... functors)
|
||||
{
|
||||
GKernelPackage pkg;
|
||||
int unused[] = { 0, (pkg.include(functors), 0)... };
|
||||
cv::util::suppress_unused_warning(unused);
|
||||
return pkg;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Combines multiple G-API kernel packages into one
|
||||
*
|
||||
* @overload
|
||||
*
|
||||
* This function successively combines the passed kernel packages using a right fold.
|
||||
* Calling `combine(a, b, c)` is equal to `combine(a, combine(b, c))`.
|
||||
*
|
||||
* @return The resulting kernel package
|
||||
*/
|
||||
template<typename... Ps>
|
||||
cv::GKernelPackage combine(const cv::GKernelPackage &a, const cv::GKernelPackage &b, Ps&&... rest)
|
||||
{
|
||||
return combine(a, combine(b, rest...));
|
||||
}
|
||||
// NB(DM): Variadic-arg version in Python may require the same
|
||||
// approach as used in GComputation::compile/apply.
|
||||
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief cv::gapi::use_only() is a special combinator which hints G-API to use only
|
||||
* kernels specified in cv::GComputation::compile() (and not to extend kernels available by
|
||||
* default with that package).
|
||||
*/
|
||||
struct GAPI_EXPORTS use_only
|
||||
{
|
||||
GKernelPackage pkg;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
} // namespace gapi
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<> struct CompileArgTag<cv::GKernelPackage>
|
||||
{
|
||||
static const char* tag() { return "gapi.kernel_package"; }
|
||||
};
|
||||
|
||||
template<> struct CompileArgTag<cv::gapi::use_only>
|
||||
{
|
||||
static const char* tag() { return "gapi.use_only"; }
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GKERNEL_HPP
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GMAT_HPP
|
||||
#define OPENCV_GAPI_GMAT_HPP
|
||||
|
||||
#include <ostream>
|
||||
#include <memory> // std::shared_ptr
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp> // GShape
|
||||
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
|
||||
// TODO GAPI_EXPORTS or so
|
||||
namespace cv
|
||||
{
|
||||
// Forward declaration; GNode and GOrigin are an internal
|
||||
// (user-inaccessible) classes.
|
||||
class GNode;
|
||||
struct GOrigin;
|
||||
|
||||
/** \addtogroup gapi_data_objects
|
||||
* @{
|
||||
*
|
||||
* @brief G-API data objects used to build G-API expressions.
|
||||
*
|
||||
* These objects do not own any particular data (except compile-time
|
||||
* associated values like with cv::GScalar or `cv::GArray<T>`) and are
|
||||
* used only to construct graphs.
|
||||
*
|
||||
* Every graph in G-API starts and ends with data objects.
|
||||
*
|
||||
* Once constructed and compiled, G-API operates with regular host-side
|
||||
* data instead. Refer to the below table to find the mapping between
|
||||
* G-API and regular data types when passing input and output data
|
||||
* structures to G-API:
|
||||
*
|
||||
* G-API data type | I/O data type
|
||||
* ------------------ | -------------
|
||||
* cv::GMat | cv::Mat, cv::UMat, cv::RMat
|
||||
* cv::GScalar | cv::Scalar
|
||||
* `cv::GArray<T>` | std::vector<T>
|
||||
* `cv::GOpaque<T>` | T
|
||||
* cv::GFrame | cv::MediaFrame
|
||||
*/
|
||||
/**
|
||||
* @brief GMat class represents image or tensor data in the
|
||||
* graph.
|
||||
*
|
||||
* GMat doesn't store any data itself, instead it describes a
|
||||
* functional relationship between operations consuming and producing
|
||||
* GMat objects.
|
||||
*
|
||||
* GMat is a virtual counterpart of Mat and UMat, but it
|
||||
* doesn't mean G-API use Mat or UMat objects internally to represent
|
||||
* GMat objects -- the internal data representation may be
|
||||
* backend-specific or optimized out at all.
|
||||
*
|
||||
* @sa Mat, GMatDesc
|
||||
*/
|
||||
class GAPI_EXPORTS_W_SIMPLE GMat
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs an empty GMat
|
||||
*
|
||||
* Normally, empty G-API data objects denote a starting point of
|
||||
* the graph. When an empty GMat is assigned to a result of some
|
||||
* operation, it obtains a functional link to this operation (and
|
||||
* is not empty anymore).
|
||||
*/
|
||||
GAPI_WRAP GMat(); // Empty constructor
|
||||
|
||||
/// @private
|
||||
GMat(const GNode &n, std::size_t out); // Operation result constructor
|
||||
/// @private
|
||||
GOrigin& priv(); // Internal use only
|
||||
/// @private
|
||||
const GOrigin& priv() const; // Internal use only
|
||||
|
||||
private:
|
||||
std::shared_ptr<GOrigin> m_priv;
|
||||
};
|
||||
|
||||
class GAPI_EXPORTS GMatP : public GMat
|
||||
{
|
||||
public:
|
||||
using GMat::GMat;
|
||||
};
|
||||
|
||||
class RMat;
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_meta_args
|
||||
* @{
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE GMatDesc
|
||||
{
|
||||
// FIXME: Default initializers in C++14
|
||||
GAPI_PROP int depth;
|
||||
GAPI_PROP int chan;
|
||||
GAPI_PROP cv::Size size; // NB.: no multi-dimensional cases covered yet
|
||||
GAPI_PROP bool planar;
|
||||
GAPI_PROP std::vector<int> dims; // FIXME: Maybe it's real questionable to have it here
|
||||
|
||||
GAPI_WRAP GMatDesc(int d, int c, cv::Size s, bool p = false)
|
||||
: depth(d), chan(c), size(s), planar(p) {}
|
||||
|
||||
GAPI_WRAP GMatDesc(int d, const std::vector<int> &dd)
|
||||
: depth(d), chan(-1), size{-1,-1}, planar(false), dims(dd) {}
|
||||
|
||||
GAPI_WRAP GMatDesc(int d, std::vector<int> &&dd)
|
||||
: depth(d), chan(-1), size{-1,-1}, planar(false), dims(std::move(dd)) {}
|
||||
|
||||
GAPI_WRAP GMatDesc() : GMatDesc(-1, -1, {-1,-1}) {}
|
||||
|
||||
inline bool operator== (const GMatDesc &rhs) const
|
||||
{
|
||||
return depth == rhs.depth
|
||||
&& chan == rhs.chan
|
||||
&& size == rhs.size
|
||||
&& planar == rhs.planar
|
||||
&& dims == rhs.dims;
|
||||
}
|
||||
|
||||
inline bool operator!= (const GMatDesc &rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
bool isND() const { return !dims.empty(); }
|
||||
|
||||
// Checks if the passed mat can be described by this descriptor
|
||||
// (it handles the case when
|
||||
// 1-channel mat can be reinterpreted as is (1-channel mat)
|
||||
// and as a 3-channel planar mat with height divided by 3)
|
||||
bool canDescribe(const cv::Mat& mat) const;
|
||||
|
||||
bool canDescribe(const cv::RMat& mat) const;
|
||||
|
||||
// Meta combinator: return a new GMatDesc which differs in size by delta
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
// FIXME: a better name?
|
||||
GAPI_WRAP GMatDesc withSizeDelta(cv::Size delta) const
|
||||
{
|
||||
GMatDesc desc(*this);
|
||||
desc.size += delta;
|
||||
return desc;
|
||||
}
|
||||
// Meta combinator: return a new GMatDesc which differs in size by delta
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
//
|
||||
// This is an overload.
|
||||
GAPI_WRAP GMatDesc withSizeDelta(int dx, int dy) const
|
||||
{
|
||||
return withSizeDelta(cv::Size{dx,dy});
|
||||
}
|
||||
|
||||
GAPI_WRAP GMatDesc withSize(cv::Size sz) const
|
||||
{
|
||||
GMatDesc desc(*this);
|
||||
desc.size = sz;
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Meta combinator: return a new GMatDesc with specified data depth.
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
GAPI_WRAP GMatDesc withDepth(int ddepth) const
|
||||
{
|
||||
GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
|
||||
GMatDesc desc(*this);
|
||||
if (ddepth != -1) desc.depth = ddepth;
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Meta combinator: return a new GMatDesc with specified data depth
|
||||
// and number of channels.
|
||||
// (all other fields are taken unchanged from this GMatDesc)
|
||||
GAPI_WRAP GMatDesc withType(int ddepth, int dchan) const
|
||||
{
|
||||
GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
|
||||
GMatDesc desc = withDepth(ddepth);
|
||||
desc.chan = dchan;
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Meta combinator: return a new GMatDesc with planar flag set
|
||||
// (no size changes are performed, only channel interpretation is changed
|
||||
// (interleaved -> planar)
|
||||
GAPI_WRAP GMatDesc asPlanar() const
|
||||
{
|
||||
GAPI_Assert(planar == false);
|
||||
GMatDesc desc(*this);
|
||||
desc.planar = true;
|
||||
return desc;
|
||||
}
|
||||
|
||||
// Meta combinator: return a new GMatDesc
|
||||
// reinterpreting 1-channel input as planar image
|
||||
// (size height is divided by plane number)
|
||||
GAPI_WRAP GMatDesc asPlanar(int planes) const
|
||||
{
|
||||
GAPI_Assert(planar == false);
|
||||
GAPI_Assert(chan == 1);
|
||||
GAPI_Assert(planes > 1);
|
||||
GAPI_Assert(size.height % planes == 0);
|
||||
GMatDesc desc(*this);
|
||||
desc.size.height /= planes;
|
||||
desc.chan = planes;
|
||||
return desc.asPlanar();
|
||||
}
|
||||
|
||||
// Meta combinator: return a new GMatDesc with planar flag set to false
|
||||
// (no size changes are performed, only channel interpretation is changed
|
||||
// (planar -> interleaved)
|
||||
GAPI_WRAP GMatDesc asInterleaved() const
|
||||
{
|
||||
GAPI_Assert(planar == true);
|
||||
GMatDesc desc(*this);
|
||||
desc.planar = false;
|
||||
return desc;
|
||||
}
|
||||
};
|
||||
|
||||
static inline GMatDesc empty_gmat_desc() { return GMatDesc{-1,-1,{-1,-1}}; }
|
||||
|
||||
namespace gapi { namespace detail {
|
||||
/** Checks GMatDesc fields if the passed matrix is a set of n-dimentional points.
|
||||
@param in GMatDesc to check.
|
||||
@param n expected dimensionality.
|
||||
@return the amount of points. In case input matrix can't be described as vector of points
|
||||
of expected dimensionality, returns -1.
|
||||
*/
|
||||
int checkVector(const GMatDesc& in, const size_t n);
|
||||
|
||||
/** @overload
|
||||
|
||||
Checks GMatDesc fields if the passed matrix can be described as a set of points of any
|
||||
dimensionality.
|
||||
|
||||
@return array of two elements in form of std::vector<int>: the amount of points
|
||||
and their calculated dimensionality. In case input matrix can't be described as vector of points,
|
||||
returns {-1, -1}.
|
||||
*/
|
||||
std::vector<int> checkVector(const GMatDesc& in);
|
||||
}} // namespace gapi::detail
|
||||
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
GAPI_EXPORTS GMatDesc descr_of(const cv::UMat &mat);
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
|
||||
//Fwd declarations
|
||||
namespace gapi { namespace own {
|
||||
class Mat;
|
||||
GAPI_EXPORTS GMatDesc descr_of(const Mat &mat);
|
||||
}}//gapi::own
|
||||
|
||||
GAPI_EXPORTS GMatDesc descr_of(const RMat &mat);
|
||||
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
GAPI_EXPORTS GMatDesc descr_of(const cv::Mat &mat);
|
||||
#else
|
||||
using gapi::own::descr_of;
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
|
||||
GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc);
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GMAT_HPP
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GMETAARG_HPP
|
||||
#define OPENCV_GAPI_GMETAARG_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
#include <opencv2/gapi/util/variant.hpp>
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/gscalar.hpp>
|
||||
#include <opencv2/gapi/garray.hpp>
|
||||
#include <opencv2/gapi/gopaque.hpp>
|
||||
#include <opencv2/gapi/gframe.hpp>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
// FIXME: Rename to GMeta?
|
||||
// FIXME: user shouldn't deal with it - put to detail?
|
||||
// GMetaArg is an union type over descriptions of G-types which can serve as
|
||||
// GComputation's in/output slots.
|
||||
//
|
||||
// GMetaArg objects are passed as arguments to GComputation::compile()
|
||||
// to specify which data a compiled computation should be specialized on.
|
||||
// For manual compile(), user must supply this metadata, in case of apply()
|
||||
// this metadata is taken from arguments computation should operate on.
|
||||
//
|
||||
// The first type (monostate) is equal to "uninitialized"/"unresolved" meta.
|
||||
using GMetaArg = util::variant
|
||||
< util::monostate
|
||||
, GMatDesc
|
||||
, GScalarDesc
|
||||
, GArrayDesc
|
||||
, GOpaqueDesc
|
||||
, GFrameDesc
|
||||
>;
|
||||
GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const GMetaArg &);
|
||||
|
||||
using GMetaArgs = std::vector<GMetaArg>;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// These traits are used by GComputation::compile()
|
||||
|
||||
// FIXME: is_constructible<T> doesn't work as variant doesn't do any SFINAE
|
||||
// in its current template constructor
|
||||
|
||||
template<typename T> struct is_meta_descr : std::false_type {};
|
||||
template<> struct is_meta_descr<GMatDesc> : std::true_type {};
|
||||
template<> struct is_meta_descr<GScalarDesc> : std::true_type {};
|
||||
template<> struct is_meta_descr<GArrayDesc> : std::true_type {};
|
||||
template<> struct is_meta_descr<GOpaqueDesc> : std::true_type {};
|
||||
|
||||
template<typename... Ts>
|
||||
using are_meta_descrs = all_satisfy<is_meta_descr, Ts...>;
|
||||
|
||||
template<typename... Ts>
|
||||
using are_meta_descrs_but_last = all_satisfy<is_meta_descr, typename all_but_last<Ts...>::type>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Note: descr_of(std::vector<..>) returns a GArrayDesc, while
|
||||
// descrs_of(std::vector<..>) returns an array of Meta args!
|
||||
class UMat;
|
||||
GAPI_EXPORTS cv::GMetaArgs descrs_of(const std::vector<cv::Mat> &vec);
|
||||
GAPI_EXPORTS cv::GMetaArgs descrs_of(const std::vector<cv::UMat> &vec);
|
||||
namespace gapi { namespace own {
|
||||
GAPI_EXPORTS cv::GMetaArgs descrs_of(const std::vector<Mat> &vec);
|
||||
}} // namespace gapi::own
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GMETAARG_HPP
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GOPAQUE_HPP
|
||||
#define OPENCV_GAPI_GOPAQUE_HPP
|
||||
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
#include <memory>
|
||||
|
||||
#include <opencv2/gapi/own/exports.hpp>
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include <opencv2/gapi/util/variant.hpp>
|
||||
#include <opencv2/gapi/util/throw.hpp>
|
||||
#include <opencv2/gapi/util/type_traits.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
|
||||
#include <opencv2/gapi/gcommon.hpp> // OpaqueKind
|
||||
#include <opencv2/gapi/garray.hpp> // TypeHintBase
|
||||
|
||||
namespace cv
|
||||
{
|
||||
// Forward declaration; GNode and GOrigin are an internal
|
||||
// (user-inaccessible) classes.
|
||||
class GNode;
|
||||
struct GOrigin;
|
||||
template<typename T> class GOpaque;
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_meta_args
|
||||
* @{
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE GOpaqueDesc
|
||||
{
|
||||
// FIXME: Body
|
||||
// FIXME: Also implement proper operator== then
|
||||
bool operator== (const GOpaqueDesc&) const { return true; }
|
||||
};
|
||||
template<typename U> GOpaqueDesc descr_of(const U &) { return {};}
|
||||
GAPI_EXPORTS_W inline GOpaqueDesc empty_gopaque_desc() {return {}; }
|
||||
/** @} */
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const cv::GOpaqueDesc &desc);
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// ConstructOpaque is a callback which stores information about T and is used by
|
||||
// G-API runtime to construct an object in host memory (T remains opaque for G-API).
|
||||
// ConstructOpaque is carried into G-API internals by GOpaqueU.
|
||||
// Currently it is suitable for Host (CPU) plugins only, real offload may require
|
||||
// more information for manual memory allocation on-device.
|
||||
class OpaqueRef;
|
||||
using ConstructOpaque = std::function<void(OpaqueRef&)>;
|
||||
|
||||
// FIXME: garray.hpp already contains hint classes (for actual T type verification),
|
||||
// need to think where it can be moved (currently opaque uses it from garray)
|
||||
|
||||
// This class strips type information from GOpaque<T> and makes it usable
|
||||
// in the G-API graph compiler (expression unrolling, graph generation, etc).
|
||||
// Part of GProtoArg.
|
||||
class GAPI_EXPORTS GOpaqueU
|
||||
{
|
||||
public:
|
||||
GOpaqueU(const GNode &n, std::size_t out); // Operation result constructor
|
||||
|
||||
template <typename T>
|
||||
bool holds() const; // Check if was created from GOpaque<T>
|
||||
|
||||
GOrigin& priv(); // Internal use only
|
||||
const GOrigin& priv() const; // Internal use only
|
||||
|
||||
protected:
|
||||
GOpaqueU(); // Default constructor
|
||||
template<class> friend class cv::GOpaque; // (available for GOpaque<T> only)
|
||||
|
||||
void setConstructFcn(ConstructOpaque &&cv); // Store T-aware constructor
|
||||
|
||||
template <typename T>
|
||||
void specifyType(); // Store type of initial GOpaque<T>
|
||||
|
||||
template <typename T>
|
||||
void storeKind();
|
||||
|
||||
void setKind(cv::detail::OpaqueKind);
|
||||
|
||||
std::shared_ptr<GOrigin> m_priv;
|
||||
std::shared_ptr<TypeHintBase> m_hint;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool GOpaqueU::holds() const{
|
||||
GAPI_Assert(m_hint != nullptr);
|
||||
using U = util::decay_t<T>;
|
||||
return dynamic_cast<TypeHint<U>*>(m_hint.get()) != nullptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void GOpaqueU::specifyType(){
|
||||
m_hint.reset(new TypeHint<util::decay_t<T>>);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void GOpaqueU::storeKind(){
|
||||
// FIXME: Add assert here on cv::Mat and cv::Scalar?
|
||||
setKind(cv::detail::GOpaqueTraits<T>::kind);
|
||||
};
|
||||
|
||||
// This class represents a typed object reference.
|
||||
// Depending on origins, this reference may be either "just a" reference to
|
||||
// an object created externally, OR actually own the underlying object
|
||||
// (be value holder).
|
||||
class BasicOpaqueRef
|
||||
{
|
||||
public:
|
||||
cv::GOpaqueDesc m_desc;
|
||||
virtual ~BasicOpaqueRef() {}
|
||||
|
||||
virtual void mov(BasicOpaqueRef &ref) = 0;
|
||||
virtual const void* ptr() const = 0;
|
||||
virtual void set(const cv::util::any &a) = 0;
|
||||
};
|
||||
|
||||
template<typename T> class OpaqueRefT final: public BasicOpaqueRef
|
||||
{
|
||||
using empty_t = util::monostate;
|
||||
using ro_ext_t = const T *;
|
||||
using rw_ext_t = T *;
|
||||
using rw_own_t = T ;
|
||||
util::variant<empty_t, ro_ext_t, rw_ext_t, rw_own_t> m_ref;
|
||||
|
||||
inline bool isEmpty() const { return util::holds_alternative<empty_t>(m_ref); }
|
||||
inline bool isROExt() const { return util::holds_alternative<ro_ext_t>(m_ref); }
|
||||
inline bool isRWExt() const { return util::holds_alternative<rw_ext_t>(m_ref); }
|
||||
inline bool isRWOwn() const { return util::holds_alternative<rw_own_t>(m_ref); }
|
||||
|
||||
void init(const T* obj = nullptr)
|
||||
{
|
||||
if (obj) m_desc = cv::descr_of(*obj);
|
||||
}
|
||||
|
||||
public:
|
||||
OpaqueRefT() { init(); }
|
||||
virtual ~OpaqueRefT() {}
|
||||
|
||||
explicit OpaqueRefT(const T& obj) : m_ref(&obj) { init(&obj); }
|
||||
explicit OpaqueRefT( T& obj) : m_ref(&obj) { init(&obj); }
|
||||
explicit OpaqueRefT( T&& obj) : m_ref(std::move(obj)) { init(&obj); }
|
||||
|
||||
// Reset a OpaqueRefT. Called only for objects instantiated
|
||||
// internally in G-API (e.g. temporary GOpaque<T>'s within a
|
||||
// computation). Reset here means both initialization
|
||||
// (creating an object) and reset (discarding its existing
|
||||
// content before the next execution). Must never be called
|
||||
// for external OpaqueRefTs.
|
||||
void reset()
|
||||
{
|
||||
if (isEmpty())
|
||||
{
|
||||
T empty_obj{};
|
||||
m_desc = cv::descr_of(empty_obj);
|
||||
m_ref = std::move(empty_obj);
|
||||
GAPI_Assert(isRWOwn());
|
||||
}
|
||||
else if (isRWOwn())
|
||||
{
|
||||
util::get<rw_own_t>(m_ref) = {};
|
||||
}
|
||||
else GAPI_Error("InternalError"); // shouldn't be called in *EXT modes
|
||||
}
|
||||
|
||||
// Obtain a WRITE reference to underlying object
|
||||
// Used by CPU kernel API wrappers when a kernel execution frame
|
||||
// is created
|
||||
T& wref()
|
||||
{
|
||||
GAPI_Assert(isRWExt() || isRWOwn());
|
||||
if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
|
||||
if (isRWOwn()) return util::get<rw_own_t>(m_ref);
|
||||
util::throw_error(std::logic_error("Impossible happened"));
|
||||
}
|
||||
|
||||
// Obtain a READ reference to underlying object
|
||||
// Used by CPU kernel API wrappers when a kernel execution frame
|
||||
// is created
|
||||
const T& rref() const
|
||||
{
|
||||
// ANY object can be accessed for reading, even if it declared for
|
||||
// output. Example -- a GComputation from [in] to [out1,out2]
|
||||
// where [out2] is a result of operation applied to [out1]:
|
||||
//
|
||||
// GComputation boundary
|
||||
// . . . . . . .
|
||||
// . .
|
||||
// [in] ----> foo() ----> [out1]
|
||||
// . . :
|
||||
// . . . .:. . .
|
||||
// . V .
|
||||
// . bar() ---> [out2]
|
||||
// . . . . . . . . . . . .
|
||||
//
|
||||
if (isROExt()) return *util::get<ro_ext_t>(m_ref);
|
||||
if (isRWExt()) return *util::get<rw_ext_t>(m_ref);
|
||||
if (isRWOwn()) return util::get<rw_own_t>(m_ref);
|
||||
util::throw_error(std::logic_error("Impossible happened"));
|
||||
}
|
||||
|
||||
virtual void mov(BasicOpaqueRef &v) override {
|
||||
OpaqueRefT<T> *tv = dynamic_cast<OpaqueRefT<T>*>(&v);
|
||||
GAPI_Assert(tv != nullptr);
|
||||
wref() = std::move(tv->wref());
|
||||
}
|
||||
|
||||
virtual const void* ptr() const override { return &rref(); }
|
||||
|
||||
virtual void set(const cv::util::any &a) override {
|
||||
wref() = util::any_cast<T>(a);
|
||||
}
|
||||
};
|
||||
|
||||
// This class strips type information from OpaqueRefT<> and makes it usable
|
||||
// in the G-API executables (carrying run-time data/information to kernels).
|
||||
// Part of GRunArg.
|
||||
// Its methods are typed proxies to OpaqueRefT<T>.
|
||||
// OpaqueRef maintains "reference" semantics so two copies of OpaqueRef refer
|
||||
// to the same underlying object.
|
||||
class OpaqueRef
|
||||
{
|
||||
std::shared_ptr<BasicOpaqueRef> m_ref;
|
||||
cv::detail::OpaqueKind m_kind = cv::detail::OpaqueKind::CV_UNKNOWN;
|
||||
|
||||
template<typename T> inline void check() const
|
||||
{
|
||||
GAPI_DbgAssert(dynamic_cast<OpaqueRefT<T>*>(m_ref.get()) != nullptr);
|
||||
}
|
||||
|
||||
public:
|
||||
OpaqueRef() = default;
|
||||
|
||||
template<
|
||||
typename T,
|
||||
typename = util::are_different_t<OpaqueRef, T>
|
||||
>
|
||||
// FIXME: probably won't work with const object
|
||||
explicit OpaqueRef(T&& obj) :
|
||||
m_ref(new OpaqueRefT<util::decay_t<T>>(std::forward<T>(obj))),
|
||||
m_kind(GOpaqueTraits<util::decay_t<T>>::kind) {}
|
||||
|
||||
cv::detail::OpaqueKind getKind() const
|
||||
{
|
||||
return m_kind;
|
||||
}
|
||||
|
||||
template<typename T> void reset()
|
||||
{
|
||||
if (!m_ref) m_ref.reset(new OpaqueRefT<T>());
|
||||
check<T>();
|
||||
storeKind<T>();
|
||||
static_cast<OpaqueRefT<T>&>(*m_ref).reset();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void storeKind()
|
||||
{
|
||||
m_kind = cv::detail::GOpaqueTraits<T>::kind;
|
||||
}
|
||||
|
||||
template<typename T> T& wref()
|
||||
{
|
||||
check<T>();
|
||||
return static_cast<OpaqueRefT<T>&>(*m_ref).wref();
|
||||
}
|
||||
|
||||
template<typename T> const T& rref() const
|
||||
{
|
||||
check<T>();
|
||||
return static_cast<OpaqueRefT<T>&>(*m_ref).rref();
|
||||
}
|
||||
|
||||
void mov(OpaqueRef &v)
|
||||
{
|
||||
m_ref->mov(*v.m_ref);
|
||||
}
|
||||
|
||||
cv::GOpaqueDesc descr_of() const
|
||||
{
|
||||
return m_ref->m_desc;
|
||||
}
|
||||
|
||||
// May be used to uniquely identify this object internally
|
||||
const void *ptr() const { return m_ref->ptr(); }
|
||||
|
||||
// Introduced for in-graph meta handling
|
||||
OpaqueRef& operator= (const cv::util::any &a)
|
||||
{
|
||||
m_ref->set(a);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
/** \addtogroup gapi_data_objects
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief `cv::GOpaque<T>` template class represents an object of
|
||||
* class `T` in the graph.
|
||||
*
|
||||
* `cv::GOpaque<T>` describes a functional relationship between operations
|
||||
* consuming and producing object of class `T`. `cv::GOpaque<T>` is
|
||||
* designed to extend G-API with user-defined data types, which are
|
||||
* often required with user-defined operations. G-API can't apply any
|
||||
* optimizations to user-defined types since these types are opaque to
|
||||
* the framework. However, there is a number of G-API operations
|
||||
* declared with `cv::GOpaque<T>` as a return type,
|
||||
* e.g. cv::gapi::streaming::timestamp() or cv::gapi::streaming::size().
|
||||
*
|
||||
* @sa `cv::GArray<T>`
|
||||
*/
|
||||
template<typename T> class GOpaque
|
||||
{
|
||||
public:
|
||||
// Host type (or Flat type) - the type this GOpaque is actually
|
||||
// specified to.
|
||||
/// @private
|
||||
using HT = typename detail::flatten_g<util::decay_t<T>>::type;
|
||||
|
||||
/**
|
||||
* @brief Constructs an empty `cv::GOpaque<T>`
|
||||
*
|
||||
* Normally, empty G-API data objects denote a starting point of
|
||||
* the graph. When an empty `cv::GOpaque<T>` is assigned to a result
|
||||
* of some operation, it obtains a functional link to this
|
||||
* operation (and is not empty anymore).
|
||||
*/
|
||||
GOpaque() { putDetails(); } // Empty constructor
|
||||
|
||||
/// @private
|
||||
explicit GOpaque(detail::GOpaqueU &&ref) // GOpaqueU-based constructor
|
||||
: m_ref(ref) { putDetails(); } // (used by GCall, not for users)
|
||||
|
||||
/// @private
|
||||
detail::GOpaqueU strip() const {
|
||||
return m_ref;
|
||||
}
|
||||
/// @private
|
||||
static void Ctor(detail::OpaqueRef& ref) {
|
||||
ref.reset<HT>();
|
||||
}
|
||||
private:
|
||||
void putDetails() {
|
||||
m_ref.setConstructFcn(&Ctor);
|
||||
m_ref.specifyType<HT>();
|
||||
m_ref.storeKind<HT>();
|
||||
}
|
||||
|
||||
detail::GOpaqueU m_ref;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GOPAQUE_HPP
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GPROTO_HPP
|
||||
#define OPENCV_GAPI_GPROTO_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <ostream>
|
||||
|
||||
#include <opencv2/gapi/util/variant.hpp>
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/gscalar.hpp>
|
||||
#include <opencv2/gapi/garray.hpp>
|
||||
#include <opencv2/gapi/gopaque.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/gmetaarg.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
// FIXME: user shouldn't deal with it - put to detail?
|
||||
// GProtoArg is an union type over G-types which can serve as
|
||||
// GComputation's in/output slots. In other words, GProtoArg
|
||||
// wraps any type which can serve as G-API exchange type.
|
||||
//
|
||||
// In Runtime, GProtoArgs are substituted with appropriate GRunArgs.
|
||||
//
|
||||
// GProtoArg objects are constructed in-place when user describes
|
||||
// (captures) computations, user doesn't interact with these types
|
||||
// directly.
|
||||
using GProtoArg = util::variant
|
||||
< GMat
|
||||
, GMatP
|
||||
, GFrame
|
||||
, GScalar
|
||||
, detail::GArrayU // instead of GArray<T>
|
||||
, detail::GOpaqueU // instead of GOpaque<T>
|
||||
>;
|
||||
|
||||
using GProtoArgs = std::vector<GProtoArg>;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename... Ts> inline GProtoArgs packArgs(Ts... args)
|
||||
{
|
||||
return GProtoArgs{ GProtoArg(wrap_gapi_helper<Ts>::wrap(args))... };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<class Tag>
|
||||
struct GIOProtoArgs
|
||||
{
|
||||
public:
|
||||
// NB: Used by python wrapper
|
||||
GIOProtoArgs() = default;
|
||||
explicit GIOProtoArgs(const GProtoArgs& args) : m_args(args) {}
|
||||
explicit GIOProtoArgs(GProtoArgs &&args) : m_args(std::move(args)) {}
|
||||
|
||||
GProtoArgs m_args;
|
||||
|
||||
// TODO: Think about the addition operator
|
||||
/**
|
||||
* @brief This operator allows to complement the proto vectors at runtime.
|
||||
*
|
||||
* It's an ordinary overload of addition assignment operator.
|
||||
*
|
||||
* Example of usage:
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/dynamic_graph_snippets.cpp GIOProtoArgs usage
|
||||
*
|
||||
*/
|
||||
template<typename Tg>
|
||||
friend GIOProtoArgs<Tg>& operator += (GIOProtoArgs<Tg> &lhs, const GIOProtoArgs<Tg> &rhs);
|
||||
};
|
||||
|
||||
template<typename Tg>
|
||||
cv::GIOProtoArgs<Tg>& operator += (cv::GIOProtoArgs<Tg> &lhs, const cv::GIOProtoArgs<Tg> &rhs)
|
||||
{
|
||||
lhs.m_args.reserve(lhs.m_args.size() + rhs.m_args.size());
|
||||
lhs.m_args.insert(lhs.m_args.end(), rhs.m_args.begin(), rhs.m_args.end());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
struct In_Tag{};
|
||||
struct Out_Tag{};
|
||||
|
||||
using GProtoInputArgs = GIOProtoArgs<In_Tag>;
|
||||
using GProtoOutputArgs = GIOProtoArgs<Out_Tag>;
|
||||
|
||||
// Perfect forwarding
|
||||
template<typename... Ts> inline GProtoInputArgs GIn(Ts&&... ts)
|
||||
{
|
||||
return GProtoInputArgs(detail::packArgs(std::forward<Ts>(ts)...));
|
||||
}
|
||||
|
||||
template<typename... Ts> inline GProtoOutputArgs GOut(Ts&&... ts)
|
||||
{
|
||||
return GProtoOutputArgs(detail::packArgs(std::forward<Ts>(ts)...));
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// Extract elements form tuple
|
||||
// FIXME: Someday utilize a generic tuple_to_vec<> routine
|
||||
template<typename... Ts, int... Indexes>
|
||||
static GProtoOutputArgs getGOut_impl(const std::tuple<Ts...>& ts, detail::Seq<Indexes...>)
|
||||
{
|
||||
return GProtoOutputArgs{ detail::packArgs(std::get<Indexes>(ts)...)};
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Ts> inline GProtoOutputArgs GOut(const std::tuple<Ts...>& ts)
|
||||
{
|
||||
// TODO: think of std::forward(ts)
|
||||
return detail::getGOut_impl(ts, typename detail::MkSeq<sizeof...(Ts)>::type());
|
||||
}
|
||||
|
||||
// Takes rvalue as input arg
|
||||
template<typename... Ts> inline GProtoOutputArgs GOut(std::tuple<Ts...>&& ts)
|
||||
{
|
||||
// TODO: think of std::forward(ts)
|
||||
return detail::getGOut_impl(ts, typename detail::MkSeq<sizeof...(Ts)>::type());
|
||||
}
|
||||
|
||||
// Extract run-time arguments from node origin
|
||||
// Can be used to extract constant values associated with G-objects
|
||||
// (like GScalar) at graph construction time
|
||||
GRunArg value_of(const GOrigin &origin);
|
||||
|
||||
// Transform run-time computation arguments into a collection of metadata
|
||||
// extracted from that arguments
|
||||
GMetaArg GAPI_EXPORTS descr_of(const GRunArg &arg );
|
||||
GMetaArgs GAPI_EXPORTS descr_of(const GRunArgs &args);
|
||||
|
||||
// Transform run-time operation result argument into metadata extracted from that argument
|
||||
// Used to compare the metadata, which generated at compile time with the metadata result operation in run time
|
||||
GMetaArg GAPI_EXPORTS descr_of(const GRunArgP& argp);
|
||||
|
||||
// Checks if run-time computation argument can be described by metadata
|
||||
bool GAPI_EXPORTS can_describe(const GMetaArg& meta, const GRunArg& arg);
|
||||
bool GAPI_EXPORTS can_describe(const GMetaArgs& metas, const GRunArgs& args);
|
||||
|
||||
// Checks if run-time computation result argument can be described by metadata.
|
||||
// Used to check if the metadata generated at compile time
|
||||
// coincides with output arguments passed to computation in cpu and ocl backends
|
||||
bool GAPI_EXPORTS can_describe(const GMetaArg& meta, const GRunArgP& argp);
|
||||
|
||||
// Validates input arguments
|
||||
void GAPI_EXPORTS validate_input_arg(const GRunArg& arg);
|
||||
void GAPI_EXPORTS validate_input_args(const GRunArgs& args);
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GPROTO_HPP
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GPU_CORE_API_HPP
|
||||
#define OPENCV_GAPI_GPU_CORE_API_HPP
|
||||
/** @file
|
||||
* @deprecated Use <opencv2/gapi/ocl/core.hpp> instead.
|
||||
*/
|
||||
|
||||
#include <opencv2/gapi/ocl/core.hpp>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace core {
|
||||
namespace gpu {
|
||||
using namespace ocl;
|
||||
} // namespace gpu
|
||||
} // namespace core
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_GPU_CORE_API_HPP
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GGPUKERNEL_HPP
|
||||
#define OPENCV_GAPI_GGPUKERNEL_HPP
|
||||
/** @file
|
||||
* @deprecated Use <opencv2/gapi/ocl/goclkernel.hpp> instead.
|
||||
*/
|
||||
|
||||
#include <opencv2/gapi/ocl/goclkernel.hpp>
|
||||
#define GAPI_GPU_KERNEL GAPI_OCL_KERNEL
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_GGPUKERNEL_HPP
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GPU_IMGPROC_API_HPP
|
||||
#define OPENCV_GAPI_GPU_IMGPROC_API_HPP
|
||||
/** @file
|
||||
* @deprecated Use <opencv2/gapi/ocl/imgproc.hpp> instead.
|
||||
*/
|
||||
|
||||
#include <opencv2/gapi/ocl/imgproc.hpp>
|
||||
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace imgproc {
|
||||
namespace gpu {
|
||||
using namespace ocl;
|
||||
} // namespace gpu
|
||||
} // namespace imgproc
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
|
||||
#endif // OPENCV_GAPI_GPU_IMGPROC_API_HPP
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
// This file is part of OpenCV project.
|
||||
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GSCALAR_HPP
|
||||
#define OPENCV_GAPI_GSCALAR_HPP
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp> // GShape
|
||||
#include <opencv2/gapi/util/optional.hpp>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
// Forward declaration; GNode and GOrigin are an internal
|
||||
// (user-inaccessible) classes.
|
||||
class GNode;
|
||||
struct GOrigin;
|
||||
|
||||
/** \addtogroup gapi_data_objects
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief GScalar class represents cv::Scalar data in the graph.
|
||||
*
|
||||
* GScalar may be associated with a cv::Scalar value, which becomes
|
||||
* its constant value bound in graph compile time. cv::GScalar describes a
|
||||
* functional relationship between operations consuming and producing
|
||||
* GScalar objects.
|
||||
*
|
||||
* GScalar is a virtual counterpart of cv::Scalar, which is usually used
|
||||
* to represent the GScalar data in G-API during the execution.
|
||||
*
|
||||
* @sa Scalar
|
||||
*/
|
||||
class GAPI_EXPORTS_W_SIMPLE GScalar
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs an empty GScalar
|
||||
*
|
||||
* Normally, empty G-API data objects denote a starting point of
|
||||
* the graph. When an empty GScalar is assigned to a result of some
|
||||
* operation, it obtains a functional link to this operation (and
|
||||
* is not empty anymore).
|
||||
*/
|
||||
GAPI_WRAP GScalar();
|
||||
|
||||
/**
|
||||
* @brief Constructs a value-initialized GScalar
|
||||
*
|
||||
* In contrast with GMat (which can be either an explicit graph input
|
||||
* or a result of some operation), GScalars may have their values
|
||||
* be associated at graph construction time. It is useful when
|
||||
* some operation has a GScalar input which doesn't change during
|
||||
* the program execution, and is set only once. In this case,
|
||||
* there is no need to declare such GScalar as a graph input.
|
||||
*
|
||||
* @note The value of GScalar may be overwritten by assigning some
|
||||
* other GScalar to the object using `operator=` -- on the
|
||||
* assignment, the old GScalar value is discarded.
|
||||
*
|
||||
* @param s a cv::Scalar value to associate with this GScalar object.
|
||||
*/
|
||||
GAPI_WRAP
|
||||
explicit GScalar(const cv::Scalar& s);
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @brief Constructs a value-initialized GScalar
|
||||
*
|
||||
* @param s a cv::Scalar value to associate with this GScalar object.
|
||||
*/
|
||||
explicit GScalar(cv::Scalar&& s); // Constant value move-constructor from cv::Scalar
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @brief Constructs a value-initialized GScalar
|
||||
*
|
||||
* @param v0 A `double` value to associate with this GScalar. Note
|
||||
* that only the first component of a four-component cv::Scalar is
|
||||
* set to this value, with others remain zeros.
|
||||
*
|
||||
* This constructor overload is not marked `explicit` and can be
|
||||
* used in G-API expression code like this:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp gscalar_implicit
|
||||
*
|
||||
* Here operator+(GMat,GScalar) is used to wrap cv::gapi::addC()
|
||||
* and a value-initialized GScalar is created on the fly.
|
||||
*
|
||||
* @overload
|
||||
*/
|
||||
GScalar(double v0); // Constant value constructor from double
|
||||
|
||||
/// @private
|
||||
GScalar(const GNode &n, std::size_t out); // Operation result constructor
|
||||
/// @private
|
||||
GOrigin& priv(); // Internal use only
|
||||
/// @private
|
||||
const GOrigin& priv() const; // Internal use only
|
||||
|
||||
private:
|
||||
std::shared_ptr<GOrigin> m_priv;
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_meta_args
|
||||
* @{
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE GScalarDesc
|
||||
{
|
||||
// NB.: right now it is empty
|
||||
|
||||
inline bool operator== (const GScalarDesc &) const
|
||||
{
|
||||
return true; // NB: implement this method if GScalar meta appears
|
||||
}
|
||||
|
||||
inline bool operator!= (const GScalarDesc &rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
};
|
||||
|
||||
GAPI_EXPORTS_W inline GScalarDesc empty_scalar_desc() { return GScalarDesc(); }
|
||||
|
||||
GAPI_EXPORTS GScalarDesc descr_of(const cv::Scalar &scalar);
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const cv::GScalarDesc &desc);
|
||||
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GSCALAR_HPP
|
||||
+430
@@ -0,0 +1,430 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GSTREAMING_COMPILED_HPP
|
||||
#define OPENCV_GAPI_GSTREAMING_COMPILED_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/own/assert.hpp>
|
||||
#include <opencv2/gapi/util/optional.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/streaming/source.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
template<class T> using optional = cv::util::optional<T>;
|
||||
|
||||
namespace detail {
|
||||
template<typename T> struct wref_spec {
|
||||
using type = T;
|
||||
};
|
||||
template<typename T> struct wref_spec<std::vector<T> > {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template<typename RefHolder>
|
||||
struct OptRef {
|
||||
struct OptHolder {
|
||||
virtual void mov(RefHolder &h) = 0;
|
||||
virtual void reset() = 0;
|
||||
virtual ~OptHolder() = default;
|
||||
using Ptr = std::shared_ptr<OptHolder>;
|
||||
};
|
||||
template<class T> struct Holder final: OptHolder {
|
||||
std::reference_wrapper<cv::optional<T> > m_opt_ref;
|
||||
|
||||
explicit Holder(cv::optional<T>& opt) : m_opt_ref(std::ref(opt)) {
|
||||
}
|
||||
virtual void mov(RefHolder &h) override {
|
||||
using U = typename wref_spec<T>::type;
|
||||
m_opt_ref.get() = cv::util::make_optional(std::move(h.template wref<U>()));
|
||||
}
|
||||
virtual void reset() override {
|
||||
m_opt_ref.get().reset();
|
||||
}
|
||||
};
|
||||
template<class T>
|
||||
explicit OptRef(cv::optional<T>& t) : m_opt{new Holder<T>(t)} {}
|
||||
void mov(RefHolder &h) { m_opt->mov(h); }
|
||||
void reset() { m_opt->reset();}
|
||||
private:
|
||||
typename OptHolder::Ptr m_opt;
|
||||
};
|
||||
using OptionalVectorRef = OptRef<cv::detail::VectorRef>;
|
||||
using OptionalOpaqueRef = OptRef<cv::detail::OpaqueRef>;
|
||||
} // namespace detail
|
||||
|
||||
// TODO: Keep it in sync with GRunArgP (derive the type automatically?)
|
||||
using GOptRunArgP = util::variant<
|
||||
optional<cv::Mat>*,
|
||||
optional<cv::RMat>*,
|
||||
optional<cv::MediaFrame>*,
|
||||
optional<cv::Scalar>*,
|
||||
cv::detail::OptionalVectorRef,
|
||||
cv::detail::OptionalOpaqueRef
|
||||
>;
|
||||
using GOptRunArgsP = std::vector<GOptRunArgP>;
|
||||
|
||||
using GOptRunArg = util::variant<
|
||||
optional<cv::Mat>,
|
||||
optional<cv::RMat>,
|
||||
optional<cv::MediaFrame>,
|
||||
optional<cv::Scalar>,
|
||||
optional<cv::detail::VectorRef>,
|
||||
optional<cv::detail::OpaqueRef>
|
||||
>;
|
||||
using GOptRunArgs = std::vector<GOptRunArg>;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T> inline GOptRunArgP wrap_opt_arg(optional<T>& arg) {
|
||||
// By default, T goes to an OpaqueRef. All other types are specialized
|
||||
return GOptRunArgP{OptionalOpaqueRef(arg)};
|
||||
}
|
||||
|
||||
template<typename T> inline GOptRunArgP wrap_opt_arg(optional<std::vector<T> >& arg) {
|
||||
return GOptRunArgP{OptionalVectorRef(arg)};
|
||||
}
|
||||
|
||||
template<> inline GOptRunArgP wrap_opt_arg(optional<cv::Mat> &m) {
|
||||
return GOptRunArgP{&m};
|
||||
}
|
||||
|
||||
template<> inline GOptRunArgP wrap_opt_arg(optional<cv::RMat> &m) {
|
||||
return GOptRunArgP{&m};
|
||||
}
|
||||
|
||||
template<> inline GOptRunArgP wrap_opt_arg(optional<cv::MediaFrame> &f) {
|
||||
return GOptRunArgP{&f};
|
||||
}
|
||||
|
||||
template<> inline GOptRunArgP wrap_opt_arg(optional<cv::Scalar> &s) {
|
||||
return GOptRunArgP{&s};
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Now cv::gout() may produce an empty vector (see "dynamic graphs"), so
|
||||
// there may be a conflict between these two. State here that Opt version
|
||||
// _must_ have at least one input for this overload
|
||||
template<typename T, typename... Ts>
|
||||
inline GOptRunArgsP gout(optional<T>&arg, optional<Ts>&... args)
|
||||
{
|
||||
return GOptRunArgsP{ detail::wrap_opt_arg(arg), detail::wrap_opt_arg(args)... };
|
||||
}
|
||||
|
||||
/**
|
||||
* \addtogroup gapi_main_classes
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief Represents a computation (graph) compiled for streaming.
|
||||
*
|
||||
* This class represents a product of graph compilation (calling
|
||||
* cv::GComputation::compileStreaming()). Objects of this class
|
||||
* actually do stream processing, and the whole pipeline execution
|
||||
* complexity is incapsulated into objects of this class. Execution
|
||||
* model has two levels: at the very top, the execution of a
|
||||
* heterogeneous graph is aggressively pipelined; at the very bottom
|
||||
* the execution of every internal block is determined by its
|
||||
* associated backend. Backends are selected based on kernel packages
|
||||
* passed via compilation arguments ( see @ref gapi_compile_args,
|
||||
* GNetworkPackage, GKernelPackage for details).
|
||||
*
|
||||
* GStreamingCompiled objects have a "player" semantics -- there are
|
||||
* methods like start() and stop(). GStreamingCompiled has a full
|
||||
* control over a videostream and so is stateful. You need to specify the
|
||||
* input stream data using setSource() and then call start() to
|
||||
* actually start processing. After that, use pull() or try_pull() to
|
||||
* obtain next processed data frame from the graph in a blocking or
|
||||
* non-blocking way, respectively.
|
||||
*
|
||||
* Currently a single GStreamingCompiled can process only one video
|
||||
* streat at time. Produce multiple GStreamingCompiled objects to run the
|
||||
* same graph on multiple video streams.
|
||||
*
|
||||
* @sa GCompiled
|
||||
*/
|
||||
class GAPI_EXPORTS_W_SIMPLE GStreamingCompiled
|
||||
{
|
||||
public:
|
||||
class GAPI_EXPORTS Priv;
|
||||
GAPI_WRAP GStreamingCompiled();
|
||||
|
||||
// FIXME: More overloads?
|
||||
/**
|
||||
* @brief Specify the input data to GStreamingCompiled for
|
||||
* processing, a generic version.
|
||||
*
|
||||
* Use gin() to create an input parameter vector.
|
||||
*
|
||||
* Input vectors must have the same number of elements as defined
|
||||
* in the cv::GComputation protocol (at the moment of its
|
||||
* construction). Shapes of elements also must conform to protocol
|
||||
* (e.g. cv::Mat needs to be passed where cv::GMat has been
|
||||
* declared as input, and so on). Run-time exception is generated
|
||||
* on type mismatch.
|
||||
*
|
||||
* In contrast with regular GCompiled, user can also pass an
|
||||
* object of type GVideoCapture for a GMat parameter of the parent
|
||||
* GComputation. The compiled pipeline will start fetching data
|
||||
* from that GVideoCapture and feeding it into the
|
||||
* pipeline. Pipeline stops when a GVideoCapture marks end of the
|
||||
* stream (or when stop() is called).
|
||||
*
|
||||
* Passing a regular Mat for a GMat parameter makes it "infinite"
|
||||
* source -- pipeline may run forever feeding with this Mat until
|
||||
* stopped explicitly.
|
||||
*
|
||||
* Currently only a single GVideoCapture is supported as input. If
|
||||
* the parent GComputation is declared with multiple input GMat's,
|
||||
* one of those can be specified as GVideoCapture but all others
|
||||
* must be regular Mat objects.
|
||||
*
|
||||
* Throws if pipeline is already running. Use stop() and then
|
||||
* setSource() to run the graph on a new video stream.
|
||||
*
|
||||
* @note This method is not thread-safe (with respect to the user
|
||||
* side) at the moment. Protect the access if
|
||||
* start()/stop()/setSource() may be called on the same object in
|
||||
* multiple threads in your application.
|
||||
*
|
||||
* @param ins vector of inputs to process.
|
||||
* @sa gin
|
||||
*/
|
||||
void setSource(GRunArgs &&ins);
|
||||
|
||||
/// @private -- Exclude this function from OpenCV documentation
|
||||
GAPI_WRAP void setSource(const cv::detail::ExtractArgsCallback& callback);
|
||||
|
||||
/**
|
||||
* @brief Specify an input video stream for a single-input
|
||||
* computation pipeline.
|
||||
*
|
||||
* Throws if pipeline is already running. Use stop() and then
|
||||
* setSource() to run the graph on a new video stream.
|
||||
*
|
||||
* @overload
|
||||
* @param s a shared pointer to IStreamSource representing the
|
||||
* input video stream.
|
||||
*/
|
||||
void setSource(const gapi::wip::IStreamSource::Ptr& s);
|
||||
|
||||
/**
|
||||
* @brief Constructs and specifies an input video stream for a
|
||||
* single-input computation pipeline with the given parameters.
|
||||
*
|
||||
* Throws if pipeline is already running. Use stop() and then
|
||||
* setSource() to run the graph on a new video stream.
|
||||
*
|
||||
* @overload
|
||||
* @param args arguments used to construct and initialize a stream
|
||||
* source.
|
||||
*/
|
||||
template<typename T, typename... Args>
|
||||
void setSource(Args&&... args) {
|
||||
setSource(cv::gapi::wip::make_src<T>(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start the pipeline execution.
|
||||
*
|
||||
* Use pull()/try_pull() to obtain data. Throws an exception if
|
||||
* a video source was not specified.
|
||||
*
|
||||
* setSource() must be called first, even if the pipeline has been
|
||||
* working already and then stopped (explicitly via stop() or due
|
||||
* stream completion)
|
||||
*
|
||||
* @note This method is not thread-safe (with respect to the user
|
||||
* side) at the moment. Protect the access if
|
||||
* start()/stop()/setSource() may be called on the same object in
|
||||
* multiple threads in your application.
|
||||
*/
|
||||
GAPI_WRAP void start();
|
||||
|
||||
/**
|
||||
* @brief Get the next processed frame from the pipeline.
|
||||
*
|
||||
* Use gout() to create an output parameter vector.
|
||||
*
|
||||
* Output vectors must have the same number of elements as defined
|
||||
* in the cv::GComputation protocol (at the moment of its
|
||||
* construction). Shapes of elements also must conform to protocol
|
||||
* (e.g. cv::Mat needs to be passed where cv::GMat has been
|
||||
* declared as output, and so on). Run-time exception is generated
|
||||
* on type mismatch.
|
||||
*
|
||||
* This method writes new data into objects passed via output
|
||||
* vector. If there is no data ready yet, this method blocks. Use
|
||||
* try_pull() if you need a non-blocking version.
|
||||
*
|
||||
* @param outs vector of output parameters to obtain.
|
||||
* @return true if next result has been obtained,
|
||||
* false marks end of the stream.
|
||||
*/
|
||||
bool pull(cv::GRunArgsP &&outs);
|
||||
|
||||
// NB: Used from python
|
||||
/// @private -- Exclude this function from OpenCV documentation
|
||||
GAPI_WRAP std::tuple<bool, cv::util::variant<cv::GRunArgs, cv::GOptRunArgs>> pull();
|
||||
|
||||
/**
|
||||
* @brief Get some next available data from the pipeline.
|
||||
*
|
||||
* This method takes a vector of cv::optional object. An object is
|
||||
* assigned to some value if this value is available (ready) at
|
||||
* the time of the call, and resets the object to empty() if it is
|
||||
* not.
|
||||
*
|
||||
* This is a blocking method which guarantees that some data has
|
||||
* been written to the output vector on return.
|
||||
*
|
||||
* Using this method only makes sense if the graph has
|
||||
* desynchronized parts (see cv::gapi::desync). If there is no
|
||||
* desynchronized parts in the graph, the behavior of this
|
||||
* method is identical to the regular pull() (all data objects are
|
||||
* produced synchronously in the output vector).
|
||||
*
|
||||
* Use gout() to create an output parameter vector.
|
||||
*
|
||||
* Output vectors must have the same number of elements as defined
|
||||
* in the cv::GComputation protocol (at the moment of its
|
||||
* construction). Shapes of elements also must conform to protocol
|
||||
* (e.g. cv::optional<cv::Mat> needs to be passed where cv::GMat
|
||||
* has been declared as output, and so on). Run-time exception is
|
||||
* generated on type mismatch.
|
||||
*
|
||||
* This method writes new data into objects passed via output
|
||||
* vector. If there is no data ready yet, this method blocks. Use
|
||||
* try_pull() if you need a non-blocking version.
|
||||
*
|
||||
* @param outs vector of output parameters to obtain.
|
||||
* @return true if next result has been obtained,
|
||||
* false marks end of the stream.
|
||||
*
|
||||
* @sa cv::gapi::desync
|
||||
*/
|
||||
bool pull(cv::GOptRunArgsP &&outs);
|
||||
|
||||
/**
|
||||
* @brief Try to get the next processed frame from the pipeline.
|
||||
*
|
||||
* Use gout() to create an output parameter vector.
|
||||
*
|
||||
* This method writes new data into objects passed via output
|
||||
* vector. If there is no data ready yet, the output vector
|
||||
* remains unchanged and false is returned.
|
||||
*
|
||||
* @return true if data has been obtained, and false if it was
|
||||
* not. Note: false here doesn't mark the end of the stream.
|
||||
*/
|
||||
bool try_pull(cv::GRunArgsP &&outs);
|
||||
|
||||
/**
|
||||
* @brief Stop (abort) processing the pipeline.
|
||||
*
|
||||
* Note - it is not pause but a complete stop. Calling start()
|
||||
* will cause G-API to start processing the stream from the early beginning.
|
||||
*
|
||||
* Throws if the pipeline is not running.
|
||||
*/
|
||||
GAPI_WRAP void stop();
|
||||
|
||||
/**
|
||||
* @brief Test if the pipeline is running.
|
||||
*
|
||||
* @note This method is not thread-safe (with respect to the user
|
||||
* side) at the moment. Protect the access if
|
||||
* start()/stop()/setSource() may be called on the same object in
|
||||
* multiple threads in your application.
|
||||
*
|
||||
* @return true if the current stream is not over yet.
|
||||
*/
|
||||
GAPI_WRAP bool running() const;
|
||||
|
||||
/// @private
|
||||
Priv& priv();
|
||||
|
||||
/**
|
||||
* @brief Check if compiled object is valid (non-empty)
|
||||
*
|
||||
* @return true if the object is runnable (valid), false otherwise
|
||||
*/
|
||||
explicit operator bool () const;
|
||||
|
||||
/**
|
||||
* @brief Vector of metadata this graph was compiled for.
|
||||
*
|
||||
* @return Unless _reshape_ is not supported, return value is the
|
||||
* same vector which was passed to cv::GComputation::compile() to
|
||||
* produce this compiled object. Otherwise, it is the latest
|
||||
* metadata vector passed to reshape() (if that call was
|
||||
* successful).
|
||||
*/
|
||||
const GMetaArgs& metas() const; // Meta passed to compile()
|
||||
|
||||
/**
|
||||
* @brief Vector of metadata descriptions of graph outputs
|
||||
*
|
||||
* @return vector with formats/resolutions of graph's output
|
||||
* objects, auto-inferred from input metadata vector by
|
||||
* operations which form this computation.
|
||||
*
|
||||
* @note GCompiled objects produced from the same
|
||||
* cv::GComputiation graph with different input metas may return
|
||||
* different values in this vector.
|
||||
*/
|
||||
const GMetaArgs& outMetas() const;
|
||||
|
||||
protected:
|
||||
/// @private
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
/** @} */
|
||||
|
||||
namespace gapi {
|
||||
|
||||
/**
|
||||
* @brief This namespace contains G-API functions, structures, and
|
||||
* symbols related to the Streaming execution mode.
|
||||
*
|
||||
* Some of the operations defined in this namespace (e.g. size(),
|
||||
* BGR(), etc.) can be used in the traditional execution mode too.
|
||||
*/
|
||||
namespace streaming {
|
||||
/**
|
||||
* @brief Specify queue capacity for streaming execution.
|
||||
*
|
||||
* In the streaming mode the pipeline steps are connected with queues
|
||||
* and this compile argument controls every queue's size.
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE queue_capacity
|
||||
{
|
||||
GAPI_WRAP
|
||||
explicit queue_capacity(size_t cap = 1) : capacity(cap) { };
|
||||
GAPI_PROP_RW
|
||||
size_t capacity;
|
||||
};
|
||||
/** @} */
|
||||
} // namespace streaming
|
||||
} // namespace gapi
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<> struct CompileArgTag<cv::gapi::streaming::queue_capacity>
|
||||
{
|
||||
static const char* tag() { return "gapi.queue_capacity"; }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // OPENCV_GAPI_GSTREAMING_COMPILED_HPP
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_GTRANSFORM_HPP
|
||||
#define OPENCV_GAPI_GTRANSFORM_HPP
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/gtype_traits.hpp>
|
||||
#include <opencv2/gapi/util/compiler_hints.hpp>
|
||||
#include <opencv2/gapi/gcomputation.hpp>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
|
||||
struct GAPI_EXPORTS GTransform
|
||||
{
|
||||
// FIXME: consider another simplified
|
||||
// class instead of GComputation
|
||||
using F = std::function<GComputation()>;
|
||||
|
||||
std::string description;
|
||||
F pattern;
|
||||
F substitute;
|
||||
|
||||
GTransform(const std::string& d, const F &p, const F &s) : description(d), pattern(p), substitute(s) {}
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <typename, typename, typename>
|
||||
struct TransHelper;
|
||||
|
||||
template <typename K, typename... Ins, typename Out>
|
||||
struct TransHelper<K, std::tuple<Ins...>, Out>
|
||||
{
|
||||
template <typename Callable, int... IIs, int... OIs>
|
||||
static GComputation invoke(Callable f, Seq<IIs...>, Seq<OIs...>)
|
||||
{
|
||||
const std::tuple<Ins...> ins;
|
||||
const auto r = tuple_wrap_helper<Out>::get(f(std::get<IIs>(ins)...));
|
||||
return GComputation(cv::GIn(std::get<IIs>(ins)...),
|
||||
cv::GOut(std::get<OIs>(r)...));
|
||||
}
|
||||
|
||||
static GComputation get_pattern()
|
||||
{
|
||||
return invoke(K::pattern, typename MkSeq<sizeof...(Ins)>::type(),
|
||||
typename MkSeq<std::tuple_size<typename tuple_wrap_helper<Out>::type>::value>::type());
|
||||
}
|
||||
static GComputation get_substitute()
|
||||
{
|
||||
return invoke(K::substitute, typename MkSeq<sizeof...(Ins)>::type(),
|
||||
typename MkSeq<std::tuple_size<typename tuple_wrap_helper<Out>::type>::value>::type());
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <typename, typename>
|
||||
class GTransformImpl;
|
||||
|
||||
template <typename K, typename R, typename... Args>
|
||||
class GTransformImpl<K, std::function<R(Args...)>> : public cv::detail::TransHelper<K, std::tuple<Args...>, R>,
|
||||
public cv::detail::TransformTag
|
||||
{
|
||||
public:
|
||||
// FIXME: currently there is no check that transformations' signatures are unique
|
||||
// and won't be any intersection in graph compilation stage
|
||||
using API = K;
|
||||
|
||||
static GTransform transformation()
|
||||
{
|
||||
return GTransform(K::descr(), &K::get_pattern, &K::get_substitute);
|
||||
}
|
||||
};
|
||||
} // namespace cv
|
||||
|
||||
#define G_DESCR_HELPER_CLASS(Class) Class##DescrHelper
|
||||
|
||||
#define G_DESCR_HELPER_BODY(Class, Descr) \
|
||||
namespace detail \
|
||||
{ \
|
||||
struct G_DESCR_HELPER_CLASS(Class) \
|
||||
{ \
|
||||
static constexpr const char *descr() { return Descr; }; \
|
||||
}; \
|
||||
}
|
||||
|
||||
#define GAPI_TRANSFORM(Class, API, Descr) \
|
||||
G_DESCR_HELPER_BODY(Class, Descr) \
|
||||
struct Class final : public cv::GTransformImpl<Class, std::function API>, \
|
||||
public detail::G_DESCR_HELPER_CLASS(Class)
|
||||
|
||||
#endif // OPENCV_GAPI_GTRANSFORM_HPP
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018-2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GTYPE_TRAITS_HPP
|
||||
#define OPENCV_GAPI_GTYPE_TRAITS_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/gscalar.hpp>
|
||||
#include <opencv2/gapi/garray.hpp>
|
||||
#include <opencv2/gapi/gopaque.hpp>
|
||||
#include <opencv2/gapi/gframe.hpp>
|
||||
#include <opencv2/gapi/streaming/source.hpp>
|
||||
#include <opencv2/gapi/media.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
#include <opencv2/gapi/util/util.hpp>
|
||||
#include <opencv2/gapi/own/convert.hpp>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename, typename = void>
|
||||
struct contains_shape_field : std::false_type {};
|
||||
|
||||
template<typename TaggedTypeCandidate>
|
||||
struct contains_shape_field<TaggedTypeCandidate,
|
||||
void_t<decltype(TaggedTypeCandidate::shape)>> :
|
||||
std::is_same<typename std::decay<decltype(TaggedTypeCandidate::shape)>::type, GShape>
|
||||
{};
|
||||
|
||||
template<typename Type>
|
||||
struct has_gshape : contains_shape_field<Type> {};
|
||||
|
||||
// FIXME: These traits and enum and possible numerous switch(kind)
|
||||
// block may be replaced with a special Handler<T> object or with
|
||||
// a double dispatch
|
||||
enum class ArgKind: int
|
||||
{
|
||||
OPAQUE_VAL, // Unknown, generic, opaque-to-GAPI data type - STATIC
|
||||
// Note: OPAQUE is sometimes defined in Win sys headers
|
||||
#if !defined(OPAQUE) && !defined(CV_DOXYGEN)
|
||||
OPAQUE = OPAQUE_VAL, // deprecated value used for compatibility, use OPAQUE_VAL instead
|
||||
#endif
|
||||
GOBJREF, // <internal> reference to object
|
||||
GMAT, // a cv::GMat
|
||||
GMATP, // a cv::GMatP
|
||||
GFRAME, // a cv::GFrame
|
||||
GSCALAR, // a cv::GScalar
|
||||
GARRAY, // a cv::GArrayU (note - exactly GArrayU, not GArray<T>!)
|
||||
GOPAQUE, // a cv::GOpaqueU (note - exactly GOpaqueU, not GOpaque<T>!)
|
||||
};
|
||||
|
||||
// Describe G-API types (G-types) with traits. Mostly used by
|
||||
// cv::GArg to store meta information about types passed into
|
||||
// operation arguments. Please note that cv::GComputation is
|
||||
// defined on GProtoArgs, not GArgs!
|
||||
template<typename T> struct GTypeTraits;
|
||||
template<typename T> struct GTypeTraits
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::OPAQUE_VAL;
|
||||
static constexpr const OpaqueKind op_kind = OpaqueKind::CV_UNKNOWN;
|
||||
};
|
||||
template<> struct GTypeTraits<cv::GMat>
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GMAT;
|
||||
static constexpr const GShape shape = GShape::GMAT;
|
||||
static constexpr const OpaqueKind op_kind = OpaqueKind::CV_UNKNOWN;
|
||||
};
|
||||
template<> struct GTypeTraits<cv::GMatP>
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GMATP;
|
||||
static constexpr const GShape shape = GShape::GMAT;
|
||||
static constexpr const OpaqueKind op_kind = OpaqueKind::CV_UNKNOWN;
|
||||
};
|
||||
template<> struct GTypeTraits<cv::GFrame>
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GFRAME;
|
||||
static constexpr const GShape shape = GShape::GFRAME;
|
||||
static constexpr const OpaqueKind op_kind = OpaqueKind::CV_UNKNOWN;
|
||||
};
|
||||
template<> struct GTypeTraits<cv::GScalar>
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GSCALAR;
|
||||
static constexpr const GShape shape = GShape::GSCALAR;
|
||||
static constexpr const OpaqueKind op_kind = OpaqueKind::CV_UNKNOWN;
|
||||
};
|
||||
template<class T> struct GTypeTraits<cv::GArray<T> >
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GARRAY;
|
||||
static constexpr const GShape shape = GShape::GARRAY;
|
||||
static constexpr const OpaqueKind op_kind = GOpaqueTraits<T>::kind;
|
||||
using host_type = std::vector<T>;
|
||||
using strip_type = cv::detail::VectorRef;
|
||||
static cv::detail::GArrayU wrap_value(const cv::GArray<T> &t) { return t.strip();}
|
||||
static cv::detail::VectorRef wrap_in (const std::vector<T> &t) { return detail::VectorRef(t); }
|
||||
static cv::detail::VectorRef wrap_out ( std::vector<T> &t) { return detail::VectorRef(t); }
|
||||
};
|
||||
template<class T> struct GTypeTraits<cv::GOpaque<T> >
|
||||
{
|
||||
static constexpr const ArgKind kind = ArgKind::GOPAQUE;
|
||||
static constexpr const GShape shape = GShape::GOPAQUE;
|
||||
static constexpr const OpaqueKind op_kind = GOpaqueTraits<T>::kind;
|
||||
using host_type = T;
|
||||
using strip_type = cv::detail::OpaqueRef;
|
||||
static cv::detail::GOpaqueU wrap_value(const cv::GOpaque<T> &t) { return t.strip();}
|
||||
static cv::detail::OpaqueRef wrap_in (const T &t) { return detail::OpaqueRef(t); }
|
||||
static cv::detail::OpaqueRef wrap_out ( T &t) { return detail::OpaqueRef(t); }
|
||||
};
|
||||
|
||||
// Tests if Trait for type T requires extra marshalling ("custom wrap") or not.
|
||||
// If Traits<T> has wrap_value() defined, it does.
|
||||
template<class T> struct has_custom_wrap
|
||||
{
|
||||
template<class,class> class check;
|
||||
template<typename C> static std::true_type test(check<C, decltype(>ypeTraits<C>::wrap_value)> *);
|
||||
template<typename C> static std::false_type test(...);
|
||||
using type = decltype(test<T>(nullptr));
|
||||
static const constexpr bool value = std::is_same<std::true_type, decltype(test<T>(nullptr))>::value;
|
||||
};
|
||||
|
||||
// Resolve a Host type back to its associated G-Type.
|
||||
// FIXME: Probably it can be avoided
|
||||
// FIXME: GMatP is not present here.
|
||||
// (Actually these traits is used only to check
|
||||
// if associated G-type has custom wrap functions
|
||||
// and GMat behavior is correct for GMatP)
|
||||
template<typename T> struct GTypeOf;
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
template<> struct GTypeOf<cv::UMat> { using type = cv::GMat; };
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
template<> struct GTypeOf<cv::Mat> { using type = cv::GMat; };
|
||||
template<> struct GTypeOf<cv::RMat> { using type = cv::GMat; };
|
||||
template<> struct GTypeOf<cv::Scalar> { using type = cv::GScalar; };
|
||||
template<typename U> struct GTypeOf<std::vector<U> > { using type = cv::GArray<U>; };
|
||||
template<typename U> struct GTypeOf { using type = cv::GOpaque<U>;};
|
||||
template<> struct GTypeOf<cv::MediaFrame> { using type = cv::GFrame; };
|
||||
// FIXME: This is not quite correct since IStreamSource may produce not only Mat but also Scalar
|
||||
// and vector data. TODO: Extend the type dispatching on these types too.
|
||||
template<> struct GTypeOf<cv::gapi::wip::IStreamSource::Ptr> { using type = cv::GMat;};
|
||||
template<class T> using g_type_of_t = typename GTypeOf<T>::type;
|
||||
|
||||
// Marshalling helper for G-types and its Host types. Helps G-API
|
||||
// to store G types in internal generic containers for further
|
||||
// processing. Implements the following callbacks:
|
||||
//
|
||||
// * wrap() - converts user-facing G-type into an internal one
|
||||
// for internal storage.
|
||||
// Used when G-API operation is instantiated (G<Kernel>::on(),
|
||||
// etc) during expressing a pipeline. Mostly returns input
|
||||
// value "as is" except the case when G-type is a template. For
|
||||
// template G-classes, calls custom wrap() from Traits.
|
||||
// The value returned by wrap() is then wrapped into GArg() and
|
||||
// stored in G-API metadata.
|
||||
//
|
||||
// Example:
|
||||
// - cv::GMat arguments are passed as-is.
|
||||
// - integers, pointers, STL containers, user types are passed as-is.
|
||||
// - cv::GArray<T> is converted to cv::GArrayU.
|
||||
//
|
||||
// * wrap_in() / wrap_out() - convert Host type associated with
|
||||
// G-type to internal representation type.
|
||||
//
|
||||
// - For "simple" (non-template) G-types, returns value as-is.
|
||||
// Example: cv::GMat has host type cv::Mat, when user passes a
|
||||
// cv::Mat, system stores it internally as cv::Mat.
|
||||
//
|
||||
// - For "complex" (template) G-types, utilizes custom
|
||||
// wrap_in()/wrap_out() as described in Traits.
|
||||
// Example: cv::GArray<T> has host type std::vector<T>, when
|
||||
// user passes a std::vector<T>, system stores it
|
||||
// internally as VectorRef (with <T> stripped away).
|
||||
template<typename T, class Custom = void> struct WrapValue
|
||||
{
|
||||
static auto wrap(const T& t) ->
|
||||
typename std::remove_reference<T>::type
|
||||
{
|
||||
return static_cast<typename std::remove_reference<T>::type>(t);
|
||||
}
|
||||
|
||||
template<typename U> static U wrap_in (const U &u) { return u; }
|
||||
template<typename U> static U* wrap_out(U &u) { return &u; }
|
||||
};
|
||||
template<typename T> struct WrapValue<T, typename std::enable_if<has_custom_wrap<T>::value>::type>
|
||||
{
|
||||
static auto wrap(const T& t) -> decltype(GTypeTraits<T>::wrap_value(t))
|
||||
{
|
||||
return GTypeTraits<T>::wrap_value(t);
|
||||
}
|
||||
template<typename U> static auto wrap_in (const U &u) -> typename GTypeTraits<T>::strip_type
|
||||
{
|
||||
static_assert(!(cv::detail::has_gshape<GTypeTraits<U>>::value
|
||||
|| cv::detail::contains<typename std::decay<U>::type, GAPI_OWN_TYPES_LIST>::value),
|
||||
"gin/gout must not be used with G* classes or cv::gapi::own::*");
|
||||
return GTypeTraits<T>::wrap_in(u);
|
||||
}
|
||||
template<typename U> static auto wrap_out(U &u) -> typename GTypeTraits<T>::strip_type
|
||||
{
|
||||
static_assert(!(cv::detail::has_gshape<GTypeTraits<U>>::value
|
||||
|| cv::detail::contains<typename std::decay<U>::type, GAPI_OWN_TYPES_LIST>::value),
|
||||
"gin/gout must not be used with G* classes or cv::gapi::own::*");
|
||||
return GTypeTraits<T>::wrap_out(u);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> using wrap_gapi_helper = WrapValue<typename std::decay<T>::type>;
|
||||
template<typename T> using wrap_host_helper = WrapValue<typename std::decay<g_type_of_t<T> >::type>;
|
||||
|
||||
// Union type for various user-defined type constructors (GArray<T>,
|
||||
// GOpaque<T>, etc)
|
||||
//
|
||||
// TODO: Replace construct-only API with a more generic one (probably
|
||||
// with bits of introspection)
|
||||
//
|
||||
// Not required for non-user-defined types (GMat, GScalar, etc)
|
||||
using HostCtor = util::variant
|
||||
< util::monostate
|
||||
, detail::ConstructVec
|
||||
, detail::ConstructOpaque
|
||||
>;
|
||||
|
||||
template<typename T> struct GObtainCtor {
|
||||
static HostCtor get() { return HostCtor{}; }
|
||||
};
|
||||
template<typename T> struct GObtainCtor<GArray<T> > {
|
||||
static HostCtor get() { return HostCtor{ConstructVec{&GArray<T>::VCtor}}; };
|
||||
};
|
||||
template<typename T> struct GObtainCtor<GOpaque<T> > {
|
||||
static HostCtor get() { return HostCtor{ConstructOpaque{&GOpaque<T>::Ctor}}; };
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_GTYPE_TRAITS_HPP
|
||||
+246
@@ -0,0 +1,246 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2018 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_GTYPED_HPP
|
||||
#define OPENCV_GAPI_GTYPED_HPP
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <opencv2/gapi/gcomputation.hpp>
|
||||
#include <opencv2/gapi/gcompiled.hpp>
|
||||
#include <opencv2/gapi/gproto.hpp>
|
||||
#include <opencv2/gapi/gcommon.hpp>
|
||||
|
||||
namespace cv {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// FIXME: How to prevent coolhackers from extending it by their own types?
|
||||
// FIXME: ...Should we care?
|
||||
template<typename T> struct ProtoToParam;
|
||||
template<> struct ProtoToParam<cv::GMat> { using type = cv::Mat; };
|
||||
template<> struct ProtoToParam<cv::GScalar> { using type = cv::Scalar; };
|
||||
template<typename U> struct ProtoToParam<cv::GArray<U> > { using type = std::vector<U>; };
|
||||
template<> struct ProtoToParam<cv::GArray<cv::GMat>> { using type = std::vector<cv::Mat>; };
|
||||
template<typename U> struct ProtoToParam<cv::GOpaque<U> > { using type = U; };
|
||||
template<typename T> using ProtoToParamT = typename ProtoToParam<T>::type;
|
||||
|
||||
template<typename T> struct ProtoToMeta;
|
||||
template<> struct ProtoToMeta<cv::GMat> { using type = cv::GMatDesc; };
|
||||
template<> struct ProtoToMeta<cv::GScalar> { using type = cv::GScalarDesc; };
|
||||
template<typename U> struct ProtoToMeta<cv::GArray<U> > { using type = cv::GArrayDesc; };
|
||||
template<typename U> struct ProtoToMeta<cv::GOpaque<U> > { using type = cv::GOpaqueDesc; };
|
||||
template<typename T> using ProtoToMetaT = typename ProtoToMeta<T>::type;
|
||||
|
||||
//workaround for MSVC 19.0 bug
|
||||
template <typename T>
|
||||
auto make_default()->decltype(T{}) {return {};}
|
||||
}; // detail
|
||||
|
||||
/**
|
||||
* @brief This class is a typed wrapper over a regular GComputation.
|
||||
*
|
||||
* `std::function<>`-like template parameter specifies the graph
|
||||
* signature so methods so the object's constructor, methods like
|
||||
* `apply()` and the derived `GCompiledT::operator()` also become
|
||||
* typed.
|
||||
*
|
||||
* There is no need to use cv::gin() or cv::gout() modifiers with
|
||||
* objects of this class. Instead, all input arguments are followed
|
||||
* by all output arguments in the order from the template argument
|
||||
* signature.
|
||||
*
|
||||
* Refer to the following example. Regular (untyped) code is written this way:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp Untyped_Example
|
||||
*
|
||||
* Here:
|
||||
*
|
||||
* - cv::GComputation object is created with a lambda constructor
|
||||
* where it is defined as a two-input, one-output graph.
|
||||
*
|
||||
* - Its method `apply()` in fact takes arbitrary number of arguments
|
||||
* (as vectors) so user can pass wrong number of inputs/outputs
|
||||
* here. C++ compiler wouldn't notice that since the cv::GComputation
|
||||
* API is polymorphic, and only a run-time error will be generated.
|
||||
*
|
||||
* Now the same code written with typed API:
|
||||
*
|
||||
* @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp Typed_Example
|
||||
*
|
||||
* The key difference is:
|
||||
*
|
||||
* - Now the constructor lambda *must take* parameters and *must
|
||||
* return* values as defined in the `GComputationT<>` signature.
|
||||
* - Its method `apply()` does not require any extra specifiers to
|
||||
* separate input arguments from the output ones
|
||||
* - A `GCompiledT` (compilation product) takes input/output
|
||||
* arguments with no extra specifiers as well.
|
||||
*/
|
||||
template<typename> class GComputationT;
|
||||
|
||||
// Single return value implementation
|
||||
template<typename R, typename... Args> class GComputationT<R(Args...)>
|
||||
{
|
||||
public:
|
||||
typedef std::function<R(Args...)> Gen;
|
||||
|
||||
class GCompiledT
|
||||
{
|
||||
private:
|
||||
friend class GComputationT<R(Args...)>;
|
||||
|
||||
cv::GCompiled m_comp;
|
||||
|
||||
explicit GCompiledT(const cv::GCompiled &comp) : m_comp(comp) {}
|
||||
|
||||
public:
|
||||
GCompiledT() {}
|
||||
|
||||
void operator()(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R> &outArg)
|
||||
{
|
||||
m_comp(cv::gin(inArgs...), cv::gout(outArg));
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return static_cast<bool>(m_comp);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
typedef std::pair<R, GProtoInputArgs > Captured;
|
||||
|
||||
Captured capture(const Gen& g, Args... args)
|
||||
{
|
||||
return Captured(g(args...), cv::GIn(args...));
|
||||
}
|
||||
|
||||
Captured m_capture;
|
||||
cv::GComputation m_comp;
|
||||
|
||||
public:
|
||||
GComputationT(const Gen &generator)
|
||||
: m_capture(capture(generator, detail::make_default<Args>()...))
|
||||
, m_comp(cv::GProtoInputArgs(std::move(m_capture.second)),
|
||||
cv::GOut(m_capture.first))
|
||||
{
|
||||
}
|
||||
|
||||
void apply(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R> &outArg,
|
||||
GCompileArgs &&args)
|
||||
{
|
||||
m_comp.apply(cv::gin(inArgs...), cv::gout(outArg), std::move(args));
|
||||
}
|
||||
|
||||
void apply(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R> &outArg)
|
||||
{
|
||||
apply(inArgs..., outArg, GCompileArgs());
|
||||
}
|
||||
|
||||
|
||||
GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs)
|
||||
{
|
||||
GMetaArgs inMetas = { GMetaArg(inDescs)... };
|
||||
return GCompiledT(m_comp.compile(std::move(inMetas), GCompileArgs()));
|
||||
}
|
||||
|
||||
GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs, GCompileArgs &&args)
|
||||
{
|
||||
GMetaArgs inMetas = { GMetaArg(inDescs)... };
|
||||
return GCompiledT(m_comp.compile(std::move(inMetas), std::move(args)));
|
||||
}
|
||||
};
|
||||
|
||||
// Multiple (fixed) return value implementation. FIXME: How to avoid copy-paste?
|
||||
template<typename... R, typename... Args> class GComputationT<std::tuple<R...>(Args...)>
|
||||
{
|
||||
public:
|
||||
typedef std::function<std::tuple<R...>(Args...)> Gen;
|
||||
|
||||
class GCompiledT
|
||||
{
|
||||
private:
|
||||
friend class GComputationT<std::tuple<R...>(Args...)>;
|
||||
|
||||
cv::GCompiled m_comp;
|
||||
explicit GCompiledT(const cv::GCompiled &comp) : m_comp(comp) {}
|
||||
|
||||
public:
|
||||
GCompiledT() {}
|
||||
|
||||
void operator()(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R>&... outArgs)
|
||||
{
|
||||
m_comp(cv::gin(inArgs...), cv::gout(outArgs...));
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return static_cast<bool>(m_comp);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
typedef std::pair<GProtoArgs, GProtoArgs> Captured;
|
||||
|
||||
template<int... IIs>
|
||||
Captured capture(GProtoArgs &&args, const std::tuple<R...> &rr, detail::Seq<IIs...>)
|
||||
{
|
||||
return Captured(cv::GOut(std::get<IIs>(rr)...).m_args, args);
|
||||
}
|
||||
|
||||
Captured capture(const Gen& g, Args... args)
|
||||
{
|
||||
return capture(cv::GIn(args...).m_args, g(args...), typename detail::MkSeq<sizeof...(R)>::type());
|
||||
}
|
||||
|
||||
Captured m_capture;
|
||||
cv::GComputation m_comp;
|
||||
|
||||
public:
|
||||
GComputationT(const Gen &generator)
|
||||
: m_capture(capture(generator, detail::make_default<Args>()...))
|
||||
, m_comp(cv::GProtoInputArgs(std::move(m_capture.second)),
|
||||
cv::GProtoOutputArgs(std::move(m_capture.first)))
|
||||
{
|
||||
}
|
||||
|
||||
void apply(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R>&... outArgs,
|
||||
GCompileArgs &&args)
|
||||
{
|
||||
m_comp.apply(cv::gin(inArgs...), cv::gout(outArgs...), std::move(args));
|
||||
}
|
||||
|
||||
void apply(detail::ProtoToParamT<Args>... inArgs,
|
||||
detail::ProtoToParamT<R>&... outArgs)
|
||||
{
|
||||
apply(inArgs..., outArgs..., GCompileArgs());
|
||||
}
|
||||
|
||||
|
||||
GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs)
|
||||
{
|
||||
GMetaArgs inMetas = { GMetaArg(inDescs)... };
|
||||
return GCompiledT(m_comp.compile(std::move(inMetas), GCompileArgs()));
|
||||
}
|
||||
|
||||
GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs, GCompileArgs &&args)
|
||||
{
|
||||
GMetaArgs inMetas = { GMetaArg(inDescs)... };
|
||||
return GCompiledT(m_comp.compile(std::move(inMetas), std::move(args)));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cv
|
||||
#endif // !defined(GAPI_STANDALONE)
|
||||
#endif // OPENCV_GAPI_GTYPED_HPP
|
||||
+1769
File diff suppressed because it is too large
Load Diff
+717
@@ -0,0 +1,717 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019-2021 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_HPP
|
||||
#define OPENCV_GAPI_INFER_HPP
|
||||
|
||||
// FIXME: Inference API is currently only available in full mode
|
||||
#if !defined(GAPI_STANDALONE)
|
||||
|
||||
#include <functional>
|
||||
#include <string> // string
|
||||
#include <utility> // tuple
|
||||
#include <type_traits> // is_same, false_type
|
||||
|
||||
#include <opencv2/gapi/util/util.hpp> // all_satisfy
|
||||
#include <opencv2/gapi/util/any.hpp> // any<>
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelType[M], GBackend
|
||||
#include <opencv2/gapi/garg.hpp> // GArg
|
||||
#include <opencv2/gapi/gcommon.hpp> // CompileArgTag
|
||||
#include <opencv2/gapi/gmetaarg.hpp> // GMetaArg
|
||||
|
||||
namespace cv {
|
||||
|
||||
template<typename, typename> class GNetworkType;
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Infer ///////////////////////////////////////////////////////////////////////
|
||||
template<typename T>
|
||||
struct accepted_infer_types {
|
||||
static constexpr const auto value =
|
||||
std::is_same<typename std::decay<T>::type, cv::GMat>::value
|
||||
|| std::is_same<typename std::decay<T>::type, cv::GFrame>::value;
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
using valid_infer_types = all_satisfy<accepted_infer_types, Ts...>;
|
||||
|
||||
// Infer2 //////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename, typename>
|
||||
struct valid_infer2_types;
|
||||
|
||||
// Terminal case 1 (50/50 success)
|
||||
template<typename T>
|
||||
struct valid_infer2_types< std::tuple<cv::GMat>, std::tuple<T> > {
|
||||
// By default, Nets are limited to GMat argument types only
|
||||
// for infer2, every GMat argument may translate to either
|
||||
// GArray<GMat> or GArray<Rect>. GArray<> part is stripped
|
||||
// already at this point.
|
||||
static constexpr const auto value =
|
||||
std::is_same<typename std::decay<T>::type, cv::GMat>::value
|
||||
|| std::is_same<typename std::decay<T>::type, cv::Rect>::value;
|
||||
};
|
||||
|
||||
// Terminal case 2 (100% failure)
|
||||
template<typename... Ts>
|
||||
struct valid_infer2_types< std::tuple<>, std::tuple<Ts...> >
|
||||
: public std::false_type {
|
||||
};
|
||||
|
||||
// Terminal case 3 (100% failure)
|
||||
template<typename... Ns>
|
||||
struct valid_infer2_types< std::tuple<Ns...>, std::tuple<> >
|
||||
: public std::false_type {
|
||||
};
|
||||
|
||||
// Recursion -- generic
|
||||
template<typename... Ns, typename T, typename...Ts>
|
||||
struct valid_infer2_types< std::tuple<cv::GMat,Ns...>, std::tuple<T,Ts...> > {
|
||||
static constexpr const auto value =
|
||||
valid_infer2_types< std::tuple<cv::GMat>, std::tuple<T> >::value
|
||||
&& valid_infer2_types< std::tuple<Ns...>, std::tuple<Ts...> >::value;
|
||||
};
|
||||
|
||||
// Struct stores network input/output names.
|
||||
// Used by infer<Generic>
|
||||
struct InOutInfo
|
||||
{
|
||||
std::vector<std::string> in_names;
|
||||
std::vector<std::string> out_names;
|
||||
};
|
||||
|
||||
template <typename OutT>
|
||||
class GInferOutputsTyped
|
||||
{
|
||||
public:
|
||||
GInferOutputsTyped() = default;
|
||||
GInferOutputsTyped(std::shared_ptr<cv::GCall> call)
|
||||
: m_priv(std::make_shared<Priv>(std::move(call)))
|
||||
{
|
||||
}
|
||||
|
||||
OutT at(const std::string& name)
|
||||
{
|
||||
auto it = m_priv->blobs.find(name);
|
||||
if (it == m_priv->blobs.end()) {
|
||||
// FIXME: Avoid modifying GKernel
|
||||
auto shape = cv::detail::GTypeTraits<OutT>::shape;
|
||||
auto kind = cv::detail::GTypeTraits<OutT>::op_kind;
|
||||
m_priv->call->kernel().outShapes.push_back(shape);
|
||||
m_priv->call->kernel().outCtors.emplace_back(cv::detail::GObtainCtor<OutT>::get());
|
||||
m_priv->call->kernel().outKinds.emplace_back(kind);
|
||||
auto out_idx = static_cast<int>(m_priv->blobs.size());
|
||||
it = m_priv->blobs.emplace(name,
|
||||
cv::detail::Yield<OutT>::yield(*(m_priv->call), out_idx)).first;
|
||||
m_priv->info->out_names.push_back(name);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
private:
|
||||
struct Priv
|
||||
{
|
||||
Priv(std::shared_ptr<cv::GCall> c)
|
||||
: call(std::move(c)), info(cv::util::any_cast<InOutInfo>(&call->params()))
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<cv::GCall> call;
|
||||
InOutInfo* info = nullptr;
|
||||
std::unordered_map<std::string, OutT> blobs;
|
||||
};
|
||||
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
class GInferInputsTyped
|
||||
{
|
||||
public:
|
||||
GInferInputsTyped()
|
||||
: m_priv(std::make_shared<Priv>())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
GInferInputsTyped<Ts...>& setInput(const std::string& name, U in)
|
||||
{
|
||||
m_priv->blobs.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(name),
|
||||
std::forward_as_tuple(in));
|
||||
return *this;
|
||||
}
|
||||
|
||||
using StorageT = cv::util::variant<Ts...>;
|
||||
StorageT& operator[](const std::string& name) {
|
||||
return m_priv->blobs[name];
|
||||
}
|
||||
|
||||
using Map = std::unordered_map<std::string, StorageT>;
|
||||
const Map& getBlobs() const {
|
||||
return m_priv->blobs;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Priv
|
||||
{
|
||||
std::unordered_map<std::string, StorageT> blobs;
|
||||
};
|
||||
|
||||
std::shared_ptr<Priv> m_priv;
|
||||
};
|
||||
|
||||
template<typename InferT>
|
||||
std::shared_ptr<cv::GCall> makeCall(const std::string &tag,
|
||||
std::vector<cv::GArg> &&args,
|
||||
std::vector<std::string> &&names,
|
||||
cv::GKinds &&kinds) {
|
||||
auto call = std::make_shared<cv::GCall>(GKernel{
|
||||
InferT::id(),
|
||||
tag,
|
||||
InferT::getOutMeta,
|
||||
{}, // outShape will be filled later
|
||||
std::move(kinds),
|
||||
{}, // outCtors will be filled later
|
||||
{}, // outKinds will be filled later
|
||||
});
|
||||
|
||||
call->setArgs(std::move(args));
|
||||
call->params() = cv::detail::InOutInfo{std::move(names), {}};
|
||||
|
||||
return call;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// TODO: maybe tuple_wrap_helper from util.hpp may help with this.
|
||||
// Multiple-return-value network definition (specialized base class)
|
||||
template<typename K, typename... R, typename... Args>
|
||||
class GNetworkType<K, std::function<std::tuple<R...>(Args...)> >
|
||||
{
|
||||
public:
|
||||
using InArgs = std::tuple<Args...>;
|
||||
using OutArgs = std::tuple<R...>;
|
||||
|
||||
using Result = OutArgs;
|
||||
using API = std::function<Result(Args...)>;
|
||||
|
||||
using ResultL = std::tuple< cv::GArray<R>... >;
|
||||
};
|
||||
|
||||
// Single-return-value network definition (specialized base class)
|
||||
template<typename K, typename R, typename... Args>
|
||||
class GNetworkType<K, std::function<R(Args...)> >
|
||||
{
|
||||
public:
|
||||
using InArgs = std::tuple<Args...>;
|
||||
using OutArgs = std::tuple<R>;
|
||||
|
||||
using Result = R;
|
||||
using API = std::function<R(Args...)>;
|
||||
|
||||
using ResultL = cv::GArray<R>;
|
||||
};
|
||||
|
||||
// InferAPI: Accepts either GMat or GFrame for very individual network's input
|
||||
template<class Net, class... Ts>
|
||||
struct InferAPI {
|
||||
using type = typename std::enable_if
|
||||
< detail::valid_infer_types<Ts...>::value
|
||||
&& std::tuple_size<typename Net::InArgs>::value == sizeof...(Ts)
|
||||
, std::function<typename Net::Result(Ts...)>
|
||||
>::type;
|
||||
};
|
||||
|
||||
// InferAPIRoi: Accepts a rectangle and either GMat or GFrame
|
||||
template<class Net, class T>
|
||||
struct InferAPIRoi {
|
||||
using type = typename std::enable_if
|
||||
< detail::valid_infer_types<T>::value
|
||||
&& std::tuple_size<typename Net::InArgs>::value == 1u
|
||||
, std::function<typename Net::Result(cv::GOpaque<cv::Rect>, T)>
|
||||
>::type;
|
||||
};
|
||||
|
||||
// InferAPIList: Accepts a list of rectangles and list of GMat/GFrames;
|
||||
// crops every input.
|
||||
template<class Net, class... Ts>
|
||||
struct InferAPIList {
|
||||
using type = typename std::enable_if
|
||||
< detail::valid_infer_types<Ts...>::value
|
||||
&& std::tuple_size<typename Net::InArgs>::value == sizeof...(Ts)
|
||||
, std::function<typename Net::ResultL(cv::GArray<cv::Rect>, Ts...)>
|
||||
>::type;
|
||||
};
|
||||
|
||||
// APIList2 is also template to allow different calling options
|
||||
// (GArray<cv::Rect> vs GArray<cv::GMat> per input)
|
||||
template<class Net, typename T, class... Ts>
|
||||
struct InferAPIList2 {
|
||||
using type = typename std::enable_if
|
||||
< detail::valid_infer_types<T>::value &&
|
||||
cv::detail::valid_infer2_types< typename Net::InArgs
|
||||
, std::tuple<Ts...> >::value,
|
||||
std::function<typename Net::ResultL(T, cv::GArray<Ts>...)>
|
||||
>::type;
|
||||
};
|
||||
|
||||
// Base "Infer" kernel. Note - for whatever network, kernel ID
|
||||
// is always the same. Different inference calls are distinguished by
|
||||
// network _tag_ (an extra field in GCall)
|
||||
//
|
||||
// getOutMeta is a stub callback collected by G-API kernel subsystem
|
||||
// automatically. This is a rare case when this callback is defined by
|
||||
// a particular backend, not by a network itself.
|
||||
struct GInferBase {
|
||||
static constexpr const char * id() {
|
||||
return "org.opencv.dnn.infer"; // Universal stub
|
||||
}
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) {
|
||||
return GMetaArgs{}; // One more universal stub
|
||||
}
|
||||
};
|
||||
|
||||
// Base "InferROI" kernel.
|
||||
// All notes from "Infer" kernel apply here as well.
|
||||
struct GInferROIBase {
|
||||
static constexpr const char * id() {
|
||||
return "org.opencv.dnn.infer-roi"; // Universal stub
|
||||
}
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) {
|
||||
return GMetaArgs{}; // One more universal stub
|
||||
}
|
||||
};
|
||||
|
||||
// Base "Infer list" kernel.
|
||||
// All notes from "Infer" kernel apply here as well.
|
||||
struct GInferListBase {
|
||||
static constexpr const char * id() {
|
||||
return "org.opencv.dnn.infer-roi-list-1"; // Universal stub
|
||||
}
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) {
|
||||
return GMetaArgs{}; // One more universal stub
|
||||
}
|
||||
};
|
||||
|
||||
// Base "Infer list 2" kernel.
|
||||
// All notes from "Infer" kernel apply here as well.
|
||||
struct GInferList2Base {
|
||||
static constexpr const char * id() {
|
||||
return "org.opencv.dnn.infer-roi-list-2"; // Universal stub
|
||||
}
|
||||
static GMetaArgs getOutMeta(const GMetaArgs &, const GArgs &) {
|
||||
return GMetaArgs{}; // One more universal stub
|
||||
}
|
||||
};
|
||||
|
||||
// A generic inference kernel. API (::on()) is fully defined by the Net
|
||||
// template parameter.
|
||||
// Acts as a regular kernel in graph (via KernelTypeMedium).
|
||||
template<typename Net, typename... Args>
|
||||
struct GInfer final
|
||||
: public GInferBase
|
||||
, public detail::KernelTypeMedium< GInfer<Net, Args...>
|
||||
, typename InferAPI<Net, Args...>::type > {
|
||||
using GInferBase::getOutMeta; // FIXME: name lookup conflict workaround?
|
||||
|
||||
static constexpr const char* tag() { return Net::tag(); }
|
||||
};
|
||||
|
||||
// A specific roi-inference kernel. API (::on()) is fixed here and
|
||||
// verified against Net.
|
||||
template<typename Net, typename T>
|
||||
struct GInferROI final
|
||||
: public GInferROIBase
|
||||
, public detail::KernelTypeMedium< GInferROI<Net, T>
|
||||
, typename InferAPIRoi<Net, T>::type > {
|
||||
using GInferROIBase::getOutMeta; // FIXME: name lookup conflict workaround?
|
||||
|
||||
static constexpr const char* tag() { return Net::tag(); }
|
||||
};
|
||||
|
||||
|
||||
// A generic roi-list inference kernel. API (::on()) is derived from
|
||||
// the Net template parameter (see more in infer<> overload).
|
||||
template<typename Net, typename... Args>
|
||||
struct GInferList final
|
||||
: public GInferListBase
|
||||
, public detail::KernelTypeMedium< GInferList<Net, Args...>
|
||||
, typename InferAPIList<Net, Args...>::type > {
|
||||
using GInferListBase::getOutMeta; // FIXME: name lookup conflict workaround?
|
||||
|
||||
static constexpr const char* tag() { return Net::tag(); }
|
||||
};
|
||||
|
||||
// An even more generic roi-list inference kernel. API (::on()) is
|
||||
// derived from the Net template parameter (see more in infer<>
|
||||
// overload).
|
||||
// Takes an extra variadic template list to reflect how this network
|
||||
// was called (with Rects or GMats as array parameters)
|
||||
template<typename Net, typename T, typename... Args>
|
||||
struct GInferList2 final
|
||||
: public GInferList2Base
|
||||
, public detail::KernelTypeMedium< GInferList2<Net, T, Args...>
|
||||
, typename InferAPIList2<Net, T, Args...>::type > {
|
||||
using GInferList2Base::getOutMeta; // FIXME: name lookup conflict workaround?
|
||||
|
||||
static constexpr const char* tag() { return Net::tag(); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief G-API object used to collect network inputs
|
||||
*/
|
||||
using GInferInputs = cv::detail::GInferInputsTyped<cv::GMat, cv::GFrame>;
|
||||
|
||||
/**
|
||||
* @brief G-API object used to collect the list of network inputs
|
||||
*/
|
||||
using GInferListInputs = cv::detail::GInferInputsTyped<cv::GArray<cv::GMat>, cv::GArray<cv::Rect>>;
|
||||
|
||||
/**
|
||||
* @brief G-API object used to collect network outputs
|
||||
*/
|
||||
using GInferOutputs = cv::detail::GInferOutputsTyped<cv::GMat>;
|
||||
|
||||
/**
|
||||
* @brief G-API object used to collect the list of network outputs
|
||||
*/
|
||||
using GInferListOutputs = cv::detail::GInferOutputsTyped<cv::GArray<cv::GMat>>;
|
||||
|
||||
namespace detail {
|
||||
void inline unpackBlobs(const cv::GInferInputs::Map& blobs,
|
||||
std::vector<cv::GArg>& args,
|
||||
std::vector<std::string>& names,
|
||||
cv::GKinds& kinds)
|
||||
{
|
||||
for (auto&& p : blobs) {
|
||||
names.emplace_back(p.first);
|
||||
switch (p.second.index()) {
|
||||
case cv::GInferInputs::StorageT::index_of<cv::GMat>():
|
||||
args.emplace_back(cv::util::get<cv::GMat>(p.second));
|
||||
kinds.emplace_back(cv::detail::OpaqueKind::CV_MAT);
|
||||
break;
|
||||
case cv::GInferInputs::StorageT::index_of<cv::GFrame>():
|
||||
args.emplace_back(cv::util::get<cv::GFrame>(p.second));
|
||||
kinds.emplace_back(cv::detail::OpaqueKind::CV_UNKNOWN);
|
||||
break;
|
||||
default:
|
||||
GAPI_Error("InternalError");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename InferType>
|
||||
struct InferROITraits;
|
||||
|
||||
template <>
|
||||
struct InferROITraits<GInferROIBase>
|
||||
{
|
||||
using outType = cv::GInferOutputs;
|
||||
using inType = cv::GOpaque<cv::Rect>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct InferROITraits<GInferListBase>
|
||||
{
|
||||
using outType = cv::GInferListOutputs;
|
||||
using inType = cv::GArray<cv::Rect>;
|
||||
};
|
||||
|
||||
template<typename InferType>
|
||||
typename InferROITraits<InferType>::outType
|
||||
inferGenericROI(const std::string& tag,
|
||||
const typename InferROITraits<InferType>::inType& in,
|
||||
const cv::GInferInputs& inputs)
|
||||
{
|
||||
std::vector<cv::GArg> args;
|
||||
std::vector<std::string> names;
|
||||
cv::GKinds kinds;
|
||||
|
||||
args.emplace_back(in);
|
||||
kinds.emplace_back(cv::detail::OpaqueKind::CV_RECT);
|
||||
|
||||
unpackBlobs(inputs.getBlobs(), args, names, kinds);
|
||||
|
||||
auto call = cv::detail::makeCall<InferType>(tag,
|
||||
std::move(args),
|
||||
std::move(names),
|
||||
std::move(kinds));
|
||||
|
||||
return {std::move(call)};
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace cv
|
||||
|
||||
// FIXME: Probably the <API> signature makes a function/tuple/function round-trip
|
||||
#define G_API_NET(Class, API, Tag) \
|
||||
struct Class final: public cv::GNetworkType<Class, std::function API> { \
|
||||
static constexpr const char * tag() { return Tag; } \
|
||||
}
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
|
||||
/** @brief Calculates response for the specified network (template
|
||||
* parameter) for the specified region in the source image.
|
||||
* Currently expects a single-input network only.
|
||||
*
|
||||
* @tparam A network type defined with G_API_NET() macro.
|
||||
* @param in input image where to take ROI from.
|
||||
* @param roi an object describing the region of interest
|
||||
* in the source image. May be calculated in the same graph dynamically.
|
||||
* @return an object of return type as defined in G_API_NET().
|
||||
* If a network has multiple return values (defined with a tuple), a tuple of
|
||||
* objects of appropriate type is returned.
|
||||
* @sa G_API_NET()
|
||||
*/
|
||||
template<typename Net, typename T>
|
||||
typename Net::Result infer(cv::GOpaque<cv::Rect> roi, T in) {
|
||||
return GInferROI<Net, T>::on(roi, in);
|
||||
}
|
||||
|
||||
/** @brief Calculates responses for the specified network (template
|
||||
* parameter) for every region in the source image.
|
||||
*
|
||||
* @tparam A network type defined with G_API_NET() macro.
|
||||
* @param roi a list of rectangles describing regions of interest
|
||||
* in the source image. Usually an output of object detector or tracker.
|
||||
* @param args network's input parameters as specified in G_API_NET() macro.
|
||||
* NOTE: verified to work reliably with 1-input topologies only.
|
||||
* @return a list of objects of return type as defined in G_API_NET().
|
||||
* If a network has multiple return values (defined with a tuple), a tuple of
|
||||
* GArray<> objects is returned with the appropriate types inside.
|
||||
* @sa G_API_NET()
|
||||
*/
|
||||
template<typename Net, typename... Args>
|
||||
typename Net::ResultL infer(cv::GArray<cv::Rect> roi, Args&&... args) {
|
||||
return GInferList<Net, Args...>::on(roi, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/** @brief Calculates responses for the specified network (template
|
||||
* parameter) for every region in the source image, extended version.
|
||||
*
|
||||
* @tparam A network type defined with G_API_NET() macro.
|
||||
* @param image A source image containing regions of interest
|
||||
* @param args GArray<> objects of cv::Rect or cv::GMat, one per every
|
||||
* network input:
|
||||
* - If a cv::GArray<cv::Rect> is passed, the appropriate
|
||||
* regions are taken from `image` and preprocessed to this particular
|
||||
* network input;
|
||||
* - If a cv::GArray<cv::GMat> is passed, the underlying data traited
|
||||
* as tensor (no automatic preprocessing happen).
|
||||
* @return a list of objects of return type as defined in G_API_NET().
|
||||
* If a network has multiple return values (defined with a tuple), a tuple of
|
||||
* GArray<> objects is returned with the appropriate types inside.
|
||||
* @sa G_API_NET()
|
||||
*/
|
||||
|
||||
template<typename Net, typename T, typename... Args>
|
||||
typename Net::ResultL infer2(T image, cv::GArray<Args>... args) {
|
||||
// FIXME: Declared as "2" because in the current form it steals
|
||||
// overloads from the regular infer
|
||||
return GInferList2<Net, T, Args...>::on(image, args...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates response for the specified network (template
|
||||
* parameter) given the input data.
|
||||
*
|
||||
* @tparam A network type defined with G_API_NET() macro.
|
||||
* @param args network's input parameters as specified in G_API_NET() macro.
|
||||
* @return an object of return type as defined in G_API_NET().
|
||||
* If a network has multiple return values (defined with a tuple), a tuple of
|
||||
* objects of appropriate type is returned.
|
||||
* @sa G_API_NET()
|
||||
*/
|
||||
template<typename Net, typename... Args>
|
||||
typename Net::Result infer(Args&&... args) {
|
||||
return GInfer<Net, Args...>::on(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Generic network type: input and output layers are configured dynamically at runtime
|
||||
*
|
||||
* Unlike the network types defined with G_API_NET macro, this one
|
||||
* doesn't fix number of network inputs and outputs at the compilation stage
|
||||
* thus providing user with an opportunity to program them in runtime.
|
||||
*/
|
||||
struct Generic { };
|
||||
|
||||
/**
|
||||
* @brief Calculates response for generic network
|
||||
*
|
||||
* @param tag a network tag
|
||||
* @param inputs networks's inputs
|
||||
* @return a GInferOutputs
|
||||
*/
|
||||
template<typename T = Generic> cv::GInferOutputs
|
||||
infer(const std::string& tag, const cv::GInferInputs& inputs)
|
||||
{
|
||||
std::vector<cv::GArg> args;
|
||||
std::vector<std::string> names;
|
||||
cv::GKinds kinds;
|
||||
|
||||
cv::detail::unpackBlobs(inputs.getBlobs(), args, names, kinds);
|
||||
|
||||
auto call = cv::detail::makeCall<GInferBase>(tag,
|
||||
std::move(args),
|
||||
std::move(names),
|
||||
std::move(kinds));
|
||||
|
||||
return cv::GInferOutputs{std::move(call)};
|
||||
}
|
||||
|
||||
/** @brief Calculates response for the generic network
|
||||
* for the specified region in the source image.
|
||||
* Currently expects a single-input network only.
|
||||
*
|
||||
* @param tag a network tag
|
||||
* @param roi a an object describing the region of interest
|
||||
* in the source image. May be calculated in the same graph dynamically.
|
||||
* @param inputs networks's inputs
|
||||
* @return a cv::GInferOutputs
|
||||
*/
|
||||
template<typename T = Generic> cv::GInferOutputs
|
||||
infer(const std::string& tag, const cv::GOpaque<cv::Rect>& roi, const cv::GInferInputs& inputs)
|
||||
{
|
||||
return cv::detail::inferGenericROI<GInferROIBase>(tag, roi, inputs);
|
||||
}
|
||||
|
||||
/** @brief Calculates responses for the specified network
|
||||
* for every region in the source image.
|
||||
*
|
||||
* @param tag a network tag
|
||||
* @param rois a list of rectangles describing regions of interest
|
||||
* in the source image. Usually an output of object detector or tracker.
|
||||
* @param inputs networks's inputs
|
||||
* @return a cv::GInferListOutputs
|
||||
*/
|
||||
template<typename T = Generic> cv::GInferListOutputs
|
||||
infer(const std::string& tag, const cv::GArray<cv::Rect>& rois, const cv::GInferInputs& inputs)
|
||||
{
|
||||
return cv::detail::inferGenericROI<GInferListBase>(tag, rois, inputs);
|
||||
}
|
||||
|
||||
/** @brief Calculates responses for the specified network
|
||||
* for every region in the source image, extended version.
|
||||
*
|
||||
* @param tag a network tag
|
||||
* @param in a source image containing regions of interest.
|
||||
* @param inputs networks's inputs
|
||||
* @return a cv::GInferListOutputs
|
||||
*/
|
||||
template<typename T = Generic, typename Input>
|
||||
typename std::enable_if<cv::detail::accepted_infer_types<Input>::value, cv::GInferListOutputs>::type
|
||||
infer2(const std::string& tag,
|
||||
const Input& in,
|
||||
const cv::GInferListInputs& inputs)
|
||||
{
|
||||
std::vector<cv::GArg> args;
|
||||
std::vector<std::string> names;
|
||||
cv::GKinds kinds;
|
||||
|
||||
args.emplace_back(in);
|
||||
auto k = cv::detail::GOpaqueTraits<Input>::kind;
|
||||
kinds.emplace_back(k);
|
||||
|
||||
for (auto&& p : inputs.getBlobs()) {
|
||||
names.emplace_back(p.first);
|
||||
switch (p.second.index()) {
|
||||
case cv::GInferListInputs::StorageT::index_of<cv::GArray<cv::GMat>>():
|
||||
args.emplace_back(cv::util::get<cv::GArray<cv::GMat>>(p.second));
|
||||
kinds.emplace_back(cv::detail::OpaqueKind::CV_MAT);
|
||||
break;
|
||||
case cv::GInferListInputs::StorageT::index_of<cv::GArray<cv::Rect>>():
|
||||
args.emplace_back(cv::util::get<cv::GArray<cv::Rect>>(p.second));
|
||||
kinds.emplace_back(cv::detail::OpaqueKind::CV_RECT);
|
||||
break;
|
||||
default:
|
||||
GAPI_Error("InternalError");
|
||||
}
|
||||
}
|
||||
|
||||
auto call = cv::detail::makeCall<GInferList2Base>(tag,
|
||||
std::move(args),
|
||||
std::move(names),
|
||||
std::move(kinds));
|
||||
|
||||
return cv::GInferListOutputs{std::move(call)};
|
||||
}
|
||||
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // GAPI_STANDALONE
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
|
||||
// Note: the below code _is_ part of STANDALONE build,
|
||||
// just to make our compiler code compileable.
|
||||
|
||||
// A type-erased form of network parameters.
|
||||
// Similar to how a type-erased GKernel is represented and used.
|
||||
/// @private
|
||||
struct GAPI_EXPORTS_W_SIMPLE GNetParam {
|
||||
std::string tag; // FIXME: const?
|
||||
GBackend backend; // Specifies the execution model
|
||||
util::any params; // Backend-interpreted parameter structure
|
||||
};
|
||||
|
||||
/** \addtogroup gapi_compile_args
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief A container class for network configurations. Similar to
|
||||
* GKernelPackage. Use cv::gapi::networks() to construct this object.
|
||||
*
|
||||
* @sa cv::gapi::networks
|
||||
*/
|
||||
struct GAPI_EXPORTS_W_SIMPLE GNetPackage {
|
||||
GAPI_WRAP GNetPackage() = default;
|
||||
GAPI_WRAP explicit GNetPackage(std::vector<GNetParam> nets);
|
||||
explicit GNetPackage(std::initializer_list<GNetParam> ii);
|
||||
std::vector<GBackend> backends() const;
|
||||
std::vector<GNetParam> networks;
|
||||
};
|
||||
/** @} gapi_compile_args */
|
||||
} // namespace gapi
|
||||
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
gapi::GNetParam strip(T&& t) {
|
||||
return gapi::GNetParam { t.tag()
|
||||
, t.backend()
|
||||
, t.params()
|
||||
};
|
||||
}
|
||||
|
||||
template<> struct CompileArgTag<cv::gapi::GNetPackage> {
|
||||
static const char* tag() { return "gapi.net_package"; }
|
||||
};
|
||||
|
||||
} // namespace cv::detail
|
||||
|
||||
namespace gapi {
|
||||
template<typename... Args>
|
||||
cv::gapi::GNetPackage networks(Args&&... args) {
|
||||
return cv::gapi::GNetPackage({ cv::detail::strip(args)... });
|
||||
}
|
||||
|
||||
inline cv::gapi::GNetPackage& operator += ( cv::gapi::GNetPackage& lhs,
|
||||
const cv::gapi::GNetPackage& rhs) {
|
||||
lhs.networks.reserve(lhs.networks.size() + rhs.networks.size());
|
||||
lhs.networks.insert(lhs.networks.end(), rhs.networks.begin(), rhs.networks.end());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_HPP
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_BINDINGS_IE_HPP
|
||||
#define OPENCV_GAPI_INFER_BINDINGS_IE_HPP
|
||||
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include "opencv2/gapi/own/exports.hpp" // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/infer/ie.hpp> // Params
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace ie {
|
||||
|
||||
// NB: Used by python wrapper
|
||||
// This class can be marked as SIMPLE, because it's implemented as pimpl
|
||||
class GAPI_EXPORTS_W_SIMPLE PyParams {
|
||||
public:
|
||||
GAPI_WRAP
|
||||
PyParams() = default;
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &weights,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& constInput(const std::string &layer_name,
|
||||
const cv::Mat &data,
|
||||
TraitAs hint = TraitAs::TENSOR);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgNumRequests(size_t nireq);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgBatchSize(const size_t size);
|
||||
|
||||
GBackend backend() const;
|
||||
std::string tag() const;
|
||||
cv::util::any params() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Params<cv::gapi::Generic>> m_priv;
|
||||
};
|
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &weights,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &device);
|
||||
} // namespace ie
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_BINDINGS_IE_HPP
|
||||
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level
|
||||
// directory of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_BINDINGS_ONNX_HPP
|
||||
#define OPENCV_GAPI_INFER_BINDINGS_ONNX_HPP
|
||||
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/infer/onnx.hpp> // Params
|
||||
#include "opencv2/gapi/own/exports.hpp" // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace onnx {
|
||||
|
||||
// NB: Used by python wrapper
|
||||
// This class can be marked as SIMPLE, because it's implemented as pimpl
|
||||
class GAPI_EXPORTS_W_SIMPLE PyParams {
|
||||
public:
|
||||
GAPI_WRAP
|
||||
PyParams() = default;
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams(const std::string& tag, const std::string& model_path);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgMeanStd(const std::string &layer_name,
|
||||
const cv::Scalar &m,
|
||||
const cv::Scalar &s);
|
||||
GAPI_WRAP
|
||||
PyParams& cfgNormalize(const std::string &layer_name, bool flag);
|
||||
|
||||
GBackend backend() const;
|
||||
std::string tag() const;
|
||||
cv::util::any params() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Params<cv::gapi::Generic>> m_priv;
|
||||
};
|
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string& tag, const std::string& model_path);
|
||||
|
||||
} // namespace onnx
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_BINDINGS_ONNX_HPP
|
||||
Vendored
+128
@@ -0,0 +1,128 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2023 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_BINDINGS_OV_HPP
|
||||
#define OPENCV_GAPI_INFER_BINDINGS_OV_HPP
|
||||
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include "opencv2/gapi/own/exports.hpp" // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/infer/ov.hpp> // Params
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace ov {
|
||||
|
||||
// NB: Used by python wrapper
|
||||
// This class can be marked as SIMPLE, because it's implemented as pimpl
|
||||
class GAPI_EXPORTS_W_SIMPLE PyParams {
|
||||
public:
|
||||
GAPI_WRAP
|
||||
PyParams() = default;
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams(const std::string &tag,
|
||||
const std::string &model_path,
|
||||
const std::string &bin_path,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams(const std::string &tag,
|
||||
const std::string &blob_path,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgPluginConfig(
|
||||
const std::map<std::string, std::string> &config);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgInputTensorLayout(std::string tensor_layout);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgInputTensorLayout(
|
||||
std::map<std::string, std::string> layout_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgInputModelLayout(std::string tensor_layout);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgInputModelLayout(
|
||||
std::map<std::string, std::string> layout_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputTensorLayout(std::string tensor_layout);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputTensorLayout(
|
||||
std::map<std::string, std::string> layout_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputModelLayout(std::string tensor_layout);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputModelLayout(
|
||||
std::map<std::string, std::string> layout_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputTensorPrecision(int precision);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgOutputTensorPrecision(
|
||||
std::map<std::string, int> precision_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgReshape(std::vector<size_t> new_shape);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgReshape(
|
||||
std::map<std::string, std::vector<size_t>> new_shape_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgNumRequests(const size_t nireq);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgMean(std::vector<float> mean_values);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgMean(
|
||||
std::map<std::string, std::vector<float>> mean_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgScale(std::vector<float> scale_values);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgScale(
|
||||
std::map<std::string, std::vector<float>> scale_map);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgResize(int interpolation);
|
||||
|
||||
GAPI_WRAP
|
||||
PyParams& cfgResize(std::map<std::string, int> interpolation);
|
||||
|
||||
GBackend backend() const;
|
||||
std::string tag() const;
|
||||
cv::util::any params() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Params<cv::gapi::Generic>> m_priv;
|
||||
};
|
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag,
|
||||
const std::string &model_path,
|
||||
const std::string &weights,
|
||||
const std::string &device);
|
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag,
|
||||
const std::string &bin_path,
|
||||
const std::string &device);
|
||||
} // namespace ov
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_BINDINGS_OV_HPP
|
||||
+711
@@ -0,0 +1,711 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2019-2023 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_IE_HPP
|
||||
#define OPENCV_GAPI_INFER_IE_HPP
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <tuple> // tuple, tuple_size
|
||||
#include <map>
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
|
||||
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/infer.hpp> // Generic
|
||||
#include <opencv2/gapi/streaming/onevpl/accel_types.hpp> // Preproc Dev & Ctx
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
// FIXME: introduce a new sub-namespace for NN?
|
||||
|
||||
/**
|
||||
* @brief This namespace contains G-API OpenVINO backend functions,
|
||||
* structures, and symbols.
|
||||
*/
|
||||
namespace ie {
|
||||
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
|
||||
/**
|
||||
* Specifies how G-API and IE should trait input data
|
||||
*
|
||||
* In OpenCV, the same cv::Mat is used to represent both
|
||||
* image and tensor data. Sometimes those are hardly distinguishable,
|
||||
* so this extra parameter is used to give G-API a hint.
|
||||
*
|
||||
* This hint controls how G-API reinterprets the data when converting
|
||||
* it to IE Blob format (and which layout/etc is assigned to this data).
|
||||
*/
|
||||
enum class TraitAs: int
|
||||
{
|
||||
TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor and passes dimensions as-is
|
||||
IMAGE //!< G-API traits an associated cv::Mat as an image so creates an "image" blob (NCHW/NHWC, etc)
|
||||
};
|
||||
|
||||
using IEConfig = std::map<std::string, std::string>;
|
||||
|
||||
enum InferMode {Sync, Async};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
using AttrMap = std::map<std::string, T>;
|
||||
// NB: This type is used to hold in/out layers
|
||||
// attributes such as precision, layout, shape etc.
|
||||
//
|
||||
// User can provide attributes either:
|
||||
// 1. cv::util::monostate - No value specified explicitly.
|
||||
// 2. Attr - value specified explicitly that should be broadcasted to all layers.
|
||||
// 3. AttrMap[str->T] - map specifies value for particular layer.
|
||||
template <typename Attr>
|
||||
using LayerVariantAttr = cv::util::variant< cv::util::monostate
|
||||
, AttrMap<Attr>
|
||||
, Attr>;
|
||||
|
||||
struct ParamDesc {
|
||||
std::string model_path;
|
||||
std::string weights_path;
|
||||
std::string device_id;
|
||||
|
||||
std::vector<std::string> input_names;
|
||||
std::vector<std::string> output_names;
|
||||
|
||||
using ConstInput = std::pair<cv::Mat, TraitAs>;
|
||||
std::unordered_map<std::string, ConstInput> const_inputs;
|
||||
|
||||
std::size_t num_in;
|
||||
std::size_t num_out;
|
||||
|
||||
enum class Kind {Load, Import};
|
||||
Kind kind;
|
||||
bool is_generic;
|
||||
IEConfig config;
|
||||
|
||||
std::map<std::string, std::vector<std::size_t>> reshape_table;
|
||||
std::unordered_set<std::string> layer_names_to_reshape;
|
||||
|
||||
// NB: Number of asyncrhonious infer requests
|
||||
size_t nireq;
|
||||
|
||||
// NB: An optional config to setup RemoteContext for IE
|
||||
cv::util::any context_config;
|
||||
|
||||
// NB: batch_size can't be equal to 1 by default, because some of models
|
||||
// have 2D (Layout::NC) input and if the first dimension not equal to 1
|
||||
// net.setBatchSize(1) will overwrite it.
|
||||
cv::optional<size_t> batch_size;
|
||||
|
||||
cv::optional<cv::gapi::wip::onevpl::Device> vpl_preproc_device;
|
||||
cv::optional<cv::gapi::wip::onevpl::Context> vpl_preproc_ctx;
|
||||
|
||||
InferMode mode;
|
||||
|
||||
using PrecisionT = int;
|
||||
using PrecisionMapT = std::unordered_map<std::string, PrecisionT>;
|
||||
// NB: This parameter can contain:
|
||||
// 1. cv::util::monostate - Don't specify precision, but use default from IR/Blob.
|
||||
// 2. PrecisionT (CV_8U, CV_32F, ...) - Specifies precision for all output layers.
|
||||
// 3. PrecisionMapT ({{"layer0", CV_32F}, {"layer1", CV_16F}} - Specifies precision for certain output layer.
|
||||
// cv::util::monostate is default value that means precision wasn't specified.
|
||||
using PrecisionVariantT = cv::util::variant<cv::util::monostate,
|
||||
PrecisionT,
|
||||
PrecisionMapT>;
|
||||
|
||||
PrecisionVariantT output_precision;
|
||||
LayerVariantAttr<std::string> input_layout;
|
||||
LayerVariantAttr<std::string> output_layout;
|
||||
LayerVariantAttr<int> interpolation;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
// FIXME: this is probably a shared (reusable) thing
|
||||
template<typename Net>
|
||||
struct PortCfg {
|
||||
using In = std::array
|
||||
< std::string
|
||||
, std::tuple_size<typename Net::InArgs>::value >;
|
||||
using Out = std::array
|
||||
< std::string
|
||||
, std::tuple_size<typename Net::OutArgs>::value >;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This structure provides functions
|
||||
* that fill inference parameters for "OpenVINO Toolkit" model.
|
||||
*/
|
||||
template<typename Net> class Params {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on model information and specifies default values for other
|
||||
inference description parameters. Model is loaded and compiled using "OpenVINO Toolkit".
|
||||
|
||||
@param model Path to topology IR (.xml file).
|
||||
@param weights Path to weights (.bin file).
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &model,
|
||||
const std::string &weights,
|
||||
const std::string &device)
|
||||
: desc{ model, weights, device, {}, {}, {}
|
||||
, std::tuple_size<typename Net::InArgs>::value // num_in
|
||||
, std::tuple_size<typename Net::OutArgs>::value // num_out
|
||||
, detail::ParamDesc::Kind::Load
|
||||
, false
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, 1u
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, InferMode::Async
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, {} } {
|
||||
};
|
||||
|
||||
/** @overload
|
||||
Use this constructor to work with pre-compiled network.
|
||||
Model is imported from a pre-compiled blob.
|
||||
|
||||
@param model Path to model.
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &model,
|
||||
const std::string &device)
|
||||
: desc{ model, {}, device, {}, {}, {}
|
||||
, std::tuple_size<typename Net::InArgs>::value // num_in
|
||||
, std::tuple_size<typename Net::OutArgs>::value // num_out
|
||||
, detail::ParamDesc::Kind::Import
|
||||
, false
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, 1u
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, InferMode::Async
|
||||
, {}
|
||||
, {}
|
||||
, {}
|
||||
, {} } {
|
||||
};
|
||||
|
||||
/** @brief Specifies sequence of network input layers names for inference.
|
||||
|
||||
The function is used to associate cv::gapi::infer<> inputs with the model inputs.
|
||||
Number of names has to match the number of network inputs as defined in G_API_NET().
|
||||
In case a network has only single input layer, there is no need to specify name manually.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Contains names of input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &layer_names) {
|
||||
desc.input_names.clear();
|
||||
desc.input_names.reserve(layer_names.size());
|
||||
std::copy(layer_names.begin(), layer_names.end(),
|
||||
std::back_inserter(desc.input_names));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies sequence of network output layers names for inference.
|
||||
|
||||
The function is used to associate cv::gapi::infer<> outputs with the model outputs.
|
||||
Number of names has to match the number of network outputs as defined in G_API_NET().
|
||||
In case a network has only single output layer, there is no need to specify name manually.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of outputs
|
||||
as defined in the @ref G_API_NET. Contains names of output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &layer_names) {
|
||||
desc.output_names.clear();
|
||||
desc.output_names.reserve(layer_names.size());
|
||||
std::copy(layer_names.begin(), layer_names.end(),
|
||||
std::back_inserter(desc.output_names));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies a constant input.
|
||||
|
||||
The function is used to set a constant input. This input has to be
|
||||
a preprocessed tensor if its type is TENSOR. Need to provide name of the
|
||||
network layer which will receive provided data.
|
||||
|
||||
@param layer_name Name of network layer.
|
||||
@param data cv::Mat that contains data which will be associated with network layer.
|
||||
@param hint Input type @sa cv::gapi::ie::TraitAs.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& constInput(const std::string &layer_name,
|
||||
const cv::Mat &data,
|
||||
TraitAs hint = TraitAs::TENSOR) {
|
||||
desc.const_inputs[layer_name] = {data, hint};
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies OpenVINO plugin configuration.
|
||||
|
||||
The function is used to set configuration for OpenVINO plugin. Some parameters
|
||||
can be different for each plugin. Please follow https://docs.openvinotoolkit.org/latest/index.html
|
||||
to check information about specific plugin.
|
||||
|
||||
@param cfg Map of pairs: (config parameter name, config parameter value).
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params& pluginConfig(const IEConfig& cfg) {
|
||||
desc.config = cfg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
Function with a rvalue parameter.
|
||||
|
||||
@param cfg rvalue map of pairs: (config parameter name, config parameter value).
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params& pluginConfig(IEConfig&& cfg) {
|
||||
desc.config = std::move(cfg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies configuration for RemoteContext in InferenceEngine.
|
||||
|
||||
When RemoteContext is configured the backend imports the networks using the context.
|
||||
It also expects cv::MediaFrames to be actually remote, to operate with blobs via the context.
|
||||
|
||||
@param ctx_cfg cv::util::any value which holds InferenceEngine::ParamMap.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params& cfgContextParams(const cv::util::any& ctx_cfg) {
|
||||
desc.context_config = ctx_cfg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
Function with an rvalue parameter.
|
||||
|
||||
@param ctx_cfg cv::util::any value which holds InferenceEngine::ParamMap.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params& cfgContextParams(cv::util::any&& ctx_cfg) {
|
||||
desc.context_config = std::move(ctx_cfg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies number of asynchronous inference requests.
|
||||
|
||||
@param nireq Number of inference asynchronous requests.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params& cfgNumRequests(size_t nireq) {
|
||||
GAPI_Assert(nireq > 0 && "Number of infer requests must be greater than zero!");
|
||||
desc.nireq = nireq;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies new input shapes for the network inputs.
|
||||
|
||||
The function is used to specify new input shapes for the network inputs.
|
||||
Follow https://docs.openvinotoolkit.org/latest/classInferenceEngine_1_1networkNetwork.html
|
||||
for additional information.
|
||||
|
||||
@param reshape_table Map of pairs: name of corresponding data and its dimension.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputReshape(const std::map<std::string, std::vector<std::size_t>>& reshape_table) {
|
||||
desc.reshape_table = reshape_table;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params<Net>& cfgInputReshape(std::map<std::string, std::vector<std::size_t>>&& reshape_table) {
|
||||
desc.reshape_table = std::move(reshape_table);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param layer_name Name of layer.
|
||||
@param layer_dims New dimensions for this layer.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputReshape(const std::string& layer_name, const std::vector<size_t>& layer_dims) {
|
||||
desc.reshape_table.emplace(layer_name, layer_dims);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params<Net>& cfgInputReshape(std::string&& layer_name, std::vector<size_t>&& layer_dims) {
|
||||
desc.reshape_table.emplace(layer_name, layer_dims);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param layer_names set of names of network layers that will be used for network reshape.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputReshape(const std::unordered_set<std::string>& layer_names) {
|
||||
desc.layer_names_to_reshape = layer_names;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param layer_names rvalue set of the selected layers will be reshaped automatically
|
||||
its input image size.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputReshape(std::unordered_set<std::string>&& layer_names) {
|
||||
desc.layer_names_to_reshape = std::move(layer_names);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies the inference batch size.
|
||||
|
||||
The function is used to specify inference batch size.
|
||||
Follow https://docs.openvinotoolkit.org/latest/classInferenceEngine_1_1CNNNetwork.html#a8e9d19270a48aab50cb5b1c43eecb8e9 for additional information
|
||||
|
||||
@param size batch size which will be used.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgBatchSize(const size_t size) {
|
||||
desc.batch_size = cv::util::make_optional(size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Params<Net>& cfgPreprocessingParams(const cv::gapi::wip::onevpl::Device &device,
|
||||
const cv::gapi::wip::onevpl::Context &ctx) {
|
||||
desc.vpl_preproc_device = cv::util::make_optional(device);
|
||||
desc.vpl_preproc_ctx = cv::util::make_optional(ctx);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies which api will be used to run inference.
|
||||
|
||||
The function is used to specify mode for OpenVINO inference.
|
||||
OpenVINO has two options to run inference:
|
||||
1. Asynchronous (using StartAsync: https://docs.openvino.ai/latest/classInferenceEngine_1_1InferRequest.html#doxid-class-inference-engine-1-1-infer-request-1a405293e8423d82a5b45f642a3bef0d24)
|
||||
2. Synchronous (using Infer: https://docs.openvino.ai/latest/classInferenceEngine_1_1InferRequest.html#doxid-class-inference-engine-1-1-infer-request-1a3391ce30894abde730523e9ca9371ce8)
|
||||
By default asynchronous mode is used.
|
||||
|
||||
@param mode Inference mode which will be used.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInferMode(InferMode mode) {
|
||||
desc.mode = mode;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies the output precision for model.
|
||||
|
||||
The function is used to set an output precision for model.
|
||||
|
||||
@param precision Precision in OpenCV format (CV_8U, CV_32F, ...)
|
||||
will be applied to all output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputPrecision(detail::ParamDesc::PrecisionT precision) {
|
||||
desc.output_precision = precision;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param precision_map Map of pairs: name of corresponding output layer
|
||||
and its precision in OpenCV format (CV_8U, CV_32F, ...)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgOutputPrecision(detail::ParamDesc::PrecisionMapT precision_map) {
|
||||
desc.output_precision = precision_map;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies the input layout for model.
|
||||
|
||||
The function is used to set an input layout for model.
|
||||
|
||||
@param layout Layout in string representation ("NCHW", "NHWC", etc)
|
||||
will be applied to all input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputLayout(std::string layout) {
|
||||
desc.input_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param layout_map Map of pairs: name of corresponding input layer
|
||||
and its layout in string representation ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgInputLayout(detail::AttrMap<std::string> layout_map) {
|
||||
desc.input_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies the output layout for model.
|
||||
|
||||
The function is used to set an output layout for model.
|
||||
|
||||
@param layout Layout in string representation ("NCHW", "NHWC", etc)
|
||||
will be applied to all output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputLayout(std::string layout) {
|
||||
desc.output_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param layout_map Map of pairs: name of corresponding output layer
|
||||
and its layout in string representation ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgOutputLayout(detail::AttrMap<std::string> layout_map) {
|
||||
desc.output_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies resize interpolation algorithm.
|
||||
*
|
||||
The function is used to configure resize preprocessing for input layer.
|
||||
|
||||
@param interpolation Resize interpolation algorithm.
|
||||
Supported algorithms: #INTER_LINEAR, #INTER_AREA.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgResize(int interpolation) {
|
||||
desc.interpolation = interpolation;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param interpolation Map of pairs: name of corresponding input layer
|
||||
and its resize algorithm.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgResize(detail::AttrMap<int> interpolation) {
|
||||
desc.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::ie::backend(); }
|
||||
std::string tag() const { return Net::tag(); }
|
||||
cv::util::any params() const { return { desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
|
||||
protected:
|
||||
detail::ParamDesc desc;
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief This structure provides functions for generic network type that
|
||||
* fill inference parameters.
|
||||
* @see struct Generic
|
||||
*/
|
||||
template<>
|
||||
class Params<cv::gapi::Generic> {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on model information and sets default values for other
|
||||
inference description parameters. Model is loaded and compiled using OpenVINO Toolkit.
|
||||
|
||||
@param tag string tag of the network for which these parameters are intended.
|
||||
@param model path to topology IR (.xml file).
|
||||
@param weights path to weights (.bin file).
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &weights,
|
||||
const std::string &device)
|
||||
: desc{ model, weights, device, {}, {}, {}, 0u, 0u,
|
||||
detail::ParamDesc::Kind::Load, true, {}, {}, {}, 1u,
|
||||
{}, {}, {}, {}, InferMode::Async, {}, {}, {}, {} },
|
||||
m_tag(tag) {
|
||||
};
|
||||
|
||||
/** @overload
|
||||
|
||||
This constructor for pre-compiled networks. Model is imported from pre-compiled
|
||||
blob.
|
||||
|
||||
@param tag string tag of the network for which these parameters are intended.
|
||||
@param model path to model.
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &tag,
|
||||
const std::string &model,
|
||||
const std::string &device)
|
||||
: desc{ model, {}, device, {}, {}, {}, 0u, 0u,
|
||||
detail::ParamDesc::Kind::Import, true, {}, {}, {}, 1u,
|
||||
{}, {}, {}, {}, InferMode::Async, {}, {}, {}, {} },
|
||||
m_tag(tag) {
|
||||
};
|
||||
|
||||
/** @see ie::Params::pluginConfig. */
|
||||
Params& pluginConfig(const IEConfig& cfg) {
|
||||
desc.config = cfg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& pluginConfig(IEConfig&& cfg) {
|
||||
desc.config = std::move(cfg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::constInput. */
|
||||
Params& constInput(const std::string &layer_name,
|
||||
const cv::Mat &data,
|
||||
TraitAs hint = TraitAs::TENSOR) {
|
||||
desc.const_inputs[layer_name] = {data, hint};
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgNumRequests. */
|
||||
Params& cfgNumRequests(size_t nireq) {
|
||||
GAPI_Assert(nireq > 0 && "Number of infer requests must be greater than zero!");
|
||||
desc.nireq = nireq;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgInputReshape */
|
||||
Params& cfgInputReshape(const std::map<std::string, std::vector<std::size_t>>&reshape_table) {
|
||||
desc.reshape_table = reshape_table;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgInputReshape(std::map<std::string, std::vector<std::size_t>> && reshape_table) {
|
||||
desc.reshape_table = std::move(reshape_table);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgInputReshape(std::string && layer_name, std::vector<size_t> && layer_dims) {
|
||||
desc.reshape_table.emplace(layer_name, layer_dims);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgInputReshape(const std::string & layer_name, const std::vector<size_t>&layer_dims) {
|
||||
desc.reshape_table.emplace(layer_name, layer_dims);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgInputReshape(std::unordered_set<std::string> && layer_names) {
|
||||
desc.layer_names_to_reshape = std::move(layer_names);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgInputReshape(const std::unordered_set<std::string>&layer_names) {
|
||||
desc.layer_names_to_reshape = layer_names;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgBatchSize */
|
||||
Params& cfgBatchSize(const size_t size) {
|
||||
desc.batch_size = cv::util::make_optional(size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgInferAPI */
|
||||
Params& cfgInferMode(InferMode mode) {
|
||||
desc.mode = mode;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgOutputPrecision */
|
||||
Params& cfgOutputPrecision(detail::ParamDesc::PrecisionT precision) {
|
||||
desc.output_precision = precision;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgOutputPrecision(detail::ParamDesc::PrecisionMapT precision_map) {
|
||||
desc.output_precision = precision_map;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgInputLayout */
|
||||
Params& cfgInputLayout(std::string layout) {
|
||||
desc.input_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgInputLayout(detail::AttrMap<std::string> layout_map) {
|
||||
desc.input_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgOutputLayout */
|
||||
Params& cfgOutputLayout(std::string layout) {
|
||||
desc.output_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgOutputLayout(detail::AttrMap<std::string> layout_map) {
|
||||
desc.output_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ie::Params::cfgResize */
|
||||
Params& cfgResize(int interpolation) {
|
||||
desc.interpolation = interpolation;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgResize(detail::AttrMap<int> interpolation) {
|
||||
desc.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::ie::backend(); }
|
||||
std::string tag() const { return m_tag; }
|
||||
cv::util::any params() const { return { desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
|
||||
protected:
|
||||
detail::ParamDesc desc;
|
||||
std::string m_tag;
|
||||
};
|
||||
|
||||
} // namespace ie
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_IE_HPP
|
||||
+335
@@ -0,0 +1,335 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020-2021 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_ONNX_HPP
|
||||
#define OPENCV_GAPI_INFER_ONNX_HPP
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <tuple> // tuple, tuple_size
|
||||
|
||||
#include <opencv2/gapi/opencv_includes.hpp>
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
|
||||
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
|
||||
#include <opencv2/gapi/infer.hpp> // Generic
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
|
||||
/**
|
||||
* @brief This namespace contains G-API ONNX Runtime backend functions, structures, and symbols.
|
||||
*/
|
||||
namespace onnx {
|
||||
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
|
||||
enum class TraitAs: int {
|
||||
TENSOR, //!< G-API traits an associated cv::Mat as a raw tensor
|
||||
// and passes dimensions as-is
|
||||
IMAGE //!< G-API traits an associated cv::Mat as an image so
|
||||
// creates an "image" blob (NCHW/NHWC, etc)
|
||||
};
|
||||
|
||||
using PostProc = std::function<void(const std::unordered_map<std::string, cv::Mat> &,
|
||||
std::unordered_map<std::string, cv::Mat> &)>;
|
||||
|
||||
namespace detail {
|
||||
/**
|
||||
* @brief This structure contains description of inference parameters
|
||||
* which is specific to ONNX models.
|
||||
*/
|
||||
struct ParamDesc {
|
||||
std::string model_path; //!< Path to model.
|
||||
|
||||
// NB: nun_* may differ from topology's real input/output port numbers
|
||||
// (e.g. topology's partial execution)
|
||||
std::size_t num_in; //!< How many inputs are defined in the operation
|
||||
std::size_t num_out; //!< How many outputs are defined in the operation
|
||||
|
||||
// NB: Here order follows the `Net` API
|
||||
std::vector<std::string> input_names; //!< Names of input network layers.
|
||||
std::vector<std::string> output_names; //!< Names of output network layers.
|
||||
|
||||
using ConstInput = std::pair<cv::Mat, TraitAs>;
|
||||
std::unordered_map<std::string, ConstInput> const_inputs; //!< Map with pair of name of network layer and ConstInput which will be associated with this.
|
||||
|
||||
std::vector<cv::Scalar> mean; //!< Mean values for preprocessing.
|
||||
std::vector<cv::Scalar> stdev; //!< Standard deviation values for preprocessing.
|
||||
|
||||
std::vector<cv::GMatDesc> out_metas; //!< Out meta information about your output (type, dimension).
|
||||
PostProc custom_post_proc; //!< Post processing function.
|
||||
|
||||
std::vector<bool> normalize; //!< Vector of bool values that enabled or disabled normalize of input data.
|
||||
|
||||
std::vector<std::string> names_to_remap; //!< Names of output layers that will be processed in PostProc function.
|
||||
|
||||
bool is_generic;
|
||||
|
||||
// TODO: Needs to modify the rest of ParamDesc accordingly to support
|
||||
// both generic and non-generic options without duplication
|
||||
// (as it was done for the OV IE backend)
|
||||
// These values are pushed into the respective vector<> fields above
|
||||
// when the generic infer parameters are unpacked (see GONNXBackendImpl::unpackKernel)
|
||||
std::unordered_map<std::string, std::pair<cv::Scalar, cv::Scalar> > generic_mstd;
|
||||
std::unordered_map<std::string, bool> generic_norm;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template<typename Net>
|
||||
struct PortCfg {
|
||||
using In = std::array
|
||||
< std::string
|
||||
, std::tuple_size<typename Net::InArgs>::value >;
|
||||
using Out = std::array
|
||||
< std::string
|
||||
, std::tuple_size<typename Net::OutArgs>::value >;
|
||||
using NormCoefs = std::array
|
||||
< cv::Scalar
|
||||
, std::tuple_size<typename Net::InArgs>::value >;
|
||||
using Normalize = std::array
|
||||
< bool
|
||||
, std::tuple_size<typename Net::InArgs>::value >;
|
||||
};
|
||||
|
||||
/**
|
||||
* Contains description of inference parameters and kit of functions that
|
||||
* fill this parameters.
|
||||
*/
|
||||
template<typename Net> class Params {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on model information and sets default values for other
|
||||
inference description parameters.
|
||||
|
||||
@param model Path to model (.onnx file).
|
||||
*/
|
||||
Params(const std::string &model) {
|
||||
desc.model_path = model;
|
||||
desc.num_in = std::tuple_size<typename Net::InArgs>::value;
|
||||
desc.num_out = std::tuple_size<typename Net::OutArgs>::value;
|
||||
desc.is_generic = false;
|
||||
};
|
||||
|
||||
/** @brief Specifies sequence of network input layers names for inference.
|
||||
|
||||
The function is used to associate data of graph inputs with input layers of
|
||||
network topology. Number of names has to match the number of network inputs. If a network
|
||||
has only one input layer, there is no need to call it as the layer is
|
||||
associated with input automatically but this doesn't prevent you from
|
||||
doing it yourself. Count of names has to match to number of network inputs.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Contains names of input layers.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &layer_names) {
|
||||
desc.input_names.assign(layer_names.begin(), layer_names.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies sequence of output layers names for inference.
|
||||
|
||||
The function is used to associate data of graph outputs with output layers of
|
||||
network topology. If a network has only one output layer, there is no need to call it
|
||||
as the layer is associated with output automatically but this doesn't prevent
|
||||
you from doing it yourself. Count of names has to match to number of network
|
||||
outputs or you can set your own output but for this case you have to
|
||||
additionally use @ref cfgPostProc function.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of outputs
|
||||
as defined in the @ref G_API_NET. Contains names of output layers.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgOutputLayers(const typename PortCfg<Net>::Out &layer_names) {
|
||||
desc.output_names.assign(layer_names.begin(), layer_names.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Sets a constant input.
|
||||
|
||||
The function is used to set constant input. This input has to be
|
||||
a prepared tensor since preprocessing is disabled for this case. You should
|
||||
provide name of network layer which will receive provided data.
|
||||
|
||||
@param layer_name Name of network layer.
|
||||
@param data cv::Mat that contains data which will be associated with network layer.
|
||||
@param hint Type of input (TENSOR).
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& constInput(const std::string &layer_name,
|
||||
const cv::Mat &data,
|
||||
TraitAs hint = TraitAs::TENSOR) {
|
||||
desc.const_inputs[layer_name] = {data, hint};
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies mean value and standard deviation for preprocessing.
|
||||
|
||||
The function is used to set mean value and standard deviation for preprocessing
|
||||
of input data.
|
||||
|
||||
@param m std::array<cv::Scalar, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Contains mean values.
|
||||
@param s std::array<cv::Scalar, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Contains standard deviation values.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgMeanStd(const typename PortCfg<Net>::NormCoefs &m,
|
||||
const typename PortCfg<Net>::NormCoefs &s) {
|
||||
desc.mean.assign(m.begin(), m.end());
|
||||
desc.stdev.assign(s.begin(), s.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Configures graph output and provides the post processing function from user.
|
||||
|
||||
The function is used when you work with networks with dynamic outputs.
|
||||
Since we can't know dimensions of inference result needs provide them for
|
||||
construction of graph output. This dimensions can differ from inference result.
|
||||
So you have to provide @ref PostProc function that gets information from inference
|
||||
result and fill output which is constructed by dimensions from out_metas.
|
||||
|
||||
@param out_metas Out meta information about your output (type, dimension).
|
||||
@param remap_function Post processing function, which has two parameters. First is onnx
|
||||
result, second is graph output. Both parameters is std::map that contain pair of
|
||||
layer's name and cv::Mat.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &out_metas,
|
||||
const PostProc &remap_function) {
|
||||
desc.out_metas = out_metas;
|
||||
desc.custom_post_proc = remap_function;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
Function with a rvalue parameters.
|
||||
|
||||
@param out_metas rvalue out meta information about your output (type, dimension).
|
||||
@param remap_function rvalue post processing function, which has two parameters. First is onnx
|
||||
result, second is graph output. Both parameters is std::map that contain pair of
|
||||
layer's name and cv::Mat.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgPostProc(std::vector<cv::GMatDesc> &&out_metas,
|
||||
PostProc &&remap_function) {
|
||||
desc.out_metas = std::move(out_metas);
|
||||
desc.custom_post_proc = std::move(remap_function);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
The function has additional parameter names_to_remap. This parameter provides
|
||||
information about output layers which will be used for inference and post
|
||||
processing function.
|
||||
|
||||
@param out_metas Out meta information.
|
||||
@param remap_function Post processing function.
|
||||
@param names_to_remap Names of output layers. network's inference will
|
||||
be done on these layers. Inference's result will be processed in post processing
|
||||
function using these names.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgPostProc(const std::vector<cv::GMatDesc> &out_metas,
|
||||
const PostProc &remap_function,
|
||||
const std::vector<std::string> &names_to_remap) {
|
||||
desc.out_metas = out_metas;
|
||||
desc.custom_post_proc = remap_function;
|
||||
desc.names_to_remap = names_to_remap;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
Function with a rvalue parameters and additional parameter names_to_remap.
|
||||
|
||||
@param out_metas rvalue out meta information.
|
||||
@param remap_function rvalue post processing function.
|
||||
@param names_to_remap rvalue names of output layers. network's inference will
|
||||
be done on these layers. Inference's result will be processed in post processing
|
||||
function using these names.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgPostProc(std::vector<cv::GMatDesc> &&out_metas,
|
||||
PostProc &&remap_function,
|
||||
std::vector<std::string> &&names_to_remap) {
|
||||
desc.out_metas = std::move(out_metas);
|
||||
desc.custom_post_proc = std::move(remap_function);
|
||||
desc.names_to_remap = std::move(names_to_remap);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies normalize parameter for preprocessing.
|
||||
|
||||
The function is used to set normalize parameter for preprocessing of input data.
|
||||
|
||||
@param normalizations std::array<cv::Scalar, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Сontains bool values that enabled or disabled
|
||||
normalize of input data.
|
||||
@return the reference on modified object.
|
||||
*/
|
||||
Params<Net>& cfgNormalize(const typename PortCfg<Net>::Normalize &normalizations) {
|
||||
desc.normalize.assign(normalizations.begin(), normalizations.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::onnx::backend(); }
|
||||
std::string tag() const { return Net::tag(); }
|
||||
cv::util::any params() const { return { desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
|
||||
protected:
|
||||
detail::ParamDesc desc;
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief This structure provides functions for generic network type that
|
||||
* fill inference parameters.
|
||||
* @see struct Generic
|
||||
*/
|
||||
template<>
|
||||
class Params<cv::gapi::Generic> {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on input information and sets default values for other
|
||||
inference description parameters.
|
||||
|
||||
@param tag string tag of the network for which these parameters are intended.
|
||||
@param model_path path to model file (.onnx file).
|
||||
*/
|
||||
Params(const std::string& tag, const std::string& model_path)
|
||||
: desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {} }, m_tag(tag) {}
|
||||
|
||||
void cfgMeanStdDev(const std::string &layer,
|
||||
const cv::Scalar &m,
|
||||
const cv::Scalar &s) {
|
||||
desc.generic_mstd[layer] = std::make_pair(m, s);
|
||||
}
|
||||
|
||||
void cfgNormalize(const std::string &layer, bool flag) {
|
||||
desc.generic_norm[layer] = flag;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::onnx::backend(); }
|
||||
std::string tag() const { return m_tag; }
|
||||
cv::util::any params() const { return { desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
protected:
|
||||
detail::ParamDesc desc;
|
||||
std::string m_tag;
|
||||
};
|
||||
|
||||
} // namespace onnx
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_HPP
|
||||
+685
@@ -0,0 +1,685 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2023 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_OV_HPP
|
||||
#define OPENCV_GAPI_INFER_OV_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <opencv2/gapi/util/any.hpp>
|
||||
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
|
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelType[M], GBackend
|
||||
#include <opencv2/gapi/infer.hpp> // Generic
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
|
||||
/**
|
||||
* @brief This namespace contains G-API OpenVINO 2.0 backend functions,
|
||||
* structures, and symbols.
|
||||
*/
|
||||
namespace ov {
|
||||
|
||||
GAPI_EXPORTS cv::gapi::GBackend backend();
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
using AttrMap = std::map<std::string, T>;
|
||||
// NB: This type is supposed to be used to hold in/out layers
|
||||
// attributes such as precision, layout, shape etc.
|
||||
//
|
||||
// User can provide attributes either:
|
||||
// 1. cv::util::monostate - No value specified explicitly.
|
||||
// 2. Attr - value specified explicitly that should be broadcasted to all layers.
|
||||
// 3. AttrMap[str->T] - map specifies value for particular layer.
|
||||
template <typename Attr>
|
||||
using LayerVariantAttr = cv::util::variant< cv::util::monostate
|
||||
, AttrMap<Attr>
|
||||
, Attr>;
|
||||
|
||||
struct ParamDesc {
|
||||
struct Model {
|
||||
|
||||
Model(const std::string &model_path_,
|
||||
const std::string &bin_path_)
|
||||
: model_path(model_path_), bin_path(bin_path_) {
|
||||
}
|
||||
|
||||
std::string model_path;
|
||||
std::string bin_path;
|
||||
|
||||
LayerVariantAttr<std::string> input_tensor_layout;
|
||||
LayerVariantAttr<std::string> input_model_layout;
|
||||
LayerVariantAttr<std::string> output_tensor_layout;
|
||||
LayerVariantAttr<std::string> output_model_layout;
|
||||
LayerVariantAttr<int> output_tensor_precision;
|
||||
|
||||
LayerVariantAttr<std::vector<size_t>> new_shapes;
|
||||
|
||||
LayerVariantAttr<std::vector<float>> mean_values;
|
||||
LayerVariantAttr<std::vector<float>> scale_values;
|
||||
|
||||
LayerVariantAttr<int> interpolation;
|
||||
};
|
||||
|
||||
struct CompiledModel {
|
||||
std::string blob_path;
|
||||
};
|
||||
|
||||
using Kind = cv::util::variant<Model, CompiledModel>;
|
||||
|
||||
ParamDesc(Kind &&kind_,
|
||||
const std::string &device_,
|
||||
const bool is_generic_,
|
||||
const size_t num_in_,
|
||||
const size_t num_out_)
|
||||
: kind(std::move(kind_)), device(device_),
|
||||
is_generic(is_generic_),
|
||||
num_in(num_in_), num_out(num_out_) {
|
||||
}
|
||||
|
||||
Kind kind;
|
||||
|
||||
std::string device;
|
||||
bool is_generic;
|
||||
|
||||
std::size_t num_in;
|
||||
std::size_t num_out;
|
||||
|
||||
std::vector<std::string> input_names;
|
||||
std::vector<std::string> output_names;
|
||||
|
||||
using PluginConfigT = std::map<std::string, std::string>;
|
||||
PluginConfigT config;
|
||||
|
||||
size_t nireq = 1;
|
||||
};
|
||||
|
||||
// NB: Just helper to avoid code duplication.
|
||||
static detail::ParamDesc::Model&
|
||||
getModelToSetAttrOrThrow(detail::ParamDesc::Kind &kind,
|
||||
const std::string &attr_name) {
|
||||
if (cv::util::holds_alternative<detail::ParamDesc::CompiledModel>(kind)) {
|
||||
cv::util::throw_error(
|
||||
std::logic_error("Specifying " + attr_name + " isn't"
|
||||
" possible for compiled model."));
|
||||
}
|
||||
GAPI_Assert(cv::util::holds_alternative<detail::ParamDesc::Model>(kind));
|
||||
return cv::util::get<detail::ParamDesc::Model>(kind);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
* @brief This structure provides functions
|
||||
* that fill inference parameters for "OpenVINO Toolkit" model.
|
||||
*/
|
||||
template<typename Net> struct Params {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on model information and specifies default values for other
|
||||
inference description parameters. Model is loaded and compiled using "OpenVINO Toolkit".
|
||||
|
||||
@param model_path Path to a model.
|
||||
@param bin_path Path to a data file.
|
||||
For IR format (*.bin):
|
||||
If path is empty, will try to read a bin file with the same name as xml.
|
||||
If the bin file with the same name is not found, will load IR without weights.
|
||||
For PDPD (*.pdmodel) and ONNX (*.onnx) formats bin_path isn't used.
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &model_path,
|
||||
const std::string &bin_path,
|
||||
const std::string &device)
|
||||
: m_desc( detail::ParamDesc::Kind{detail::ParamDesc::Model{model_path, bin_path}}
|
||||
, device
|
||||
, false /* is generic */
|
||||
, std::tuple_size<typename Net::InArgs>::value
|
||||
, std::tuple_size<typename Net::OutArgs>::value) {
|
||||
}
|
||||
|
||||
/** @overload
|
||||
Use this constructor to work with pre-compiled network.
|
||||
Model is imported from a pre-compiled blob.
|
||||
|
||||
@param blob_path path to the compiled model (*.blob).
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &blob_path,
|
||||
const std::string &device)
|
||||
: m_desc( detail::ParamDesc::Kind{detail::ParamDesc::CompiledModel{blob_path}}
|
||||
, device
|
||||
, false /* is generic */
|
||||
, std::tuple_size<typename Net::InArgs>::value
|
||||
, std::tuple_size<typename Net::OutArgs>::value) {
|
||||
}
|
||||
|
||||
/** @brief Specifies sequence of network input layers names for inference.
|
||||
|
||||
The function is used to associate cv::gapi::infer<> inputs with the model inputs.
|
||||
Number of names has to match the number of network inputs as defined in G_API_NET().
|
||||
In case a network has only single input layer, there is no need to specify name manually.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of inputs
|
||||
as defined in the @ref G_API_NET. Contains names of input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputLayers(const std::vector<std::string> &layer_names) {
|
||||
m_desc.input_names = layer_names;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies sequence of network output layers names for inference.
|
||||
|
||||
The function is used to associate cv::gapi::infer<> outputs with the model outputs.
|
||||
Number of names has to match the number of network outputs as defined in G_API_NET().
|
||||
In case a network has only single output layer, there is no need to specify name manually.
|
||||
|
||||
@param layer_names std::array<std::string, N> where N is the number of outputs
|
||||
as defined in the @ref G_API_NET. Contains names of output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputLayers(const std::vector<std::string> &layer_names) {
|
||||
m_desc.output_names = layer_names;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies OpenVINO plugin configuration.
|
||||
|
||||
The function is used to set configuration for OpenVINO plugin. Some parameters
|
||||
can be different for each plugin. Please follow https://docs.openvinotoolkit.org/latest/index.html
|
||||
to check information about specific plugin.
|
||||
|
||||
@param config Map of pairs: (config parameter name, config parameter value).
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgPluginConfig(const detail::ParamDesc::PluginConfigT &config) {
|
||||
m_desc.config = config;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies tensor layout for an input layer.
|
||||
|
||||
The function is used to set tensor layout for an input layer.
|
||||
|
||||
@param layout Tensor layout ("NCHW", "NWHC", etc)
|
||||
will be applied to all input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputTensorLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input tensor layout")
|
||||
.input_tensor_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
@param layout_map Map of pairs: name of corresponding input layer
|
||||
and its tensor layout represented in std::string ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgInputTensorLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input tensor layout")
|
||||
.input_tensor_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies model layout for an input layer.
|
||||
|
||||
The function is used to set model layout for an input layer.
|
||||
|
||||
@param layout Model layout ("NCHW", "NHWC", etc)
|
||||
will be applied to all input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgInputModelLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input model layout")
|
||||
.input_model_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
@param layout_map Map of pairs: name of corresponding input layer
|
||||
and its model layout ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgInputModelLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input model layout")
|
||||
.input_model_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies tensor layout for an output layer.
|
||||
|
||||
The function is used to set tensor layout for an output layer.
|
||||
|
||||
@param layout Tensor layout ("NCHW", "NWHC", etc)
|
||||
will be applied to all output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputTensorLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor layout")
|
||||
.output_tensor_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
@param layout_map Map of pairs: name of corresponding output layer
|
||||
and its tensor layout represented in std::string ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgOutputTensorLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor layout")
|
||||
.output_tensor_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies model layout for an output layer.
|
||||
|
||||
The function is used to set model layout for an output layer.
|
||||
|
||||
@param layout Model layout ("NCHW", "NHWC", etc)
|
||||
will be applied to all output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputModelLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output model layout")
|
||||
.output_model_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
@param layout_map Map of pairs: name of corresponding output layer
|
||||
and its model layout ("NCHW", "NHWC", etc)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgOutputModelLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output model layout")
|
||||
.output_model_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies tensor precision for an output layer.
|
||||
|
||||
The function is used to set tensor precision for an output layer..
|
||||
|
||||
@param precision Precision in OpenCV format (CV_8U, CV_32F, ...)
|
||||
will be applied to all output layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgOutputTensorPrecision(int precision) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor precision")
|
||||
.output_tensor_precision = precision;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param precision_map Map of pairs: name of corresponding output layer
|
||||
and its precision in OpenCV format (CV_8U, CV_32F, ...)
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgOutputTensorPrecision(detail::AttrMap<int> precision_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor precision")
|
||||
.output_tensor_precision = std::move(precision_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies the new shape for input layers.
|
||||
|
||||
The function is used to set new shape for input layers.
|
||||
|
||||
@param new_shape New shape will be applied to all input layers.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgReshape(std::vector<size_t> new_shape) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "reshape")
|
||||
.new_shapes = std::move(new_shape);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param new_shape_map Map of pairs: name of corresponding output layer
|
||||
and its new shape.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>&
|
||||
cfgReshape(detail::AttrMap<std::vector<size_t>> new_shape_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "reshape")
|
||||
.new_shapes = std::move(new_shape_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies number of asynchronous inference requests.
|
||||
|
||||
@param nireq Number of inference asynchronous requests.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgNumRequests(const size_t nireq) {
|
||||
if (nireq == 0) {
|
||||
cv::util::throw_error(
|
||||
std::logic_error("Number of inference requests"
|
||||
" must be greater than zero."));
|
||||
}
|
||||
m_desc.nireq = nireq;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies mean values for preprocessing.
|
||||
*
|
||||
The function is used to set mean values for input layer preprocessing.
|
||||
|
||||
@param mean_values Float vector contains mean values
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgMean(std::vector<float> mean_values) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "mean values")
|
||||
.mean_values = std::move(mean_values);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param mean_map Map of pairs: name of corresponding input layer
|
||||
and its mean values.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgMean(detail::AttrMap<std::vector<float>> mean_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "mean values")
|
||||
.mean_values = std::move(mean_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies scale values for preprocessing.
|
||||
*
|
||||
The function is used to set scale values for input layer preprocessing.
|
||||
|
||||
@param scale_values Float vector contains scale values
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgScale(std::vector<float> scale_values) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "scale values")
|
||||
.scale_values = std::move(scale_values);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param scale_map Map of pairs: name of corresponding input layer
|
||||
and its mean values.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgScale(detail::AttrMap<std::vector<float>> scale_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "scale values")
|
||||
.scale_values = std::move(scale_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @brief Specifies resize interpolation algorithm.
|
||||
*
|
||||
The function is used to configure resize preprocessing for input layer.
|
||||
|
||||
@param interpolation Resize interpolation algorithm.
|
||||
Supported algorithms: #INTER_NEAREST, #INTER_LINEAR, #INTER_CUBIC.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgResize(int interpolation) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "resize preprocessing")
|
||||
.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
@param interpolation Map of pairs: name of corresponding input layer
|
||||
and its resize algorithm.
|
||||
@return reference to this parameter structure.
|
||||
*/
|
||||
Params<Net>& cfgResize(detail::AttrMap<int> interpolation) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "resize preprocessing")
|
||||
.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::ov::backend(); }
|
||||
std::string tag() const { return Net::tag(); }
|
||||
cv::util::any params() const { return { m_desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
|
||||
protected:
|
||||
detail::ParamDesc m_desc;
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief This structure provides functions for generic network type that
|
||||
* fill inference parameters.
|
||||
* @see struct Generic
|
||||
*/
|
||||
template<>
|
||||
class Params<cv::gapi::Generic> {
|
||||
public:
|
||||
/** @brief Class constructor.
|
||||
|
||||
Constructs Params based on model information and specifies default values for other
|
||||
inference description parameters. Model is loaded and compiled using "OpenVINO Toolkit".
|
||||
|
||||
@param tag string tag of the network for which these parameters are intended.
|
||||
@param model_path Path to a model.
|
||||
@param bin_path Path to a data file.
|
||||
For IR format (*.bin):
|
||||
If path is empty, will try to read a bin file with the same name as xml.
|
||||
If the bin file with the same name is not found, will load IR without weights.
|
||||
For PDPD (*.pdmodel) and ONNX (*.onnx) formats bin_path isn't used.
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &tag,
|
||||
const std::string &model_path,
|
||||
const std::string &bin_path,
|
||||
const std::string &device)
|
||||
: m_tag(tag),
|
||||
m_desc( detail::ParamDesc::Kind{detail::ParamDesc::Model{model_path, bin_path}}
|
||||
, device
|
||||
, true /* is generic */
|
||||
, 0u
|
||||
, 0u) {
|
||||
}
|
||||
|
||||
/** @overload
|
||||
|
||||
This constructor for pre-compiled networks. Model is imported from pre-compiled
|
||||
blob.
|
||||
|
||||
@param tag string tag of the network for which these parameters are intended.
|
||||
@param blob_path path to the compiled model (*.blob).
|
||||
@param device target device to use.
|
||||
*/
|
||||
Params(const std::string &tag,
|
||||
const std::string &blob_path,
|
||||
const std::string &device)
|
||||
: m_tag(tag),
|
||||
m_desc( detail::ParamDesc::Kind{detail::ParamDesc::CompiledModel{blob_path}}
|
||||
, device
|
||||
, true /* is generic */
|
||||
, 0u
|
||||
, 0u) {
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgPluginConfig. */
|
||||
Params& cfgPluginConfig(const detail::ParamDesc::PluginConfigT &config) {
|
||||
m_desc.config = config;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgInputTensorLayout. */
|
||||
Params& cfgInputTensorLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input tensor layout")
|
||||
.input_tensor_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgInputTensorLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input tensor layout")
|
||||
.input_tensor_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgInputModelLayout. */
|
||||
Params& cfgInputModelLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input model layout")
|
||||
.input_model_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgInputModelLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "input model layout")
|
||||
.input_model_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgOutputTensorLayout. */
|
||||
Params& cfgOutputTensorLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor layout")
|
||||
.output_tensor_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgOutputTensorLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor layout")
|
||||
.output_tensor_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgOutputModelLayout. */
|
||||
Params& cfgOutputModelLayout(std::string layout) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output model layout")
|
||||
.output_model_layout = std::move(layout);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgOutputModelLayout(detail::AttrMap<std::string> layout_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output model layout")
|
||||
.output_model_layout = std::move(layout_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgOutputTensorPrecision. */
|
||||
Params& cfgOutputTensorPrecision(int precision) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor precision")
|
||||
.output_tensor_precision = precision;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgOutputTensorPrecision(detail::AttrMap<int> precision_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "output tensor precision")
|
||||
.output_tensor_precision = std::move(precision_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgReshape. */
|
||||
Params& cfgReshape(std::vector<size_t> new_shape) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "reshape")
|
||||
.new_shapes = std::move(new_shape);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params&
|
||||
cfgReshape(detail::AttrMap<std::vector<size_t>> new_shape_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "reshape")
|
||||
.new_shapes = std::move(new_shape_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgNumRequests. */
|
||||
Params& cfgNumRequests(const size_t nireq) {
|
||||
if (nireq == 0) {
|
||||
cv::util::throw_error(
|
||||
std::logic_error("Number of inference requests"
|
||||
" must be greater than zero."));
|
||||
}
|
||||
m_desc.nireq = nireq;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgMean. */
|
||||
Params& cfgMean(std::vector<float> mean_values) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "mean values")
|
||||
.mean_values = std::move(mean_values);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgMean(detail::AttrMap<std::vector<float>> mean_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "mean values")
|
||||
.mean_values = std::move(mean_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgScale. */
|
||||
Params& cfgScale(std::vector<float> scale_values) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "scale values")
|
||||
.scale_values = std::move(scale_values);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgScale(detail::AttrMap<std::vector<float>> scale_map) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "scale values")
|
||||
.scale_values = std::move(scale_map);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @see ov::Params::cfgResize. */
|
||||
Params& cfgResize(int interpolation) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "resize preprocessing")
|
||||
.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** @overload */
|
||||
Params& cfgResize(detail::AttrMap<int> interpolation) {
|
||||
detail::getModelToSetAttrOrThrow(m_desc.kind, "resize preprocessing")
|
||||
.interpolation = std::move(interpolation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// BEGIN(G-API's network parametrization API)
|
||||
GBackend backend() const { return cv::gapi::ov::backend(); }
|
||||
std::string tag() const { return m_tag; }
|
||||
cv::util::any params() const { return { m_desc }; }
|
||||
// END(G-API's network parametrization API)
|
||||
|
||||
protected:
|
||||
std::string m_tag;
|
||||
detail::ParamDesc m_desc;
|
||||
};
|
||||
|
||||
} // namespace ov
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_OV_HPP
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
|
||||
#ifndef OPENCV_GAPI_PARSERS_HPP
|
||||
#define OPENCV_GAPI_PARSERS_HPP
|
||||
|
||||
#include <utility> // std::tuple
|
||||
|
||||
#include <opencv2/gapi/gmat.hpp>
|
||||
#include <opencv2/gapi/gkernel.hpp>
|
||||
|
||||
namespace cv { namespace gapi {
|
||||
namespace nn {
|
||||
namespace parsers {
|
||||
using GRects = GArray<Rect>;
|
||||
using GDetections = std::tuple<GArray<Rect>, GArray<int>>;
|
||||
|
||||
G_TYPED_KERNEL(GParseSSDBL, <GDetections(GMat, GOpaque<Size>, float, int)>,
|
||||
"org.opencv.nn.parsers.parseSSD_BL") {
|
||||
static std::tuple<GArrayDesc,GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&, float, int) {
|
||||
return std::make_tuple(empty_array_desc(), empty_array_desc());
|
||||
}
|
||||
};
|
||||
|
||||
G_TYPED_KERNEL(GParseSSD, <GRects(GMat, GOpaque<Size>, float, bool, bool)>,
|
||||
"org.opencv.nn.parsers.parseSSD") {
|
||||
static GArrayDesc outMeta(const GMatDesc&, const GOpaqueDesc&, float, bool, bool) {
|
||||
return empty_array_desc();
|
||||
}
|
||||
};
|
||||
|
||||
G_TYPED_KERNEL(GParseYolo, <GDetections(GMat, GOpaque<Size>, float, float, std::vector<float>)>,
|
||||
"org.opencv.nn.parsers.parseYolo") {
|
||||
static std::tuple<GArrayDesc, GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&,
|
||||
float, float, const std::vector<float>&) {
|
||||
return std::make_tuple(empty_array_desc(), empty_array_desc());
|
||||
}
|
||||
static const std::vector<float>& defaultAnchors() {
|
||||
static std::vector<float> anchors {
|
||||
0.57273f, 0.677385f, 1.87446f, 2.06253f, 3.33843f, 5.47434f, 7.88282f, 3.52778f, 9.77052f, 9.16828f
|
||||
};
|
||||
return anchors;
|
||||
}
|
||||
};
|
||||
} // namespace parsers
|
||||
} // namespace nn
|
||||
|
||||
/** @brief Parses output of SSD network.
|
||||
|
||||
Extracts detection information (box, confidence, label) from SSD output and
|
||||
filters it by given confidence and label.
|
||||
|
||||
@note Function textual ID is "org.opencv.nn.parsers.parseSSD_BL"
|
||||
|
||||
@param in Input CV_32F tensor with {1,1,N,7} dimensions.
|
||||
@param inSz Size to project detected boxes to (size of the input image).
|
||||
@param confidenceThreshold If confidence of the
|
||||
detection is smaller than confidence threshold, detection is rejected.
|
||||
@param filterLabel If provided (!= -1), only detections with
|
||||
given label will get to the output.
|
||||
@return a tuple with a vector of detected boxes and a vector of appropriate labels.
|
||||
*/
|
||||
GAPI_EXPORTS_W std::tuple<GArray<Rect>, GArray<int>> parseSSD(const GMat& in,
|
||||
const GOpaque<Size>& inSz,
|
||||
const float confidenceThreshold = 0.5f,
|
||||
const int filterLabel = -1);
|
||||
|
||||
/** @brief Parses output of SSD network.
|
||||
|
||||
Extracts detection information (box, confidence) from SSD output and
|
||||
filters it by given confidence and by going out of bounds.
|
||||
|
||||
@note Function textual ID is "org.opencv.nn.parsers.parseSSD"
|
||||
|
||||
@param in Input CV_32F tensor with {1,1,N,7} dimensions.
|
||||
@param inSz Size to project detected boxes to (size of the input image).
|
||||
@param confidenceThreshold If confidence of the
|
||||
detection is smaller than confidence threshold, detection is rejected.
|
||||
@param alignmentToSquare If provided true, bounding boxes are extended to squares.
|
||||
The center of the rectangle remains unchanged, the side of the square is
|
||||
the larger side of the rectangle.
|
||||
@param filterOutOfBounds If provided true, out-of-frame boxes are filtered.
|
||||
@return a vector of detected bounding boxes.
|
||||
*/
|
||||
GAPI_EXPORTS_W GArray<Rect> parseSSD(const GMat& in,
|
||||
const GOpaque<Size>& inSz,
|
||||
const float confidenceThreshold,
|
||||
const bool alignmentToSquare,
|
||||
const bool filterOutOfBounds);
|
||||
|
||||
/** @brief Parses output of Yolo network.
|
||||
|
||||
Extracts detection information (box, confidence, label) from Yolo output,
|
||||
filters it by given confidence and performs non-maximum suppression for overlapping boxes.
|
||||
|
||||
@note Function textual ID is "org.opencv.nn.parsers.parseYolo"
|
||||
|
||||
@param in Input CV_32F tensor with {1,13,13,N} dimensions, N should satisfy:
|
||||
\f[\texttt{N} = (\texttt{num_classes} + \texttt{5}) * \texttt{5},\f]
|
||||
where num_classes - a number of classes Yolo network was trained with.
|
||||
@param inSz Size to project detected boxes to (size of the input image).
|
||||
@param confidenceThreshold If confidence of the
|
||||
detection is smaller than confidence threshold, detection is rejected.
|
||||
@param nmsThreshold Non-maximum suppression threshold which controls minimum
|
||||
relative box intersection area required for rejecting the box with a smaller confidence.
|
||||
If 1.f, nms is not performed and no boxes are rejected.
|
||||
@param anchors Anchors Yolo network was trained with.
|
||||
@note The default anchor values are specified for YOLO v2 Tiny as described in Intel Open Model Zoo
|
||||
<a href="https://github.com/openvinotoolkit/open_model_zoo/blob/master/models/public/yolo-v2-tiny-tf/yolo-v2-tiny-tf.md">documentation</a>.
|
||||
@return a tuple with a vector of detected boxes and a vector of appropriate labels.
|
||||
*/
|
||||
GAPI_EXPORTS_W std::tuple<GArray<Rect>, GArray<int>> parseYolo(const GMat& in,
|
||||
const GOpaque<Size>& inSz,
|
||||
const float confidenceThreshold = 0.5f,
|
||||
const float nmsThreshold = 0.5f,
|
||||
const std::vector<float>& anchors
|
||||
= nn::parsers::GParseYolo::defaultAnchors());
|
||||
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
// Reimport parseSSD & parseYolo under their initial namespace
|
||||
namespace cv {
|
||||
namespace gapi {
|
||||
namespace streaming {
|
||||
|
||||
using cv::gapi::parseSSD;
|
||||
using cv::gapi::parseYolo;
|
||||
|
||||
} // namespace streaming
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_PARSERS_HPP
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user