在opencv中,已经嵌入了opencl运行的方式,通过使用UMat对象,opencv会自动在支持OpenCL的设备上使用GPU运算,在不支持OpenCL的设备仍然使用CPU运算,这样就避免了程序运行失败,而且统一了接口。

Mat转UMat
// 从Mat获得UMat矩阵
// 方法一
cv::UMat usrc = cv::imread("bmc1.bmp", 0).getUMat(cv::ACCESS_READ);
// 方法二
cv::UMat usrc_;
cv::Mat src = imread("bmc1.bmp", 0);
src.copyTo(usrc_);
UMat转Mat
cv::Mat src = usrc.getMat(cv::ACCESS_RW);

两者的转换是不共享矩阵体的。所以,一般转换成另一个类型进行修改之后,需要再转回去,内容才能一致。而且转换需要花费大量时间(从CPU将数据移到GPU或相反),如果计算量不大,建议还是用Mat计算
因为UMat和Mat有着共同的基类InputOutputArray,所以大部分OpenCV的函数UMat都能使用,但是UMat自带的方法没有Mat丰富。比如说,UMat没有data指针指向数据,没有ptr<>方法。

getMat和getUMat的参数可以如下:
  • ACCESS_FAST
  • ACCESS_MASK
  • ACCESS_READ
  • ACCESS_RW
  • ACCESS_WRITE
GitHub 加速计划 / opencv31 / opencv
159
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
fc994a6a Feature: weighted Hough Transform #21407 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV - [x] The PR is proposed to proper branch - [x] There is reference to original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 16 小时前
2aee9475 Fix for png durations and memory leak #26714 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 17 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐