win7+opencv+V2015环境搭建

win7+opencv+V2015环境搭建

使用官方的opencv环境(3.3.0,win版本)挺长一段时间之后发现了问题,一是直接安装官方exe的opencv其实是opencv官网编译好的库,在使用时遇到了问题(运行出错,调试崩溃),然后我选择重装;二是下载源代码自己编译之后发现缺少一些库(3.0版本之后人脸识别的库就被放到了opencv_contrib仓库,而不是master中),我又重装了一遍。而且官方给的脚本也有问题,编译花了很多时间,写这篇自己和他人一个提醒。

搭建环境:win7+VS2015,opencv3.3.0

下载cmake

可以在cmake官网(https://cmake.org/download/)下载安装。

下载opencv

opencv模块简介

在github上opencv主要分为2个仓库(3.3.0为例):

  • opencv:包含了opencv的基础操作,功能较为稳定的模块。具体包括:

    • core module:opencv的基础构建模块,如Mat等;

    • imgproc module:图像处理模块

    • highgi module:opencv内置图形用户界面的模块

    • imgcodecs module:图像的输入输出

    • videoio module:视频的输入输出

    • calib3d module:摄像机校准和三维重建

    • feature2d module:opencv内置2D特征模块

    • video module:视频分析模块

    • objdetect module:对象检测模块

    • dnn module:深度学习网络模块

    • ml module:机器学习模块

    • photo module:计算机摄影模块

    • stitching module:图像拼接模块

    • cuda module:GPU加速模块

  • opencv_contrib:包含了opencv的其他一些模块(如人脸识别等),具体可以参考:https://github.com/opencv/opencv_contrib/tree/master/modules

请在https://github.com/opencv根据需要下载最新版的opencv(本文下载的是3.3.0)。opencv仓库中是opencv官方给出的稳定和较为基础的模块源代码,我会用到人脸识别,因此我还会下载opencv_contrib仓库下的源代码。如果有安装git,也可以用shell脚本的git命令下载:

#!/bin/bash -e

cd OPENCV_SORCE_DIR

mkdir opencv

mkdir opencv_contrib

cd opencv

git clone https://github.com/opencv/opencv.git

cd ../opencv_contrib

git git clone https://github.com/opencv/opencv_contrib.git

使用cmake编译

(1)打开cmake-gui.exe

(2)在cmake-gui中选择需要编译的opencv的源码目录(OPENCV_SORCE_DIR)和编译后存放目标的目录(OPENCV_TARGET_DIR),点击“Configure”。这里源码目录是指opencv仓库存放的源码目录(OPENCV_SORCE_DIR/opencv),该目录如下:

(3)之后需要选择cmake编译的VS版本,根据自己电脑的配置进行选择,这里选择“Visual Studio 14 2015 Win64”

(4)在第一次config完之后,会出现

(5)之后要根据自己的需求进行配置。记得将BUILD_opencv_world选上,默认情况似乎是不选的,如果不安装opencv_contrib 中的模块就可以直接点击“Configure”了。这里由于需要安装 opencv_contrib 中的模块,因此要将OPENCV_EXTRA_MODULES_PATH设置为opencv_contrib所在的目录下的modules目录(OPENCV_SORCE_DIR/opencv_contrib/modules),注意分隔符是/,之后再点击“Configure”

(6)两次生成config完之后,点击“Generate”,cmake-gui的下方出现“Generate done”,cmake的编译就结束了,cmake生成的是VS的工程,之后是用VS来编译opencv。

用VS编译工程

(1)在cmake编译的目标目录(OPENCV_TARGET_DIR),用VS打开OpenCV.sln工程文件:

(2)在VS中的Debug和Release条件下分别依次编译CMakeTargets下的“ALL_BUILD”和“INSTALL”:右键点击,选择“生成”:

(3)等待编译结束,需要花挺长时间的。

配置opencv环境

(1)在环境变量path中添加opencv可执行文件的目录(OPENCV_TARGET_DIR\install\x64\vc14\bin),这里的分隔符是\

(2)在VS中任意打开工程,在属性管理器中点击“Microsoft.Cpp.Win32.user”属性页,在“VC++目录”->“包含目录”下添加:

OPENCV_TARGET_DIR\install\include

OPENCV_TARGET_DIR\install\include\opencv

OPENCV_TARGET_DIR\install\include\opencv2

(3)在“VC++目录”->“库目录”下添加:

OPENCV_TARGET_DIR\install\x64\vc14\lib

(4)在“链接器”->“输入”->“附加依赖项”中添加OPENCV_TARGET_DIR\install\x64\vc14\lib目录下相应库文件的文件名,注意Debug和Release配置添加时要分别添加,Debug下添加以d结尾的lib(*d.lib),Release下添加不带d的lib(*.lib),以下是本人编译添加的,Debug:

opencv_aruco330d.lib
opencv_bgsegm330d.lib
opencv_bioinspired330d.lib
opencv_calib3d330d.lib
opencv_ccalib330d.lib
opencv_core330d.lib
opencv_datasets330d.lib
opencv_dnn330d.lib
opencv_dpm330d.lib
opencv_face330d.lib
opencv_features2d330d.lib
opencv_flann330d.lib
opencv_fuzzy330d.lib
opencv_highgui330d.lib
opencv_img_hash330d.lib
opencv_imgcodecs330d.lib
opencv_imgproc330d.lib
opencv_line_descriptor330d.lib
opencv_ml330d.lib
opencv_objdetect330d.lib
opencv_optflow330d.lib
opencv_phase_unwrapping330d.lib
opencv_photo330d.lib
opencv_plot330d.lib
opencv_reg330d.lib
opencv_rgbd330d.lib
opencv_saliency330d.lib
opencv_shape330d.lib
opencv_stereo330d.lib
opencv_stitching330d.lib
opencv_structured_light330d.lib
opencv_superres330d.lib
opencv_surface_matching330d.lib
opencv_text330d.lib
opencv_tracking330d.lib
opencv_video330d.lib
opencv_videoio330d.lib
opencv_videostab330d.lib
opencv_world330d.lib
opencv_xfeatures2d330d.lib
opencv_ximgproc330d.lib
opencv_xobjdetect330d.lib
opencv_xphoto330d.lib

Release:

opencv_aruco330.lib
opencv_bgsegm330.lib
opencv_bioinspired330.lib
opencv_calib3d330.lib
opencv_ccalib330.lib
opencv_core330.lib
opencv_datasets330.lib
opencv_dnn330.lib
opencv_dpm330.lib
opencv_face330.lib
opencv_features2d330.lib
opencv_flann330.lib
opencv_fuzzy330.lib
opencv_highgui330.lib
opencv_img_hash330.lib
opencv_imgcodecs330.lib
opencv_imgproc330.lib
opencv_line_descriptor330.lib
opencv_ml330.lib
opencv_objdetect330.lib
opencv_optflow330.lib
opencv_phase_unwrapping330.lib
opencv_photo330.lib
opencv_plot330.lib
opencv_reg330.lib
opencv_rgbd330.lib
opencv_saliency330.lib
opencv_shape330.lib
opencv_stereo330.lib
opencv_stitching330.lib
opencv_structured_light330.lib
opencv_superres330.lib
opencv_surface_matching330.lib
opencv_text330.lib
opencv_tracking330.lib
opencv_video330.lib
opencv_videoio330.lib
opencv_videostab330.lib
opencv_world330.lib
opencv_xfeatures2d330.lib
opencv_ximgproc330.lib
opencv_xobjdetect330.lib
opencv_xphoto330.lib

结束

配置完成之后可以用简单的显示图片来测试配置是否完成:

祝你好运!

你可能感兴趣的:(opencv)