第一步,用cmkae的gui工具configure要编译的OpenCV工程:
(1)开始->Visula Studio 2015->Visual Studio Tools->Developer Comand Prompt for VS2015
(2)在弹出的命令行窗口输入cmake-gui.exe的全路径打开cmake,并按照下图配置source地址和build地址:
(3)点击左下角的Configure按钮,在弹出的编译器选择框内选择“Visual Studio 14 2015”
(4)在configure成功后会出现很多红色栏,不用担心,此时我们需要做最重要的一步:打开OpenGL的支持。在”Search”框内输入”opengl”,将Value勾上。
(5)再点击一次左下角的Configure,这一次没有红色栏,全部变为白色,表示配置成功。
(6)点击左下角Configure按钮旁边的”Generate”,会在opencv-2.4.10\build文件夹下生成解决方案文件”OpenCV.sln”
(7)我们双击打开”OpenCV.sln”,在Solution Explorer结构中找到ALL_BUILD,右键选中并点击Build;待生成成功后,找到INSTALL,右键选中并点击Build。
至此,支持OpenGL的OpenCV源码编译过程就结束了。
选择下面这本书”Mastering OpenCV with Practical Computer Vision Project”的例子工程”Chapter 3: Marker-less Augmented Reality”进行AR的第一个程序运行。
全书的代码可以去下面的github地址进行下载:
https://github.com/MasteringOpenCV/code
我们进入第三章的Mark-less AR例程的文件夹:
code-master\Chapter3_MarkerlessAR
shift+右键在这里打开命令行窗口,然后依次执行下面的命令:
#1
set OpenCV_DIR="D:\opencv-2.4.10\build"
#2
mkdir build
#3
cd build
#4
cmake -G "Visual Studio 14 2015" -D OpenCV_DIR=%OpenCV_DIR% ..
#5
start EXAMPLE_MARKERLESS_AR.sln
现在我们就打开了最终生成的VS2015工程,右键选中ALL_BUILD进行生成:
但是报了很多下面的错误,分析可以知道都是和OpenGL有关的。
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): error C2144: syntax error: 'void' should be preceded by ';'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): error C2182: 'APIENTRY': illegal use of type 'void'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): error C2146: syntax error: missing ';' before identifier 'glAccum'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C2144: syntax error: 'void' should be preceded by ';'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C2086: 'int WINGDIAPI': redefinition
2> C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): note: see declaration of 'WINGDIAPI'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C2182: 'APIENTRY': illegal use of type 'void'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C2086: 'int APIENTRY': redefinition
2> C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): note: see declaration of 'APIENTRY'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1158): error C2146: syntax error: missing ';' before identifier 'glAlphaFunc'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1159): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1159): error C2086: 'int WINGDIAPI': redefinition
2> C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1157): note: see declaration of 'WINGDIAPI'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1159): error C2146: syntax error: missing ';' before identifier 'GLboolean'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1160): error C2144: syntax error: 'void' should be preceded by ';'
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1160): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>C:\Program Files (x86)\Windows Kits\8.1\Include\um\gl/gl.h(1160): error C2086: 'int WINGDIAPI': redefinition
在一番google之后,在stackoverflow上找到了类似的错误:
https://stackoverflow.com/questions/6222509/getting-many-opengl-errors
%%you need to include "windows.h" before "gl.h"
#include "windows.h"
#include "gl.h"
找到markerless_ar_demo工程下的main.cpp,第一个include文件是ARDrawingContext.hpp,我们就在这里加上对windows.h的include:
#ifndef ARDrawingContext_HPP
#define ARDrawingContext_HPP
////////////////////////////////////////////////////////////////////
// File includes:
#include "windows.h"
#include "GeometryTypes.hpp"
#include "CameraCalibration.hpp"
////////////////////////////////////////////////////////////////////
// Standard includes:
#include
再次生成ALL_BUILD,可以通过。
但是要运行markerless_ar_demo,还需要命令行参数。
markerless_ar_demo pattern.png test_image.png
在文件夹code-master\Chapter3_MarkerlessAR\src下面有两个图片,其中PyramidPattern.jpg是pattern,PyramidPatternTest.bmp是测试图片,将这两幅图片的全路径加入到工程”markerless_ar_demo”的调试命令行参数里:右键Properties->Configuration Properties->Debugging->Command Arguments
最后一切都配置完成,进行调试,看一下效果:
Book: Mastering OpenCV with Practical Computer Vision Project