Getting started with OpenCV 2.4 and MinGW on Windows 7
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:\opencv\build\x86\mingw\bin
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:\opencv\build\x86\mingw\bin
(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:\MinGW\bin
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:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" 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?
- Go to the samples dir →
C:\opencv\samples\cpp
. - Read and compile some code.
- Write your own code.
How to setup MinGw on Windows 7 64 bit?
x86_64-w64-mingw32-g++
is the name of the compiler with the name of the target architecture applied. This is to difference between different versions of gcc on one machine. When you don't have any other gcc installed, you can copy x86_64-w64-mingw32-g++ to g++ and x86_64-w64-mingw32-gcc to gcc.
You can check if you have other versions of gcc, if you open a terminal (start->run->cmd), and type there gcc -dumpmachine
. If it says something which sounds like a 32bit compiler, you have somewhere the 32bit compiler installed. If it says it is a 64bit compiler, you have nothing to do. If it says there is no gcc, the you have to copy the x86_64-w64-mingw32* compilers and add their path to the system path.
OpenCV 2.4.5, eclipse CDT Juno, MinGW error 0xc0000005
After many trials and errors I decided to follow this tutorial and to compile my own binaries as it seems that too many people are complaining that precompiled binaries are NOT working for them. Eclipse CDT Juno was already installed.
My procedure was as follows:
- Download and install MinGW and add to the system PATH with
c:/mingw/bin - Download cmake from http://www.cmake.org and install it
- Download OpenCV2.4.5 Windows version
- Install/unzip Opencv to C:\OpenCV245PC\ (README,index.rst and CMakeLists.txt are there with all subfolders)
- Run CMake GUI tool, then
- Choose C:\OpenCV245PC\ as source
- Choose the destination, C:\OpenCV245MinGW\x86 where to build the binaries
- Press Configure button, choose MinGW Makefiles as the generator. There are some red highlights in the window, choose options as you need.
- Press the Configure button again. Configuring is now done.
- Press the Generate button.
- Exit the program when the generating is done.
- Exit the Cmake program.
- Run the command line mode (cmd.exe) and go to the destination
directory C:\OpenCV245MinGW\x86 - Type "mingw32-make". You will see a progress of building
binaries. If the command is not found, you must make sure that the
system PATH is added with c:/mingw/bin. The build continues
according the chosen options to a completion. - In Windows system PATH (My Computer > Right button click >
Properties > Advanced > Environment Variables > Path) add the
destination's bin directory, C:\OpenCV245MinGW\x86\bin - RESTART COMPUTER
- Go to the Eclipse CDT IDE, create a C++ program using the sample OpenCV code (You can use code from top of this topic).
- Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add
the source OpenCV folder "C:\OpenCV245PC\build\include" - Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) ONE BY ONE (this could vary from project to project, you can add all of them if you like or some of them just the ones that you need for your project): opencv_calib3d245 opencv_contrib245 opencv_core245 opencv_features2d245 opencv_flann245 opencv_gpu245 opencv_highgui245 opencv_imgproc245 opencv_legacy245 opencv_ml245 opencv_nonfree245 opencv_objdetect245 opencv_photo245 opencv_stitching245 opencv_video245 opencv_videostab245
- Add the built OpenCV library folder, "C:\OpenCV245MinGW\x86\lib" to Library search path (-L).
You can use this code to test your setup:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("c:/lenna.png", CV_LOAD_IMAGE_COLOR);
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
waitKey(0);
return 0;
}
Don't forget to put image to the C:/ (or wherever you might find suitable, just be sure that eclipse have read acess.
OpenCV 2.4.2 with MinGW and gcc 4.7.1
And did you try to google your problem? It seems that no, and that's very pity:
- undeclared variable in flann/lsh_table.h with -std=gnu++0x (Bug #2179)
- compile error in opencv2/flann/lsh_table.h when compiling bgslibrary
NetBeans 7.2 MinGW installing for OpenCV
How to build and use openCV with Netbeans 7.2 . Step by step
Preparations
In the "c:\mingw\bin" and "c:\mingw\lib"
- Search and delete all libopencv*.dll , libopencv*.dll.a , libopencv*.a
Tested with the following programs
Download OpenCV-2.4.2.exe (222.9 MB) http://sourceforge.net/projects/opencvlibrary/files/
Download mingw-get-inst-20120426.exe (662.7 kB) http://sourceforge.net/projects/mingw/files/
Download cmake-2.8.9-win32-x86.zip http://www.cmake.org/files/v2.8/cmake-2.8.9-win32-x86.zip
All references used in this manual:
All references in this manual begin with "T: \". Please change to the actual drive letter! ( mostly "c:\" )
- C:\msys\1.0\src --- (If no "msys" exists, create or use
"C:\mingw\src"). - C:\mingw\bin
Note:
I use for new projects in the msys-mingw environment always the "C:\msys\1.0\src" directory.
To have the same conditions, gcc should also be "4.7.0" not "4.6.2"
- Put at the front of your PATH "C:\mingw\bin;C:\msys\1.0\bin;"
As you've probably noticed, you can not use the libraries that come with OpenCV-2.4.2.exe.
Ignore so the folder "...\opencv\build\x86\mingw\bin" and "...\opencv\build\x86\mingw\lib" completely.
Let's get the new mingw files.
Start mingw-get-inst-20120426.exe
Use : Download latest repository ...
Make sure that the specified directory is correct, usually "C:\mingw".
Check as described below.
If you also need msys, then also select "MinGW Developer Toolkit".
Now MinGW-Get downloads all new files. ( 4.7.0 )
Run "OpenCV-2.4.2.exe".
The folder where the files go should be "c:\msys\1.0\src".
That writes all the files in the new directory "opencv".
The new folder : "C:\msys\1.0\src\opencv". From now on, this is our top-level directory
Open "cmake-2.8.9-win32-x86.zip" and copy the files to your cmake folder.
Creating the Makefiles
Run "C:\cmake\bin\cmake-gui.exe"
- A: Set the required fields, click "configure"
B: Set, click "next"
Set and click "ok"
A possible error: sh.exe was found in your PATH
- A: Error "sh.exe was found in your PATH"
B: Search for sh.exe in "C:\msys\1.0\bin" or "C:\mingw\bin" and
rename to "shxx.exe"click "configure" again.
A possible error: CMAKE_MAKE_PROGRAM is not set
- mark "CMAKE_MAKE_PROGRAM" and copy, click "Add Entry"
- Paste, Set and click "ok"
- click "configure" again.
Now it should work. Cmake searches the compiler environment
Cmake shows the option page
- Scroll through the red options. Don't change anything
- click "configure" again
Let's generate the Makefiles
- click "Generate"
Building openCV
- Run "cmd" go to the directory "C:\msys\1.0\src\opencv\build\mingw". There typing "make"
Please wait!
It takes about 4 hours on my computer
Check that all files are in the right place
- B: *.dll in "C:\msys\1.0\src\opencv\build\mingw\bin"
- C: *.dll.a in "C:\msys\1.0\src\opencv\build\mingw\lib"
Building an opencv program
- create a new folder "OpenCV-MakeC" in "C:\msys\1.0\src\opencv"
- create in the folder "OpenCV-MakeC" a "Makefile" and "imgdisplay.cpp".
To avoid copy and paste error. Here are the 2 files for download:
Makefile
MKDIR_P = C:/msys/1.0/bin/mkdir -p
DESTDIRB = build/Debug
DESTDIRD = dist/Debug
LIBDIRCV = ../build/mingw/lib
CFLAGS = -O2
CXX = C:/mingw/bin/g++.exe
Applic = imgdisplay
ALL: imgdisd
imgdisd:
$(CXX) $(CFLAGS) -c -g $(Applic).cpp -I../build/include -MMD -MP -MF $(DESTDIRB)/$(Applic).o.d -o $(DESTDIRB)/$(Applic).o
$(CXX) $(CFLAGS) -o $(DESTDIRD)/$(Applic) $(DESTDIRB)/$(Applic).o -L../build/mingw/bin $(LIBDIRCV)/libopencv_calib3d242.dll.a $(LIBDIRCV)/libopencv_contrib242.dll.a $(LIBDIRCV)/libopencv_core242.dll.a \
$(LIBDIRCV)/libopencv_features2d242.dll.a $(LIBDIRCV)/libopencv_flann242.dll.a $(LIBDIRCV)/libopencv_gpu242.dll.a $(LIBDIRCV)/libopencv_highgui242.dll.a $(LIBDIRCV)/libopencv_imgproc242.dll.a \
$(LIBDIRCV)/libopencv_legacy242.dll.a $(LIBDIRCV)/libopencv_ml242.dll.a $(LIBDIRCV)/libopencv_nonfree242.dll.a $(LIBDIRCV)/libopencv_objdetect242.dll.a $(LIBDIRCV)/libopencv_photo242.dll.a \
$(LIBDIRCV)/libopencv_stitching242.dll.a $(LIBDIRCV)/libopencv_ts242.a $(LIBDIRCV)/libopencv_video242.dll.a $(LIBDIRCV)/libopencv_videostab242.dll.a
md:
$(MKDIR_P) "$(DESTDIRB)"
$(MKDIR_P) "$(DESTDIRD)"
imgdisplay.cpp
/*
* File: imgdisplay.cpp
* Author: Administrator
*
* Created on 14. September 2012, 01:12
*/
#include <cstdlib>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int main( int argc, char* argv[] )
{
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
IplImage* img;
img = cvLoadImage("D:/grafik/Lightroom/light01.jpg");
if (!img) {
std::cout << "Could not open the image file" << std::endl;
return -1;
}
cvShowImage("My Picture", img);
cvWaitKey(0);
cvReleaseImage(&img);
return 0;
}
Netbeans 7.2 create a project
- File -- New Project
Use settings as described below.
Created Project
Create a new target
- Click with the right mouse button on "Makefile"
Note: If you get an error : Makefile:12: *** missing separator. Stop.
Mostly a copy and paste error ! (Make sure no spaces at the start lines. But 2 tab set).
Create new folder
- Click with the right mouse button on "Makefile"
- Use the new target "md"
The projects folder now looks like ..
Build the project
Debug the project
- Copy needed dll's ( C:\msys\1.0\src\opencv\build\mingw\bin to T:\msys\1.0\src\opencv\OpenCV-MakeC\dist\Debug)
- Open imgdisplay.cpp (double click)
Set breakpoint Line 17
17 cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
Click the Debug button
Program stops on Line 17
Unresolved Identifier
If the source of the .cpp file looks like this
Click with the right mouse button on your project.
Check C/C++ Code As...
Run Reparse Project.
If that is not enough.
Go to Project Properties
Fill in the Include
input field as described.
Clean Up
- If you renamed sh.exe to shxx.exe ! Rename it back !
- If you have spaces in your PATH Variable, put quotation marks around spaces (only around relevant programs like visual studio and so).
I know that they are not necessary but one or two programs can't handle the spaces
in the PATH !! - On a
Related Topics
Call a C Function from C++ Code
Is It Legal to Recurse into Main() in C++
Rotate an Image Without Cropping in Opencv in C++
Dividing Two Integers to Produce a Float Result
What Are Some Reasons a Release Build Would Run Differently Than a Debug Build
Creating an Input Stream from Constant Memory
Arrays VS Vectors: Introductory Similarities and Differences
Efficient Way of Storing Huffman Tree
Does Mingw-W64 Support Std::Thread Out of the Box When Using the Win32 Threading Model
What Is a Subnormal Floating Point Number
Difference Between Atan and Atan2 in C++
Constexpr If and Static_Assert
What Makes More Sense - Char* String or Char *String
Best Compiler Warning Level For C/C++ Compilers
How Does This Template Magic Determine Array Parameter Size