1
$\begingroup$

Rosanswers logo

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})

Originally posted by sep_vanmarcke on ROS Answers with karma: 27 on 2017-03-21

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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)


Originally posted by Martin Peris with karma: 5625 on 2017-03-21

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by sep_vanmarcke on 2017-03-22:
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?

Comment by gvdhoorn on 2017-03-22:
Note that this will cause CMake to consider all libraries that it can find in /usr/local before it uses system-wide installs (ie: by the pkg manager).

Comment by Martin Peris on 2017-03-22:
@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.

Comment by LostInTheWoods on 2017-03-23:
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.

$\endgroup$
2
$\begingroup$

In case someone comes up here looking for how to use the source installed OpenCV installation with Python, look for following.

  1. When you build the OpenCV installation, make sure to add following arguments to the cmake command:

    -D PYTHON3_EXECUTABLE=$(which python3) \
        -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
        -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
        -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/  \
    -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) ..
    

    Reference: https://docs.opencv.org/3.4/d7/d9f/tutorial_linux_install.html

  2. During the build process, you will come across a statement in the command line output stating:

    add the following line to the PYTHONPATH to use this version for python development:
    <path to source opencv folder>/build/python_loader
    
    for eg:
    /home/radiant/kstrike_dep/opencv/build/python_loader
    

    Therefore, in addition to the above commands in the accepted answer, add the following statement as well to the bashrc:

    export PYTHONPATH=<path to source opencv folder>/build/python_loader:${PYTHONPATH}
    
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.