From 7213e5f68a3d8e80ad01d31eb891f47baf18bb91 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 19 Jun 2015 20:52:14 +0300 Subject: [PATCH 1/3] ocl: correct disabling of OpenCL code --- cmake/cl2cpp.cmake | 8 ++++++-- modules/calib3d/src/stereobm.cpp | 10 ++++++++++ modules/core/src/mathfuncs.cpp | 12 +++++++++--- modules/core/src/precomp.hpp | 10 +++++++++- modules/core/src/system.cpp | 2 ++ modules/core/test/test_umat.cpp | 6 ++++++ modules/features2d/src/fast.cpp | 5 ++++- modules/features2d/src/matchers.cpp | 10 ++++++++++ modules/features2d/src/orb.cpp | 17 +++++++++++++++-- modules/objdetect/src/cascadedetect.cpp | 7 ++++++- modules/objdetect/src/cascadedetect.hpp | 5 ++++- modules/stitching/src/warpers.cpp | 8 ++++++-- modules/video/src/bgfg_gaussmix2.cpp | 22 ++++++++++++++++++---- modules/video/src/lkpyramid.cpp | 4 ++++ modules/video/src/optflowgf.cpp | 4 ++++ modules/video/src/tvl1flow.cpp | 15 ++++++++++++++- 16 files changed, 127 insertions(+), 18 deletions(-) diff --git a/cmake/cl2cpp.cmake b/cmake/cl2cpp.cmake index c0e211b900..d5e3d5f030 100644 --- a/cmake/cl2cpp.cmake +++ b/cmake/cl2cpp.cmake @@ -26,6 +26,8 @@ set(STR_CPP "// This file is auto-generated. Do not edit! #include \"precomp.hpp\" #include \"${OUTPUT_HPP_NAME}\" +#ifdef HAVE_OPENCL + namespace cv { 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/opencl/ocl_defs.hpp\" +#ifdef HAVE_OPENCL + namespace cv { namespace ocl @@ -82,8 +86,8 @@ foreach(cl ${cl_list}) set(STR_HPP "${STR_HPP}${STR_HPP_DECL}") endforeach() -set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n") -set(STR_HPP "${STR_HPP}}\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#endif\n") file(WRITE "${OUTPUT}" "${STR_CPP}") diff --git a/modules/calib3d/src/stereobm.cpp b/modules/calib3d/src/stereobm.cpp index 760abefa84..8f53e770bd 100644 --- a/modules/calib3d/src/stereobm.cpp +++ b/modules/calib3d/src/stereobm.cpp @@ -86,6 +86,7 @@ struct StereoBMParams int dispType; }; +#ifdef HAVE_OPENCL 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)); @@ -106,6 +107,7 @@ static bool ocl_prefilter_norm(InputArray _input, OutputArray _output, int winsi return k.run(2, globalThreads, NULL, false); } +#endif 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) { 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); } +#endif static void 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) { if( state->preFilterType == StereoBM::PREFILTER_NORMALIZED_RESPONSE ) @@ -867,6 +872,7 @@ static bool ocl_prefiltering(InputArray left0, InputArray right0, OutputArray le } return true; } +#endif struct PrefilterInvoker : public ParallelLoopBody { @@ -896,6 +902,7 @@ struct PrefilterInvoker : public ParallelLoopBody StereoBMParams* state; }; +#ifdef HAVE_OPENCL static bool ocl_stereobm( InputArray _left, InputArray _right, OutputArray _disp, StereoBMParams* state) { @@ -940,6 +947,7 @@ static bool ocl_stereobm( InputArray _left, InputArray _right, idx = k.set(idx, state->uniquenessRatio); return k.run(3, globalThreads, localThreads, false); } +#endif struct FindStereoCorrespInvoker : public ParallelLoopBody { @@ -1074,6 +1082,7 @@ public: int FILTERED = (params.minDisparity - 1) << DISPARITY_SHIFT; +#ifdef HAVE_OPENCL if(ocl::useOpenCL() && disparr.isUMat() && params.textureThreshold == 0) { UMat left, right; @@ -1090,6 +1099,7 @@ public: } } } +#endif Mat left0 = leftarr.getMat(), right0 = rightarr.getMat(); disparr.create(left0.size(), dtype); diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 97f36299d8..107dafce9e 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -1364,10 +1364,16 @@ void pow( InputArray _src, double power, OutputArray _dst ) { int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), ipower = cvRound(power); - bool is_ipower = fabs(ipower - power) < DBL_EPSILON, - useOpenCL = _dst.isUMat() && _src.dims() <= 2; + bool is_ipower = fabs(ipower - power) < DBL_EPSILON; +#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 ) { diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index e20a66f697..cf15359eab 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -54,7 +54,9 @@ #include "opencv2/core/private.hpp" #include "opencv2/core/private.cuda.hpp" +#ifdef HAVE_OPENCL #include "opencv2/core/ocl.hpp" +#endif #include "opencv2/hal.hpp" @@ -262,7 +264,11 @@ struct ImplCollector struct CoreTLSData { - CoreTLSData() : device(0), useOpenCL(-1), useIPP(-1) + CoreTLSData() : +#ifdef HAVE_OPENCL + device(0), useOpenCL(-1), +#endif + useIPP(-1) { #ifdef HAVE_TEGRA_OPTIMIZATION useTegra = -1; @@ -270,9 +276,11 @@ struct CoreTLSData } RNG rng; +#ifdef HAVE_OPENCL int device; ocl::Queue oclQueue; 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 #ifdef HAVE_TEGRA_OPTIMIZATION int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 743341daf5..bcad78c766 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -405,7 +405,9 @@ void setUseOptimized( bool flag ) USE_SSE2 = currentFeatures->have[CV_CPU_SSE2]; ipp::setUseIPP(flag); +#ifdef HAVE_OPENCL ocl::setUseOpenCL(flag); +#endif #ifdef HAVE_TEGRA_OPTIMIZATION ::tegra::setUseTegra(flag); #endif diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index bdd38a0a12..5afafbdb73 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -691,6 +691,7 @@ INSTANTIATE_TEST_CASE_P(UMat, getUMat, Combine( ///////////////////////////////////////////////////////////////// OpenCL //////////////////////////////////////////////////////////////////////////// +#ifdef HAVE_OPENCL TEST(UMat, BufferPoolGrowing) { #ifdef _DEBUG @@ -716,6 +717,7 @@ TEST(UMat, BufferPoolGrowing) else std::cout << "Skipped, no OpenCL" << std::endl; } +#endif class CV_UMatTest : public cvtest::BaseTest @@ -956,6 +958,10 @@ TEST(UMat, CopyToIfDeviceCopyIsObsolete) TEST(UMat, setOpenCL) { +#ifndef HAVE_OPENCL + return; // test skipped +#endif + // save the current state bool useOCL = cv::ocl::useOpenCL(); diff --git a/modules/features2d/src/fast.cpp b/modules/features2d/src/fast.cpp index 11c039a0cc..25473c84af 100644 --- a/modules/features2d/src/fast.cpp +++ b/modules/features2d/src/fast.cpp @@ -250,6 +250,7 @@ void FAST_t(InputArray _img, std::vector& keypoints, int threshold, bo } } +#ifdef HAVE_OPENCL template struct cmp_pt { @@ -326,16 +327,18 @@ static bool ocl_FAST( InputArray _img, std::vector& keypoints, return true; } - +#endif void FAST(InputArray _img, std::vector& keypoints, int threshold, bool nonmax_suppression, int type) { +#ifdef HAVE_OPENCL if( ocl::useOpenCL() && _img.isUMat() && type == FastFeatureDetector::TYPE_9_16 && ocl_FAST(_img, keypoints, threshold, nonmax_suppression, 10000)) { CV_IMPL_ADD(CV_IMPL_OCL); return; } +#endif switch(type) { case FastFeatureDetector::TYPE_5_8: diff --git a/modules/features2d/src/matchers.cpp b/modules/features2d/src/matchers.cpp index 8d2f69ec83..413340ca66 100644 --- a/modules/features2d/src/matchers.cpp +++ b/modules/features2d/src/matchers.cpp @@ -52,6 +52,7 @@ namespace cv /////////////////////// ocl functions for BFMatcher /////////////////////////// +#ifdef HAVE_OPENCL static void ensureSizeIsEnough(int rows, int cols, int type, UMat &m) { 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); } +#endif /****************************************************************************************\ * DescriptorMatcher * @@ -693,6 +695,7 @@ Ptr BFMatcher::clone( bool emptyTrainData ) const return matcher; } +#ifdef HAVE_OPENCL static bool ocl_match(InputArray query, InputArray _train, std::vector< std::vector > &matches, int dstType) { UMat trainIdx, distance; @@ -714,6 +717,7 @@ static bool ocl_knnMatch(InputArray query, InputArray _train, std::vector< std:: return false; return true; } +#endif void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector >& matches, int knn, InputArrayOfArrays _masks, bool compactResult ) @@ -744,6 +748,7 @@ void BFMatcher::knnMatchImpl( InputArray _queryDescriptors, std::vector > &matches, float maxDistance, int dstType, bool compactResult) { @@ -861,6 +868,7 @@ static bool ocl_radiusMatch(InputArray query, InputArray _train, std::vector< st return false; return true; } +#endif void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector >& matches, float maxDistance, InputArrayOfArrays _masks, bool compactResult ) @@ -888,6 +896,7 @@ void BFMatcher::radiusMatchImpl( InputArray _queryDescriptors, std::vector inline void copyVectorToUMat(const std::vector<_Tp>& v, O Mat(1, (int)(v.size()*sizeof(v[0])), CV_8U, (void*)&v[0]).copyTo(um); } +#ifdef HAVE_OPENCL static bool ocl_HarrisResponses(const UMat& imgbuf, const UMat& layerinfo, @@ -120,7 +121,7 @@ ocl_computeOrbDescriptors(const UMat& imgbuf, const UMat& layerInfo, ocl::KernelArg::PtrReadOnly(pattern), nkeypoints, dsize).run(1, globalSize, 0, true); } - +#endif /** * Function that computes the Harris responses in a @@ -726,6 +727,7 @@ int ORB_Impl::defaultNorm() const return NORM_HAMMING; } +#ifdef HAVE_OPENCL static void uploadORBKeypoints(const std::vector& src, std::vector& buf, OutputArray dst) { size_t i, n = src.size(); @@ -758,7 +760,7 @@ static void uploadORBKeypoints(const std::vector& src, } copyVectorToUMat(buf, dst); } - +#endif /** Compute the ORB_Impl keypoints on an image * @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, bool useOCL, int fastThreshold ) { +#ifndef HAVE_OPENCL + (void)uimagePyramid;(void)ulayerInfo;(void)useOCL; +#endif + int i, nkeypoints, level, nlevels = (int)layerInfo.size(); std::vector nfeaturesPerLevel(nlevels); @@ -862,6 +868,7 @@ static void computeKeyPoints(const Mat& imagePyramid, // Select best features using the Harris cornerness (better scoring than FAST) if( scoreType == ORB_Impl::HARRIS_SCORE ) { +#ifdef HAVE_OPENCL if( useOCL ) { uploadORBKeypoints(allKeypoints, ukeypoints_buf, ukeypoints); @@ -877,6 +884,7 @@ static void computeKeyPoints(const Mat& imagePyramid, } if( !useOCL ) +#endif HarrisResponses(imagePyramid, layerInfo, allKeypoints, 7, HARRIS_K); std::vector newAllKeypoints; @@ -902,6 +910,8 @@ static void computeKeyPoints(const Mat& imagePyramid, } nkeypoints = (int)allKeypoints.size(); + +#ifdef HAVE_OPENCL if( useOCL ) { UMat uumax; @@ -922,6 +932,7 @@ static void computeKeyPoints(const Mat& imagePyramid, } if( !useOCL ) +#endif { 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); } +#ifdef HAVE_OPENCL if( useOCL ) { imagePyramid.copyTo(uimagePyramid); @@ -1166,6 +1178,7 @@ void ORB_Impl::detectAndCompute( InputArray _image, InputArray _mask, } if( !useOCL ) +#endif { Mat descriptors = _descriptors.getMat(); computeOrbDescriptors(imagePyramid, layerInfo, layerScale, diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 20800ae525..afe772a47d 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -1040,7 +1040,7 @@ public: struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } }; struct getNeighbors { int operator ()(const CvAvgComp& e) const { return e.neighbors; } }; - +#ifdef HAVE_OPENCL bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vector& scales, std::vector& candidates ) { @@ -1171,6 +1171,7 @@ bool CascadeClassifierImpl::ocl_detectMultiScaleNoGrouping( const std::vectorsetImage(gray, scales) ) return; +#ifdef HAVE_OPENCL // OpenCL code CV_OCL_RUN(use_ocl, ocl_detectMultiScaleNoGrouping( scales, candidates )) tryOpenCL = false; +#endif // CPU code featureEvaluator->getMats(); @@ -1527,9 +1530,11 @@ bool CascadeClassifierImpl::Data::read(const FileNode &root) bool CascadeClassifierImpl::read_(const FileNode& root) { +#ifdef HAVE_OPENCL tryOpenCL = true; haarKernel = ocl::Kernel(); lbpKernel = ocl::Kernel(); +#endif ustages.release(); unodes.release(); uleaves.release(); diff --git a/modules/objdetect/src/cascadedetect.hpp b/modules/objdetect/src/cascadedetect.hpp index 696ab40d05..a395428d67 100644 --- a/modules/objdetect/src/cascadedetect.hpp +++ b/modules/objdetect/src/cascadedetect.hpp @@ -125,9 +125,10 @@ protected: int yStep, double factor, std::vector& candidates, std::vector& rejectLevels, std::vector& levelWeights, Size sumSize0, bool outputRejectLevels = false ); +#ifdef HAVE_OPENCL bool ocl_detectMultiScaleNoGrouping( const std::vector& scales, std::vector& candidates ); - +#endif void detectMultiScaleNoGrouping( InputArray image, std::vector& candidates, std::vector& rejectLevels, std::vector& levelWeights, double scaleFactor, Size minObjectSize, Size maxObjectSize, @@ -218,8 +219,10 @@ protected: Ptr maskGenerator; UMat ugrayImage; UMat ufacepos, ustages, unodes, uleaves, usubsets; +#ifdef HAVE_OPENCL ocl::Kernel haarKernel, lbpKernel; bool tryOpenCL; +#endif Mutex mtx; }; diff --git a/modules/stitching/src/warpers.cpp b/modules/stitching/src/warpers.cpp index 141fdec357..60dce0ff34 100644 --- a/modules/stitching/src/warpers.cpp +++ b/modules/stitching/src/warpers.cpp @@ -110,6 +110,7 @@ Rect PlaneWarper::buildMaps(Size src_size, InputArray K, InputArray R, InputArra _xmap.create(dsize, CV_32FC1); _ymap.create(dsize, CV_32FC1); +#ifdef HAVE_OPENCL if (ocl::useOpenCL()) { 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(); @@ -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) { +#ifdef HAVE_OPENCL if (ocl::useOpenCL()) { 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::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) { +#ifdef HAVE_OPENCL if (ocl::useOpenCL()) { 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::buildMaps(src_size, K, R, xmap, ymap); } diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index 226af9dc02..55cbf9d980 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -142,8 +142,9 @@ public: fCT = defaultfCT2; nShadowDetection = defaultnShadowDetection2; fTau = defaultfTau; - +#ifdef HAVE_OPENCL opencl_ON = true; +#endif } //! the full constructor that takes the length of the history, // the number of gaussian mixtures, the background ratio parameter and the noise strength @@ -168,8 +169,9 @@ public: nShadowDetection = defaultnShadowDetection2; fTau = defaultfTau; name_ = "BackgroundSubtractor.MOG2"; - +#ifdef HAVE_OPENCL opencl_ON = true; +#endif } //! the destructor ~BackgroundSubtractorMOG2Impl() {} @@ -190,6 +192,7 @@ public: CV_Assert( nchannels <= CV_CN_MAX ); CV_Assert( nmixtures <= 255); +#ifdef HAVE_OPENCL if (ocl::useOpenCL() && opencl_ON) { create_ocl_apply_kernel(); @@ -218,6 +221,7 @@ public: u_bgmodelUsedModes.setTo(cv::Scalar::all(0)); } else +#endif { // for each gaussian mixture of each pixel bg model we store ... // the mixture weight (w), @@ -263,11 +267,13 @@ public: if ((bShadowDetection && detectshadows) || (!bShadowDetection && !detectshadows)) return; bShadowDetection = detectshadows; +#ifdef HAVE_OPENCL if (!kernel_apply.empty()) { create_ocl_apply_kernel(); CV_Assert( !kernel_apply.empty() ); } +#endif } virtual int getShadowValue() const { return nShadowDetection; } @@ -316,6 +322,7 @@ protected: Mat bgmodel; Mat bgmodelUsedModes;//keep track of number of modes per pixel +#ifdef HAVE_OPENCL //for OCL mutable bool opencl_ON; @@ -327,6 +334,7 @@ protected: mutable ocl::Kernel kernel_apply; mutable ocl::Kernel kernel_getBg; +#endif int nframes; int history; @@ -379,9 +387,11 @@ protected: String name_; +#ifdef HAVE_OPENCL bool ocl_getBackgroundImage(OutputArray backgroundImage) const; bool ocl_apply(InputArray _image, OutputArray _fgmask, double learningRate=-1); void create_ocl_apply_kernel(); +#endif }; struct GaussBGStatModel2Params @@ -810,8 +820,6 @@ bool BackgroundSubtractorMOG2Impl::ocl_getBackgroundImage(OutputArray _backgroun return kernel_getBg.run(2, globalsize, NULL, false); } -#endif - void BackgroundSubtractorMOG2Impl::create_ocl_apply_kernel() { 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); } +#endif + void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, double learningRate) { 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 ) initialize(_image.size(), _image.type()); +#ifdef HAVE_OPENCL if (opencl_ON) { CV_OCL_RUN(opencl_ON, ocl_apply(_image, _fgmask, learningRate)) @@ -833,6 +844,7 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, opencl_ON = false; initialize(_image.size(), _image.type()); } +#endif Mat image = _image.getMat(); _fgmask.create( image.size(), CV_8U ); @@ -856,6 +868,7 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImage) const { +#ifdef HAVE_OPENCL if (opencl_ON) { CV_OCL_RUN(opencl_ON, ocl_getBackgroundImage(backgroundImage)) @@ -863,6 +876,7 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage(OutputArray backgroundImag opencl_ON = false; return; } +#endif int nchannels = CV_MAT_CN(frameType); CV_Assert(nchannels == 1 || nchannels == 3); diff --git a/modules/video/src/lkpyramid.cpp b/modules/video/src/lkpyramid.cpp index a34e73d519..df82aabd6b 100644 --- a/modules/video/src/lkpyramid.cpp +++ b/modules/video/src/lkpyramid.cpp @@ -837,6 +837,7 @@ int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Si return maxLevel; } +#ifdef HAVE_OPENCL namespace cv { class PyrLKOpticalFlow @@ -1084,6 +1085,7 @@ namespace cv return opticalFlow.sparse(_prevImg.getUMat(), _nextImg.getUMat(), _prevPts.getUMat(), umatNextPts, umatStatus, umatErr); } }; +#endif void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg, InputArray _prevPts, InputOutputArray _nextPts, @@ -1092,6 +1094,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg, TermCriteria criteria, int flags, double minEigThreshold ) { +#ifdef HAVE_OPENCL bool use_opencl = ocl::useOpenCL() && (_prevImg.isUMat() || _nextImg.isUMat()) && ocl::Image2D::isFormatSupported(CV_32F, 1, false); @@ -1100,6 +1103,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg, CV_IMPL_ADD(CV_IMPL_OCL); return; } +#endif Mat prevPtsMat = _prevPts.getMat(); const int derivDepth = DataType::depth; diff --git a/modules/video/src/optflowgf.cpp b/modules/video/src/optflowgf.cpp index 1d6524e30b..9057f6c82c 100644 --- a/modules/video/src/optflowgf.cpp +++ b/modules/video/src/optflowgf.cpp @@ -583,6 +583,7 @@ FarnebackUpdateFlow_GaussianBlur( const Mat& _R0, const Mat& _R1, } +#ifdef HAVE_OPENCL namespace cv { class FarnebackOpticalFlow @@ -1074,17 +1075,20 @@ static bool ocl_calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0, return true; } } +#endif // HAVE_OPENCL void cv::calcOpticalFlowFarneback( InputArray _prev0, InputArray _next0, InputOutputArray _flow0, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags ) { +#ifdef HAVE_OPENCL 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)) { CV_IMPL_ADD(CV_IMPL_OCL); return; } +#endif Mat prev0 = _prev0.getMat(), next0 = _next0.getMat(); const int min_size = 32; diff --git a/modules/video/src/tvl1flow.cpp b/modules/video/src/tvl1flow.cpp index 90fe48f474..865e569001 100644 --- a/modules/video/src/tvl1flow.cpp +++ b/modules/video/src/tvl1flow.cpp @@ -122,11 +122,13 @@ protected: int medianFiltering; private: - void procOneScale(const Mat_& I0, const Mat_& I1, Mat_& u1, Mat_& u2, Mat_& u3); + void procOneScale(const Mat_& I0, const Mat_& I1, Mat_& u1, Mat_& u2, Mat_& u3); +#ifdef HAVE_OPENCL bool procOneScale_ocl(const UMat& I0, const UMat& I1, UMat& u1, UMat& u2); bool calc_ocl(InputArray I0, InputArray I1, InputOutputArray flow); +#endif struct dataMat { std::vector > I0s; @@ -170,6 +172,8 @@ private: Mat_ u3x_buf; Mat_ u3y_buf; } dm; + +#ifdef HAVE_OPENCL struct dataUMat { std::vector I0s; @@ -195,8 +199,10 @@ private: UMat diff_buf; UMat norm_buf; } dum; +#endif }; +#ifdef HAVE_OPENCL namespace cv_ocl_tvl1flow { 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); } +#endif OpticalFlowDual_TVL1::OpticalFlowDual_TVL1() { @@ -499,6 +506,7 @@ void OpticalFlowDual_TVL1::calc(InputArray _I0, InputArray _I1, InputOutputArray merge(uxy, 2, _flow); } +#ifdef HAVE_OPENCL bool OpticalFlowDual_TVL1::calc_ocl(InputArray _I0, InputArray _I1, InputOutputArray _flow) { UMat I0 = _I0.getUMat(); @@ -598,6 +606,7 @@ bool OpticalFlowDual_TVL1::calc_ocl(InputArray _I0, InputArray _I1, InputOutputA merge(uxy, _flow); return true; } +#endif //////////////////////////////////////////////////////////// // buildFlowMap @@ -1180,6 +1189,7 @@ void estimateDualVariables(const Mat_& u1x, const Mat_& u1y, 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) { using namespace cv_ocl_tvl1flow; @@ -1267,6 +1277,7 @@ bool OpticalFlowDual_TVL1::procOneScale_ocl(const UMat& I0, const UMat& I1, UMat } return true; } +#endif void OpticalFlowDual_TVL1::procOneScale(const Mat_& I0, const Mat_& I1, Mat_& u1, Mat_& u2, Mat_& u3) { @@ -1402,6 +1413,7 @@ void OpticalFlowDual_TVL1::collectGarbage() dm.u2x_buf.release(); dm.u2y_buf.release(); +#ifdef HAVE_OPENCL //dataUMat structure dum dum.I0s.clear(); dum.I1s.clear(); @@ -1425,6 +1437,7 @@ void OpticalFlowDual_TVL1::collectGarbage() dum.diff_buf.release(); dum.norm_buf.release(); +#endif } } // namespace From 1a8d7f95873228a5a8903c167f4f8ea5d76d3cec Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 22 Jun 2015 14:24:11 +0300 Subject: [PATCH 2/3] ocl: temporary restore OpenCL tls data (ocl.cpp without fix) --- modules/core/src/precomp.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index cf15359eab..43cacd2ef7 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -265,9 +265,9 @@ struct ImplCollector struct CoreTLSData { CoreTLSData() : -#ifdef HAVE_OPENCL +//#ifdef HAVE_OPENCL device(0), useOpenCL(-1), -#endif +//#endif useIPP(-1) { #ifdef HAVE_TEGRA_OPTIMIZATION @@ -276,11 +276,11 @@ struct CoreTLSData } RNG rng; -#ifdef HAVE_OPENCL +//#ifdef HAVE_OPENCL int device; ocl::Queue oclQueue; int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized -#endif +//#endif int useIPP; // 1 - use, 0 - do not use, -1 - auto/not initialized #ifdef HAVE_TEGRA_OPTIMIZATION int useTegra; // 1 - use, 0 - do not use, -1 - auto/not initialized From bf42b9603bd4f8748919d0643e29f9c6ff2ee449 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 14 Sep 2015 13:11:12 +0300 Subject: [PATCH 3/3] fix "unreachable code" warning --- modules/core/test/test_umat.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index 5afafbdb73..75c6353ad1 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -960,8 +960,7 @@ TEST(UMat, setOpenCL) { #ifndef HAVE_OPENCL return; // test skipped -#endif - +#else // save the current state bool useOCL = cv::ocl::useOpenCL(); @@ -999,6 +998,7 @@ TEST(UMat, setOpenCL) // reset state to the previous one cv::ocl::setUseOpenCL(useOCL); +#endif } TEST(UMat, ReadBufferRect)