开篇
最近开始做毕业设计,其中有一个需求是使用普通摄像头进行图像采集,并生成全景图像。既然涉及到了图像处理,那OpenCV是一个非常不错的选择,在OpenCV官网上浏览了一下,发现新版本的OpenCV-2.4.3中内置了stitching模块,这个模块就是用来做图像拼接的,于是选择最新版本进行配置。
准备工作
配置OpenCV前需要下载并安装的软件:
- Visual Studio 2010
- CMake 2.8
安装OpenCV
- 下载OpenCV for Windows安装包,下载下来名称为OpenCV-2.4.3.exe;
- 运行安装包,解压到D盘根目录(可以修改为其他目录);
- 运行CMake,设置好OpenCV的源码路径及构建路径,如下图所示:
- 点击Configure按钮,如下图所示:
- 在弹出的编译器选择界面中选择Visual Studio 10,如下图所示:
- 等待配置完成,出现如下界面:
- 将BUILD_EXAMPLES选项勾上,其余的使用默认配置即可,如果需要使用OpenNI、OpenGL、QT、TBB库的请自行配置库路径;
- 再次点击Configure按钮,再次进行配置;
- 配置成功后,点击Generate按钮,生成VS 2010所需的工程文件;
- 切换目录到D:/OpenCV24(以自己的build路径为准),打开使用VS 2010打开OpenCV.sln;
- 切换Build模式为Debug,右键选择解决方案"OpenCV",在其中选择"重新生成解决方案",如下图所示:
- 等待Build完成后,找到INSTALL子项目,右键选择"生成",如下图所示:
- 切换Build模式为Release,重新进行一次构建;
配置OpenCV
- 用VS2010新建一个控制台项目,创建主文件,文件内容如下:
- #include
- #include
- using namespace cv;
- using namespace std;
- #pragma comment(lib, "opencv_core243d")
- #pragma comment(lib, "opencv_highgui243d")
- #pragma comment(lib, "opencv_imgproc243d")
- #pragma comment(lib, "opencv_ml243d")
- #pragma comment(lib, "opencv_stitching243d")
- int main()
- {
- string imagename = "D:\\1.jpg";
- //读入图像
- Mat img = imread(imagename);
- //如果读入图像失败
- if(img.empty())
- {
- return -1;
- }
- //创建窗口
- namedWindow("image",1);
- //显示图像
- imshow("image", img);
- //等待按键,按键盘任意键返回
- waitKey();
- return 0;
- }
以下开始切换到(2)
- 将所需的dll和lib文件拷贝到项目的Debug文件夹内,如下图所示:
- 接下来,切换到"属性管理器",在项目的"Debug|Win32"下,找到“Microsoft.Cpp.Win32.user”选项,右键选择"属性",如下图所示:
- 在弹出的属性对话框中,切换到“VC++目录”,如下图所示:
- 需要配置的选项为"可执行文件目录"、"包含目录"、"库目录",其中"可执行文件目录"和"库目录"配置如上图所示,"包含目录"如下图所示:
- Release模式的配置方法与Debug模式类似,不再详述;
- 完成上述配置后,在D盘根目录下保存一个1.jpg图像,在Debug模式下,编译并运行程序,即可测试OpenCV是否配置成功。
结语
OpenCV-2.4.3版本的源码及构建目录结构发生了一些变化,使用老版本的配置方法将不能工作,需要特别注意。另外新版本中的例程增加了很多,对学习OpenCV很有帮助。
另外需要吐槽一句,OpenCV的stitching模块真的很慢,合并10张640*480像素的jpg图像,在我的电脑上要运行2分钟,故不推荐使用。
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\vc10Here 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 folders like bin andinclude. 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 thePath Editor and add the following new entry (right click in the application to bring up the menu):
%OPENCV_DIR%\binSave 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 setxcommand inside a command window.
可以不设 采用绝对目录
Use for example the OpenCV_Debug name. Then by selecting the sheet Right Click ‣ Properties. In the following I will show to set the OpenCV rules locally, as I find unnecessary to pollute projects with custom rules that I do not use it. Go the C++ groups General entry and under the “Additional Include Directories” add the path to your OpenCV include. If you don’t have “C/C++” group, you should add any .c/.cpp file to the project.
$(OPENCV_DIR)\includeWhen adding third party libraries settings it is generally a good idea to use the power behind the environment variables. The full location of the OpenCV library may change on each system. Moreover, you may even end up yourself with moving the install directory for some reason. If you would give explicit paths inside your property sheet your project will end up not working when you pass it further to someone else who has a different OpenCV install path. Moreover, fixing this would require to manually modifying every explicit path. A more elegant solution is to use the environment variables. Anything that you put inside a parenthesis started with a dollar sign will be replaced at runtime with the current environment variables value. Here comes in play the environment variable setting we already made in our previous tutorial.
Next go to the Linker ‣ General and under the “Additional Library Directories” add the libs directory:
$(OPENCV_DIR)\libsThen you need to specify the libraries in which the linker should look into. To do this go to the Linker ‣ Input and under the “Additional Dependencies” entry add the name of all modules which you want to use:
The names of the libraries are as follow:
opencv_(The Name of the module)(The version Number of the library you use)d.libA full list, for the currently latest trunk version would contain:
opencv_core231d.lib opencv_imgproc231d.lib opencv_highgui231d.lib opencv_ml231d.lib opencv_video231d.lib opencv_features2d231d.lib opencv_calib3d231d.lib opencv_objdetect231d.lib opencv_contrib231d.lib opencv_legacy231d.lib opencv_flann231d.libThe letter d at the end just indicates that these are the libraries required for the debug. Now click ok to save and do the same with a new property inside the Release rule section. Make sure to omit the d letters from the library names and to save the property sheets with the save icon above them.
出现错误 缺少某个文件