Can not create cuda::ORB. Program Stucks

asked 2016-03-27 04:16:33 -0500

Hi everyone,

I have a simple piece of code that uses both cv::ORB and Cuda:ORB. In my code, I can not use cv::Ptr<cuda::ORB> orb = cuda::ORB::create(); It stucks my program.

For example this is the code I am using; (I did not remove the unused .hpp files for this example because In my real program I need them too)

#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
#include "opencv2/cudawarping.hpp"
#include "opencv2/cudafilters.hpp"
#include "opencv2/cudafeatures2d.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/core/cuda.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/ximgproc.hpp"
#include <time.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[]) {

    Mat img1 = imread("amanda.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    Mat img2 = imread("amanda.jpg", CV_LOAD_IMAGE_GRAYSCALE);

    std::cout << "1 " << std::endl;

    cv::cuda::GpuMat img1_gpu, img2_gpu;

    std::cout << "2 " << std::endl;

    cv::Ptr<cuda::ORB> orb = cuda::ORB::create();

    cv::imshow("Show Image", img1);

    return 0;


To be sure what is going on here I put two print statement in my code. Above code just writes "1" to console and stuck. How is it possible? How did this program get stuck? How did print 1 and not 2?

If I change Cuda:ORB to cv:ORB everything is fine. If I put cv::Ptr<cuda::ORB> orb = Cuda::ORB::create(); beginning of the code, program prints nothing to console which is expected in my case.

Things get more interesting here. If I wait 30 seconds or more sometimes program does print 2. And If I use Cuda::ORB, it does the job. For a really small image, why it takes too much time to response?

Is this a bug? Should I open a bug report on Github?

For the ones who wants to test this can use this little piece of code. It makes me wait too much and sometimes never response:

    int main(int argc, char* argv[]) {

    std::cout << "1 " << std::endl;
    cv::Ptr<cuda::ORB> orb = cuda::ORB::create();
    std::cout << "2 " << std::endl;

    return 0;

Any comment or idea are appreciated.

edit retag flag offensive close merge delete


On my computer, the cuda::ORB is working fine with an image of 800x800 size in less than 2 sec (upload one image to the GPU + detect and draw keypoints). What do you mean by a really small image, what is your image size ?

Eduardo gravatar imageEduardo ( 2016-03-27 10:34:24 -0500 )edit

@Eduardo Thank you for your answer, It gives me some light at least. The image I am using is about 600x600. It seems I have an unknown problem. Do you have any idea what could be the problem? Hardware, software? I get same behaviour both Ubuntu and Windows with OpenCV 3.1..

Ender Tunç gravatar imageEnder Tunç ( 2016-03-27 11:22:52 -0500 )edit

What is your GPU model ?

You can try to launch an OpenCV test: for example, if you have enabled in CMake and built the OpenCV test, you can try to launch opencv_test_cudaarithm in the build folder.

If there is a problem, you can try to update the GPU driver or the cuda driver ?

Eduardo gravatar imageEduardo ( 2016-03-27 12:59:34 -0500 )edit

@Eduardo to be sure can you please send me a piece of code to test? My old laptop has GPU problems but now I got a new one with Cuda 7.5, Nvidia drive 361 and Ubuntu 15.10.

Ender Tunç gravatar imageEnder Tunç ( 2016-04-05 09:49:34 -0500 )edit

In my opinion, the best would be to build and launch the OpenCV test (opencv_test_cuda*).

Otherwise you can try:

#include <opencv2/opencv.hpp>
#include <opencv2/cudafeatures2d.hpp>

int main()
    cv::cuda::GpuMat gpu_img;
    cv::Mat img = cv::imread("image.jpg", CV_LOAD_IMAGE_GRAYSCALE);

    cv::Ptr<cv::cuda::ORB> orb = cv::cuda::ORB::create();
    std::vector<cv::KeyPoint> keypoints;
    orb->detect(gpu_img, keypoints);
    std::cout << "keypoints=" << keypoints.size() << std::endl;

    cv::drawKeypoints(img, keypoints, img);
    cv::imshow("ORB", img);

    return 0;
Eduardo gravatar imageEduardo ( 2016-04-06 14:45:42 -0500 )edit

@Eduardo thank you for help. This code takes 25 seconds on my machine. My partner tried it on his machine and it takes in 2-3 seconds. I have Nvidia 960M and latest driver 364.15 is installed on ubuntu 15.10. I do not know what else I can do. I want to try to run test but I do not know how. This is the code that I use for building OpenCV. What should I add to build tests too? How to run test? Maybe I should add these two -DBUILD_TESTS=ON -DINSTALL_TESTS=ON?

Ender Tunç gravatar imageEnder Tunç ( 2016-04-11 01:46:52 -0500 )edit

Yes, you should add -DBUILD_TESTS=ON and -DBUILD_PERF_TESTS=ON for performance tests (you can also run ccmake for a gui version of CMake).

Then, the test binaries should be in your OpenCV build directory in the bin folder (e.g. OpenCV_build/bin).

Also, look at this tutorial as some tests need the OpenCV extra module.

Eduardo gravatar imageEduardo ( 2016-04-11 04:55:59 -0500 )edit

@Eduardo Now I can run the test. As you suggested I run opencv_test_cudaarithm and addition to that I also run opencv_test_cudafeatures2d. There some failed test I do not know why they failed. You can check the outputs: (this one is very large it takes some time to open) In StackOverflow, they say some test may fail because of the missing images. They suggest put lena.png to bin folder so I did it. Do you have any idea what should I do next? Should I create bug report or my system is broken? Btw, thanks for you help again.

Ender Tunç gravatar imageEnder Tunç ( 2016-04-11 09:31:09 -0500 )edit

You have to download the OpenCV extra modules to have the images used for test and performance:

You will have to add in your bashrc: export OPENCV_TEST_DATA_PATH=path_to_opencv_extra/testdata.

A runtime of 25 sec is too long especially if your teamate run it in 3 sec (did you use the same image, release mode, comparable GPU ?). As I am not at all an expert on CUDA, I think that you can open an issue here: You can add the links to the ouput of opencv_test_cudafeatures2d and opencv_perf_cudafeatures2d in the bug report I think.

Eduardo gravatar imageEduardo ( 2016-04-11 10:51:49 -0500 )edit