美文网首页
OpenCL 使用速度对比

OpenCL 使用速度对比

作者: _酒酿芋圆 | 来源:发表于2019-03-11 19:00 被阅读0次

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

相关文章

网友评论

      本文标题:OpenCL 使用速度对比

      本文链接:https://www.haomeiwen.com/subject/fmfbpqtx.html