OpenCV中OpenCL模块函数

本文档介绍了使用OpenCL进行图像处理和机器学习的方法,包括核心操作如矩阵运算、特征检测、匹配模板等,还涵盖了高级应用如光流计算、对象检测与分类等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

It currently develop and test on GPU devices only. This includes both discrete GPUs(NVidia,AMD), as well as integrated chips(AMD APU and intel HD devices).

The ocl module can be found under the “modules”directory. In “modules/ocl/src” you can find the source code for the cpp class that wrap around the direct kernel invocation. The kernels themselves can be found in “modules/ocl/src/kernels.” Samples can be found under “samples/ocl.”Accuracy tests can be found in “modules/ocl/test,”and performance tests under “module/ocl/perf.”

If a function support 4-channel operator, it should support 3-channel operator as well, because All the 3-channel matrix(i.e. RGB image) are represented by 4-channel matrix in oclMat. It means 3-channel image have 4-channel space with the last channel unused.

 

1.      getDevice:returns the list of devices;

2.      setDevice:sets adevice and initializes it;

3.      setBinpath:if you call this function and set a valid path, the OCL module will save the complied kernel to the address in the first time and reload the binary since that, it can save compilation time at the runtime;

4.      getoclContext:returns the pointer to the opencl context;

5.      getoclCommandQueue:returns the pointer to the opencl command queue;

6.      class::oclMat:OpenCV C++1-D or 2-D dense array class, the oclMat is the mirror of Mat with the extension of ocl feature;

7.      oclMat::convertTo:the method converts source pixel values to the target datatype, saturate cast is applied in the end to avoid possible overflows;

8.      oclMat::copyTo:copies the matrix to another one;

9.      oclMat::setTo:sets all or some of the array elements to the specified value;

10.  absdiff:computes per-element absolute difference between two arrays or between array and ascalar;

11.  add:computes per-element addition between two arrays or between array and a scalar;

12.  subtract:computes per-element subtract between two arrays or between array and a scalar;

13.  multiply:computes per-element multiply between two arrays or between array and a scalar;

14.  divide:computes per-element divide between two arrays or between array and a scalar;

15.  bitwise_and:computesper-element bitwise_and between two arrays or between array and a scalar

16.  bitwise_or:computes per-element bitwise_or between two arrays or between array and a scalar;

17.  bitwise_xor:computes per-element bitwise_xor between two arrays or between array and a scalar;

18.  bitwise_not:the function bitwise not compute per-element bit-wise inversion of the source array;

19.  cartToPolar:calculates the magnitude and angle of 2d vectors;

20.  polarToCart:calculates the Cartesian coordinates of each 2D vector represented by the correspondingelements of magnitude and angle;

21.  compare:performs per-element comparison of two arrays or an array and scalar value;

22.  exp:calculates the exponent of every element of the input array;

23.  log:calculates the log of every element of the input array;

24.  LUT:performs a look-up table transform of an array;

25.  magnitude:calculates magnitude of 2D vectors formed from the corresponding element of x and y arrays;

26.  flip:flips the array in one of three different ways(row and column indices are 0-based);

27.  meanStdDev:compute the mean and the standard deviation M of array elements, independently for eachchannel, and return it via the output parameters;

28.  merge:composes a multi-channel array from several single-channel arrays;

29.  split:split multi-channel array into separate single-channel arrays;

30.  norm:calculates absolute array norm, absolute difference norm, or relative difference norm;

31.  phase:computes the rotation angle of each 2D vector that is formed from the corresponding elementsof x and y;

32.  pow:raises every element of the input array to p;

33.  transpose:transposes a matrix;

34.  dft(cv::dft):performs a forward or inverse discrete Fourier transform(1D or 2D) of the floating pointmatrix;

35.  gemm(cv::gemm):performs generalized matrix multiplication;

36.  countNonZero:returns the number of non-zero elements in src;

37.  minMax:finds global minimum and maximum in a whole array or sub-array;

38.  minMaxLoc:find minimum and maximum element values and their positions;

39.  sum:returns the sum of matrix elements for each channel;

40.  sqrSum:returns the squared sum of matrix elements for each channel;

41.  Sobel:computesthe first x- or y- spatial image derivative using Sobel operator

42.  Scharr:computes the first x- or y- spatial image derivative using Scharr operator;

43.  GaussianBlur:convolves the source image with the specified Gaussian kernel;

44.  boxFilter:smoothes image using box filter;

45.  Laplacian:calculates the Laplacian of the source image by adding up the second x and y derivativescalculated using the Sobel operator;

46.  convolue:convolves an image with the kernel;

47.  bilateralFilter:applies bilateral filter to the image;

48.  copyMakeBorder:forms a border around the image;

49.  dilate:dilatesthe source image using the specified structuring element that determines theshape of a pixel neighborhood over which the maximum is taken

50.  erode:erodes the source image using the specified structuring element that determines the shapeof a pixel neighborhood over which the minimum is taken;

51.  morphologyEx:a wrapper for erode and dilate;

52.  pyrDown(cv::pyrDown):smoothes an image and downsamples it;

53.  pyrUp(cv::pyrUp):upsamples an image and then smoothes it;

54.  columnSum:computes a vertical(column) sum;

55.  blendLinear:performs linear blending of two images;

56.  cornerHarris:calculate Harris corner;

57.  cornerMinEigenVal:calculate MinEigenVal;

58.  calcHist:calculates histogram of one or more arrays;

59.  remap:transforms the source image using the specified map;

60.  resize:resizes an image;

61.  warpAffine: transforms the source image using the specified matrix;

62.  warpPerspective:applies a perspective transformation to an image;

63.  cvtColor:converts image from one color space to another;

64.  threshold:applies fixed-level thresholding to a single-channel array;

65.  buildWarpPlaneMaps:builds plane warping maps;

66.  buildWarpCylindricalMaps:builds cylindrical warping maps;

67.  buildWarpSphericalMaps:builds spherical warping maps;

68.  buildWarpPerspectiveMaps(ocl::warpPerspective):builds transformation maps for perspective transformation;

69.  buildWarpAffineMaps(ocl::warpAffine):builds transformation maps for affine transformation;

70.  class::PyrLKOpticalFlow(cv::calcOpticalFlowPyrLK):class used for calculating an optical flow;

71.  PyrLKOpticalFlow::sparse:calculate an optical flow for a sparse feature set;

72.  PyrLKOpticalFlow::dense:calculate dense optical flow;

73.  PyrLKOpticalFlow::releaseMemory:releases inner buffers memory;

74.  interpolateFrames:interpolateframes(images) using provided optical flow(displacement field);

75.  class::OclCascadeClassifier:cascade classifier class used for object detection;

76.  OclCascadeClassifier::oclHaarDetectObjects:returns the detected objects by a list rectangles;

77.  struct::MatchTemplateBuf:class providing memory buffers for matchTemplate function, plus it allows to adjustsome specific parameters;

78.  matchTemplate(cv::matchTemplate):computes aproximity map for a raster template and an image where the template is searchedfor;

79.  Canny(cv::Canny):finds edgesin an image using the Canny algorithm;

80.  class::BruteForceMatcher_OCL_base(cv::DescriptorMatcher,cv::BFMatcher):Brute-force descriptor matcher, for each descriptor in the firstset, this matcher finds the closest descriptor in the second set by trying eachone;

81.  BruteForceMatcher_OCL_base::match:finds the best match for each descriptor from a query set with traindescriptors;

82.  BruteForceMatcher_OCL_base::makeGpuCollection:performs a GPU collection of traindescriptors and masks in a suitable format for the matchCollection function;

83.  BruteForceMatcher_OCL_base::matchDownload:downloads matrices obtained viamatchSingle or matchCollection to vector with DMatch;

84.  BruteForceMatcher_OCL_base::matchConvert:converts matrices obtained viamatchSingle or matchCollection to vector with DMatch;

85.  BruteForceMatcher_OCL_base::knnMatch:finds the k best matches for eachdescriptor from a query set with train descriptors;

86.  BruteForceMatcher_OCL_base::knnMatchDownload:downloads matrices obtained viaknnMatchSingle or knnMatch2Collection to vector with DMatch;

87.  BruteForceMatcher_OCL_base::knnMatchConvert:converts matrices obtained viaknnMatchSingle or knnMatch2Collection to CPU vector with DMatch;

88.  BruteForceMatcher_OCL_base::radiusMatch:for each query descriptor, finds thebest matches with a distance less than a given threshold;

89.  BruteForceMatcher_OCL_base::radiusMatchDownload:downloads matrices obtained viaradiusMatchSingle or radiusMatchCollection to vector with DMatch;

90.  BruteForceMatcher_OCL_base::radiusMatchConvert:converts matrices obtained viaradiusMatchSingle or radiusMatchCollection to vector with DMatch;

91.  struct::HOGDescriptor:the class implements Histogram ofOriented Gradients object detector;

92.  HOGDescriptor::getDescriptorSize:returns the number of coefficientsrequired for the classification;

93.  HOGDescriptor::getBlockHistogramSize:returns the block histogram size;

94.  HOGDescriptor::setSVMDetector:sets coefficients for the linear SVMclassifier;

95.  HOGDescriptor::getDefaultPeopleDetector:returns coefficients of the classifiertrained for people detection(for default window size);

96.  HOGDescriptor::getPeopleDetector48x96:returns coefficients of the classifiertrained for people detection(for 48x96 windows);

97.  HOGDescriptor::getPeopleDetector64x128:returns coefficients of the classifiertrained for people detection(for 64x128 windows);

98.  HOGDescriptor::detect:performs object detection without amulti-scale window;

99.  HOGDescriptor::detectMultiScale:performs object detection with amulti-scale window;

100.  HOGDescriptor::getDescriptors:returns block descriptors computed forthe whole image;

 

### 启用和使用 OpenCVOpenCL 加速 #### 1. 启用 OpenCL 支持 为了在 OpenCV 中启用 OpenCL 支持,需要确保编译时启用了 OpenCL 功能。这通常涉及设置 CMake 参数 `WITH_OPENCL` 和其他相关参数。 对于 Linux 平台,在 Ubuntu 16.04 上可以通过以下方式完成编译环境配置[^2]: - 安装 Intel GPU OpenCL 驱动。 - 创建必要的目录结构并下载 deb 安装包。 - 使用 root 权限安装驱动程序。 - 运行工具如 `clinfo` 查看 OpenCL 设备信息以确认驱动已正确加载。 随后,按照标准流程编译 OpenCV 源码,并指定 `-D WITH_OPENCL=ON` 参数[^2]。 #### 2. 注册全局 OpenCL 设备 一旦 OpenCL 支持被成功启用,下一步是在应用程序中注册可用的 OpenCL 设备。此操作可通过调用 `cv::ocl::setUseOpenCL(true)` 实现[^3]。该函数允许开发者控制是否开启或关闭 OpenCL 加速功能。 #### 3. 数据传输与转换 当准备执行基于 OpenCL 的图像处理任务时,需先将数据从主机内存上传至显卡上的专用存储器(即显存)。这一过程可借助 `cv::UMat` 或者更传统的 `cv::ocl::oclMat` 类型完成[^3]: ```cpp cv::Mat hostImage = cv::imread("input.jpg", cv::IMREAD_COLOR); cv::UMat deviceImage; hostImage.copyTo(deviceImage); // 将 Mat 对象复制到 UMat (设备端) ``` 上述代码片段展示了如何将 CPU 上的标准矩阵对象 (`cv::Mat`) 转移到支持 OpenCL 的设备上进行进一步的操作[^3]。 #### 4. 执行 OpenCL 加速算法 许多常见的 OpenCV 函数都具有对应的 OpenCL 版本,这些版本能够自动检测输入数据类型并决定采用哪种路径执行计算。例如,高斯模糊滤波器可以直接作用于 `cv::UMat` 输入而无需额外修改逻辑[^1]: ```cpp cv::GaussianBlur(deviceImage, deviceImage, cv::Size(5, 5), 1.5); deviceImage.copyTo(hostImage); // 结果回传给主机侧变量 ``` 这里需要注意的是,尽管某些情况下可能不需要手动管理上下文切换或者资源分配等问题,但对于性能敏感的应用场景来说,合理规划数据流动仍然非常重要。 #### 5. Android 平台上构建含 OpenCLOpenCV 库 如果目标平台为 Android,则可以参照特定脚本来自动化整个 SDK 构建工作流[^4]。命令如下所示: ```bash ../opencv/platforms/android/build_sdk.py \ --extra_modules_path=../opencv_contrib/modules \ --no_samples_build --no_kotlin --opencl \ --sdk_path=/path/to/android/sdk \ --ndk_path=/path/to/android/ndk/r19b/ ``` 这条指令不仅会生成基础框架文件夹还包括扩展模块集合以及激活硬件加速选项开关。 --- ### 总结 综上所述,要在 OpenCV 中有效利用 OpenCL 提升图像处理效率,主要分为以下几个方面:首先是确保软件栈具备必要依赖项;其次是编写兼容性良好的应用层接口设计;最后则是针对具体需求优化底层交互细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值