gpt4 book ai didi

c++ - OpenCV ORB GPU 实现比 CPU 慢

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:39 26 4
gpt4 key购买 nike

我正在尝试对视频帧运行 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 秒。我在其他视频(非高清)上得到了类似的结果。电脑规范:

  • i7-4770K CPU 3.50 GHz

  • 英伟达 GeForce GTX 650

  • 16 GB 内存

其他特征检测/描述算法(如 SURF)在我机器上的 GPU 实现上执行得更快。

有没有人在他的机器上比较过ORB的两种实现方式?

最佳答案

取自this post :

cv::ORB applies a GaussianBlur (about 20 lines from the end of orb.cpp) before computing descriptors. There is no way to control this through the public interface.

cv::gpu::ORB_GPU has a public member bool blurForDescriptor, which by default constructs as false. When I set it instead to true, I find that min/avg/max hamming distance drops to 0/7.2/30 bits, which seems much more reasonable.

关于c++ - OpenCV ORB GPU 实现比 CPU 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com