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

How to use arbitrary version of OpenCV

asked 2017-03-21 20:58:51 -0600

sep_vanmarcke gravatar image

updated 2017-03-21 22:27:13 -0600

Hi, i have ROS Kinetic installed and it comes with OpenCV. I also have the latested OpenCV that i downloaded from github and installed in /usr/local. The following code that i wrote will always use OpenCV that came with the ROS installation, can i make it use an arbitrary version of OpenCV that i have on the system? Here's the CMakeLists.txt file:

cmake_minimum_required(VERSION 2.6)
project(my_project)
find_package(OpenCV 3.0.0 REQUIRED)
message("OpenCV version: ${OpenCV_VERSION}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
set(SRC
  main.cpp
)
add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

When i run it i get:

Found OpenCV: /opt/ros/kinetic (found suitable version "3.2.0", minimum required is "3.0.0") 
OpenCV version: 3.2.0

UPDATE

I found a way to make it work. I'am open for alternatives to this. This is the CMakeLists.txt that i have now:

cmake_minimum_required(VERSION 2.6)
project(tut)

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})
link_directories(${OpenCV_LIB_DIR})
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2017-03-21 23:25:05 -0600

In the console before executing catkin_make try to execute the following

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

This should give preference to your custom OpenCV installation when doing the find_package(OpenCV 3.0.0 REQUIRED)

edit flag offensive delete link more

Comments

That did the trick, thanks. It's the same version as the one under Kinetic,3.2.0, but it's better to use the most recent in github, right?

sep_vanmarcke gravatar image sep_vanmarcke  ( 2017-03-22 05:10:59 -0600 )edit

Note that this will cause CMake to consider all libraries that it can find in /usr/localbefore it uses system-wide installs (ie: by the pkg manager).

gvdhoorn gravatar image gvdhoorn  ( 2017-03-22 13:57:34 -0600 )edit

@sep_vanmarcke using the latest OpenCV from github is only worth it if you are a hardcore OpenCV developer who needs the latest features of the library. Otherwise, the system version packed with Ubuntu works just fine. It is also worth noting @gvdhoorn comment.

Martin Peris gravatar image Martin Peris  ( 2017-03-22 20:16:12 -0600 )edit
3

Worth noting is that the OpenCV from the standard Ubuntu repo isn't compiled with CUDA support. So if this is needed it is needed to compile OpenCV from source. In respect to your question, if there are two separate versions of OpenCV (e.g 3.0.0 and 3.2.0), you could specify EXACT to find_package.

LostInTheWoods gravatar image LostInTheWoods  ( 2017-03-23 06:58:58 -0600 )edit
0

answered 2019-05-06 07:04:16 -0600

ancientghost gravatar image

updated 2019-05-06 07:16:08 -0600

First, you need to find where 'OpenCVConfig.cmake' is, using following command:

locate OpenCVConfig.cmake

And then you just need to add the following command before 'find_package'.

set(OpenCV_DIR path_include_OpenCVConfig.cmake)

For example,

set(OpenCV_DIR /usr/local/lib/cmake/opencv4)
find_package(OpenCV 4.0.0 REQUIRED)
edit flag offensive delete link more

Comments

@ ancientghost I have the same problem but for not ROS, I am using python but python by default detects the "OpenCV-3.3.1-dev", How can I change it for python? Thanks

Ibrahim_aerospace gravatar image Ibrahim_aerospace  ( 2019-07-03 00:11:48 -0600 )edit
0

answered 2017-03-22 03:52:13 -0600

gvdhoorn gravatar image

The FindOpenCV.cmake script should support a OpenCV_ROOT_DIR variable. You could see whether setting that lets you override the preferred search order without setting all the other variables manually.

edit flag offensive delete link more

Comments

To what value should i set OpenCV_ROOT_DIR? I tried to set it to opencv cloned folder in my home directory and it didn't work. Then i tried to set to /usr/local/include and it also didnd't work.

sep_vanmarcke gravatar image sep_vanmarcke  ( 2017-03-22 05:01:00 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2017-03-21 20:58:51 -0600

Seen: 10,020 times

Last updated: May 06 '19