4

我在两台不同的计算机上运行以下代码,第一台有Nvidia GPU Quadro FX 880M,第二台是Quadro FX 1000M(在VS2010中编译,opencv242,64bit;opencv是从源代码编译的)。我正在运行的代码如下:

int n = 1000;  //number of iterations
int t = CV_TM_CCORR_NORMED; //correlation type




//reset GPU, print device info
cv::gpu::printCudaDeviceInfo(cv::gpu::getDevice());
cv::gpu::resetDevice(); 




//read big image
cv::Mat imgA = cv::imread("img.bmp"  ,CV_LOAD_IMAGE_GRAYSCALE);
//read small, template image
cv::Mat imgB = cv::imread("tmplt.bmp",CV_LOAD_IMAGE_GRAYSCALE);




//upload images to GPU
cv::gpu::GpuMat imgA_GPU,imgB_GPU;
imgA_GPU.upload(imgA);
imgB_GPU.upload(imgB);





cv::gpu::GpuMat imgC_GPU; //correlation results, computer in GPU
cv::Mat         imgC_CPU; //correlation results, computer in CPU




//matchTemplate in GPU, print average time(mSec)
size_t t1 = clock();
for(int i = 0;i!=n;++i)
    cv::gpu::matchTemplate(imgA_GPU , imgB_GPU, imgC_GPU , t);

std::cout << "GPU: " <<
(double(clock())-t1)/CLOCKS_PER_SEC*1000.0/n <<std::endl;





//matchTemplate in CPU, print average time(mSec)
size_t t2 = clock();
for(int i = 0;i!=n;++i)
    cv::     matchTemplate(imgA     , imgB    , imgC_CPU , t);

std::cout << "CPU: " <<
(double(clock())-t2)/CLOCKS_PER_SEC*1000.0/n <<std::endl;






//download GPU image to host
cv::Mat imgC_GPUhost;
imgC_GPU.download(imgC_GPUhost);




//convert images to 8U
imgC_CPU.convertTo(imgC_CPU,CV_8U,255);
imgC_GPUhost.convertTo(imgC_GPUhost,CV_8U,255);




//!!!!!! imgC_GPUhost should be equal to imgC_CPU
cv::Mat diff;
cv::absdiff(imgC_CPU,imgC_GPUhost,diff);
//expected: RESULTS DIFF: 0
std::cout << "RESULTS DIFF: " << cv::sum(diff).val[0] << std::endl;





cv::imwrite("cor2.bmp",imgC_CPU);
cv::imwrite("cor.bmp",imgC_GPUhost);
char s;
std::cin >> s;

有两件主要的事情让我感到困惑:

  1. 在 Quadro FX 880M 上,此功能不起作用:GPU 输出图像 (imgC_GPU) 全部为零 - 输入类型(8U 或 32F)或相关方法(ccor、ccoef 等)无关紧要。另一方面,在 Quadro FX 1000M 中,我在 CPU 和 GPU 之间获得了一致的结果。怎么可能,我需要做什么才能让它在 Quadro FX 880M 上工作?

  2. 在模板匹配中,输出图像中的每个像素都可以独立于其他像素进行计算——因此并行很容易,GPU 实现非常适合。怎么可能,即使查看平均时间(如代码中所示),GPU 性能也比 CPU 慢 3 倍?它在两台计算机上都经过验证,后台没有运行其他进程。

奥哈德

4

1 回答 1

0

看起来 matchTemplate 的 GPU 版本是在考虑同时搜索多个模板的情况下开发的。事实上,使用一个图像/一个模板,CPU 版本似乎更快。

http://answers.opencv.org/question/16061/opencv-matchtemplate-cuda-large-images-templates/

于 2015-02-16T11:46:45.170 回答