Configuring CMake to build OpenCV on WindowsВ¶
General procedureВ¶
Start the GUI version of CMake (cmake-gui).
Select the folder C:\OpenCV\sources as the source directory.
Select the folder C:\OpenCV\builds as the build directory.
Enable the Grouped and Advanced checkboxes just below the build directory name. These will impact the way the packages information will be displayed in the CMake GUI in the following steps.
Press the “Configure” button. A window pops up, letting you specify the compiler (and IDE) you want to use. Pick Visual Studio 10, 32-bit or 64-bit according to your Matlab version. Select also “Use default native compilers” and click Finish.
In case you would like to redo all the steps from the beginning (if you made a mistake somewhere, for instance), you can clear CMake cache in the menu option “File > Delete Cache”.
CMake will start out and based on your system variables will try to automatically locate as many packages as possible. You can modify the packages to use for the build in the WITH > WITH_X menu points (where X is the package abbreviation).
Configure CMake until all the elements are found. Follow the instructions below.
If you don’t need it, you might want to deactivate the Python module both in 32-bit and 64-bit versions since it might not build correctly.
Once you are comfortable with your CMake configuration, press Generate and close CMake.
Here is an alternate version of those instructions, but it does not goes as deep in the details as this tutorial: http://seevisionc.blogspot.co.uk/2014/03/compiling-opencv-30-with-matlab-support.html.
Configuration detailsВ¶
BUILD groupВ¶
Activate everything except:
- BUILD_CUDA_STUBS
- BUILD_DOCS
- BUILD_OPENEXR
- BUILD_TBB
- BUILD_WITH_DYNAMIC_IPP
- BUILD_opencv_apps
- BUILD_opencv_python2
- BUILD_opencv_python3
- BUILD_opencv_ts
- BUILD_opencv_world
For more info about what each OpenCV module does, please visit http://docs.opencv.org/.
To build the doc, Doxygen must be installed. This is why I disabled it, because the doc is easily accessible online anyway.
The Python interface does not build properly on my machine, this is why I have disabled both items about Python. However, feel free to enable the one corresponding to your Python installation (python2 or python3).
ENABLE groupВ¶
Make sure to check ENABLE_SOLUTION_FOLDERS.
INSTALL groupВ¶
Check INSTALL_C_EXAMPLES and INSTALL_TESTS.
MATLAB groupВ¶
Make sure every field is populated here (should be the case automatically).
OPENCV groupВ¶
Complete the entry OPENCV_EXTRA_MODULES_PATH with the path to the extra modules: C:\OpenCV\contrib\modules .
WITH groupВ¶
Activate the following items (15 in total):
- WITH_DIRECTX
- WITH_DSHOW
- WITH_EIGEN
- WITH_FFMPEG
- WITH_IPP
- WITH_JASPER
- WITH_JPEG
- WITH_OPENCL
- WITH_OPENCLAMDBLAS
- WITH_OPENCLAMDFFT
- WITH_PNG
- WITH_TBB
- WITH_TIFF
- WITH_VFW
- WITH_WIN32UI
Intel TBBВ¶
From CMake, configure one time OpenCV (should already be done).
Be careful NOT enabling BUILD_TBB: the automatic downloading and building of OpenCV failed miserably for me despite many efforts. If you had previously enabled BUILD_TBB, my suggestion is to restart from the beginnning, clearing cmake cache.
Configure a second time OpenCV.
Then a new group should appear (in red), named “Ungrouped Entries”. If the TBB include directory is not found, this group will have the option “TBB_INCLUDE_DIRS” with the value “TBB_INCLUDE_DIRS-NOTFOUND”.
Click on the value and then on the tiny button ”. ” appearing on the right-side of the field. Provide the path to the TBB headers, which should be C:\OpenCV\tbb\include .
When done, press the “Configure” button again.
Now, CMake should have been able to locate the TBB headers (include dir). Therefore, a new group “TBB” should appear in the interface. If it is not the case, then retry to locate the TBB headers. They should lie in some include directory within the tbb folder.
In the “TBB” group, there are two new variables whose default values are correct. Note that the value of TBB_LIB_DIR depends on your compiler.
The “TBB” group is now complete. Here are the three variables it contains with their values:
Riteshlogin / OpenCVconfig.cmake
# =================================================================================== |
# The OpenCV CMake configuration file |
# |
# ** File generated automatically, do not modify ** |
# |
# Usage from an external project: |
# In your CMakeLists.txt, add these lines: |
# |
# FIND_PACKAGE(OpenCV REQUIRED) |
# TARGET_LINK_LIBRARIES(MY_TARGET_NAME $ |
# |
# Or you can search for specific OpenCV modules: |
# |
# FIND_PACKAGE(OpenCV REQUIRED core highgui) |
# |
# If the module is found then OPENCV_ _FOUND is set to TRUE. |
# |
# This file will define the following variables: |
# — OpenCV_LIBS : The list of all imported targets for OpenCV modules. |
# — OpenCV_INCLUDE_DIRS : The OpenCV include directories. |
# — OpenCV_COMPUTE_CAPABILITIES : The version of compute capability |
# — OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API |
# — OpenCV_VERSION : The version of this OpenCV build: «2.4.8» |
# — OpenCV_VERSION_MAJOR : Major version part of OpenCV_VERSION: «2» |
# — OpenCV_VERSION_MINOR : Minor version part of OpenCV_VERSION: «4» |
# — OpenCV_VERSION_PATCH : Patch version part of OpenCV_VERSION: «8» |
# — OpenCV_VERSION_TWEAK : Tweak version part of OpenCV_VERSION: «0» |
# |
# Advanced variables: |
# — OpenCV_SHARED |
# — OpenCV_CONFIG_PATH |
# — OpenCV_INSTALL_PATH (not set on Windows) |
# — OpenCV_LIB_COMPONENTS |
# — OpenCV_USE_MANGLED_PATHS |
# — OpenCV_HAVE_ANDROID_CAMERA |
# |
# =================================================================================== |
set (modules_file_suffix «» ) |
if (ANDROID) |
string ( REPLACE — _ modules_file_suffix «_ $ |
endif () |
if ( NOT TARGET opencv_core) |
include ( $ |
endif () |
# TODO All things below should be reviewed. What is about of moving this code into related modules (special vars/hooks/files) |
# Version Compute Capability from which OpenCV has been compiled is remembered |
set (OpenCV_COMPUTE_CAPABILITIES «» ) |
set (OpenCV_CUDA_VERSION ) |
set (OpenCV_USE_CUBLAS ) |
set (OpenCV_USE_CUFFT ) |
set (OpenCV_USE_NVCUVID ) |
# Android API level from which OpenCV has been compiled is remembered |
set (OpenCV_ANDROID_NATIVE_API_LEVEL 0) |
# Some additional settings are required if OpenCV is built as static libs |
set (OpenCV_SHARED ON ) |
# Enables mangled install paths, that help with side by side installs |
set (OpenCV_USE_MANGLED_PATHS FALSE ) |
# Extract the directory where *this* file has been installed (determined at cmake run-time) |
get_filename_component (OpenCV_CONFIG_PATH » $ |
if ( NOT WIN32 OR OpenCV_ANDROID_NATIVE_API_LEVEL GREATER 0) |
if (OpenCV_ANDROID_NATIVE_API_LEVEL GREATER 0) |
set (OpenCV_INSTALL_PATH » $ |
else () |
set (OpenCV_INSTALL_PATH » $ |
endif () |
# Get the absolute path with no ../.. relative marks, to eliminate implicit linker warnings |
if ( $ |
get_filename_component (OpenCV_INSTALL_PATH » $ |
else () |
get_filename_component (OpenCV_INSTALL_PATH » $ |
endif () |
endif () |
# Presence of Android native camera wrappers |
set (OpenCV_HAVE_ANDROID_CAMERA OFF ) |
# ====================================================== |
# Include directories to add to the user project: |
# ====================================================== |
# Provide the include directories to the caller |
set (OpenCV_INCLUDE_DIRS » $ |
include_directories ( $ |
# ====================================================== |
# Link directories to add to the user project: |
# ====================================================== |
# Provide the libs directories to the caller |
set (OpenCV_LIB_DIR_OPT CACHE PATH «Path where release OpenCV libraries are located» ) |
set (OpenCV_LIB_DIR_DBG CACHE PATH «Path where debug OpenCV libraries are located» ) |
set (OpenCV_3RDPARTY_LIB_DIR_OPT CACHE PATH «Path where release 3rdpaty OpenCV dependencies are located» ) |
set (OpenCV_3RDPARTY_LIB_DIR_DBG CACHE PATH «Path where debug 3rdpaty OpenCV dependencies are located» ) |
mark_as_advanced ( FORCE OpenCV_LIB_DIR_OPT OpenCV_LIB_DIR_DBG OpenCV_3RDPARTY_LIB_DIR_OPT OpenCV_3RDPARTY_LIB_DIR_DBG OpenCV_CONFIG_PATH) |
# ====================================================== |
# Version variables: |
# ====================================================== |
SET (OpenCV_VERSION 2.4.8) |
SET (OpenCV_VERSION_MAJOR 2) |
SET (OpenCV_VERSION_MINOR 4) |
SET (OpenCV_VERSION_PATCH 8) |
SET (OpenCV_VERSION_TWEAK 0) |
# ==================================================================== |
# Link libraries: e.g. opencv_core;opencv_imgproc; etc. |
# ==================================================================== |
SET (OpenCV_LIB_COMPONENTS opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib3d) |
# ============================================================== |
# Extra include directories, needed by OpenCV 2 new structure |
# ============================================================== |
SET (OpenCV2_INCLUDE_DIRS «» ) |
if (OpenCV2_INCLUDE_DIRS) |
include_directories ( $ |
list ( APPEND OpenCV_INCLUDE_DIRS $ |
set (OpenCV_ADD_DEBUG_RELEASE FALSE ) |
if (OpenCV_ADD_DEBUG_RELEASE) |
set (OpenCV_LIB_DIR_OPT » $ |
set (OpenCV_LIB_DIR_DBG » $ |
set (OpenCV_3RDPARTY_LIB_DIR_OPT » $ |
set (OpenCV_3RDPARTY_LIB_DIR_DBG » $ |
endif () |
endif () |
# ============================================================== |
# Check OpenCV availability |
# ============================================================== |
if (ANDROID AND OpenCV_ANDROID_NATIVE_API_LEVEL GREATER ANDROID_NATIVE_API_LEVEL) |
message ( FATAL_ERROR «Minimum required by OpenCV API level is android- $ |
# always FATAL_ERROR because we can’t say to the caller that OpenCV is not found |
# http://www.mail-archive.com/cmake@cmake.org/msg37831.html |
if (OpenCV_FIND_REQUIRED) |
message ( FATAL_ERROR «Minimum required by OpenCV API level is android- $ |
elseif ( NOT OpenCV_FIND_QUIETLY) |
message (WARNING «Minimum required by OpenCV API level is android- $ |
endif () |
set (OpenCV_FOUND » OpenCV_FOUND-NOTFOUND » ) |
return () # Android toolchain requires CMake > 2.6 |
endif () |
# ============================================================== |
# Form list of modules (components) to find |
# ============================================================== |
if ( NOT OpenCV_FIND_COMPONENTS) |
set (OpenCV_FIND_COMPONENTS $ |
list ( REMOVE_ITEM OpenCV_FIND_COMPONENTS opencv_java) |
if (GTest_FOUND OR GTEST_FOUND) |
list ( REMOVE_ITEM OpenCV_FIND_COMPONENTS opencv_ts) |
endif () |
endif () |
# expand short module names and see if requested components exist |
set (OpenCV_FIND_COMPONENTS_ «» ) |
foreach (__cvcomponent $ |
if ( NOT __cvcomponent MATCHES «^opencv_» ) |
set (__cvcomponent opencv_ $ <__cvcomponent>) |
endif () |
list ( FIND OpenCV_LIB_COMPONENTS $ <__cvcomponent>__cvcomponentIdx) |
if (__cvcomponentIdx LESS 0) |
# requested component is not found. |
if (OpenCV_FIND_REQUIRED) |
message ( FATAL_ERROR » $ <__cvcomponent>is required but was not found» ) |
elseif ( NOT OpenCV_FIND_QUIETLY) |
message (WARNING » $ <__cvcomponent>is required but was not found» ) |
endif () |
# indicate that module is NOT found |
string ( TOUPPER » $ <__cvcomponent>» __cvcomponent) |
set ( $ <__cvcomponent>_FOUND » $<__cvcomponent>_FOUND-NOTFOUND » ) |
else () |
list ( APPEND OpenCV_FIND_COMPONENTS_ $ <__cvcomponent>) |
# Not using list(APPEND) here, because OpenCV_LIBS may not exist yet. |
# Also not clearing OpenCV_LIBS anywhere, so that multiple calls |
# to find_package(OpenCV) with different component lists add up. |
set (OpenCV_LIBS $ |
# indicate that module is found |
string ( TOUPPER » $ <__cvcomponent>» __cvcomponent) |
set ( $ <__cvcomponent>_FOUND 1) |
endif () |
endforeach () |
set (OpenCV_FIND_COMPONENTS $ |
# ============================================================== |
# Resolve dependencies |
# ============================================================== |
if (OpenCV_USE_MANGLED_PATHS) |
set (OpenCV_LIB_SUFFIX «. $ |
else () |
set (OpenCV_LIB_SUFFIX «» ) |
endif () |
foreach (__opttype OPT DBG) |
SET (OpenCV_LIBS_ $ <__opttype>» $ |
SET (OpenCV_EXTRA_LIBS_ $ <__opttype>«» ) |
# CUDA |
if (OpenCV_CUDA_VERSION AND (CMAKE_CROSSCOMPILING OR ( WIN32 AND NOT OpenCV_SHARED))) |
if ( NOT CUDA_FOUND) |
find_package (CUDA $ |
else () |
if ( NOT CUDA_VERSION_STRING VERSION_EQUAL OpenCV_CUDA_VERSION) |
message ( FATAL_ERROR «OpenCV static library was compiled with CUDA $ |
endif () |
endif () |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
if ( $ |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
else () |
find_cuda_helper_libs(nppc) |
find_cuda_helper_libs(nppi) |
find_cuda_helper_libs(npps) |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
endif () |
if (OpenCV_USE_CUBLAS) |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
endif () |
if (OpenCV_USE_CUFFT) |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
endif () |
if (OpenCV_USE_NVCUVID) |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
endif () |
if ( WIN32 ) |
list ( APPEND OpenCV_EXTRA_LIBS_ $ <__opttype>$ |
endif () |
endif () |
endforeach () |
# ============================================================== |
# Android camera helper macro |
# ============================================================== |
if (OpenCV_HAVE_ANDROID_CAMERA) |
macro (COPY_NATIVE_CAMERA_LIBS target) |
get_target_property (target_location $ |
get_filename_component (target_location » $ |
file ( GLOB camera_wrappers » $ |
foreach (wrapper $ |
add_custom_command ( |
TARGET $ |
POST_BUILD |
COMMAND $ |
) |
endforeach () |
endmacro () |
endif () |
# ============================================================== |
# Compatibility stuff |
# ============================================================== |
if (CMAKE_BUILD_TYPE MATCHES «Debug» ) |
SET (OpenCV_LIB_DIR $ |
else () |
SET (OpenCV_LIB_DIR $ |
endif () |
set (OpenCV_LIBRARIES $ |
if (CMAKE_CROSSCOMPILING AND OpenCV_SHARED AND (CMAKE_SYSTEM_NAME MATCHES «Linux» )) |
foreach (dir $ |
set (CMAKE_EXE_LINKER_FLAGS » $ |
set (CMAKE_SHARED_LINKER_FLAGS » $ |
set (CMAKE_MODULE_LINKER_FLAGS » $ |
endforeach () |
endif () |
# |
# Some macroses for samples |
# |
macro (ocv_check_dependencies) |
set (OCV_DEPENDENCIES_FOUND TRUE) |
foreach (d $ |
if ( NOT TARGET $ |
set (OCV_DEPENDENCIES_FOUND FALSE ) |
break () |
endif () |
endforeach () |
endmacro () |
# adds include directories in such way that directories from the OpenCV source tree go first |
function (ocv_include_directories) |
set (__add_before «» ) |
file ( TO_CMAKE_PATH » $ |
foreach (dir $ |
get_filename_component (__abs_dir » $ |
if ( » $ <__abs_dir>» MATCHES «^ $ <__basedir>» ) |
list ( APPEND __add_before » $ |
else () |
include_directories ( AFTER SYSTEM » $ |
endif () |
endforeach () |
include_directories ( BEFORE $ <__add_before>) |
endfunction () |
macro (ocv_include_modules) |
include_directories ( BEFORE » $ |
endmacro () |
# remove all matching elements from the list |
macro (ocv_list_filterout lst regex) |
foreach (item $ < $ |
if (item MATCHES » $ |
list ( REMOVE_ITEM $ |
endif () |
endforeach () |
endmacro () |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.