Win下源码安装

官方发布的安装包仅提供了特定环境下的常规操作,如果需要更多的特性支持,就需要自己进行编译。

1下载源代码

通过GitHub下载最新源代码并切换到Tags-3.2.0分支,或者直接下载对应分支的源代码。采用对应分支代码编译的原因,一是统一笔者和读者的开发环境,方便定位书中代码的位置;二是最新代码中可能存在官方正在开发调试的代码,会有编译不通过的情况。以下步骤,读者也可以安装TortoiseGit,通过界面化操作完成。

cd /path/to/workspaces
gitclone https://github.com/opencv/opencv.git
cd opencv
git checkout -b Branch_3.2.0 3.2.0
cd /path/to/workspaces
gitclone https://github.com/opencv/opencv_contrib.git
cd opencv
git checkout -b Branch_3.2.0 3.2.0

2使用CMake编译

编译Visual Studio版本

  • 安装并打开CMake3.8.1程序
  • 在Where is the source code栏填入源码所在目录Driver:/path/to/opencv
  • 在Where to build thebinaries栏填入编译后的文件存放目录Driver:/path/to/opencv/build(此处笔者是在opencv源码目录下建了一个build/VS2015目录方便区分不同版本)
  • 点击Add Entry按钮添加自定义参数OPENCV_EXTRA_MODULES_PATH=Driver:/path/to/opencv_contrib/modules
  • 点击Configure按钮,在弹出的对话框中Specify the generator for this project栏选择对应编译器版本Visual Studio 14 2015
  • 点击Finish按钮确定并等待配置过程执行完成
  • 点击Generate按钮生成配置文件
  • 打开VS2015的MSBuild命令提示符,执行编译
  • cd Driver:/path/to/opencv/build/VS2015
    msbuild /m OpenCV.sln /t:Build /p:Configuration=Release /v:m
    

编译Qt版本

  • 下载并安装qt-opensource-windows-x86-mingw530-5.8.0.exe
  • 将C:\Qt\Qt5.8.0\Tools\mingw530_32\bin加入到Path环境变量
  • 安装并打开CMake3.8.1程序
  • 在Where is the source code栏填入源码所在目录Driver:/path/to/opencv
  • 在Where to build thebinaries栏填入编译后的文件存放目录Driver:/path/to/opencv/build
  • 点击Add Entry按钮添加自定义参数OPENCV_EXTRA_MODULES_PATH=Driver:/path/to/opencv_contrib/modules
  • 点击Configure按钮,在弹出的对话框中Specify the generator for this project栏选择对应编译器版本MingGW Makefiles,然后选择Specify native compilers
  • 在C编译器中填入C:/Qt/Qt5.8.0/Tools/mingw530_32/bin/gcc.exe
  • 在C++编译器中填入C:/Qt/Qt5.8.0/Tools/mingw530_32/bin/g++.exe
  • 点击Finish按钮确定并等待配置过程执行完成
  • 在配置栏中找到WITH_QT和WITH_OPENGL并勾选,再次点击Configure按钮
  • 此时会弹出Error in configuration process,project files may be invalid错误提示,在配置栏中补充一下配置项
  • QT_QMAKE_EXECUTABLE=C:/Qt/Qt5.8.0/5.8/mingw53_32/bin/qmake.exe
    Qt5Concurrent_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5Concurrent
    Qt5Core_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5Core
    Qt5Gui_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5Gui
    Qt5Test_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5Test
    Qt5Widgets_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5Widgets
    Qt5OpenGL_DIR=C:\Qt\Qt5.8.0\5.8\mingw53_32\lib\cmake\Qt5OpenGL
    
  • 再次点击Configure按钮,直到配置栏中没有红色提示项
  • 点击Generate按钮生成配置文件
  • 进入Driver:\path\to\opencv\build\MinGW目录
  • 执行mingw32-make命令并等待完成,次过程耗时较长
  • 执行mingw32-make install命令并等待完成,默认安装目录为Driver:\path\to\opencv\build\MinGW\install,可通过CMAKE_INSTALL_PREFIX配置项进行更改

2编写Qt程序

打开Qt Creator 4.2.1 (Community)新建Qt Console Application,在主文件中输入测试代码。

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char* argv[])
{
    //声明IplImage指针
    IplImage*pImg;
    //载入图片
    pImg=cvLoadImage("C:/Users/Ouyang/Pictures/test.jpg", 1);
    //创建窗口
    cvNamedWindow("Image", 1);
    //显示图像
    cvShowImage("Image", pImg);
    //等待按键
    cvWaitKey(0);
    //销毁窗口
    cvDestroyWindow("Image");
    //释放图像
    cvReleaseImage(&pImg);
    return 0;
}

编辑项目配置文件project.pro,增加如下配置项。

INCLUDEPATH += Driver:/path/to/opencv/build/MinGW/install/include/opencv \
               Driver:/path/to/opencv/build/MinGW/install/include/opencv2 \
               Driver:/path/to/opencv/build/MinGW/install/include

LIBS += -LDriver:/path/to/opencv/build/MinGW/install/x86/mingw/lib

将依赖的DLL文件拷贝至项目的build-qt_demo-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug目录下,或者将DLL文件所在目录加入到环境变量中。

Driver:/path/to/opencv/build/MinGW/bin/*.dll

编译并执行。

results matching ""

    No results matching ""