OpenCV ORB GPU实现比CPU慢

时间:2014-07-17 12:42:04

标签: c++ opencv gpu orb

我正在尝试将ORB OpenCV算法运行到视频的帧中,我注意到CPU版本的执行速度比GPU版本快得多。这是代码:

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <fstream>
#include <sstream> 
#include <math.h>
#include <omp.h>

#include <algorithm>
#include <vector>
#include <string>

using namespace cv;
using namespace std;
using namespace cv::gpu;

void process_cpu(string vid, int start_frame, int end_frame)
{
VideoCapture myCapture(vid);
Mat frame, gray_frame;
ORB myOrb(400);
Mat descriptors;
vector<KeyPoint> keypoints;

myCapture.set(CV_CAP_PROP_POS_FRAMES, start_frame);

for (int i=0; i<end_frame-start_frame; i++) {
    myCapture.read(frame);
    cvtColor(frame, gray_frame, CV_RGB2GRAY);
    myOrb(gray_frame, Mat(), keypoints, descriptors);
}
myCapture.release();
}

void process_gpu(string vid, int start_frame, int end_frame)
{
VideoCapture myCapture(vid);
Mat frame, gray_frame;
GpuMat gpu_frame;
ORB_GPU myOrb(400);
GpuMat keypoints, descriptors;

myCapture.set(CV_CAP_PROP_POS_FRAMES, start_frame);

for (int i=0; i<end_frame-start_frame; i++) {
    myCapture.read(frame);
    cvtColor(frame, gray_frame, CV_RGB2GRAY);
    gpu_frame.upload(gray_frame);
    myOrb.blurForDescriptor = true;
    myOrb(gpu_frame, GpuMat(), keypoints, descriptors);
}
myCapture.release();
}

int main (int argc, char* argv[])
{
int n = 4;
VideoCapture myCapture(argv[1]);
double frameNumber = myCapture.get(CV_CAP_PROP_FRAME_COUNT);
myCapture.release();

double TimeStart = 0;
double TotalTime = 0;
TimeStart = (double)getTickCount();

process_gpu(argv[1], 0, frameNumber);

TotalTime = (double)getTickCount() - TimeStart;
TotalTime = TotalTime / getTickFrequency();
cout << "Gpu Time : " << TotalTime << endl;

TimeStart = (double)getTickCount();

process_cpu(argv[1], 0, frameNumber);

TotalTime = (double)getTickCount() - TimeStart;
TotalTime = TotalTime / getTickFrequency();
cout << "Cpu Time : " << TotalTime << endl;

return -1;
}

在3000帧和720x480分辨率的视频上运行此操作后,GPU时间为54秒,CPU时间为24秒。我与其他视频(不是HD)获得了类似的结果。 PC规格:

  • i7-4770K CPU 3.50 GHz

  • NVIDIA GeForce GTX 650

  • 16 GB RAM

其他功能检测/描述算法(如SURF)在我的机器上实现GPU时执行速度更快。

有人在他的机器上比较了ORB的两个实现吗?

1 个答案:

答案 0 :(得分:4)

取自this post

  

cv::ORB应用GaussianBlur(距离结尾约20行)   orb.cpp)在计算描述符之前。没有办法控制这个   通过公共界面。

     

cv::gpu::ORB_GPU有一个公共成员bool blurForDescriptor,其中   默认情况下构造为false。当我把它设置为真时,我   发现min / avg / max汉明距离下降到0 / 7.2 / 30位,其中   似乎更合理。