编译源码 - 【OpenCV 3.0 + TBB + IPP + OpenMP + CUDA】- 【win7x64 + vs2012】

1、CMake

先安装好TBB和CUDA,到官网下载,配置系统环境PATH(添加bin目录);CUDA安装教程

第一次configure时会自动下载ippicv,这里需要耗费一点时间。因为3.0默认进行了ipp的优化。下载后的文件存放于“sources\3rdparty\ippicv”下

勾选“WITH_TBB”、“WITH_IPP”、“WITH_CUDA”、“WITH_OPENMP”,cuda下面还有一个“WITH_FAST_MATH”,是cuda的快速数学库,牺牲精度谋求速度,如果只要求单精度浮点可以选择该项

勾选”BUILD_opencv_world“,则最后生成的库将会合并为两项”opencv_world300.dll“或”opencv_world300d.dll“,以及”opencv_ffmpeg300.dll“,这样做的好处是只需要引用两个库,方便快捷,坏处是很多可能用不着的库,其实没必要引用。

一路configure,遇到TBB not found就设置相应的路径,最后generate

2、打开OpenCV.sln,选择debug或release,build其中的INSTALL项目,等待……

3、build完成后,生成的lib、dll放在install文件夹内,在项目中引用这些库就可以。需要注意的是,如果想debug调试时进入源码,需要在系统环境变量PATH添加OpenCV.sln所在目录的bin\Debug路径,里面含有跟踪源码所需的pdb文件。


调用opencv库的小技巧:


*通过 .props文件 配置项目属性,避免每次新建项目后手动添加路径

找到Microsoft.Cpp.Win32.user.props文件,可以通过explore搜索"Microsoft.Cpp.Win32.user.props",也可以通过”属性管理器-Debug | win32“的添加现有属性表自动定位到其目录

记事本打开Microsoft.Cpp.Win32.user.props,全部复制

新建一个.props文件,如v11_x86.props,粘贴刚刚复制的内容,然后把  <PropertyGroup>一栏修改为:

  <PropertyGroup>
    <!--执行路径,即bin文件夹-->
    <ExecutablePath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\bin;D:\Program Files\tbb44_20150728oss\bin\ia32\vc11;$(ExecutablePath)</ExecutablePath>
    <!--包含路径-->
    <IncludePath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\include;D:\Program Files\tbb44_20150728oss\include;$(IncludePath)</IncludePath>
    <ReferencePath>$(ReferencePath)</ReferencePath>
    <!--库路径-->
    <LibraryPath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\lib;D:\Program Files\tbb44_20150728oss\lib\ia32\vc11;$(LibraryPath)</LibraryPath>
    <SourcePath>$(SourcePath)</SourcePath>
    <ExcludePath>$(ExcludePath)</ExcludePath>
  </PropertyGroup>
这里设置了三种路径,每台电脑是不一样的
最后在VS中,”属性管理器“-”添加现有属性表“,找到v11_x86.props并添加

通过这样做,只需要写一次props,然后每个新项目都添加这个props,这样就不用手动添加路径了。

注意,新建props必须通过VS中的”属性管理器“-”添加新项目属性表“,不能通过新建txt改后缀的方法,会造成Unicode的问题


*引用库(附加依赖项)

在lib目录下新建一个 .bat,内容如下:

dir *300d.lib /ON /B > lib_debug.txt
这可以把lib的文件名输出到一个txt里面,release的如下:

dir *300.lib /ON /B > lib_release.txt

然后打开txt,通过“替换”把文件名修改为

#pragma comment( lib, "opencv_XXXX.lib")
的形式,XXXX为保留的内容,如:

将 

opencv_calib3d300.lib

 替换为 

#pragma comment( lib, "opencv_calib3d300.lib")

然后在头文件中引用需要的lib,只需要直接拷贝上面修改得到的代码

这里直接贴上opencv 3.0的库引用代码:

(自己编译的包括cuda、ipp、tbb、openmp的库)

debug

#pragma comment( lib, "opencv_calib3d300d.lib")
#pragma comment( lib, "opencv_core300d.lib")
#pragma comment( lib, "opencv_cudaarithm300d.lib")
#pragma comment( lib, "opencv_cudabgsegm300d.lib")
#pragma comment( lib, "opencv_cudacodec300d.lib")
#pragma comment( lib, "opencv_cudafeatures2d300d.lib")
#pragma comment( lib, "opencv_cudafilters300d.lib")
#pragma comment( lib, "opencv_cudaimgproc300d.lib")
#pragma comment( lib, "opencv_cudalegacy300d.lib")
#pragma comment( lib, "opencv_cudaobjdetect300d.lib")
#pragma comment( lib, "opencv_cudaoptflow300d.lib")
#pragma comment( lib, "opencv_cudastereo300d.lib")
#pragma comment( lib, "opencv_cudawarping300d.lib")
#pragma comment( lib, "opencv_cudev300d.lib")
#pragma comment( lib, "opencv_features2d300d.lib")
#pragma comment( lib, "opencv_flann300d.lib")
#pragma comment( lib, "opencv_hal300d.lib")
#pragma comment( lib, "opencv_highgui300d.lib")
#pragma comment( lib, "opencv_imgcodecs300d.lib")
#pragma comment( lib, "opencv_imgproc300d.lib")
#pragma comment( lib, "opencv_ml300d.lib")
#pragma comment( lib, "opencv_objdetect300d.lib")
#pragma comment( lib, "opencv_photo300d.lib")
#pragma comment( lib, "opencv_shape300d.lib")
#pragma comment( lib, "opencv_stitching300d.lib")
#pragma comment( lib, "opencv_superres300d.lib")
#pragma comment( lib, "opencv_ts300d.lib")
#pragma comment( lib, "opencv_video300d.lib")
#pragma comment( lib, "opencv_videoio300d.lib")
#pragma comment( lib, "opencv_videostab300d.lib")
release
#pragma comment( lib, "opencv_calib3d300.lib")
#pragma comment( lib, "opencv_core300.lib")
#pragma comment( lib, "opencv_cudaarithm300.lib")
#pragma comment( lib, "opencv_cudabgsegm300.lib")
#pragma comment( lib, "opencv_cudacodec300.lib")
#pragma comment( lib, "opencv_cudafeatures2d300.lib")
#pragma comment( lib, "opencv_cudafilters300.lib")
#pragma comment( lib, "opencv_cudaimgproc300.lib")
#pragma comment( lib, "opencv_cudalegacy300.lib")
#pragma comment( lib, "opencv_cudaobjdetect300.lib")
#pragma comment( lib, "opencv_cudaoptflow300.lib")
#pragma comment( lib, "opencv_cudastereo300.lib")
#pragma comment( lib, "opencv_cudawarping300.lib")
#pragma comment( lib, "opencv_cudev300.lib")
#pragma comment( lib, "opencv_features2d300.lib")
#pragma comment( lib, "opencv_flann300.lib")
#pragma comment( lib, "opencv_hal300.lib")
#pragma comment( lib, "opencv_highgui300.lib")
#pragma comment( lib, "opencv_imgcodecs300.lib")
#pragma comment( lib, "opencv_imgproc300.lib")
#pragma comment( lib, "opencv_ml300.lib")
#pragma comment( lib, "opencv_objdetect300.lib")
#pragma comment( lib, "opencv_photo300.lib")
#pragma comment( lib, "opencv_shape300.lib")
#pragma comment( lib, "opencv_stitching300.lib")
#pragma comment( lib, "opencv_superres300.lib")
#pragma comment( lib, "opencv_ts300.lib")
#pragma comment( lib, "opencv_video300.lib")
#pragma comment( lib, "opencv_videoio300.lib")
#pragma comment( lib, "opencv_videostab300.lib")

----END-----

你可能感兴趣的:(opencv)