OpenCL 使用速度对比

1.仅使用 OpenCV

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;

int main() {
    VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
    double startTime, endTime;

    if (!capturer.isOpened()) {
        cout << "Open video failed!" << endl;
        return false;
    }

    int fps = capturer.get(CV_CAP_PROP_FPS);
    int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);

    cout << "FPS:" << fps << endl;
    cout << "Width:" << width << endl;
    cout << "Height:" << height << endl;
       
    VideoWriter writer("C:/Users/76371/Desktop/opencv_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);

    startTime = getTickCount();

    Mat frame, gray;

    while (capturer.read(frame)) {
        cvtColor(frame, gray, COLOR_RGB2GRAY);
        writer.write(gray);
    }

    endTime = getTickCount();
    cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;

    capturer.release();
    writer.release();
    return 0;
}

2.集显使用 OpenCL

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;
using namespace ocl;

int main() {
    VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
    double startTime, endTime;
    
    if (!capturer.isOpened()) {
        cout << "Open video failed!" << endl;
        return false;
    }

    int fps = capturer.get(CV_CAP_PROP_FPS);
    int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);

    cout << "FPS:" << fps << endl;
    cout << "Width:" << width << endl;
    cout << "Height:" << height << endl;
       
    VideoWriter writer("C:/Users/76371/Desktop/opencl_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);
    
    startTime = getTickCount();

    UMat frame, gray;
    setUseOpenCL(true);

    while (capturer.read(frame)) {
        cvtColor(frame, gray, COLOR_RGB2GRAY);
        writer.write(gray.getMat(ACCESS_RW));
    }

    endTime = getTickCount();
    cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;

    capturer.release();
    writer.release();
    return 0;
}

3.独显使用 OpenCL

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;
using namespace ocl;

int main() {
    VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
    double startTime, endTime;
    
    if (!capturer.isOpened()) {
        cout << "Open video failed!" << endl;
        return false;
    }

    int fps = capturer.get(CV_CAP_PROP_FPS);
    int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);

    cout << "FPS:" << fps << endl;
    cout << "Width:" << width << endl;
    cout << "Height:" << height << endl;
       
    VideoWriter writer("C:/Users/76371/Desktop/opencl_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);
    
    
    startTime = getTickCount();

    UMat frame, gray;
    setUseOpenCL(true);

    while (capturer.read(frame)) {
        cvtColor(frame, gray, COLOR_RGB2GRAY);
        
        writer.write(gray.getMat(ACCESS_RW));
    }

    endTime = getTickCount();
    cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;

    capturer.release();
    writer.release();

    return 0;
}

OpenCL 使用速度对比_第1张图片

你可能感兴趣的:(OpenCL 使用速度对比)