Home > Article > Backend Development > 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:opencvbuildx86mingwbin" 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:opencvbuildx86mingwbin", 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:MinGWbin" 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:opencvsamplescpp" directory to gain a deeper understanding of OpenCV's capabilities. Alternatively, you can begin developing your own computer vision applications.
The above is the detailed content of How to Set Up OpenCV 2.4 and MinGW for Computer Vision Projects on Windows 7?. For more information, please follow other related articles on the PHP Chinese website!