Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake finding incorrect version of OpenCV

I have multiple versions of OpenCV installed, and CMake is finding the wrong one. I have installed opencv and opencv3 using brew, and they exist in the following paths:

/usr/local/opt/opencv3
/usr/local/opt/opencv

My CMakeLists.txt looks like the following:

cmake_minimum_required(VERSION 2.8)
set(ENV{OpenCV_DIR} "/usr/local/opt/opencv3")

project(TestProject)

#OpenCV
find_package( OpenCV 3 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

MESSAGE ( STATUS "Found OpenCV: ${OpenCV_VERSION}" )
MESSAGE ( STATUS "OpenCV_INCLUDE_DIRS= ${OpenCV_INCLUDE_DIRS}" )
MESSAGE ( STATUS "OpenCV_DIR= $ENV{OpenCV_DIR}" )

I am using OpenCV_DIR to point to the location of the OpenCV that I would like to use (in this example it's set immediately before just to be 100% certain that this variable points to the right place).

My output is this:

-- Found OpenCV: 3.0.0
-- OpenCV_INCLUDE_DIRS= /usr/local/include/opencv
-- OpenCV_DIR= /usr/local/opt/opencv3

So, it's finding the right version of OpenCV (3.0.0), but the include path is set to some other opencv, in this case /usr/local/include/opencv points to /usr/local/opt/opencv, which is the 2.4.8 version.

As a result, none of my programs find the right files to include! Does anyone know how to tell CMake which version to look for, if the OpenCV_DIR environment variable does not seem to work?

like image 683
user2385408 Avatar asked Jan 08 '16 23:01

user2385408


2 Answers

You can explicitly assign the path in find_package. For example,

find_package( OpenCV 3 REQUIRED PATHS "/usr/local/opt/opencv3" )

like image 86
Nukul Khadse Avatar answered Oct 24 '22 08:10

Nukul Khadse


As indicated OP is using cMake V2.8. Therefore the following applies to include your desired directory.

#OpenCV
find_package( OpenCV 3 REQUIRED )
include_directories([BEFORE] "/usr/local/opt/opencv3")  # your cv2 desired folder
                                                        #            stated here.

The [BEFORE] argument informs cMake about this particular folder to use first rather than the other CV folder.

For cMake higher than v2.8 you should not use the include_directories but rather the following:

target_include_directories(test PRIVATE ${YOUR_DIRECTORY})

like image 23
ZF007 Avatar answered Oct 24 '22 07:10

ZF007