天天看点

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

前言

前面已经记录了编译带opencl加速的库,本次记录下一个简单的测试,有需要的参考下

一、打印openCL设备基本信息

cv::ocl::setUseOpenCL(true);
    if (!cv::ocl::haveOpenCL())
    {
        qDebug() << "OpenCL is not available..." ;
        //return;
    }

    cv::ocl::Context context;
    if (!context.create(cv::ocl::Device::TYPE_GPU))
    {
        qDebug() << "Failed creating the context..." ;
        //return;
    }

    qDebug() << context.ndevices() << " GPU devices are detected." ; //This bit provides an overview of the OpenCL devices you have in your computer
    for (int i = 0; i < context.ndevices(); i++)
    {
        cv::ocl::Device device = context.device(i);
        qDebug() << "name:              " << QString::fromStdString(device.name()) ;
        qDebug() << "available:         " << device.available() ;
        qDebug() << "imageSupport:      " << device.imageSupport() ;
        qDebug() << "OpenCL_C_Version:  " << QString::fromStdString(device.OpenCL_C_Version()) ;

    }
           

1.win10下mingw 64位编译器输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

 2.win10下vs2019 64位编译器输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

 3.鸿蒙平板下 输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

 二、测试运行时间

void calEdgesCPU() {
    cv::Mat cpuBw, cpuBlur, cpuEdges;
    QTime time; time.start();
    cv::Mat cpuFrame = cv::imread("test.jpg");
//    cv::Mat cpuFrame = cv::imread("/mnt/sdcard/Download/test.jpg");
    qDebug()<<"CPU read time"<<time.elapsed()<<cpuFrame.data; time.restart();
    //namedWindow("Canny Edges CPU", 1);

    for(int i = 0; i < 3; i ++ )
    {
        cvtColor(cpuFrame, cpuBw, cv::COLOR_BGR2GRAY);
        qDebug()<<"CPU cvtColor time"<<time.elapsed(); time.restart();
        GaussianBlur(cpuBw, cpuBlur, cv::Size(1, 1), 1.5, 1.5);
        qDebug()<<"CPU GaussianBlur time"<<time.elapsed(); time.restart();
        Canny(cpuBlur, cpuEdges, 50, 100, 3);
        qDebug()<<"CPU Canny time"<<time.elapsed();
    }
    //imshow("Canny Edges CPU", cpuEdges);

}

void calEdgesGPU() {
    cv::ocl::setUseOpenCL(true);

    cv::UMat gpuBw, gpuBlur, gpuEdges,gpuFrame;
    QTime time; time.start();
    cv::Mat cpuFrame = cv::imread("test.jpg");
//    cv::Mat cpuFrame = cv::imread("/mnt/sdcard/Download/test.jpg");
    qDebug()<<"GPU read time"<<time.elapsed(); time.restart();

    for(int i = 0; i < 3; i ++ )
    {
        cpuFrame.copyTo(gpuFrame);
        qDebug()<<"GPU sentToGpu time"<<time.elapsed(); time.restart();
        //namedWindow("Canny Edges GPU", 1);
        cvtColor(gpuFrame, gpuBw, cv::COLOR_BGR2GRAY);
        qDebug()<<"GPU cvtColor time"<<time.elapsed(); time.restart();
        GaussianBlur(gpuBw, gpuBlur, cv::Size(1, 1), 1.5, 1.5);
        //    GaussianBlur(gpuBw, gpuBlur, cv::Size(7, 7), 1.5, 1.5);
        qDebug()<<"GPU GaussianBlur time"<<time.elapsed(); time.restart();
        Canny(gpuBlur, gpuEdges, 50, 100, 3);
        qDebug()<<"GPU Canny time"<<time.elapsed(); time.restart();
        
    }
    //imshow("Canny Edges CPU", gpuEdges);

}
           

1.win10下mingw 64位编译器输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

2.win10下vs2019 64位编译器输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

3.鸿蒙平板下 输出如下

Qt+openCV学习笔记(十二)Qt5.15.2+openCV4.5.5测试opencl加速

为方便看,笔者整理了一个表格

  mingw64 vs2019 64 android
次数 1 2 3 1 2 3 1 2 3
CPU read time 511     431     559    
CPU cvtColor time 148 345 245 24 112 113 30 250 176
CPU GaussianBlur time 20 6 5 9 5 6 26 5 5
CPU Canny time 322 222 216 99 100 91 234 160 160
                   
 次数  1 2  3   1  2  3  1  2  3
GPU read time 419     439     535    
GPU sentToGpu time 63 32 43 34 32 43 38 21 20
GPU cvtColor time 53 6 59 5 5
GPU GaussianBlur time 27 20 21 30 19 21 36 45 43
GPU Canny time 20 2 3 18 2 3 345 234 236

为了有效对比下,笔者用PS生成了一张10M的图片,分别使用不同编译器生成的程序处理。硬件加速的结果,有点让笔者意外,但效果还是有的

后记

笔者曾尝试查找,umat如何选择使用的哪一个openCL硬件。没有查找到方便的接口,只查到使用openCL的接口。若是有时间,确实应该进修一下了