C++ 为 Visual Studio 2010 设置 OpenCV-2.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7011238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:21:06  来源:igfitidea点击:

Setup OpenCV-2.3 for Visual Studio 2010

c++cvisual-studio-2010visual-studioopencv

提问by Elfoc

I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:

我正在尝试将 opencv 2.3 与 Visual Studio 2010 Express 一起使用。我的代码来自示例:

#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 build\binand one of build\{x86|x64}\{vc9\vc10\mingw}\binto my system path (to use DLLs).
  • Added build\{x86|x64}\{vc9\vc10\mingw}\libor build\{x86|x64}\{vc9\vc10\mingw}\staticlibas library directories to my linker settings.
  • Added build\includeand build\include\opencvas include directories to my compiler settings.
  • 添加build\bin和其中之一build\{x86|x64}\{vc9\vc10\mingw}\bin到我的系统路径(使用 DLL)。
  • 添加build\{x86|x64}\{vc9\vc10\mingw}\libbuild\{x86|x64}\{vc9\vc10\mingw}\staticlib作为库目录添加到我的链接器设置中。
  • 在我的编译器设置中添加build\includebuild\include\opencv包含目录。

And the result is:

结果是:

1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'

1>LINK : 致命错误 LNK1104: 无法打开文件 'c:\OpenCV2.3\build\x86\vc10\lib.obj'

There's no lib.objin OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.

lib.objOpenCV 文件夹中没有。我只解压过OpenCV-2.3.0-win-superpack.exe,没有使用 CMake 软件。

What am I doing wrong?

我究竟做错了什么?

回答by karlphillip

Well, the official guideis for installing OpenCV 2.1on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86version of OpenCV 2.3on Visual Studio 2010(Express), since a lot of folks seem to have problems setting it up correctly.

好吧,官方指南是用于在 VS2010 上安装OpenCV 2.1,所以我在下面写了一些说明,说明如何在Visual Studio 2010(Express)上正确安装和配置x86版本的OpenCV 2.3,因为很多人似乎都有问题正确设置。

Download OpenCV-2.3.0-win-superpack.exeand execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: buildand opencv. All the setup on VS2010 will refer to the builddirectory. For practical purposes I moved the folder OpenCV2.3to my C:\drive, so pay attention to the paths I suggest on this guide as yours might be different.

下载OpenCV-2.3.0-win-superpack.exe并执行它以将所有文件解压到名为OpenCV2.3. 在这个文件夹中有 2 个目录:buildopencv. VS2010 上的所有设置都将参考该build目录。出于实际目的,我将文件夹移到了OpenCV2.3我的C:\驱动器中,因此请注意我在本指南中建议的路径,因为您的路径可能有所不同。

On Visual Studio, create a new Win32 Console Applicationproject and name it whatever you like. After that, a new window will show up. Click on the tab Application Settingsand make sure the option Empty Projectgets selected:

在 Visual Studio 上,创建一个新的Win32 控制台应用程序项目并随意命名。之后,将出现一个新窗口。单击“应用程序设置”选项卡并确保选择“空项目”选项:

enter image description here

在此处输入图片说明

Add a new file main.cppto the folder Source Files, then add this code to main.cpp:

将一个新文件添加main.cpp到文件夹Source Files,然后将此代码添加到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>\n");
    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:

此时,我们需要配置项目,以便它可以定位 OpenCV 头文件和库。转到项目属性(ALT+F7),一旦出现新窗口,请执行以下操作:

  • On the Configurationbox, select All Configurations

  • Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directoriesto add these 3 paths (for the headers):

    C:\OpenCV2.3\build\include\opencv

    C:\OpenCV2.3\build\include\opencv2

    C:\OpenCV2.3\build\include

  • 配置框中,选择所有配置

  • 打开Configuration Properties > C/C++ > General,然后编辑字段Additional Include Directories以添加这 3 个路径(用于标题):

    C:\OpenCV2.3\build\include\opencv

    C:\OpenCV2.3\build\include\opencv2

    C:\OpenCV2.3\build\include

enter image description here

在此处输入图片说明

Note that include\opencvis for the C interface of OpenCV and include\opencv2if for the C++ interface. We are also adding the folder includeto prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.

注意include\opencv是针对 OpenCV 的 C 接口,include\opencv2如果是针对 C++ 接口。我们还添加了文件夹,include以防止我们的构建被 C 接口的某些头文件破坏,这些头文件将 C++ 头文件称为opencv2\core.

  • Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directoriesfield, add this: C:\OpenCV2.3\build\x86\vc9\lib:
  • 然后,在Configuration Properties > Linker > General上添加库的路径,并在Additional Library Directories字段上添加以下内容C:\OpenCV2.3\build\x86\vc9\lib

enter image description here

在此处输入图片说明

  • Finally, for this simple test we are going to add the libraries opencv_core230.liband opencv_highgui230.lib. So go to Configuration Properties > Linker > Inputand add them:
  • 最后,对于这个简单的测试,我们将添加库opencv_core230.libopencv_highgui230.lib. 所以去配置属性>链接器>输入并添加它们:

enter image description here

在此处输入图片说明

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.

在编写更复杂的应用程序时,您可能需要添加我在我们的这个小项目中没有提到的其他 OpenCV 库。

Press F7to Build Solutionand you should see:

F7构建解决方案,您应该看到:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

To be able to execute the application you'll need to modify the PATHenvironment variableof your system to add the location of OpenCV's DLLs. Add this to end of PATH:

为了能够执行应用程序,您需要修改系统的PATH环境变量以添加 OpenCV DLL 的位置。将此添加到 PATH 的末尾:

; C:\OpenCV2.3\build\x86\vc9\bin

回答by pimmes111

If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder: The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin

如果您正在努力编辑 PATH 环境变量,您还可以将所需的 .dll 文件复制到您的项目文件夹中: dll 文件位于此文件夹中 ../OpenCV2.3/build.x86/vc9/bin

Then copy them to the folder where .exe file is created: c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug(Ofcourse you have to change the path to your Debug folder)

然后将它们复制到创建 .exe 文件的文件夹中: c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug(当然你必须更改 Debug 文件夹的路径)

You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.

您只需要复制您在项目中使用的 .dll 文件(例如 #include)例如,如果您收到一条错误消息,指出未找到 opencv_core231d.dll,则从上述位置(bin 文件夹)获取此 .dll 文件) 并复制到您的项目 Debug 文件夹。

Hope this helps..

希望这可以帮助..

回答by rossb83

Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h

每当我制作一个使用 opencv 2.2 或更高版本的程序时,我都会包含所有内容,然后注释掉我不需要的库。试试这个,我确定你需要的不仅仅是 highgui.h

#include "opencv2\opencv.hpp"

using namespace cv;

//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")