24 June 2011

Building OpenCV 2.2.0 from source on windows using Visual C++ Express 2010

Why would you want to do this? I wanted support for Intel TBB which adds threading support to OpenCV and is not included in the standard build. Also I had problems with my web cam with the standard build and so also had to patch the code.

  • Download and install Intel TBB, in my case I installed it to: "C:/Development/Intel/TBB/Include/"
  • Download OpenCV 2.2 source code.
  • Apply the source code changes listed after this section. (Note these are not optimal, and some could be better specified with command link options, but I had trouble specifying a few command line options and this code was the first working version I came up with).
  • Open a command prompt with the Visual Studio paths and variables defined. It will be called something like "Visual Studio Command Prompt (2010)"
  • Navigate to the root OpenCV directory “OpenCV-2.2.0”, create a build directory and enter it.
    # mkdir Build
    # cd Build
  • On Windows the source code can be configured using the following command line:
    # cmake -D CMAKE_BUILD_TYPE=RELEASE ..\
    or
    # cmake -D CMAKE_BUILD_TYPE=DEBUG ..\
    Note: The way you need to specify command line arguments varys between versions of windows, see here for more details: http://opencv.willowgarage.com/wiki/InstallGuide#Build
  • Then build:
    # nmake
  • Link to the libraries from within your code, linking the *d.lib versions with the debug code and *.lib versions with release code.
  • Copy the *.dlls for both OpenCV and IntelTBB into the applications output directory.


OpenCV-2.2.0\modules\highgui\src\precomp.hpp
+++ precomp.hpp
@@ -45,8 +45,10 @@
#if _MSC_VER >= 1200
#pragma warning( disable: 4251 )
#endif
+#include "cvconfig.h"
+

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/internal.hpp"
@@ -58,9 +60,8 @@
#include
#include
#if !defined WIN32 && !defined _WIN32
-#include "cvconfig.h"
#else
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int ...
#endif


First code patch from:

  • https://code.ros.org/trac/opencv/attachment/ticket/735/patch_highgui_Camera.patch
  • https://code.ros.org/trac/opencv/attachment/ticket/735/no_video_from_camera.patch


OpenCV-2.2.0\CMakeLists.txt
+++ CMakeLists.txt
19 | P a g e
@@ -298,9 +298,9 @@ if(APPLE)
set(WITH_CARBON OFF CACHE BOOL "Use Carbon for UI instead of Cocoa")
set(WITH_QUICKTIME OFF CACHE BOOL "Use QuickTime for Video I/O ...
endif()
-set(WITH_TBB OFF CACHE BOOL "Include TBB support")
+set(WITH_TBB ON CACHE BOOL "Include TBB support")

set(WITH_EIGEN2 ON CACHE BOOL "Include Eigen2/Eigen3 support")
set(WITH_CUDA OFF CACHE BOOL "Include NVidia Cuda Runtime support")
if(WIN32)
@@ -604,9 +604,9 @@ if (WITH_TBB)
"C:/Program Files (x86)/Intel/TBB")
endif()
if (NOT HAVE_TBB)
find_path(TBB_INCLUDE_DIR "tbb/tbb.h"
- PATHS TBB_DEFAULT_INCLUDE_DIRS
+ PATHS "C:/Development/Intel/TBB/Include/"

DOC "The path to TBB headers")
if (TBB_INCLUDE_DIR)
if (UNIX)
set(TBB_LIB_DIR "${TBB_INCLUDE_DIR}/../lib" ...
@@ -647,8 +647,12 @@ if (WITH_TBB)
if (MSVC80)
set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc8")
elseif(MSVC90)
set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc9")
+ elseif(MSVC100)
+ set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc10")
+ else()
+ set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc10")

endif()
set(TBB_LIB_DIR "${_TBB_LIB_PATH}" CACHE PATH ...
link_directories("${TBB_LIB_DIR}")
endif()




Note I also found the following option worked for those who don’t link using nmake, but I haven’t tested it beyond configuring the build process:
# cmake -D CMAKE_BUILD_TYPE=RELEASE -G"Visual Studio 10" ../


For thoese who haven't configured a project to use OpenCV before here is a brief overview, a better explanation can be found at: http://opencv.willowgarage.com/wiki/VisualC%2B%2B

Project -> Project Name Properties

LibraryPath (Both debug and release):
C:\OpenCV2.2\Build\lib

IncludePath (Both debug and release):
C:\OpenCV2.2\include


AdditionalDependencies (Debug):
opencv_calib3d220d.lib
opencv_contrib220d.lib
opencv_core220d.lib
opencv_features2d220d.lib
opencv_ffmpeg220d.lib
opencv_flann220d.lib
opencv_gpu220d.lib
opencv_haartraining_engine.lib
opencv_highgui220d.lib
opencv_imgproc220d.lib
opencv_legacy220d.lib
opencv_ml220d.lib
opencv_objdetect220d.lib
opencv_video220d.lib


Additional Dependencies (Release):
opencv_calib3d220.lib
opencv_contrib220.lib
opencv_core220.lib
opencv_features2d220.lib
opencv_ffmpeg220.lib
opencv_flann220.lib
opencv_gpu220.lib
opencv_haartraining_engine.lib
opencv_highgui220.lib
opencv_imgproc220.lib
opencv_legacy220.lib
opencv_ml220.lib
opencv_objdetect220.lib
opencv_video220.lib


I've just noticed that OpenCV 2.3 is out so I might be doing this for a new version soon (I'll let you know if I do and there is any real variation): http://opencv.willowgarage.com/wiki/OpenCV%20Change%20Logs

No comments:

Post a Comment