From 06c33df307c25a5aac830e697ce57bc4858c4060 Mon Sep 17 00:00:00 2001 From: Jin Ma Date: Sun, 22 Sep 2013 10:22:09 +0800 Subject: [PATCH] Added knearest neighbor of OpenCL version. It includes the accuracy/performance test and the implementation of KNN. --- modules/ocl/CMakeLists.txt | 2 +- modules/ocl/include/opencv2/ocl/ocl.hpp | 24 +++++++++++++++++++ .../ocl/include/opencv2/ocl/private/util.hpp | 1 + modules/ocl/src/initialization.cpp | 8 +++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/ocl/CMakeLists.txt b/modules/ocl/CMakeLists.txt index 05b28b83f..69d9df52d 100644 --- a/modules/ocl/CMakeLists.txt +++ b/modules/ocl/CMakeLists.txt @@ -3,5 +3,5 @@ if(NOT HAVE_OPENCL) endif() set(the_description "OpenCL-accelerated Computer Vision") -ocv_define_module(ocl opencv_core opencv_imgproc opencv_features2d opencv_objdetect opencv_video opencv_calib3d) +ocv_define_module(ocl opencv_core opencv_imgproc opencv_features2d opencv_objdetect opencv_video opencv_calib3d opencv_ml) ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index 42ac75840..8f46244fa 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -51,6 +51,7 @@ #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/features2d/features2d.hpp" +#include "opencv2/ml/ml.hpp" namespace cv { @@ -1892,6 +1893,29 @@ namespace cv { return (total + grain - 1) / grain; } + + /*!***************K Nearest Neighbour*************!*/ + class CV_EXPORTS KNearestNeighbour: public CvKNearest + { + public: + KNearestNeighbour(); + ~KNearestNeighbour(); + KNearestNeighbour(const Mat& trainData, const Mat& labels, + const Mat& sampleIdx = Mat().setTo(Scalar::all(0)), bool isRegression = false, int max_k = 32); + + bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)), + bool isRegression = false, int max_k = 32, bool updateBase = false); + + void clear(); + + void find_nearest(const oclMat& samples, int k, oclMat& lables); + + private: + int max_k, var_count; + int total; + bool regression; + oclMat samples_ocl; + }; } } #if defined _MSC_VER && _MSC_VER >= 1200 diff --git a/modules/ocl/include/opencv2/ocl/private/util.hpp b/modules/ocl/include/opencv2/ocl/private/util.hpp index 3176a6895..9adae3823 100644 --- a/modules/ocl/include/opencv2/ocl/private/util.hpp +++ b/modules/ocl/include/opencv2/ocl/private/util.hpp @@ -167,6 +167,7 @@ namespace cv template<> bool CV_EXPORTS queryDeviceInfo(cl_kernel kernel); + unsigned long CV_EXPORTS queryLocalMemInfo(); }//namespace ocl }//namespace cv diff --git a/modules/ocl/src/initialization.cpp b/modules/ocl/src/initialization.cpp index 0e16e75ae..34d5ff5e6 100644 --- a/modules/ocl/src/initialization.cpp +++ b/modules/ocl/src/initialization.cpp @@ -1033,6 +1033,14 @@ namespace cv return impl->maxComputeUnits; } + unsigned long queryLocalMemInfo() + { + Info::Impl* impl = Context::getContext()->impl; + cl_ulong local_memory_size = 0; + clGetDeviceInfo(impl->devices[impl->devnum], CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), (void*)&local_memory_size, 0); + return local_memory_size; + } + void* Context::oclContext() { return impl->oclcontext;