Search code examples
cmakeopencv3.0roscatkin

Use custom OpenCV install in ROS package


I am trying to build a ROS package in which I use a custom OpenCV 3.4 install in which I have CUDA enabled. The OpenCV install, in /usr/local, is working just fine, I can build and run samples from the GPU folder no problem using

g++ -o houghlines houghlines.cpp `pkg-config opencv --cflags --libs`

However, I just cannot get catkin to use my install of OpenCV, rather than the one that ships with ROS. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(motion_segmentation_cuda)

set(CMAKE_CUDA_COMPILER  /usr/local/cuda-9.1/bin/nvcc)
find_package(catkin_simple REQUIRED)

set(OpenCV_INCLUDE_DIRS
  /usr/local/include
  /usr/local/include/opencv2
)
set(OpenCV_LIB_DIR
  /usr/local/lib
)
set(OpenCV_LIBS
  opencv_core
  opencv_highgui
  opencv_imgcodecs
)
include_directories(${OpenCV_INCLUDE_DIRS})

find_package(CUDA REQUIRED)
catkin_simple()

#Here you can set any gcc/cmake compiler flags, if you so wish
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3")
link_directories(${OpenCV_LIB_DIR})

#Add all of your sources here
cuda_add_executable(
  cuda_test_cu
  src/main.cpp
  src/motion_segmenter.cpp
  src/kernel.cu
  src/segmenter.cpp
)

#Link the executable to the necessary libs
target_link_libraries(
   cuda_test_cu
   ${catkin_LIBRARIES}
   ${OpenCV_LIBS}
   ${CUDA_LIBRARIES}
)

# CMake Indexing
FILE(GLOB_RECURSE LibFiles "include/*")
add_custom_target(headers SOURCES ${LibFiles})

cs_install()

On the advice of various forums I have also tried to

export CMAKE_PREFIX_PATH="/usr/lib/:$CMAKE_PREFIX_PATH"
export LD_LIBRARY_PATH="/usr/lib/:$LD_LIBRARY_PATH"

And a whole myriad of tweaks in the CMakeLists. And my package compiles just fine! But in the end, I inevitably get:

OpenCV Error: No CUDA support (The library is compiled without CUDA support) in throw_no_cuda, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/include/opencv2/core/private.cuda.hpp, line 107

As soon as I try to actually use any of the GPU enabled OpenCV classes.

Many thanks!


Solution

  • To answer your question, past me, this CMakeLists.txt works (for some reason):

    cmake_minimum_required(VERSION 2.8.3)
    project(motion_segmentation_cuda)
    
    find_package(OpenCV 3.4 REQUIRED
      COMPONENTS
        opencv_core
        opencv_highgui
        opencv_imgproc
        opencv_imgcodecs
      PATHS /usr/local
      NO_DEFAULT_PATH
      CONFIG
    )
    
    set(CMAKE_CUDA_COMPILER  /usr/local/cuda-9.1/bin/nvcc)
    find_package(catkin_simple REQUIRED)
    find_package(CUDA REQUIRED)
    catkin_simple()
    
    #Here you can set any gcc/cmake compiler flags, if you so wish
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3")
    
    include_directories(include ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
    
    #Add all of your sources here
    cuda_add_executable(
      cuda_test_cu
      src/main.cpp
      src/motion_segmenter.cpp
      src/kernel.cu
      src/segmenter.cpp
    )
    
    #Link the executable to the necessary libs
    target_link_libraries(
       cuda_test_cu
       ${catkin_LIBRARIES}
       ${OpenCV_LIBS}
       ${CUDA_LIBRARIES}
    )
    
    # CMake Indexing
    FILE(GLOB_RECURSE LibFiles "include/*")
    add_custom_target(headers SOURCES ${LibFiles})
    
    cs_install()
    

    No need to change the .bashrc in the end...