Merge pull request #5340 from alalek:ocl_off

This commit is contained in:
Vadim Pisarevsky 2015-11-10 16:53:36 +00:00
commit 3942b1f362
16 changed files with 127 additions and 18 deletions

View File

@ -26,6 +26,8 @@ set(STR_CPP "// This file is auto-generated. Do not edit!
#include \"precomp.hpp\" #include \"precomp.hpp\"
#include \"${OUTPUT_HPP_NAME}\" #include \"${OUTPUT_HPP_NAME}\"
#ifdef HAVE_OPENCL
namespace cv namespace cv
{ {
namespace ocl namespace ocl
@ -40,6 +42,8 @@ set(STR_HPP "// This file is auto-generated. Do not edit!
#include \"opencv2/core/ocl_genbase.hpp\" #include \"opencv2/core/ocl_genbase.hpp\"
#include \"opencv2/core/opencl/ocl_defs.hpp\" #include \"opencv2/core/opencl/ocl_defs.hpp\"
#ifdef HAVE_OPENCL
namespace cv namespace cv
{ {
namespace ocl namespace ocl
@ -82,8 +86,8 @@ foreach(cl ${cl_list})
set(STR_HPP "${STR_HPP}${STR_HPP_DECL}") set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
endforeach() endforeach()
set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n") set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n#endif\n")
set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n") set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n#endif\n")
file(WRITE "${OUTPUT}" "${STR_CPP}") file(WRITE "${OUTPUT}" "${STR_CPP}")

View File

@ -86,6 +86,7 @@ struct StereoBMParams
int dispType; int dispType;
}; };
#ifdef HAVE_OPENCL
static bool ocl_prefilter_norm(InputArray _input, OutputArray _output, int winsize, int prefilterCap) static bool ocl_prefilter_norm(InputArray _input, OutputArray _output, int winsize, int prefilterCap)
{ {
ocl::Kernel k("prefilter_norm", ocl::calib3d::stereobm_oclsrc, cv::format("-D WSZ=%d", winsize)); ocl::Kernel k("prefilter_norm", ocl::calib3d::stereobm_oclsrc, cv::format("-D WSZ=%d", winsize));
@ -106,6 +107,7 @@ static bool ocl_prefilter_norm(InputArray _input, OutputArray _output, int winsi
return k.run(2, globalThreads, NULL, false); return k.run(2, globalThreads, NULL, false);
} }
#endif
static void prefilterNorm( const Mat& src, Mat& dst, int winsize, int ftzero, uchar* buf ) static void prefilterNorm( const Mat& src, Mat& dst, int winsize, int ftzero, uchar* buf )
{ {
@ -170,6 +172,7 @@ static void prefilterNorm( const Mat& src, Mat& dst, int winsize, int ftzero, uc
} }
} }
#ifdef HAVE_OPENCL
static bool ocl_prefilter_xsobel(InputArray _input, OutputArray _output, int prefilterCap) static bool ocl_prefilter_xsobel(InputArray _input, OutputArray _output, int prefilterCap)
{ {
ocl::Kernel k("prefilter_xsobel", ocl::calib3d::stereobm_oclsrc); ocl::Kernel k("prefilter_xsobel", ocl::calib3d::stereobm_oclsrc);
@ -186,6 +189,7 @@ static bool ocl_prefilter_xsobel(InputArray _input, OutputArray _output, int pre
return k.run(2, globalThreads, NULL, false); return k.run(2, globalThreads, NULL, false);
} }
#endif
static void static void
prefilterXSobel( const Mat& src, Mat& dst, int ftzero ) prefilterXSobel( const Mat& src, Mat& dst, int ftzero )
@ -849,6 +853,7 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right,
} }
} }
#ifdef HAVE_OPENCL
static bool ocl_prefiltering(InputArray left0, InputArray right0, OutputArray left, OutputArray right, StereoBMParams* state) static bool ocl_prefiltering(InputArray left0, InputArray right0, OutputArray left, OutputArray right, StereoBMParams* state)
{ {
if( state->preFilterType == StereoBM::PREFILTER_NORMALIZED_RESPONSE ) if( state->preFilterType == StereoBM::PREFILTER_NORMALIZED_RESPONSE )
@ -867,6 +872,7 @@ static bool ocl_prefiltering(InputArray left0, InputArray right0, OutputArray le
} }
return true; return true;
} }
#endif
struct PrefilterInvoker : public ParallelLoopBody struct PrefilterInvoker : public ParallelLoopBody
{ {
@ -896,6 +902,7 @@ struct PrefilterInvoker : public ParallelLoopBody
StereoBMParams* state; StereoBMParams* state;
}; };
#ifdef HAVE_OPENCL
static bool ocl_stereobm( InputArray _left, InputArray _right, static bool ocl_stereobm( InputArray _left, InputArray _right,
OutputArray _disp, StereoBMParams* state) OutputArray _disp, StereoBMParams* state)
{ {
@ -940,6 +947,7 @@ static bool ocl_stereobm( InputArray _left, InputArray _right,
idx = k.set(idx, state->uniquenessRatio); idx = k.set(idx, state->uniquenessRatio);
return k.run(3, globalThreads, localThreads, false); return k.run(3, globalThreads, localThreads, false);
} }
#endif
struct FindStereoCorrespInvoker : public ParallelLoopBody struct FindStereoCorrespInvoker : public ParallelLoopBody
{ {
@ -1074,6 +1082,7 @@ public:
int FILTERED = (params.minDisparity - 1) << DISPARITY_SHIFT; int FILTERED = (params.minDisparity - 1) << DISPARITY_SHIFT;
#ifdef HAVE_OPENCL
if(ocl::useOpenCL() && disparr.isUMat() && params.textureThreshold == 0) if(ocl::useOpenCL() && disparr.isUMat() && params.textureThreshold == 0)
{ {
UMat left, right; UMat left, right;
@ -1090,6 +1099,7 @@ public:
} }
} }
} }
#endif
Mat left0 = leftarr.getMat(), right0 = rightarr.getMat(); Mat left0 = leftarr.getMat(), right0 = rightarr.getMat();
disparr.create(left0.size(), dtype); disparr.create(left0.size(), dtype);

View File

@ -1364,10 +1364,16 @@ void pow( InputArray _src, double power, OutputArray _dst )
{ {
int type = _src.type(), depth = CV_MAT_DEPTH(type), int type = _src.type(), depth = CV_MAT_DEPTH(type),
cn = CV_MAT_CN(type), ipower = cvRound(power); cn = CV_MAT_CN(type), ipower = cvRound(power);
bool is_ipower = fabs(ipower - power) < DBL_EPSILON, bool is_ipower = fabs(ipower - power) < DBL_EPSILON;
useOpenCL = _dst.isUMat() && _src.dims() <= 2; #ifdef HAVE_OPENCL
bool useOpenCL = _dst.isUMat() && _src.dims() <= 2;
#endif
if( is_ipower && !(ocl::Device::getDefault().isIntel() && useOpenCL && depth != CV_64F)) if( is_ipower
#ifdef HAVE_OPENCL
&& !(useOpenCL && ocl::Device::getDefault().isIntel() && depth != CV_64F)
#endif
)
{ {
switch( ipower ) switch( ipower )
{ {

View File

@ -54,7 +54,9 @@
#include "opencv2/core/private.hpp" #include "opencv2/core/private.hpp"
#include "opencv2/core/private.cuda.hpp" #include "opencv2/core/private.cuda.hpp"
#ifdef HAVE_OPENCL
#include "opencv2/core/ocl.hpp" #include "opencv2/core/ocl.hpp"
#endif
#include "opencv2/hal.hpp" #include "opencv2/hal.hpp"
@ -262,7 +264,11 @@ struct ImplCollector
struct CoreTLSData struct CoreTLSData
{ {
CoreTLSData() : device(0), useOpenCL(-1), useIPP(-1) CoreTLSData() :
//#ifdef HAVE_OPENCL
device(0), useOpenCL(-1),
//#endif
useIPP(-1)
{ {
#ifdef HAVE_TEGRA_OPTIMIZATION #ifdef HAVE_TEGRA_OPTIMIZATION
useTegra = -1; useTegra = -1;
@ -270,9 +276,11 @@ struct CoreTLSData
} }
RNG rng; RNG rng;
//#ifdef HAVE_OPENCL
int device; int device;
ocl::Queue oclQueue; ocl::Queue oclQueue;
int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized
//#endif
int useIPP; // 1 - use, 0 - do not use, -1 - auto/not initialized int useIPP; // 1 - use, 0 - do not use, -1 - auto/not initialized
#ifdef HAVE_TEGRA_OPTIMIZATION #ifdef HAVE_TEGRA_OPTIMIZATION
int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized

View File

@ -391,7 +391,9 @@ void setUseOptimized( bool flag )
USE_SSE2 = currentFeatures->have[CV_CPU_SSE2]; USE_SSE2 = currentFeatures->have[CV_CPU_SSE2];
ipp::setUseIPP(flag); ipp::setUseIPP(flag);
#ifdef HAVE_OPENCL
ocl::setUseOpenCL(flag); ocl::setUseOpenCL(flag);
#endif
#ifdef HAVE_TEGRA_OPTIMIZATION #ifdef HAVE_TEGRA_OPTIMIZATION
::tegra::setUseTegra(flag); ::tegra::setUseTegra(flag);
#endif #endif

View File

@ -691,6 +691,7 @@ INSTANTIATE_TEST_CASE_P(UMat, getUMat, Combine(
///////////////////////////////////////////////////////////////// OpenCL //////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// OpenCL ////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_OPENCL
TEST(UMat, BufferPoolGrowing) TEST(UMat, BufferPoolGrowing)
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -716,6 +717,7 @@ TEST(UMat, BufferPoolGrowing)
else else
std::cout << "Skipped, no OpenCL" << std::endl; std::cout << "Skipped, no OpenCL" << std::endl;
} }
#endif
class CV_UMatTest : class CV_UMatTest :
public cvtest::BaseTest public cvtest::BaseTest
@ -956,6 +958,9 @@ TEST(UMat, CopyToIfDeviceCopyIsObsolete)
TEST(UMat, setOpenCL) TEST(UMat, setOpenCL)
{ {
#ifndef HAVE_OPENCL
return; // test skipped
#else
// save the current state // save the current state
bool useOCL = cv::ocl::useOpenCL(); bool useOCL = cv::ocl::useOpenCL();
@ -993,6 +998,7 @@ TEST(UMat, setOpenCL)
// reset state to the previous one // reset state to the previous one
cv::ocl::setUseOpenCL(useOCL); cv::ocl::setUseOpenCL(useOCL);
#endif
} }
TEST(UMat, ReadBufferRect) TEST(UMat, ReadBufferRect)

View File

@ -250,6 +250,7 @@ void FAST_t(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bo
} }
} }
#ifdef HAVE_OPENCL
template<typename pt> template<typename pt>
struct cmp_pt struct cmp_pt
{ {
@ -326,16 +327,18 @@ static bool ocl_FAST( InputArray _img, std::vector<KeyPoint>& keypoints,
return true; return true;
} }
#endif
void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, int type) void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, int type)
{ {
#ifdef HAVE_OPENCL
if( ocl::useOpenCL() && _img.isUMat() && type == FastFeatureDetector::TYPE_9_16 && if( ocl::useOpenCL() && _img.isUMat() && type == FastFeatureDetector::TYPE_9_16 &&
ocl_FAST(_img, keypoints, threshold, nonmax_suppression, 10000)) ocl_FAST(_img, keypoints, threshold, nonmax_suppression, 10000))
{ {
CV_IMPL_ADD(CV_IMPL_OCL); CV_IMPL_ADD(CV_IMPL_OCL);
return; return;
} }
#endif
switch(type) { switch(type) {
case FastFeatureDetector::TYPE_5_8: case FastFeatureDetector::TYPE_5_8:

View File

@ -52,6 +52,7 @@ namespace cv
/////////////////////// ocl functions for BFMatcher /////////////////////////// /////////////////////// ocl functions for BFMatcher ///////////////////////////
#ifdef HAVE_OPENCL
static void ensureSizeIsEnough(int rows, int cols, int type, UMat &m) static void ensureSizeIsEnough(int rows, int cols, int type, UMat &m)
{ {
if (m.type() == type && m.rows >= rows && m.cols >= cols) if (m.type() == type && m.rows >= rows && m.cols >= cols)
@ -390,6 +391,7 @@ static bool ocl_radiusMatchDownload(const UMat &trainIdx, const UMat &distance,
return ocl_radiusMatchConvert(trainIdxCPU, distanceCPU, nMatchesCPU, matches, compactResult); return ocl_radiusMatchConvert(trainIdxCPU, distanceCPU, nMatchesCPU, matches, compactResult);
} }
#endif
/****************************************************************************************\ /****************************************************************************************\
* DescriptorMatcher * * DescriptorMatcher *
@ -693,6 +695,7 @@ Ptr<DescriptorMatcher> BFMatcher::clone( bool emptyTrainData ) const
return matcher; return matcher;
} }
#ifdef HAVE_OPENCL
static bool ocl_match(InputArray query, InputArray _train, std::vector< std::vector<DMatch> > &matches, int dstType) static bool ocl_match(InputArray query, InputArray _train, std::vector< std::vector<DMatch> > &matches, int dstType)
{ {
UMat trainIdx, distance; UMat trainIdx, distance;
@ -714,6 +717,7 @@ static bool ocl_knnMatch(InputArray query, InputArray _train, std::vector< std::
return false; return false;
return true; return true;
} }
#endif
void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn, void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn,
InputArrayOfArrays _masks, bool compactResult ) InputArrayOfArrays _masks, bool compactResult )
@ -744,6 +748,7 @@ void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector<std::vec
utrainDescCollection.clear(); utrainDescCollection.clear();
} }
#ifdef HAVE_OPENCL
int trainDescVectorSize = trainDescCollection.empty() ? (int)utrainDescCollection.size() : (int)trainDescCollection.size(); int trainDescVectorSize = trainDescCollection.empty() ? (int)utrainDescCollection.size() : (int)trainDescCollection.size();
Size trainDescSize = trainDescCollection.empty() ? utrainDescCollection[0].size() : trainDescCollection[0].size(); Size trainDescSize = trainDescCollection.empty() ? utrainDescCollection[0].size() : trainDescCollection[0].size();
int trainDescOffset = trainDescCollection.empty() ? (int)utrainDescCollection[0].offset : 0; int trainDescOffset = trainDescCollection.empty() ? (int)utrainDescCollection[0].offset : 0;
@ -791,6 +796,7 @@ void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector<std::vec
} }
} }
} }
#endif
Mat queryDescriptors = _queryDescriptors.getMat(); Mat queryDescriptors = _queryDescriptors.getMat();
if(trainDescCollection.empty() && !utrainDescCollection.empty()) if(trainDescCollection.empty() && !utrainDescCollection.empty())
@ -851,6 +857,7 @@ void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector<std::vec
} }
} }
#ifdef HAVE_OPENCL
static bool ocl_radiusMatch(InputArray query, InputArray _train, std::vector< std::vector<DMatch> > &matches, static bool ocl_radiusMatch(InputArray query, InputArray _train, std::vector< std::vector<DMatch> > &matches,
float maxDistance, int dstType, bool compactResult) float maxDistance, int dstType, bool compactResult)
{ {
@ -861,6 +868,7 @@ static bool ocl_radiusMatch(InputArray query, InputArray _train, std::vector< st
return false; return false;
return true; return true;
} }
#endif
void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector<std::vector<DMatch> >& matches, void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector<std::vector<DMatch> >& matches,
float maxDistance, InputArrayOfArrays _masks, bool compactResult ) float maxDistance, InputArrayOfArrays _masks, bool compactResult )
@ -888,6 +896,7 @@ void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector<std::
utrainDescCollection.clear(); utrainDescCollection.clear();
} }
#ifdef HAVE_OPENCL
int trainDescVectorSize = trainDescCollection.empty() ? (int)utrainDescCollection.size() : (int)trainDescCollection.size(); int trainDescVectorSize = trainDescCollection.empty() ? (int)utrainDescCollection.size() : (int)trainDescCollection.size();
Size trainDescSize = trainDescCollection.empty() ? utrainDescCollection[0].size() : trainDescCollection[0].size(); Size trainDescSize = trainDescCollection.empty() ? utrainDescCollection[0].size() : trainDescCollection[0].size();
int trainDescOffset = trainDescCollection.empty() ? (int)utrainDescCollection[0].offset : 0; int trainDescOffset = trainDescCollection.empty() ? (int)utrainDescCollection[0].offset : 0;
@ -913,6 +922,7 @@ void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector<std::
} }
} }
} }
#endif
Mat queryDescriptors = _queryDescriptors.getMat(); Mat queryDescriptors = _queryDescriptors.getMat();
if(trainDescCollection.empty() && !utrainDescCollection.empty()) if(trainDescCollection.empty() && !utrainDescCollection.empty())

View File

@ -57,6 +57,7 @@ template<typename _Tp> inline void copyVectorToUMat(const std::vector<_Tp>& v, O
Mat(1, (int)(v.size()*sizeof(v[0])), CV_8U, (void*)&v[0]).copyTo(um); Mat(1, (int)(v.size()*sizeof(v[0])), CV_8U, (void*)&v[0]).copyTo(um);
} }
#ifdef HAVE_OPENCL
static bool static bool
ocl_HarrisResponses(const UMat& imgbuf, ocl_HarrisResponses(const UMat& imgbuf,
const UMat& layerinfo, const UMat& layerinfo,
@ -120,7 +121,7 @@ ocl_computeOrbDescriptors(const UMat& imgbuf, const UMat& layerInfo,
ocl::KernelArg::PtrReadOnly(pattern), ocl::KernelArg::PtrReadOnly(pattern),
nkeypoints, dsize).run(1, globalSize, 0, true); nkeypoints, dsize).run(1, globalSize, 0, true);
} }
#endif
/** /**
* Function that computes the Harris responses in a * Function that computes the Harris responses in a
@ -726,6 +727,7 @@ int ORB_Impl::defaultNorm() const
return NORM_HAMMING; return NORM_HAMMING;
} }
#ifdef HAVE_OPENCL
static void uploadORBKeypoints(const std::vector<KeyPoint>& src, std::vector<Vec3i>& buf, OutputArray dst) static void uploadORBKeypoints(const std::vector<KeyPoint>& src, std::vector<Vec3i>& buf, OutputArray dst)
{ {
size_t i, n = src.size(); size_t i, n = src.size();
@ -758,7 +760,7 @@ static void uploadORBKeypoints(const std::vector<KeyPoint>& src,
} }
copyVectorToUMat(buf, dst); copyVectorToUMat(buf, dst);
} }
#endif
/** Compute the ORB_Impl keypoints on an image /** Compute the ORB_Impl keypoints on an image
* @param image_pyramid the image pyramid to compute the features and descriptors on * @param image_pyramid the image pyramid to compute the features and descriptors on
@ -776,6 +778,10 @@ static void computeKeyPoints(const Mat& imagePyramid,
int edgeThreshold, int patchSize, int scoreType, int edgeThreshold, int patchSize, int scoreType,
bool useOCL, int fastThreshold ) bool useOCL, int fastThreshold )
{ {
#ifndef HAVE_OPENCL
(void)uimagePyramid;(void)ulayerInfo;(void)useOCL;
#endif
int i, nkeypoints, level, nlevels = (int)layerInfo.size(); int i, nkeypoints, level, nlevels = (int)layerInfo.size();
std::vector<int> nfeaturesPerLevel(nlevels); std::vector<int> nfeaturesPerLevel(nlevels);
@ -862,6 +868,7 @@ static void computeKeyPoints(const Mat& imagePyramid,
// Select best features using the Harris cornerness (better scoring than FAST) // Select best features using the Harris cornerness (better scoring than FAST)
if( scoreType == ORB_Impl::HARRIS_SCORE ) if( scoreType == ORB_Impl::HARRIS_SCORE )
{ {
#ifdef HAVE_OPENCL
if( useOCL ) if( useOCL )
{ {
uploadORBKeypoints(allKeypoints, ukeypoints_buf, ukeypoints); uploadORBKeypoints(allKeypoints, ukeypoints_buf, ukeypoints);
@ -877,6 +884,7 @@ static void computeKeyPoints(const Mat& imagePyramid,
} }
if( !useOCL ) if( !useOCL )
#endif
HarrisResponses(imagePyramid, layerInfo, allKeypoints, 7, HARRIS_K); HarrisResponses(imagePyramid, layerInfo, allKeypoints, 7, HARRIS_K);
std::vector<KeyPoint> newAllKeypoints; std::vector<KeyPoint> newAllKeypoints;
@ -902,6 +910,8 @@ static void computeKeyPoints(const Mat& imagePyramid,
} }
nkeypoints = (int)allKeypoints.size(); nkeypoints = (int)allKeypoints.size();
#ifdef HAVE_OPENCL
if( useOCL ) if( useOCL )
{ {
UMat uumax; UMat uumax;
@ -922,6 +932,7 @@ static void computeKeyPoints(const Mat& imagePyramid,
} }
if( !useOCL ) if( !useOCL )
#endif
{ {
ICAngles(imagePyramid, layerInfo, allKeypoints, umax, halfPatchSize); ICAngles(imagePyramid, layerInfo, allKeypoints, umax, halfPatchSize);
} }
@ -1147,6 +1158,7 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask,
GaussianBlur(workingMat, workingMat, Size(7, 7), 2, 2, BORDER_REFLECT_101); GaussianBlur(workingMat, workingMat, Size(7, 7), 2, 2, BORDER_REFLECT_101);
} }
#ifdef HAVE_OPENCL
if( useOCL ) if( useOCL )
{ {
imagePyramid.copyTo(uimagePyramid); imagePyramid.copyTo(uimagePyramid);
@ -1166,6 +1178,7 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask,
} }
if( !useOCL ) if( !useOCL )
#endif
{ {
Mat descriptors = _descriptors.getMat(); Mat descriptors = _descriptors.getMat();
computeOrbDescriptors(imagePyramid, layerInfo, layerScale, computeOrbDescriptors(imagePyramid, layerInfo, layerScale,

View File

@ -1040,7 +1040,7 @@ public:
struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } }; struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } };
struct getNeighbors { int operator ()(const CvAvgComp& e) const { return e.neighbors; } }; struct getNeighbors { int operator ()(const CvAvgComp& e) const { return e.neighbors; } };
#ifdef HAVE_OPENCL
bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector<float>& scales, bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector<float>& scales,
std::vector<Rect>& candidates ) std::vector<Rect>& candidates )
{ {
@ -1171,6 +1171,7 @@ bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector<fl
} }
return ok; return ok;
} }
#endif
bool CascadeClassifierImpl::isOldFormatCascade() const bool CascadeClassifierImpl::isOldFormatCascade() const
{ {
@ -1278,10 +1279,12 @@ void CascadeClassifierImpl::detectMultiScaleNoGrouping( InputArray _image, std::
if( scales.size() == 0 || !featureEvaluator->setImage(gray, scales) ) if( scales.size() == 0 || !featureEvaluator->setImage(gray, scales) )
return; return;
#ifdef HAVE_OPENCL
// OpenCL code // OpenCL code
CV_OCL_RUN(use_ocl, ocl_detectMultiScaleNoGrouping( scales, candidates )) CV_OCL_RUN(use_ocl, ocl_detectMultiScaleNoGrouping( scales, candidates ))
tryOpenCL = false; tryOpenCL = false;
#endif
// CPU code // CPU code
featureEvaluator->getMats(); featureEvaluator->getMats();
@ -1527,9 +1530,11 @@ bool CascadeClassifierImpl::Data::read(const FileNode &root)
bool CascadeClassifierImpl::read_(const FileNode& root) bool CascadeClassifierImpl::read_(const FileNode& root)
{ {
#ifdef HAVE_OPENCL
tryOpenCL = true; tryOpenCL = true;
haarKernel = ocl::Kernel(); haarKernel = ocl::Kernel();
lbpKernel = ocl::Kernel(); lbpKernel = ocl::Kernel();
#endif
ustages.release(); ustages.release();
unodes.release(); unodes.release();
uleaves.release(); uleaves.release();

View File

@ -125,9 +125,10 @@ protected:
int yStep, double factor, std::vector<Rect>& candidates, int yStep, double factor, std::vector<Rect>& candidates,
std::vector<int>& rejectLevels, std::vector<double>& levelWeights, std::vector<int>& rejectLevels, std::vector<double>& levelWeights,
Size sumSize0, bool outputRejectLevels = false ); Size sumSize0, bool outputRejectLevels = false );
#ifdef HAVE_OPENCL
bool ocl_detectMultiScaleNoGrouping( const std::vector<float>& scales, bool ocl_detectMultiScaleNoGrouping( const std::vector<float>& scales,
std::vector<Rect>& candidates ); std::vector<Rect>& candidates );
#endif
void detectMultiScaleNoGrouping( InputArray image, std::vector<Rect>& candidates, void detectMultiScaleNoGrouping( InputArray image, std::vector<Rect>& candidates,
std::vector<int>& rejectLevels, std::vector<double>& levelWeights, std::vector<int>& rejectLevels, std::vector<double>& levelWeights,
double scaleFactor, Size minObjectSize, Size maxObjectSize, double scaleFactor, Size minObjectSize, Size maxObjectSize,
@ -218,8 +219,10 @@ protected:
Ptr<MaskGenerator> maskGenerator; Ptr<MaskGenerator> maskGenerator;
UMat ugrayImage; UMat ugrayImage;
UMat ufacepos, ustages, unodes, uleaves, usubsets; UMat ufacepos, ustages, unodes, uleaves, usubsets;
#ifdef HAVE_OPENCL
ocl::Kernel haarKernel, lbpKernel; ocl::Kernel haarKernel, lbpKernel;
bool tryOpenCL; bool tryOpenCL;
#endif
Mutex mtx; Mutex mtx;
}; };

View File

@ -110,6 +110,7 @@ Rect PlaneWarper::buildMaps(Size src_size, InputArray K, InputArray R, InputArra
_xmap.create(dsize, CV_32FC1); _xmap.create(dsize, CV_32FC1);
_ymap.create(dsize, CV_32FC1); _ymap.create(dsize, CV_32FC1);
#ifdef HAVE_OPENCL
if (ocl::useOpenCL()) if (ocl::useOpenCL())
{ {
ocl::Kernel k("buildWarpPlaneMaps", ocl::stitching::warpers_oclsrc); ocl::Kernel k("buildWarpPlaneMaps", ocl::stitching::warpers_oclsrc);
@ -132,6 +133,7 @@ Rect PlaneWarper::buildMaps(Size src_size, InputArray K, InputArray R, InputArra
} }
} }
} }
#endif
Mat xmap = _xmap.getMat(), ymap = _ymap.getMat(); Mat xmap = _xmap.getMat(), ymap = _ymap.getMat();
@ -310,6 +312,7 @@ void SphericalPortraitWarper::detectResultRoi(Size src_size, Point &dst_tl, Poin
Rect SphericalWarper::buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap) Rect SphericalWarper::buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)
{ {
#ifdef HAVE_OPENCL
if (ocl::useOpenCL()) if (ocl::useOpenCL())
{ {
ocl::Kernel k("buildWarpSphericalMaps", ocl::stitching::warpers_oclsrc); ocl::Kernel k("buildWarpSphericalMaps", ocl::stitching::warpers_oclsrc);
@ -339,7 +342,7 @@ Rect SphericalWarper::buildMaps(Size src_size, InputArray K, InputArray R, Outpu
} }
} }
} }
#endif
return RotationWarperBase<SphericalProjector>::buildMaps(src_size, K, R, xmap, ymap); return RotationWarperBase<SphericalProjector>::buildMaps(src_size, K, R, xmap, ymap);
} }
@ -358,6 +361,7 @@ Point SphericalWarper::warp(InputArray src, InputArray K, InputArray R, int inte
Rect CylindricalWarper::buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap) Rect CylindricalWarper::buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)
{ {
#ifdef HAVE_OPENCL
if (ocl::useOpenCL()) if (ocl::useOpenCL())
{ {
ocl::Kernel k("buildWarpCylindricalMaps", ocl::stitching::warpers_oclsrc); ocl::Kernel k("buildWarpCylindricalMaps", ocl::stitching::warpers_oclsrc);
@ -388,7 +392,7 @@ Rect CylindricalWarper::buildMaps(Size src_size, InputArray K, InputArray R, Out
} }
} }
} }
#endif
return RotationWarperBase<CylindricalProjector>::buildMaps(src_size, K, R, xmap, ymap); return RotationWarperBase<CylindricalProjector>::buildMaps(src_size, K, R, xmap, ymap);
} }

View File

@ -142,8 +142,9 @@ public:
fCT = defaultfCT2; fCT = defaultfCT2;
nShadowDetection = defaultnShadowDetection2; nShadowDetection = defaultnShadowDetection2;
fTau = defaultfTau; fTau = defaultfTau;
#ifdef HAVE_OPENCL
opencl_ON = true; opencl_ON = true;
#endif
} }
//! the full constructor that takes the length of the history, //! the full constructor that takes the length of the history,
// the number of gaussian mixtures, the background ratio parameter and the noise strength // the number of gaussian mixtures, the background ratio parameter and the noise strength
@ -168,8 +169,9 @@ public:
nShadowDetection = defaultnShadowDetection2; nShadowDetection = defaultnShadowDetection2;
fTau = defaultfTau; fTau = defaultfTau;
name_ = "BackgroundSubtractor.MOG2"; name_ = "BackgroundSubtractor.MOG2";
#ifdef HAVE_OPENCL
opencl_ON = true; opencl_ON = true;
#endif
} }
//! the destructor //! the destructor
~BackgroundSubtractorMOG2Impl() {} ~BackgroundSubtractorMOG2Impl() {}
@ -190,6 +192,7 @@ public:
CV_Assert( nchannels <= CV_CN_MAX ); CV_Assert( nchannels <= CV_CN_MAX );
CV_Assert( nmixtures <= 255); CV_Assert( nmixtures <= 255);
#ifdef HAVE_OPENCL
if (ocl::useOpenCL() && opencl_ON) if (ocl::useOpenCL() && opencl_ON)
{ {
create_ocl_apply_kernel(); create_ocl_apply_kernel();
@ -218,6 +221,7 @@ public:
u_bgmodelUsedModes.setTo(cv::Scalar::all(0)); u_bgmodelUsedModes.setTo(cv::Scalar::all(0));
} }
else else
#endif
{ {
// for each gaussian mixture of each pixel bg model we store ... // for each gaussian mixture of each pixel bg model we store ...
// the mixture weight (w), // the mixture weight (w),
@ -263,11 +267,13 @@ public:
if ((bShadowDetection && detectshadows) || (!bShadowDetection && !detectshadows)) if ((bShadowDetection && detectshadows) || (!bShadowDetection && !detectshadows))
return; return;
bShadowDetection = detectshadows; bShadowDetection = detectshadows;
#ifdef HAVE_OPENCL
if (!kernel_apply.empty()) if (!kernel_apply.empty())
{ {
create_ocl_apply_kernel(); create_ocl_apply_kernel();
CV_Assert( !kernel_apply.empty() ); CV_Assert( !kernel_apply.empty() );
} }
#endif
} }
virtual int getShadowValue() const { return nShadowDetection; } virtual int getShadowValue() const { return nShadowDetection; }
@ -316,6 +322,7 @@ protected:
Mat bgmodel; Mat bgmodel;
Mat bgmodelUsedModes;//keep track of number of modes per pixel Mat bgmodelUsedModes;//keep track of number of modes per pixel
#ifdef HAVE_OPENCL
//for OCL //for OCL
mutable bool opencl_ON; mutable bool opencl_ON;
@ -327,6 +334,7 @@ protected:
mutable ocl::Kernel kernel_apply; mutable ocl::Kernel kernel_apply;
mutable ocl::Kernel kernel_getBg; mutable ocl::Kernel kernel_getBg;
#endif
int nframes; int nframes;
int history; int history;
@ -379,9 +387,11 @@ protected:
String name_; String name_;
#ifdef HAVE_OPENCL
bool ocl_getBackgroundImage(OutputArray backgroundImage) const; bool ocl_getBackgroundImage(OutputArray backgroundImage) const;
bool ocl_apply(InputArray _image, OutputArray _fgmask, double learningRate=-1); bool ocl_apply(InputArray _image, OutputArray _fgmask, double learningRate=-1);
void create_ocl_apply_kernel(); void create_ocl_apply_kernel();
#endif
}; };
struct GaussBGStatModel2Params struct GaussBGStatModel2Params
@ -810,8 +820,6 @@ bool BackgroundSubtractorMOG2Impl::ocl_getBackgroundImage(OutputArray _backgroun
return kernel_getBg.run(2, globalsize, NULL, false); return kernel_getBg.run(2, globalsize, NULL, false);
} }
#endif
void BackgroundSubtractorMOG2Impl::create_ocl_apply_kernel() void BackgroundSubtractorMOG2Impl::create_ocl_apply_kernel()
{ {
int nchannels = CV_MAT_CN(frameType); int nchannels = CV_MAT_CN(frameType);
@ -819,6 +827,8 @@ void BackgroundSubtractorMOG2Impl::create_ocl_apply_kernel()
kernel_apply.create("mog2_kernel", ocl::video::bgfg_mog2_oclsrc, opts); kernel_apply.create("mog2_kernel", ocl::video::bgfg_mog2_oclsrc, opts);
} }
#endif
void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, double learningRate) void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, double learningRate)
{ {
bool needToInitialize = nframes == 0 || learningRate >= 1 || _image.size() != frameSize || _image.type() != frameType; bool needToInitialize = nframes == 0 || learningRate >= 1 || _image.size() != frameSize || _image.type() != frameType;
@ -826,6 +836,7 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask,
if( needToInitialize ) if( needToInitialize )
initialize(_image.size(), _image.type()); initialize(_image.size(), _image.type());
#ifdef HAVE_OPENCL
if (opencl_ON) if (opencl_ON)
{ {
CV_OCL_RUN(opencl_ON, ocl_apply(_image, _fgmask, learningRate)) CV_OCL_RUN(opencl_ON, ocl_apply(_image, _fgmask, learningRate))
@ -833,6 +844,7 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask,
opencl_ON = false; opencl_ON = false;
initialize(_image.size(), _image.type()); initialize(_image.size(), _image.type());
} }
#endif
Mat image = _image.getMat(); Mat image = _image.getMat();
_fgmask.create( image.size(), CV_8U ); _fgmask.create( image.size(), CV_8U );
@ -856,6 +868,7 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask,
void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImage) const void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImage) const
{ {
#ifdef HAVE_OPENCL
if (opencl_ON) if (opencl_ON)
{ {
CV_OCL_RUN(opencl_ON, ocl_getBackgroundImage(backgroundImage)) CV_OCL_RUN(opencl_ON, ocl_getBackgroundImage(backgroundImage))
@ -863,6 +876,7 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImag
opencl_ON = false; opencl_ON = false;
return; return;
} }
#endif
int nchannels = CV_MAT_CN(frameType); int nchannels = CV_MAT_CN(frameType);
CV_Assert(nchannels == 1 || nchannels == 3); CV_Assert(nchannels == 1 || nchannels == 3);

View File

@ -837,6 +837,7 @@ int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Si
return maxLevel; return maxLevel;
} }
#ifdef HAVE_OPENCL
namespace cv namespace cv
{ {
class PyrLKOpticalFlow class PyrLKOpticalFlow
@ -1084,6 +1085,7 @@ namespace cv
return opticalFlow.sparse(_prevImg.getUMat(), _nextImg.getUMat(), _prevPts.getUMat(), umatNextPts, umatStatus, umatErr); return opticalFlow.sparse(_prevImg.getUMat(), _nextImg.getUMat(), _prevPts.getUMat(), umatNextPts, umatStatus, umatErr);
} }
}; };
#endif
void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg, void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
InputArray _prevPts, InputOutputArray _nextPts, InputArray _prevPts, InputOutputArray _nextPts,
@ -1092,6 +1094,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
TermCriteria criteria, TermCriteria criteria,
int flags, double minEigThreshold ) int flags, double minEigThreshold )
{ {
#ifdef HAVE_OPENCL
bool use_opencl = ocl::useOpenCL() && bool use_opencl = ocl::useOpenCL() &&
(_prevImg.isUMat() || _nextImg.isUMat()) && (_prevImg.isUMat() || _nextImg.isUMat()) &&
ocl::Image2D::isFormatSupported(CV_32F, 1, false); ocl::Image2D::isFormatSupported(CV_32F, 1, false);
@ -1100,6 +1103,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
CV_IMPL_ADD(CV_IMPL_OCL); CV_IMPL_ADD(CV_IMPL_OCL);
return; return;
} }
#endif
Mat prevPtsMat = _prevPts.getMat(); Mat prevPtsMat = _prevPts.getMat();
const int derivDepth = DataType<cv::detail::deriv_type>::depth; const int derivDepth = DataType<cv::detail::deriv_type>::depth;

View File

@ -583,6 +583,7 @@ FarnebackUpdateFlow_GaussianBlur( const Mat& _R0, const Mat& _R1,
} }
#ifdef HAVE_OPENCL
namespace cv namespace cv
{ {
class FarnebackOpticalFlow class FarnebackOpticalFlow
@ -1074,17 +1075,20 @@ static bool ocl_calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0,
return true; return true;
} }
} }
#endif // HAVE_OPENCL
void cv::calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0, void cv::calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0,
InputOutputArray _flow0, double pyr_scale, int levels, int winsize, InputOutputArray _flow0, double pyr_scale, int levels, int winsize,
int iterations, int poly_n, double poly_sigma, int flags ) int iterations, int poly_n, double poly_sigma, int flags )
{ {
#ifdef HAVE_OPENCL
bool use_opencl = ocl::useOpenCL() && _flow0.isUMat(); bool use_opencl = ocl::useOpenCL() && _flow0.isUMat();
if( use_opencl && ocl_calcOpticalFlowFarneback(_prev0, _next0, _flow0, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags)) if( use_opencl && ocl_calcOpticalFlowFarneback(_prev0, _next0, _flow0, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags))
{ {
CV_IMPL_ADD(CV_IMPL_OCL); CV_IMPL_ADD(CV_IMPL_OCL);
return; return;
} }
#endif
Mat prev0 = _prev0.getMat(), next0 = _next0.getMat(); Mat prev0 = _prev0.getMat(), next0 = _next0.getMat();
const int min_size = 32; const int min_size = 32;

View File

@ -122,11 +122,13 @@ protected:
int medianFiltering; int medianFiltering;
private: private:
void procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2, Mat_<float>& u3); void procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2, Mat_<float>& u3);
#ifdef HAVE_OPENCL
bool procOneScale_ocl(const UMat& I0, const UMat& I1, UMat& u1, UMat& u2); bool procOneScale_ocl(const UMat& I0, const UMat& I1, UMat& u1, UMat& u2);
bool calc_ocl(InputArray I0, InputArray I1, InputOutputArray flow); bool calc_ocl(InputArray I0, InputArray I1, InputOutputArray flow);
#endif
struct dataMat struct dataMat
{ {
std::vector<Mat_<float> > I0s; std::vector<Mat_<float> > I0s;
@ -170,6 +172,8 @@ private:
Mat_<float> u3x_buf; Mat_<float> u3x_buf;
Mat_<float> u3y_buf; Mat_<float> u3y_buf;
} dm; } dm;
#ifdef HAVE_OPENCL
struct dataUMat struct dataUMat
{ {
std::vector<UMat> I0s; std::vector<UMat> I0s;
@ -195,8 +199,10 @@ private:
UMat diff_buf; UMat diff_buf;
UMat norm_buf; UMat norm_buf;
} dum; } dum;
#endif
}; };
#ifdef HAVE_OPENCL
namespace cv_ocl_tvl1flow namespace cv_ocl_tvl1flow
{ {
bool centeredGradient(const UMat &src, UMat &dx, UMat &dy); bool centeredGradient(const UMat &src, UMat &dx, UMat &dy);
@ -353,6 +359,7 @@ bool cv_ocl_tvl1flow::estimateDualVariables(UMat &u1, UMat &u2,
return kernel.run(2, globalsize, NULL, false); return kernel.run(2, globalsize, NULL, false);
} }
#endif
OpticalFlowDual_TVL1::OpticalFlowDual_TVL1() OpticalFlowDual_TVL1::OpticalFlowDual_TVL1()
{ {
@ -499,6 +506,7 @@ void OpticalFlowDual_TVL1::calc(InputArray _I0, InputArray _I1, InputOutputArray
merge(uxy, 2, _flow); merge(uxy, 2, _flow);
} }
#ifdef HAVE_OPENCL
bool OpticalFlowDual_TVL1::calc_ocl(InputArray _I0, InputArray _I1, InputOutputArray _flow) bool OpticalFlowDual_TVL1::calc_ocl(InputArray _I0, InputArray _I1, InputOutputArray _flow)
{ {
UMat I0 = _I0.getUMat(); UMat I0 = _I0.getUMat();
@ -598,6 +606,7 @@ bool OpticalFlowDual_TVL1::calc_ocl(InputArray _I0, InputArray _I1, InputOutputA
merge(uxy, _flow); merge(uxy, _flow);
return true; return true;
} }
#endif
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// buildFlowMap // buildFlowMap
@ -1180,6 +1189,7 @@ void estimateDualVariables(const Mat_<float>& u1x, const Mat_<float>& u1y,
parallel_for_(Range(0, u1x.rows), body); parallel_for_(Range(0, u1x.rows), body);
} }
#ifdef HAVE_OPENCL
bool OpticalFlowDual_TVL1::procOneScale_ocl(const UMat& I0, const UMat& I1, UMat& u1, UMat& u2) bool OpticalFlowDual_TVL1::procOneScale_ocl(const UMat& I0, const UMat& I1, UMat& u1, UMat& u2)
{ {
using namespace cv_ocl_tvl1flow; using namespace cv_ocl_tvl1flow;
@ -1267,6 +1277,7 @@ bool OpticalFlowDual_TVL1::procOneScale_ocl(const UMat& I0, const UMat& I1, UMat
} }
return true; return true;
} }
#endif
void OpticalFlowDual_TVL1::procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2, Mat_<float>& u3) void OpticalFlowDual_TVL1::procOneScale(const Mat_<float>& I0, const Mat_<float>& I1, Mat_<float>& u1, Mat_<float>& u2, Mat_<float>& u3)
{ {
@ -1402,6 +1413,7 @@ void OpticalFlowDual_TVL1::collectGarbage()
dm.u2x_buf.release(); dm.u2x_buf.release();
dm.u2y_buf.release(); dm.u2y_buf.release();
#ifdef HAVE_OPENCL
//dataUMat structure dum //dataUMat structure dum
dum.I0s.clear(); dum.I0s.clear();
dum.I1s.clear(); dum.I1s.clear();
@ -1425,6 +1437,7 @@ void OpticalFlowDual_TVL1::collectGarbage()
dum.diff_buf.release(); dum.diff_buf.release();
dum.norm_buf.release(); dum.norm_buf.release();
#endif
} }
} // namespace } // namespace