问题描述
我正在尝试在 Visual Studio 2010 Express 中使用 opencv 2.3.我的代码来自示例:
#include "stdafx.h"#include int _tmain(int argc, _TCHAR* argv[]){国际 c;//为图像分配内存IplImage *img;//从视频设备 #1 捕获CvCapture* 捕获 = cvCaptureFromCAM(1);//创建一个窗口来显示图像cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);//定位窗口cvMoveWindow("mainWin", 5, 5);同时(1){//检索捕获的帧img=cvQueryFrame(捕获);//在窗口中显示图像cvShowImage("mainWin", img );//等待 10 ms 等待按键被按下c=cvWaitKey(10);//退出键终止程序如果(c == 27)休息;}返回0;}
到目前为止我做了什么?
- 将
buildin 和
build{x86|x64}{vc9vc10mingw}in
之一添加到我的系统路径(以使用 DLL). - 添加了
build{x86|x64}{vc9vc10mingw}lib
或build{x86|x64}{vc9vc10mingw}staticlib
作为我的链接器设置的库目录. - 将
buildinclude
和buildincludeopencv
作为包含目录添加到我的编译器设置中.
结果是:
<块引用>1>链接:致命错误 LNK1104:无法打开文件 'c:OpenCV2.3uildx86vc10lib.obj'
OpenCV 文件夹中没有 lib.obj
.我只解压了 OpenCV-2.3.0-win-superpack.exe
,没有使用 CMake 软件.
我做错了什么?
好吧,official guide 用于在 VS2010 上安装 OpenCV 2.1,所以我在下面写了一些说明,说明如何正确安装和配置 x86 版本OpenCV 2.3 在 Visual Studio 2010 (Express) 上,因为很多人似乎在正确设置它时遇到问题.
下载OpenCV-2.3.0-win-superpack.exe 并执行它以将所有文件解压到名为 OpenCV2.3
的文件夹中.在这个文件夹中有 2 个目录:build
和 opencv
.VS2010 上的所有设置都将参考 build
目录.出于实际目的,我将文件夹 OpenCV2.3
移动到了我的 C:
驱动器,因此请注意我在本指南中建议的路径,因为您的路径可能有所不同.>
在 Visual Studio 上,创建一个新的 Win32 控制台应用程序 项目并随意命名.之后,将出现一个新窗口.点击标签应用程序设置并确保选择空项目选项:
在Source Files
文件夹中添加一个新文件main.cpp
,然后将这段代码添加到main.cpp
:
#include #include #include int main(int argc, char* argv[]){如果 (argc <2){printf("用法:./opencv_hello
");返回-1;}IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);如果(!img){返回-1;}cvNamedWindow("显示", CV_WINDOW_AUTOSIZE);cvShowImage("显示", img );cvWaitKey(0);返回0;}
此时,我们需要配置项目,以便它可以定位 OpenCV 头文件和库.转到项目属性 (ALT+F7),并在出现新窗口后执行以下操作:
在配置框中,选择所有配置
打开 Configuration Properties > C/C++ > General,然后编辑 Additional Include Directories 字段以添加以下 3 个路径(用于标题):>
C:OpenCV2.3uildincludeopencv
C:OpenCV2.3uildincludeopencv2
C:OpenCV2.3uildinclude
注意includeopencv
是OpenCV的C接口,includeopencv2
是C++接口.我们还添加了文件夹 include
以防止我们的构建被 C 接口的某些头文件破坏,这些头文件将 C++ 头文件称为 opencv2core
.
- 然后,在配置属性>链接器>常规上添加库的路径,并在附加库目录字段上添加:
C:OpenCV2.3uildx86vc9lib
:
- 最后,对于这个简单的测试,我们将添加库
opencv_core230.lib
和opencv_highgui230.lib
.所以转到配置属性>链接器>输入并添加它们:
在编写更复杂的应用程序时,您可能需要添加其他我没有的 OpenCV 库在我们的这个小项目中提到过.
按 F7 以构建解决方案,您应该看到:
========== 构建:1 成功,0 失败,0 最新,0 跳过 ==========
为了能够执行应用程序,您需要修改系统的PATH环境变量以添加OpenCV的DLL的位置.将此添加到 PATH 的末尾:
<代码>;C:OpenCV2.3uildx86vc9in
I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c;
// allocate memory for an image
IplImage *img;
// capture from video device #1
CvCapture* capture = cvCaptureFromCAM(1);
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
// retrieve the captured frame
img=cvQueryFrame(capture);
// show the image in the window
cvShowImage("mainWin", img );
// wait 10 ms for a key to be pressed
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
return 0;
}
What have I done so far?
- Added
buildin
and one ofbuild{x86|x64}{vc9vc10mingw}in
to my system path (to use DLLs). - Added
build{x86|x64}{vc9vc10mingw}lib
orbuild{x86|x64}{vc9vc10mingw}staticlib
as library directories to my linker settings. - Added
buildinclude
andbuildincludeopencv
as include directories to my compiler settings.
And the result is:
1>LINK : fatal error LNK1104: cannot open file 'c:OpenCV2.3uildx86vc10lib.obj'
There's no lib.obj
in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe
, without using CMake software.
What am I doing wrong?
Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.
Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3
. Inside this folder there are 2 directories: build
and opencv
. All the setup on VS2010 will refer to the build
directory. For practical purposes I moved the folder OpenCV2.3
to my C:
drive, so pay attention to the paths I suggest on this guide as yours might be different.
On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:
Add a new file main.cpp
to the folder Source Files
, then add this code to main.cpp
:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./opencv_hello <file.png>
");
return -1;
}
IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
return -1;
}
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:
On the Configuration box, select All Configurations
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:OpenCV2.3uildincludeopencv
C:OpenCV2.3uildincludeopencv2
C:OpenCV2.3uildinclude
Note that includeopencv
is for the C interface of OpenCV and includeopencv2
if for the C++ interface. We are also adding the folder include
to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2core
.
- Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this:
C:OpenCV2.3uildx86vc9lib
:
- Finally, for this simple test we are going to add the libraries
opencv_core230.lib
andopencv_highgui230.lib
. So go to Configuration Properties > Linker > Input and add them:
When writing more complex applications you'll probably need to add other OpenCV libs that I did not mentioned on this little project of ours.
Press F7 to Build Solution and you should see:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:
; C:OpenCV2.3uildx86vc9in
这篇关于为 Visual Studio 2010 设置 OpenCV-2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!