在 Visual C++ 2010 Express 中安装 OpenCV 2.4.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10901905/
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-27 14:36:15  来源:igfitidea点击:

Installing OpenCV 2.4.3 in Visual C++ 2010 Express

c++visual-c++opencv

提问by flowfree

How do you install and use OpenCV 2.4.3 under VC++ 2010 Express?

VC++ 2010 Express下如何安装和使用OpenCV 2.4.3?

回答by flowfree

1. Installing OpenCV 2.4.3

1. 安装 OpenCV 2.4.3

First, get OpenCV 2.4.3from sourceforge.net. Its a self-extracting so just double click to start the installation. Install it in a directory, say C:\.

首先,从 sourceforge.net获取OpenCV 2.4.3。它是自解压的,因此只需双击即可开始安装。将其安装在一个目录中,例如C:\.

OpenCV self-extractor

OpenCV 自解压器

Wait until all files get extracted. It will create a new directory C:\opencvwhich contains OpenCV header files, libraries, code samples, etc.

等到所有文件都被提取出来。它将创建一个C:\opencv包含 OpenCV 头文件、库、代码示例等的新目录。

Now you need to add the directory C:\opencv\build\x86\vc10\binto your system PATH. This directory contains OpenCV DLLs required for running your code.

现在您需要将该目录添加C:\opencv\build\x86\vc10\bin到您的系统路径中。此目录包含运行代码所需的 OpenCV DLL。

Open Control PanelSystemAdvanced system settingsAdvancedTab → Environment variables...

打开控制面板系统高级系统设置高级选项卡 →环境变量...

enter image description here

enter image description here

On the System Variables section, select Path(1), Edit(2), and type C:\opencv\build\x86\vc10\bin;(3), then click Ok.

在 System Variables 部分,选择Path(1)、Edit(2) 并键入C:\opencv\build\x86\vc10\bin;(3),然后单击Ok

On some computers, you may need to restart your computer for the system to recognize the environment path variables.

在某些计算机上,您可能需要重新启动计算机,系统才能识别环境路径变量。

This will completes the OpenCV 2.4.3 installation on your computer.

这将在您的计算机上完成 OpenCV 2.4.3 的安装。



2. Create a new project and set up Visual C++

2.新建项目并设置Visual C++

Open Visual C++ and select FileNewProject...Visual C++Empty Project. Give a name for your project (e.g: cvtest) and set the project location (e.g: c:\projects).

打开 Visual C++ 并选择FileNewProject...Visual C++Empty Project。为您的项目命名(例如:)cvtest并设置项目位置(例如:)c:\projects

New project dialog

New project dialog

Click Ok. Visual C++ will create an empty project.

单击确定。Visual C++ 将创建一个空项目。

VC++ empty project

VC++ empty project

Make sure that "Debug" is selected in the solution configuration combobox. Right-click cvtestand select PropertiesVC++ Directories.

确保在解决方案配置组合框中选择了“调试”。右键单击cvtest并选择属性VC++ 目录

Project property dialog

Project property dialog

Select Include Directoriesto add a new entry and type C:\opencv\build\include.

选择包含目录以添加新条目并键入C:\opencv\build\include

Include directories dialog

Include directories dialog

Click Okto close the dialog.

单击确定关闭对话框。

Back to the Property dialog, select Library Directoriesto add a new entry and type C:\opencv\build\x86\vc10\lib.

返回到“属性”对话框,选择“库目录”以添加新条目并键入C:\opencv\build\x86\vc10\lib

Library directories dialog

Library directories dialog

Click Okto close the dialog.

单击确定关闭对话框。

Back to the property dialog, select LinkerInputAdditional Dependenciesto add new entries. On the popup dialog, type the files below:

返回属性对话框,选择LinkerInputAdditional Dependencies以添加新条目。在弹出对话框中,输入以下文件:

opencv_calib3d243d.lib
opencv_contrib243d.lib
opencv_core243d.lib
opencv_features2d243d.lib
opencv_flann243d.lib
opencv_gpu243d.lib
opencv_haartraining_engined.lib
opencv_highgui243d.lib
opencv_imgproc243d.lib
opencv_legacy243d.lib
opencv_ml243d.lib
opencv_nonfree243d.lib
opencv_objdetect243d.lib
opencv_photo243d.lib
opencv_stitching243d.lib
opencv_ts243d.lib
opencv_video243d.lib
opencv_videostab243d.lib

Note that the filenames end with "d" (for "debug"). Also note that if you have installed another version of OpenCV (say 2.4.9) these filenames will end with 249d instead of 243d (opencv_core249d.lib..etc).

请注意,文件名以“d”(用于“调试”)结尾。另请注意,如果您安装了另一个版本的 OpenCV(比如 2.4.9),这些文件名将以 249d 而不是 243d(opencv_core249d.lib..etc)结尾。

enter image description here

enter image description here

Click Okto close the dialog. Click Okon the project properties dialog to save all settings.

单击确定关闭对话框。单击项目属性对话框上的确定以保存所有设置。

NOTE:

These steps will configure Visual C++ for the "Debug" solution. For "Release" solution (optional), you need to repeat adding the OpenCV directories and in Additional Dependenciessection, use:

opencv_core243.lib
opencv_imgproc243.lib
...

instead of:

opencv_core243d.lib
opencv_imgproc243d.lib
...

笔记:

这些步骤将为“调试”解决方案配置 Visual C++。对于“发布”解决方案(可选),您需要重复添加 OpenCV 目录并在附加依赖项部分中使用:

opencv_core243.lib
opencv_imgproc243.lib
...

代替:

opencv_core243d.lib
opencv_imgproc243d.lib
...

You've done setting up Visual C++, now is the time to write the real code. Right click your project and select AddNew Item...Visual C++C++ File.

您已经完成了 Visual C++ 的设置,现在是编写真正代码的时候了。右键单击您的项目并选择AddNew Item...Visual C++C++ File

Add new source file

Add new source file

Name your file (e.g: loadimg.cpp) and click Ok. Type the code below in the editor:

命名您的文件(例如:)loadimg.cpp并单击确定。在编辑器中输入以下代码:

#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat im = imread("c:/full/path/to/lena.jpg");
    if (im.empty()) 
    {
        cout << "Cannot load image!" << endl;
        return -1;
    }
    imshow("Image", im);
    waitKey(0);
}

The code above will load c:\full\path\to\lena.jpgand display the image. You can use any image you like, just make sure the path to the image is correct.

上面的代码将加载c:\full\path\to\lena.jpg并显示图像。您可以使用任何您喜欢的图像,只要确保图像的路径正确即可。

Type F5 to compile the code, and it will display the image in a nice window.

键入 F5 编译代码,它将在一个漂亮的窗口中显示图像。

First OpenCV program

First OpenCV program

And that is your first OpenCV program!

这是你的第一个 OpenCV 程序!



3. Where to go from here?

3. 从这里去哪里?

Now that your OpenCV environment is ready, what's next?

现在您的 OpenCV 环境已准备就绪,下一步是什么?

  1. Go to the samples dir → c:\opencv\samples\cpp.
  2. Read and compile some code.
  3. Write your own code.
  1. 转到示例目录 → c:\opencv\samples\cpp
  2. 阅读并编译一些代码。
  3. 编写自己的代码。