OpenCL 使用速度对比

1.仅使用 OpenCV

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/ocl.hpp>

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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/ocl.hpp>

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;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有时候购买的商品,属性很多,想做成表格进行筛选。自然用技术宅的爬虫方式做了 ```python # -*- ...
    超哥__阅读 4,379评论 0 8
  • 一点背景知识 OpenCV 是一个开源的计算机视觉和机器学习库。它包含成千上万优化过的算法,为各种计算机视觉应用提...
    沬日十七阅读 1,040评论 0 4
  • OpenCL是一个异构计算平台,Khronos Group将OpenCL的异构并行计算架构划分为平台模型(plat...
    Kaami阅读 1,479评论 0 1
  • Day21/21 83+乐开延+杭州 【书籍名称】 《失落的致富经典》 【阅读心得】 1.如果想有更多的休闲时间,...
    乐开延阅读 101评论 0 0
  • 1、朋友的孩子想“撞墙” 好好的孩子为什么要撞墙呀?Lyra老师来跟你说道说道。 我有个朋友,因为她也非常喜欢做手...
    慧眼识人才_陈慧阅读 224评论 1 2