Usage of CPU/GPU while using Stitching() in OpenCV & OpenCL

Refresh

April 2019

Views

104 time

1

Specs : Visual Studio 2017 ; OpenCV 3.4

I am trying to run the stitching algorithm of OpenCV on my computer GPU. To do that I use the OpenCL library, which is inclued in a transparant API in OpenCV (https://www.learnopencv.com/opencv-transparent-api/).

I am monitoring my GPU and CPU activity using MSI Afterburner, which gives differents statistics about GPU and CPU usage (as shown in the screenshots below).

Even when my variables are declared using GPU variables, my code still runs on the CPU :

CPU usage once the stitching algorithm is called, CPU usage goes to 100%.

GPU usage

While the GPU is still unused.

My source code is written as below :

#include "pch.h"
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\core\utility.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\stitching.hpp>
#include <opencv2\core\mat.hpp>
#include <opencv2\core\ocl.hpp>

using namespace cv;
using namespace std;

//GLOBAL VARIABLES
bool try_use_gpu = true;
int main()
{
    //Test if OpenCL is usable on this computer
    if (!cv::ocl::haveOpenCL())
    {
        cout << "OPEN_CL NOT AVAILABLE OR NOT LOADED" << endl;
        return -1;
    }
    cout << "OPENCL AVAILABLE" << endl;

    cv::ocl::Context ctx = cv::ocl::Context::getDefault();
    if (!ctx.ptr())
    {
        cerr << "OpenCL is not available" << endl;
        return -1;
    }

    cv::ocl::Device device = cv::ocl::Device::getDefault();
    if (!device.compilerAvailable())
    {
        cerr << "OpenCL compiler is not available" << endl;
        return -1;
    }

    vector<String> filenames;
    String folder = "C:/../Img_Proc_Stitching/Dataset/";
    cout << "start globing" << endl;
    glob(folder, filenames);
    cout << "globing ended" << endl;

    if (filenames.empty())
        return -1;
    UMat u_img;
    vector<UMat> u_img_vector;
    for (size_t i = 0; i < filenames.size(); i++)
    {
        u_img = imread(filenames[i], CV_LOAD_IMAGE_ANYCOLOR).getUMat(ACCESS_READ);
        cout << filenames[i] << endl;
        if (u_img.empty())
        {
            cout << "ERROR READING SRC" << endl;
            return -1;
        }
        else
        {
            cout << "SRC " << i << " resize" << endl;
            resize(u_img, u_img, Size(), 0.3, 0.3, INTER_AREA);
            cout << "SRC -> U_SRC" << endl;
            cout << "U_SRC -> vector_IMG" << endl;
            u_img_vector.push_back(u_img);
            u_img.release();
        }
    }

    UMat u_map;
    cout << "create stitcher " << endl;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    stitcher.Stitcher::setWarper(new PlaneWarper);
    stitcher.Stitcher::setFeaturesFinder(new detail::OrbFeaturesFinder());
    cout << "call stitcher" << endl;
    stitcher.Stitcher::stitch(u_img_vector, u_map);
    cout << "Stitcher finished" << endl;

    if (!stitcher.Stitcher::Status::OK)
    {
        cout << "Can't stitch images" << endl;
        return EXIT_FAILURE;
    }
    cout << "Stitcher status checked" << endl;
    Mat map = u_map.getMat(ACCESS_READ);
    imwrite("C:/Users/aterrier/Desktop/OpenCVRoot/Img_Proc/Final_Map.jpg", map);
    cout << "Map written" << endl;

    // EOF
    cv::waitKey(100000);
    return 0;
}

I have looked into OpenCV exemples (https://github.com/opencv/opencv/tree/master/samples/tapi) of how to use the transparant API to execute code into the GPU instead of the CPU, but I could not find what I did wrong.

0 answers