opencv c++写一个mali调用gpu和cpu对比的代码
时间: 2024-02-07 12:03:36 浏览: 203
下面是一个使用OpenCL Mali和CPU对比的OpenCV C++示例代码:
```
#include <opencv2/opencv.hpp>
#include <opencv2/ocl/ocl.hpp>
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
using namespace cv;
int main(int argc, char** argv)
{
Mat src = imread("test.jpg");
UMat dst;
// Enable OpenCL Mali
ocl::setUseOpenCL(true);
// Create OpenCL Mali filter
Ptr<ocl::Filter> filter = ocl::createGaussianFilter(src.type(), Size(5, 5), 1.5);
// Run OpenCL Mali filter
high_resolution_clock::time_point t1 = high_resolution_clock::now();
filter->apply(UMat(src), UMat(dst));
high_resolution_clock::time_point t2 = high_resolution_clock::now();
// Display OpenCL Mali result
imshow("OpenCL Mali Result", dst);
// Run CPU filter
Mat dst_cpu;
t1 = high_resolution_clock::now();
GaussianBlur(src, dst_cpu, Size(5, 5), 1.5);
t2 = high_resolution_clock::now();
// Display CPU result
imshow("CPU Result", dst_cpu);
// Calculate time taken by OpenCL Mali and CPU filters
auto duration_mali = duration_cast<microseconds>(t2 - t1).count();
auto duration_cpu = duration_cast<microseconds>(t2 - t1).count();
// Display time taken by OpenCL Mali and CPU filters
cout << "Time taken by OpenCL Mali filter: " << duration_mali << " microseconds" << endl;
cout << "Time taken by CPU filter: " << duration_cpu << " microseconds" << endl;
waitKey();
return 0;
}
```
在上述代码中,使用了`cv::ocl::setUseOpenCL(true)`来启用OpenCL Mali加速,使用`cv::ocl::createGaussianFilter`来创建一个OpenCL Mali加速的高斯滤波器。同时,还使用了CPU的`GaussianBlur`函数作为对照组进行对比。
在执行完OpenCL Mali和CPU的滤波后,计算并输出了它们的运行时间。
阅读全文