[OpenCV] GpuMat and Mat, compare cvtColor perforemence

来源:互联网 发布:购票软件有哪些 编辑:程序博客网 时间:2024/05/16 17:53

Introduction

I am going to measure the performence of my two GT650M and compare GPU with CPU version.

code segments

#include <iostream>#include <omp.h>using namespace std;#include <cuda_runtime.h>#include <opencv2/opencv.hpp>#include <opencv2/gpu/gpu.hpp>using namespace cv;#define TB__(A) int64 A; A = cv::getTickCount()#define TE__(A) cout << #A << " : " << 1.E3 * double(cv::getTickCount() - A)/double(cv::getTickFrequency()) << "ms" << endlVec3b max(cv::Mat &A, cv::Mat &B){    int width = A.cols;    int height = A.rows;    Vec3b me(0,0,0);    for (int y = 0; y < height; y++){        for (int x = 0; x < width; x++){            Vec3b p0 = A.at<Vec3b>(y,x);            Vec3b p1 = B.at<Vec3b>(y,x);            Vec3b pixel;            pixel[0] = p1[0]>p0[0]? p1[0]-p0[0]:p0[0]-p1[0];            pixel[1] = p1[1]>p0[1]? p1[1]-p0[1]:p0[1]-p1[1];            pixel[2] = p1[2]>p0[2]? p1[2]-p0[2]:p0[2]-p1[2];            if (me[0] < pixel[0])                me[0] = pixel[0];            if (me[1] < pixel[1])                me[1] = pixel[1];            if (me[2] < pixel[2])                me[2] = pixel[2];        }    }    return me;}#define REPEATES 10000int main(){  // please ensure that you have at least two Nvidia GPUs on your mathorboad.    cv::Mat cpu_src = imread("86.jpg");    cv::Mat cpu_dst;    cv::Mat gpu_dst[2];    TB__(cpu_cvt);#pragma omp parallel for num_threads(4)    for (int k = 0; k < REPEATES; k++)        cv::cvtColor(cpu_src, cpu_dst, CV_BGR2Lab);    TE__(cpu_cvt);    TB__(gpu_cvt);#pragma omp parallel num_threads(2)    {        int id = omp_get_thread_num();        cv::gpu::setDevice(id);        cv::gpu::GpuMat dev_src;        cv::gpu::GpuMat dev_dst;        dev_src.upload(cpu_src);        //#pragma omp master        //TB__(gpu_omp_dev);        #pragma omp for        for (int k = 0; k < REPEATES; k++){            dev_src.upload(cpu_src);            cv::gpu::cvtColor(dev_src, dev_dst, CV_BGR2Lab);            dev_dst.download(gpu_dst[id]);        }        cudaDeviceSynchronize();    }    TE__(gpu_cvt);  cout << cv::sum(cpu_dst - gpu_dst[0]) << endl;  cout << cv::sum(cpu_dst - gpu_dst[1]) << endl;  cout << cv::sum(gpu_dst[0] - gpu_dst[1]) << endl;    cout << endl;  cout << max(cpu_dst, gpu_dst[0]) << endl;  cout << max(cpu_dst, gpu_dst[1]) << endl;    return 0;}
1 0
原创粉丝点击