Windows 7 上的 OpenCV 2.4 和 MinGW 入门

2021-12-05 00:00:00 opencv windows mingw c++

如何安装 OpenCV 2.4 并使用 MinGW 编译我的代码?

解决方案

1.安装 OpenCV 2.4.3

首先,从 sourceforge.net 获取 并双击开始安装.只需按照向导并选择要安装的目录,例如C:MinGW.

选择要安装的C Compiler"和C++ Compiler".

安装程序会从网上下载一些包,所以你需要等待一段时间.安装完成后,使用前面描述的步骤将 C:MinGWin 添加到您的系统路径.

要测试您的 MinGW 安装是否成功,请打开命令行框并键入:gcc.如果一切正常,它将显示此消息:

gcc:致命错误:没有输入文件编译终止

这样就完成了 MinGW 的安装,现在是编写Hello, World!"的时候了.程序.

<小时>

3.编写示例代码

打开您的文本编辑器并输入以下代码并将文件保存到 loadimg.cpp.

#include "opencv2/highgui/highgui.hpp"#include 使用命名空间 cv;使用命名空间标准;int main(int argc, char** argv){Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);如果 (im.empty()){cout<<无法打开图像!"<<结束;返回-1;}imshow("图像", im);等待键(0);返回0;}

lena.jpg 或任何您喜欢的图像与上述文件放在同一目录中.打开命令行框并通过键入以下内容编译上面的代码:

g++ -I"C:opencvuildinclude" -L"C:opencvuildx86mingwlib" loadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg

如果编译成功,它将创建一个名为 loadimg.exe 的可执行文件.

类型:

loadimg

执行程序.结果:

<小时>

4.从这里去哪里?

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

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

How do I install OpenCV 2.4 and compile my code with MinGW?

解决方案

1. Installing OpenCV 2.4.3

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

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

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

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

You will see a window like shown below:

On the System Variables section,
select Path (1), click Edit... (2), add C:opencvuildx86mingwin (3) then click Ok.

This will completes the OpenCV 2.4.3 installation on your computer.


2. Installing MinGW compiler suite

I highly recommend you to use gcc (GNU Compiler Collection) for compiling your code. gcc is the compiler suite widely available in Linux systems and MinGW is the native port for Windows.

Download the MinGW installer from Sourceforge.net and double click to start installation. Just follow the wizard and select the directory to be installed, say C:MinGW.

Select "C Compiler" and "C++ Compiler" to be installed.

The installer will download some packages from the internet so you have to wait for a while. After the installation finished, add C:MinGWin to your system path using the steps described before.

To test if your MinGW installation is success, open a command-line box and type: gcc. If everything is ok, it will display this message:

gcc: fatal error: no input files
compilation terminated

This completes the MinGW installation, now is the time to write your "Hello, World!" program.


3. Write a sample code

Open your text editor and type the code below and save the file to loadimg.cpp.

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

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
  Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
  if (im.empty())
  {
    cout << "Cannot open image!" << endl;
    return -1;
  }

  imshow("image", im);
  waitKey(0);

  return 0;
}

Put lena.jpg or any image you like in the same directory with the file above. Open a command-line box and compile the code above by typing:

g++ -I"C:opencvuildinclude" -L"C:opencvuildx86mingwlib" loadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg

If it compiles successfully, it will create an executable named loadimg.exe.

Type:

loadimg

To execute the program. Result:


4. Where to go from here?

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

  1. Go to the samples dir → C:opencvsamplescpp.
  2. Read and compile some code.
  3. Write your own code.

相关文章