重装了系统,需要重新安装和配置OpenCV,在官网http://opencv.org/发现了最新的版本,OpenCV2.4.9,无需进行CMake编译,直接配置就可使用,于是就打算安装最新的。
安装和配置方法挺简单的。
首先,下载“opencv-2.4.9.exe”,为windows版本,运行解压至某目录文件夹,比如:"F:\",以下均按这一设置讲述,于是F盘下就有一文件夹"opencv";
其次,配置环境变量,右键“计算机”->“属性”->"高级系统设置"->"环境变量",找到系统变量名"Path",“编辑”,在后面添加目录";F:\opencv\build\x86\vc10\bin;",注意每个目录之间需要用";"作间隔;
再次,打开VS2010,新建一Win32控制台应用程序空工程,新建一.cpp文件,输入以下代码:
#include <opencv2\opencv.hpp> #include <iostream> #include <string> using namespace cv; using namespace std; int main() { Mat img = imread("1.jpg"); if(img.empty()) { cout<<"error"; return -1; } imshow("显示示例图",img); waitKey(); return 0; }
代码头文件是调用了opencv的文件,因此如果没有配置完整是无法运行通过的。下面接着进行VS工程配置:
点击“项目”->"属性"->"VS++目录",修改包含文件,添加三个目录:F:\opencv\build\include;F:\opencv\build\include\opencv;F:\opencv\build\include\opencv2;
修改库文件,添加一个目录:F:\opencv\build\x86\vc10\lib;
接着点击“链接器”->“输入”,在配置“Debug”下,添加“附加依赖项”:
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_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
,在配置“Release”下,添加"附加依赖项":
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_objdetect249.lib
opencv_ts249.lib
opencv_video249.lib
配置完毕,试运行,通过则正确,否则再回头检查看看哪一步发生了问题。