ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to tell Catkin_make to use opencv with gpu?

asked 2017-10-12 01:58:59 -0600

k_totoro gravatar image

updated 2017-10-13 23:06:17 -0600


I have same trouble with this Having trouble using CUDA enabled OpenCV with kinetic.

My trouble is catkin_make is looking into only non-cuda function for ros-kinetic even I use NO_MODULE to tell exact opencv path.

Test CMakeLists.txt

So I made a test with CMakeLists.txt with following statement.

# find_package(Boost REQUIRED COMPONENTS system)
find_package(OpenCV REQUIRED
NO_MODULE # should be optional, tells CMake to use config mode
PATHS /usr/local # look here
NO_DEFAULT_PATH) # and don't look anywhere else

MESSAGE(WARNING "config path ${OpenCV_CONFIG_PATH}") # look at the output of this message

Test result

As a result, I can see catkin_make can correctly locate my opencv with cuda.

CMake Warning at CMakeLists.txt:10 (MESSAGE):
  version 3.1.0

CMake Warning at CMakeLists.txt:11 (MESSAGE):
  install path /usr/local

CMake Warning at CMakeLists.txt:12 (MESSAGE):
  config path /usr/local/share/OpenCV

CMake Warning at CMakeLists.txt:13 (MESSAGE):
  include dirs /usr/local/include/opencv;/usr/local/include

CMake Warning at CMakeLists.txt:14 (MESSAGE):

Problem : Catkin_LIB including

But I find my package still do not using opencv with cuda .

Now I found my $(catkin_LIBRARIES) including ros-kinetic-opencv3 lib.( This is necessary for the cv_bridge ) This is my ${catkin_INCLUDE_DIRS}. /opt/ros/kinetic/include;/opt/ros/kinetic/share/xmlrpcpp/cmake/../../../include/xmlrpcpp;/opt/ros/kinetic/include/opencv-3.2.0-dev;/opt/ros/kinetic/include/opencv-3.2.0-dev/opencv;/usr/include

Let me show some part of my CmakeLists.

include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
include_directories( include ${catkin_INCLUDE_DIRS} )  
target_link_libraries(my_function ${OpenCV_LIBS} ${catkin_LIBRARIES}) # this do not wrok
target_link_libraries(my_gpu_function ${OpenCV_LIBS} ) # this work

"my_function" linked with ${catkin_LIBRARIES} can not use gpu, on the other hand "my_gpu_function" linked only with ${OpenCV_LIBS} does. So I assume catkin_LIB have something to do with this problem.

Updated in 2017 Oct 13: test project

I found excluding every library named opencv from catkin_libraries do works.

I put on my sample project on here.

This mean adding any of opencv3.2 in ros package cause the linker to lose cuda opencv function. Fortunately cv_bridge and image_transprot is safe to include, so I can build ros project but I feel very unease about this. Any one has any knowledge about this phenomena?


How can I use both opencv cuda and cv_bridge and any other ros opencv related package in the same time in more better way?

Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-10-16 17:11:53 -0600

k_totoro gravatar image

updated 2017-10-17 18:43:29 -0600

Hi previous me. I am still not sure how it works, but somehow I could make it work.

1. delete build and devel folder and recompile

Even after changing cmakelist, catkin_make still use previous package (I don't know why). Delete these folders can refresh some information and solve this problem.

2. Exclude /opt/ros/kinetic/lib/; from catkin_LIBRARIES

After you told your opencv location to catkin_make, there is still ros-opencv reference information in that file. So, just delete that files using following command.

string(REPLACE "/opt/ros/kinetic/lib/;" "" Modified_CLIBS "${catkin_LIBRARIES}")

Then, use this newly modified Modified_CLIBS instead of catkin_LIBRARIES.

For more detail, please look at my sample CMakeList.txt in test project.

Ex. sometimes you need to exclude whole ros-opencv packages

I found in my another project, the method <2> do not work. So, I tried next ugly code, and it worked.

string(REPLACE "/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/;/opt/ros/kinetic/lib/ ...
edit flag offensive delete link more


hi, seems nice and nearly generic to me but why not string(REPLACE "/opt/ros/kinetic/lib/;" "" catkin_LIBRARIES "${catkin_LIBRARIES}") ? ( if it works ) ( and your CmakeList in your test project has not been edited ;) way better solution than mine anyway =D

TTDM gravatar image TTDM  ( 2017-10-17 03:54:11 -0600 )edit

Hi. Your code should be work but I just want to separately use my gpu-opencv and ros-opencv. Did my test project worked in your environment?

k_totoro gravatar image k_totoro  ( 2017-10-17 14:04:09 -0600 )edit

answered 2017-10-12 06:37:05 -0600

TTDM gravatar image

updated 2017-10-13 04:25:27 -0600

To compile a workspace with a specific version of OpenCV ( for exemple a version with Cuda ) see :

You can look either at the accepted answer there or at my answer.

I don't believe you have any interest in recompiling all the packages included in the kinetic distribution (like CV_bridge) using a cuda enabled version of openCV. For your own packages, you should be working inside a workspace and in that case thelink is exactly what you need

Ps : a fast thought, what happen if you erase the build and devel directory of your workspace and compile using your current cmakefile ( in which the exact OpenCV path is specified ) ?

Edit: For the set(catkin_LIBRAIRIES first_lib_you_want; second_lib_you_want; ...), what you want to do is run your cmakelist one time printing the catkin_LIBRAIRIES and then you want to keep everything that's not an OpenCV lib and those are the libs you want to keep in your set.

edit flag offensive delete link more


Thank you for your comment. Deleting devel and build folder works to tell catkin the correct opencv dir. But I still have same problem may be because catkin_DIR has opencv path for ROS. The detail is in my updated question.

k_totoro gravatar image k_totoro  ( 2017-10-12 15:52:13 -0600 )edit

If you need it quickly, an awfull solution is to do a set(catkin_LIBRAIRIES first_lib_youwant; secondlibyouwant; ...) which is what i have done on all on the workspace on this PC, On an other PC, i have smth a lot cleaner but I don't really have time to check it for at least a week :( details in ans

TTDM gravatar image TTDM  ( 2017-10-13 04:17:26 -0600 )edit

Hi. I checked set(catkin_LIBRARIES ...) method works. I found including any opencv-ros lib in /opt/ros/kinetic/lib/ cause linking error. So I manually excluded whole libs named opencv from catkin_library.( more than 40! ) Do you know good way to do this automatically?

k_totoro gravatar image k_totoro  ( 2017-10-13 22:58:22 -0600 )edit

Hi. I found I just have to remove /opt/ros/kinetic/lib/; from catkin_libraries. And now it seems my program can locate cuda function!

I put on the sample project here.

Thank you for your help!

k_totoro gravatar image k_totoro  ( 2017-10-13 23:35:38 -0600 )edit

well that's still a hack but at least it's a lot simpler to do it than to remove all libs manually ( as i was doing too ) . You welcome, and maybe smbd will come with a clean fix ;)

TTDM gravatar image TTDM  ( 2017-10-14 05:37:45 -0600 )edit

I checked the Cmakelist on my other PC (where i'm also using kinetic and cuda OpenCV and where it works without the hack) There isnt much difference with the CmakeList on this laptop where it doesn't work.. The only differences i saw was in the order on inclusion so I made a few test but no results.

TTDM gravatar image TTDM  ( 2017-10-14 05:46:34 -0600 )edit

Question Tools



Asked: 2017-10-12 01:58:59 -0600

Seen: 2,588 times

Last updated: Oct 17 '17