下载安装
在这里下载。我下载了2.4.9的Windows版本。双击安装即可。
配置环境变量
配置环境变量的目的是为了让系统找到OpenCV的动态链接库。因此需要把动态链接库添加到系统环境变量PATH中。
在Windows下使用VS,为了方便VS中使用属性表。分两步来添加。
在系统环境变量添加OPENCV,其值为"安装目录\opencv\build";之后在PATH中添加"%OPENCV%\x86\vc12\bin"。这里选择x86还是x64,不是根据系统的处理器,而是根据VS中的配置。例如我是Win7 64位系统,但是我的VS配置为:
所以我选择了x86。
我用的是VS2013,选择vc12。
配置属性表
新建文件"OpenCV249.props",内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>$(OPENCV)\include;$(IncludePath)</IncludePath>
<LibraryPath Condition="'$(Platform)'=='Win32'">$(OPENCV)\x86\vc12\lib;$(LibraryPath)</LibraryPath>
<LibraryPath Condition="'$(Platform)'=='X64'">$(OPENCV)\x64\vc12\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_calib3d249d.lib;opencv_contrib249d.lib;opencv_core249d.lib;opencv_features2d249d.lib;opencv_flann249d.lib;opencv_gpu249d.lib;opencv_highgui249d.lib;opencv_imgproc249d.lib;opencv_legacy249d.lib;opencv_ml249d.lib;opencv_nonfree249d.lib;opencv_objdetect249d.lib;opencv_ocl249d.lib;opencv_photo249d.lib;opencv_stitching249d.lib;opencv_superres249d.lib;opencv_ts249d.lib;opencv_video249d.lib;opencv_videostab249d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_nonfree249.lib;opencv_objdetect249.lib;opencv_ocl249.lib;opencv_photo249.lib;opencv_stitching249.lib;opencv_superres249.lib;opencv_ts249.lib;opencv_video249.lib;opencv_videostab249.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
在VS的"Property Manager"中点右击项目,选择"Add Existing Property Sheet",添加即可。之后可以像使用系统库一样,使用OpenCV。
安装Python OpenCV包
OpenCV的Python包依赖Numpy,需要先去这里下载whl安装包安装。
之后把opencv\build\python\2.7\x64\cv2.pyd拷贝到Python27\Lib\site-packages目录下。这里的x64对应Python安装版本。
测试安装成功:到opencv\sources\samples\python目录下,运行drawing.py脚本。
测试
注意,Windows下路径间隔用"/",而不是"\"。
C++测试
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("");
if (img.empty())
{
cout << "error";
return -1;
}
imshow("图像", img);
waitKey();
return 0;
}
Python测试
import cv2
img = cv2.imread(r"图像", cv2.IMREAD_COLOR)
cv2.namedWindow("Image")
cv2.imshow("Image", img)
cv2.waitKey(0)
OpenCV控件
opencv安装后,在include目录下可以看到opencv和opencv2两个文件夹,其中opencv是旧版本的头文件,新版本的头文件都在opencv2文件中。在opencv2目录下,不同的目录中,包含了不同功能的头文件;opencv.hpp
包含了所有的的头文件,opencv_modules.hpp
使用宏定义,定义了不同的模块。
其中core包含了核心模块, improc包含了图像处理模块, highgui包含了高层次的GUI和媒体I/O, video是图像处理模块,calib3d是相机定标和3d重建模块, features2是二维图像特征框架, objdetect是物体检测, ml是机器学习模块, flann是多维空间聚类和搜索模块, gpu是基于gpu加速的计算机视觉模块, stitching是图像拼接模块。