VS2010下编译OpenCV2.4.6静态库

1、   从 http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/ 下载最新的OpenCV2.4.6;

2、   将OpenCV-2.4.6.0.exe存放到D:\Soft\OpenCV2.4.6文件夹下,解压到当前文件夹下,生成一个opencv文件夹;

3、   下载安装CMake:http://www.cmake.org/cmake/resources/software.html;

4、   运行CMake(cmake-gui),在Where is the source code中填入刚才步骤(2)解压缩后的文档路径D:/Soft/OpenCV2.4.6/opencv,在Where tobuild the binaries中填入编译时需要存放的路径如D:/Soft/OpenCV2.4.6/vs2010,此vs2010文件夹需自己创建;

5、   点击Configure,在弹出的对话框中选择Visual Studio 10,其它默认,点击Finish;

6、   找到BUILD_SHARED_LIBS选项,去掉勾选,为了减少编译时间,也可去掉一些其它不必要的勾选,点击Generate;

7、   上述步骤完成后,在vs2010文件夹中,就会看到有OpenCV.sln文件;

8、   使用vs2010打开OpenCV.sln文件,分别在Debug和Release下,选择Solution Explorer里的Solution OpenCV,点击右键,运行”Rebuild Solution”,点击CMake Targets下的INSTALL,运行”build”;会在D:/Soft/OpenCV2.4.6/vs2010/install文件夹下的lib文件夹中生成相应的静态库;

9、   打开vs2010,新建一个控制台应用程序TestOpenCV,选中工程-->Properties:(1)、VC++ Directories-->Include Directories:分别在Debug和Release模式下加入, D:/Soft/OpenCV2.4.6/vs2010/install/includeD:/Soft/OpenCV2.4.6/vs2010/install/include/opencv D:/Soft/OpenCV2.4.6/vs2010/install/include/opencv2  (2)、C/C++-->CodeGeneration-->Runtime Library:在Debug下,将其改为Multi-threaded Debug(/MTD),在Release下,将其改为Multi-threaded(/MT)。

stdafx.h文件代码为:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\include\\opencv2\\core\\core.hpp"
#include "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\include\\opencv2\\highgui\\highgui.hpp"
#include "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\include\\opencv2\\imgproc\\imgproc.hpp"

using namespace cv;

stdafx.cpp文件代码为:

#include "stdafx.h"

#ifdef _DEBUG
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\zlibd.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libtiffd.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libpngd.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libjpegd.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libjasperd.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\IlmImfd.lib")

#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_core246d.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_highgui246d.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_imgproc246d.lib")

#pragma comment(lib, "comctl32.lib")
#else
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\zlib.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libtiff.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libpng.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libjpeg.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\libjasper.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\share\\OpenCV\\3rdparty\\lib\\IlmImf.lib")

#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_core246.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_highgui246.lib")
#pragma comment(lib, "D:\\Soft\\OpenCV2.4.6\\vs2010\\install\\lib\\opencv_imgproc246.lib")

#pragma comment(lib, "comctl32.lib")
#endif

TestOpenCV.cpp文件代码为:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Mat image1, image2, imageResult;

	image1 = cv::imread("1.jpg", 2 | 4);
	image2 = cv::imread("2.jpg", 2 | 4);

	cv::subtract(image1, image2, imageResult);

	cv::namedWindow("show image");
	cv::imshow("show image", imageResult);
	cv::waitKey(0);
	cv::destroyWindow("show image");

	return 0;
}


你可能感兴趣的:(VS2010下编译OpenCV2.4.6静态库)