2

I want to use ROS with CUDA-enabled OpenCV on my Jetson Nano. At this point I don´t care about the versions.

The problem: Jetson Nano only supports CUDA 10 and Ubuntu 18.04. The ROS version for Ubuntu 18.04 is Melodic, which needs OpenCV 3.2, but OpenCV 3.2 only supports CUDA 8.

I have found a guide here on SO (CMake Error: Variables are set to NOTFOUND) to build it with CUDA 9, but it fails when trying it with CUDA 10 due to "error: identifier "__shfl_down" is undefined", and some other "__shfl_XXX" errors.

Anyone here succeeded in getting this to work? Or any idea on how to fix the "__shfl_down" error?

Can OpenCV 3.2 work with CUDA 10.2?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Milan
  • 109
  • 1
  • 9
  • You are only going to get errors like that if you were actually compiling with CUDA 8 or older https://stackoverflow.com/a/59018716/681865 – talonmies Aug 21 '20 at 12:19
  • But the installed cuda is definitely 10.2, which is also what cmake detects in the configuration process. Could there be something wrong in the configuration, telling cmake to try and build with cuda version <8? The Jetson Nano doesn't even support cuda <10, so I really don't know what's going wrong.. – Milan Aug 21 '20 at 14:26

2 Answers2

2
  1. Option 01:

    Nope, do not try to build with CUDA 10.2, this is my suggestion in which you are in safe side both ways. In your package CMakeLists.txt add your alternative OpenCV (comes with Nano) as follows while assumimg main.cpp is the your main file, if not change it:

       set(OpenCV_INCLUDE_DIRS
          <path_to>/include
          <path_to>/include/opencv2
        )
    
        set(OpenCV_LIB_DIR
          <path_to>/lib
        )
    
        set(OpenCV_LIBS
          opencv_core
          opencv_highgui
          opencv_imgcodecs
        )
    
        include_directories(${OpenCV_INCLUDE_DIRS})
        link_directories(${OpenCV_LIB_DIR})
        add_executable(${PROJECT_NAME} src/main.cpp)
        target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
    

    Under OpenCV_LIBS add OpenCV modules you use in your code

    Note: I am not a position to test this on my machine, so consider this as a tentative answer, if you have problems let me know, I will try to help

  2. Option 02:

    catkin_make -DOpenCV_DIR=/usr/local/share/OpenCV

    Note: OpenCV_DIR must point to a folder with opencv-conifg.cmake file. More information can be found here

GPrathap
  • 7,336
  • 7
  • 65
  • 83
  • Thank you for your suggestion, I will try to see if it works. But won´t cv_bridge still use OpenCV 3.2, resulting in compatibility issues when converting from ROS image to OpenCV image using cv_bridge? – Milan Aug 21 '20 at 18:04
  • In principle, your suggestion works, but only until I add cv_bridge as a dependency. Then I get the error "Project 'cv_bridge' specifies '/usr/include/opencv' as an include dir, which is not found". I guess I would have to build cv_bridge (and any module depending on OpenCV) from source, specifying which OpenCV to use? – Milan Aug 22 '20 at 16:23
  • Thanks for the update, I also just found that post and tried it. Unfortunately, it seems to only work if the major OpenCV version matches, as it fails with error "Could not find a configuration file for package "OpenCV" that is compatible with requested version "3"", then listing the version 4.1.1 as not accepted. – Milan Aug 22 '20 at 18:16
  • @Milan how have you built cv_bridge? Download the cv_bridge package and try to build with `catkin_make -DOpenCV_DIR=/usr/local/share/OpenCV` or change cv_bridge's CMakeLists.txt file adding alternative opencv location – GPrathap Aug 23 '20 at 07:17
  • Yes, I used your first option, downloading cv_bridge into my catkin_workspace and running catkin_make -DOpenCV_DIR=/usr/local/share/opencv4. That´s when I get the error. It seems like cv_bridge is doing a check to ensure that the opencv version is 3.x.x – Milan Aug 23 '20 at 13:01
  • change cv_bridge package CMakeLists.txt adding your OpenCV path as given in option 01 – GPrathap Aug 23 '20 at 14:26
  • In the CMakeLists.txt of cv_bridge, it looks for OpenCV version 3 with find_package(OpenCV 3 REQUIRED...). Changing that to 4 results in the build errors "cannot declare variable 'g_numpyAllocator' to be of abstract type 'NumpyAllocator'" and "class cv::Mat has no member named 'refcount'". Probably due to some changes in OpenCV? Seems like I will have to do some fiddling with the source code of cv_bridge to get this to work? – Milan Aug 23 '20 at 15:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220320/discussion-between-gprathap-and-milan). – GPrathap Aug 23 '20 at 18:24
1

I found a solution, even though I have not done extensive testing on it yet:

  1. Build OpenCV 4.2 (any version that supports CUDA 10.2 should work) from source, enabling CUDA. A good guide is available at https://www.pyimagesearch.com/2020/03/25/how-to-configure-your-nvidia-jetson-nano-for-computer-vision-and-deep-learning/ (skip parts with tensorflow etc)

  2. Install ROS Melodic. No need to build from source.

  3. After creating a workspace, clone the cv_bridge and image_transport modules into it. IMPORTANT: Switch to branch "Noetic"!

  4. In the CMakeLists.txt in cv_bridge, change the find_package(Boost REQUIRED python37) to find_package(BOOST REQUIRED python)

  5. In cv_bridge/src/module.hpp in the function do_numpy_import, change the return type from void* to void, and remove the return nullptr;

  6. Build the workspace with catkin. It should build normally.

I have tested a basic image publish, and viewing it in rqt_image_view. Works like a charm!

I know this is a bit of a hacky solution, so if anyone knows something better, please let me know!

Dharman
  • 30,962
  • 25
  • 85
  • 135
Milan
  • 109
  • 1
  • 9