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

2021-12-10 00:00:00 opencv visual-c++ c++

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

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

推荐答案

1.安装 OpenCV 2.4.3

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

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

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

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

现在您需要将目录 C:opencvuildx86vc10in 添加到您的系统路径中.此目录包含运行代码所需的 OpenCV DLL.

Now you need to add the directory C:opencvuildx86vc10in to your system PATH. This directory contains OpenCV DLLs required for running your code.

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

Open Control Panel → System → Advanced system settings → Advanced Tab → Environment variables...

在系统变量部分,选择路径 (1)、编辑 (2),然后输入C:opencvuildx86vc10in; (3),然后点击确定.

On the System Variables section, select Path (1), Edit (2), and type C:opencvuildx86vc10in; (3), then click Ok.

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

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

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

This will completes the OpenCV 2.4.3 installation on your computer.

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

打开 Visual C++ 并选择 文件 →新 →项目... →Visual C++ →空项目.为您的项目命名(例如:cvtest)并设置项目位置(例如:c:projects).

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

点击确定.Visual C++ 将创建一个空项目.

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

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

Make sure that "Debug" is selected in the solution configuration combobox. Right-click cvtest and select Properties → VC++ Directories.

选择包含目录以添加新条目并键入C:opencvuildinclude.

Select Include Directories to add a new entry and type C:opencvuildinclude.

点击确定关闭对话框.

返回属性对话框,选择库目录以添加新条目并键入C:opencvuildx86vc10lib.

Back to the Property dialog, select Library Directories to add a new entry and type C:opencvuildx86vc10lib.

点击确定关闭对话框.

返回属性对话框,选择链接器 →输入 →附加依赖项以添加新条目.在弹出对话框中,输入以下文件:

Back to the property dialog, select Linker → Input → Additional Dependencies to add new entries. On the popup dialog, type the files below:

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

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

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).

点击确定关闭对话框.在项目属性对话框中单击确定以保存所有设置.

Click Ok to close the dialog. Click Ok on the project properties dialog to save all settings.

注意:

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

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 Dependencies section, use:

opencv_core243.lib
opencv_imgproc243.lib
...

代替:

opencv_core243d.lib
opencv_imgproc243d.lib
...

您已经完成了 Visual C++ 的设置,现在是编写真正代码的时候了.右键单击您的项目并选择添加 →新项目... →Visual C++ →C++ 文件.

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

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

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

#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);
}

上面的代码将加载 c:fullpath olena.jpg 并显示图像.你可以使用您喜欢的任何图像,只需确保图像的路径正确即可.

The code above will load c:fullpath olena.jpg and display the image. You can use any image you like, just make sure the path to the image is correct.

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

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

这是你的第一个 OpenCV 程序!

And that is your first OpenCV program!

3.从这里去哪里?

既然您的 OpenCV 环境已准备就绪,接下来要做什么?

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

  1. 转到示例目录 →c:opencvsamplescpp.
  2. 阅读并编译一些代码.
  3. 编写您自己的代码.

相关文章