首页  >  文章  >  后端开发  >  如何在 Windows 7 上为计算机视觉项目设置 OpenCV 2.4 和 MinGW?

如何在 Windows 7 上为计算机视觉项目设置 OpenCV 2.4 和 MinGW?

Linda Hamilton
Linda Hamilton原创
2024-11-14 11:00:02517浏览

How to Set Up OpenCV 2.4 and MinGW for Computer Vision Projects on Windows 7?

Getting Started with OpenCV 2.4 and MinGW on Windows 7

OpenCV (Open Computer Vision) is a powerful open-source library focused on real-time computer vision. It's widely used for image processing, computer graphics, motion detection, facial recognition, and more. MinGW (Minimalist GNU for Windows) is a lightweight port of the GNU toolchain for Windows, providing a native Windows environment to compile and run your C and C++ code. This guide will take you through the steps of installing OpenCV 2.4 and setting up your development environment with MinGW.

1. Installing OpenCV 2.4.3

Begin by downloading OpenCV 2.4.3 from sourceforge.net. Execute the self-extracting file to install OpenCV in a designated directory, such as "C:\". Upon completion, you'll find a new "C:\opencv" directory containing OpenCV headers, libraries, and samples.

Next, add "C:\opencv\build\x86\mingw\bin" to your system PATH to access OpenCV DLLs required for running your code. Open Control Panel > System > Advanced system settings > Advanced Tab > Environment variables...

In the System Variables section, select "Path", click "Edit...", add "C:\opencv\build\x86\mingw\bin", and click "Ok".

2. Installing MinGW Compiler Suite

For compiling code, gcc (GNU Compiler Collection) is highly recommended. MinGW provides a native Windows port for gcc. Download the MinGW installer from Sourceforge.net and install it in a directory, such as "C:\MinGW". Choose to install both "C Compiler" and "C++ Compiler".

Upon completion, add "C:\MinGW\bin" to your system PATH as described earlier. To verify the installation, open a command-line box and type "gcc". A successful installation will display an error message: "gcc: fatal error: no input files compilation terminated".

3. Writing a Sample Code

Create a new file named "loadimg.cpp" with the following code:

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

Place an image file, such as "lena.jpg", in the same directory as the code. Compile the code using the following command:

g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg

After successful compilation, run "loadimg.exe" to display the loaded image.

4. Next Steps

Your OpenCV environment is now ready. Explore the code samples provided in the "C:\opencv\samples\cpp" directory to gain a deeper understanding of OpenCV's capabilities. Alternatively, you can begin developing your own computer vision applications.

以上是如何在 Windows 7 上为计算机视觉项目设置 OpenCV 2.4 和 MinGW?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn