C++ application with OpenCV and cuda support

Dear all,

I’m facing some difficulties trying to compile and run a simple c++ application that uses c++ and cuda support on jetson Nano.

The app should read an image from the disk and apply a bilateral filter using opencv.

Without using cuda support everything works as expected, when I try o use cuda support I cannot compile and run.

What I’ve tried so far:

I’ve tried to disable the bilateral filter by commenting the line: “cv::cuda::bilateralFilter(src,dst,3,1,1);” The app compiles but the following error appears in the command line:

Error: OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/core/include/opencv2/core/private.cuda.hpp:107: error: (-216:No CUDA support) The library is compiled without CUDA support in function ‘throw_no_cuda’

On my jetson I’ve checked if the opencv libraries with cuda support are there:
cd /usr/local/include/opencv4/opencv2/
ls -l | grep “cuda”
Capturar0

all the includes seem to be there…

My example code is the following:

#include <iostream>
#include <ctime>
#include <cmath>
#include "bits/time.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>

#define TestCUDA true

using namespace std;

int main() {
    std::clock_t begin = std::clock();

    cout << "Test....\n";
        try {
            cv::String filename = "animal.jpg";
            cv::Mat srcHost = cv::imread(filename, cv::IMREAD_GRAYSCALE);

            for(int i=0; i<1000; i++) {
                if(TestCUDA) {
                    cv::cuda::GpuMat dst, src;
                    src.upload(srcHost);

                    //cv::cuda::threshold(src,dst,128.0,255.0, CV_THRESH_BINARY);
                    cv::cuda::bilateralFilter(src,dst,3,1,1);

                    cv::Mat resultHost;
                    dst.download(resultHost);
                } else {
                    cv::Mat dst;
                    cv::bilateralFilter(srcHost,dst,3,1,1);
                }
            }

//            cv::imshow("Result",dst);
//            cv::waitKey();
        } catch(const cv::Exception& ex) {
            std::cout << "Error: " << ex.what() << std::endl;
        }

    std::clock_t end = std::clock();
    std::cout << double(end-begin) / CLOCKS_PER_SEC  << std::endl;
}

In my development environment I’m using nsight eclipse 10.2 on my host computer and here are the build settings that I find relevant:



I’ve installed opencv on my jetson with cuda support using this “popular” instruction from Jetson Hacks:

It is possible that I’m missing something simple… I appreciate your help!

Regards :)

Hi,

The default (comes with JetPack) OpenCV package doesn’t build with GPU support.
You will need to build it from the source to get the GPU version OpenCV algorithm.

Below is the building script for your reference:

Thanks.

1 Like

Dear AastaLLL,

Thank you for your input. When using the jetson hacks opencv installation scipt maybe I’ve done something wrong and the opencv cuda libraries were not working.

Using your script, everything workout fine.

Thank you !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.