VS2010平台下OpenCV 248编译和安装

来源:互联网 发布:淘宝联盟手机无法登录 编辑:程序博客网 时间:2024/04/29 14:30

这次安装OpenCV算是对windows下的系统路径设置有了更深的了解,还用CMake编译了OpenCV源码。OpenCV官方的系统路径设置方法:

Set the OpenCV enviroment variable and add it to the systems path
First we set an enviroment variable to make easier our work. This will hold the build directory of our OpenCV library
that we use in our projects. Start up a command window and enter:
setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc10 (suggested for Visual Studio 2010 - 32 bit Windows)
setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc10 (suggested for Visual Studio 2010 - 64 bit Windows)
setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc11 (suggested for Visual Studio 2012 - 32 bit Windows)
setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11 (suggested for Visual Studio 2012 - 64 bit Windows)
Here the directory is where you have your OpenCV binaries (extracted or built). You can have different platform (e.g.
x64 instead of x86) or compiler type, so substitute appropriate value. Inside this you should have two folders called
lib and bin. The -m should be added if you wish to make the settings computer wise, instead of user wise.


If you built static libraries then you are done. Otherwise, you need to add the bin folders path to the systems path. This
is cause you will use the OpenCV library in form of “Dynamic-link libraries” (also known as DLL). Inside these are
stored all the algorithms and information the OpenCV library contains. The operating system will load them only on
demand, during runtime. However, to do this he needs to know where they are. The systems PATH contains a list of
folders where DLLs can be found. Add the OpenCV library path to this and the OS will know where to look if he ever
needs the OpenCV binaries. Otherwise, you will need to copy the used DLLs right beside the applications executable
file (exe) for the OS to find it, which is highly unpleasent if you work on many projects. To do this start up again the
Path Editor and add the following new entry (right click in the application to bring up the menu):
%OPENCV_DIR%\bin

Save it to the registry and you are done. If you ever change the location of your build directories or want to try out your
applicaton with a different build all you will need to do is to update the OPENCV_DIR variable via the setx command
inside a command window.


这样比打开windows管理-》高级系统设置-》环境变量 再设置方便不到哪去,但是这样直接写到注册表,不用再重启电脑了是不是很爽啊。里面还推荐了一个软件,Path Editor,http://patheditor2.codeplex.com/ 


附加依赖项:

在VS2010中建立一个新项目fitting,在项目上右键属性,配置项目属性。

                 (1)Debug/Win32

                  配置属性-->VC++目录 需要编辑增加路径如下(先新建路径,再找到相应目录,点击应用)

                                      可执行文件目录:D:\Program Files\opencv-2.4.8\install\x86\vc10\bin

                                      包含目录:D:\Program Files\opencv-2.4.8\install\include;

                                                        D:\Program Files\opencv-2.4.8\install\include\opencv;

                                                        D:\Program Files\opencv-2.4.8\install\include;

                                      引用目录:D:\Program Files\opencv-2.4.8\install\x86\vc10\lib

                   配置属性-->链接器-->输入:(输入一项就添加回车做间隔)

                                       附加依赖项:

                                       opencv_calib3d248d.lib
                                       opencv_contrib248d.lib
                                       opencv_core248d.lib
                                       opencv_features2d248d.lib
                                       opencv_flann248d.lib
                                       opencv_gpu248d.lib
                                       opencv_highgui248d.lib
                                       opencv_imgproc248d.lib
                                       opencv_legacy248d.lib
                                       opencv_ml248d.lib
                                       opencv_nonfree248d.lib
                                       opencv_objdetect248d.lib
                                       opencv_photo248d.lib
                                       opencv_stitching248d.lib
                                       opencv_ts248d.lib
                                       opencv_video248d.lib
                                       opencv_videostab248d.lib

                (2)Release/Win32

                  配置属性-->VC++目录 需要编辑增加路径如下(先新建路径,再找到相应目录,点击应用)

                                      可执行文件目录:D:\Program Files\opencv-2.4.8\install\x86\vc10\bin

                                      包含目录:D:\Program Files\opencv-2.4.8\install\include;

                                                        D:\Program Files\opencv-2.4.8\install\include\opencv;

                                                        D:\Program Files\opencv-2.4.8\install\include;

                                      引用目录:D:\Program Files\opencv-2.4.8\install\x86\vc10\lib

          

          配置属性-->链接器-->输入:(输入一项就添加回车做间隔)

                                       附加依赖项:

                                opencv_calib3d248.lib
                                       opencv_contrib248.lib
                                       opencv_core248.lib
                                       opencv_features2d248.lib
                                       opencv_flann248.lib
                                       opencv_gpu248.lib
                                       opencv_highgui248.lib
                                       opencv_imgproc248.lib
                                       opencv_legacy248.lib
                                       opencv_ml248.lib
                                       opencv_nonfree248.lib
                                       opencv_objdetect248.lib
                                       opencv_photo248.lib
                                       opencv_stitching248.lib
                                       opencv_video248.lib
                                       opencv_video248.lib
                                       opencv_videostab248.lib



参考:

http://blog.csdn.net/wlq19910603/article/details/19824655

http://www.cnblogs.com/jhzhu/p/3216840.html



要点:
           1) XP+ Visual Stuio 2010 + OpenCV-2.4.2.exe + cmake-2.8.8-win32-x86.exe 
           2) 如何CMake OpenCV源代码得到可编译的VS2010项目
           3) 如何使用VS2010编译OpenCV源码
           4) 如何配置OpenCV工程

1. 下载OpenCV 安装包
     OpenCV-2.4.2.exe  (http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.2/)

2. 解压安装包
    双击OpenCV-2.4.2.exe 解压,比如解压安装到C:\openCV_extract
    【这里面是OpenCV自带的dll库,无法用来在VS中调试OpenCV,所以需要使用CMake重新编译】

3.  安装CMake
     cmake-2.8.8-win32-x86.exe (http://www.cmake.org/cmake/resources/software.html)

4. CMake OpenCV
    源代码选择OpenCV安装目录C:/openCV_extract/opencv
    Build目录可以设置为C:/openCV-extract/opencv_build
    点击Configure
    完成后,出现红色背景,
    勾选BUILD_SHARED_LIBS,BUILD_TESTS,OPENCV_BUILD_3DPARTY_LIBS,以及下面的WITH_JASPER,WITH_JPEG,WITH_PNG,WITH_TBB(一般这个没默认选上),和WITH_TIFF选项
    点击Generate
    

5.  编译CMake出来的工程
    (1)打开C:/openCV-extract/opencv_build目录下的VS2010工程文件OpenV.sln
    (2)分别使用Debug和Release模式,编译生成。
    (3)复制opencv_build目录下的bin和lib文件夹,到C:\openCV_extract\opencv目录下
              复制opencv\build\include 目录下的两个文件夹,到opencv\include 下,覆盖。
             
    这样得到的库文件,就可以被VS2010调用了。
      
6.  配置系统环境变量
    在PATH变量中添加:
    C:\openCV_extract\opencv\build\x86\vc10\bin;C:\ openCV_extract \opencv\bin\Debug\;C:\ openCV_extract \opencv\bin\Release\

7.  新建Win32工程,配置工程属性
     (1)VC++ Directories 下的Include Directories 
        (bin\debug可以不用加)
        
      (2)VC++ Directories 下的Library Directories 
             Debug模式为:lib\Debug
             Release模式为:lib\Release
         
     
      (3)添加库文件  Linker --> Input --> Additional Dependencies  (注意:lib文件名根据不同的opencv版本而定)
        Debug模式添加:
             opencv_calib3d242d.lib
opencv_contrib242d.lib
opencv_core242d.lib
opencv_features2d242d.lib
opencv_flann242d.lib
opencv_gpu242d.lib
opencv_haartraining_engined.lib
opencv_highgui242d.lib
opencv_imgproc242d.lib
opencv_legacy242d.lib
opencv_ml242d.lib
opencv_objdetect242d.lib
opencv_ts242d.lib
opencv_video242d.lib
       Release模式添加:    
          opencv_calib3d242.lib
opencv_contrib242.lib
opencv_core242.lib
opencv_features2d242.lib
opencv_flann242.lib
opencv_gpu242.lib
opencv_haartraining_engine.lib
opencv_highgui242.lib
opencv_imgproc242.lib
opencv_legacy242.lib
opencv_ml242.lib
opencv_objdetect242.lib
opencv_ts242.lib
opencv_video242.lib

8. 编码运行
    
[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include "highgui.h"  
  3. //using namespace cv;  
  4.  int _tmain(int argc, _TCHAR* argv[])  
  5. {    const char* imagename = "D:/Images/jc.jpg";  
  6.     cv::Mat img = cv::imread(imagename);  
  7.     if(img.empty())  
  8.     {  fprintf(stderr, "Can not load image %s\n", imagename);  
  9.         return -1;  
  10.     }  
  11.     if( !img.data )  
  12.         return -1;  
  13.     cv::namedWindow("image", CV_WINDOW_AUTOSIZE);  
  14.     cv::imshow("image", img);  
  15.     cv::waitKey();  
  16.     return 0;  
  17. //(代码来自http://blog.sina.com.cn/s/blog_68ed8b21010163uc.html)  
     运行效果如下

0 0