OpenCV 3版本的CUDA有了很大变化。刚刚在WIN7 64位系统编译好了带CUDA 7.0版本的OpenCV 3.0库。
进行了简单的测试。
CUDA版代码如下:
#include<opencv2\opencv.hpp>
#include<opencv2/cudaimgproc.hpp>
#include <time.h>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
clock_t t1 = clock();
int num_devices = cv::cuda::getCudaEnabledDeviceCount();
if (num_devices <= 0){
cerr << "There is no device." << endl;
return -1;
}
int enable_device_id = -1;
for (int i = 0; i < num_devices; i++){
cv::cuda::DeviceInfo dev_info(i);
if (dev_info.isCompatible()){
enable_device_id = i;
}
}
if (enable_device_id < 0){
cerr << "GPU module isn't built for GPU" << endl;
}
cv::cuda::setDevice(enable_device_id);
Mat src_image = imread("Hi-RGB0971.tif");
Mat dst_image;
cuda::GpuMat d_src_img(src_image);//upload src image to gpu
cuda::GpuMat d_dst_img;
cuda::cvtColor(d_src_img, d_dst_img, CV_BGR2GRAY);
d_dst_img.download(dst_image);
imshow("test", dst_image);
clock_t t2 = clock();
std::cout << "time: " << (t2 - t1) / 1000 << "s" << endl;
waitKey();
return 0;
}
不用CUDA版:
#include<opencv2\opencv.hpp>
#include <time.h>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
clock_t t1 = clock();
Mat src_image = imread("Hi-RGB0971.tif");
Mat dst_image;
cvtColor(src_image, dst_image, CV_BGR2GRAY);
imshow("test", dst_image);
clock_t t2 = clock();
std::cout << "time: " << (t2 - t1) / 1000 << "s" << endl;
waitKey();
return 0;
}
测试下来,使用了CUDA编译的OpenCV库,运行的时间比没有使用CUDA库编译的OpenCV库还要长。我觉得是由于数据载入GPU和重新载入CPU耗费了时间。
后面会利用OpenCV库和CUDA单独写函数,分块处理影像,然后进行相关的测试。