OpenCV Install on Windows With Code::Blocks and minGW

** Disclaimer **
Much to my dismay this tutorial is by far the most visited page on my website, why does this upset me? It upsets me because as glad as I am to see people using open source tools like OpenCV and MinGW rather than proprietary or commercial alternatives I feel strongly that developers should be using Linux not Windows for coding, especially for C++.

Why should you use Linux? There’s a lot of reasons in my opinion but right now I am going to keep it simple. It will make you a better coder. period. Most people I know barely understand setting up their own C++ projects and linking to 3rd party libraries etc. and using Linux is the best way to see and learn how this works. I also personally recommend staying away from IDEs.

Also Linux is quite often the first priority for developers of open source tools and windows support is sometimes an after thought. You’re obviously interested in open source or you wouldn’t be here – so I’m telling you to take the plunge, go all in, close this tab and grab an image of Ubuntu (or Mint if you want to be just like me :p ) and become enlightened!

I’ll even go one step further and link some tutorials I use to install OpenCV on Linux and a link to my OpenCV project makefile.

http://jayrambhia.wordpress.com/2012/06/20/install-opencv-2-4-in-ubuntu-12-04-precise-pangolin/
http://www.ozbotz.org/opencv-installation/
http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/

https://gist.github.com/pickle27/5311609

** Update **
I’ve been talking to the OpenCV devs about some of the issues people (and me) have been having with the latest pre-built binaries, what you need to know is they are discontinuing pre-built binaries for MinGW. From now on you will have to build your own, I have included instructions for how to make your own binaries and its pretty straight forward. I still prefer MinGW to other compilers on windows (well actually I prefer Linux, see above) and I hope this tutorial will continue to be useful.

Step 1: Install minGW
MinGW is a c/c++ compiler for windows, head to their website and download the latest version (right at the top where it says “looking or the latest version?”) http://sourceforge.net/projects/mingw/files/

Install to the default location C:\MinGW
From the options install mingw32-base and mingw32-gcc-g++, you can also install the other components if you wish, but all you need is the c++ compiler (g++).

Step 2: Add minGW to system path
Navigate to Control Panel -> System -> Advanced System Settings
and then:

add_env_var

mingw_to_path

Type a semi colon after the last entry in “path” and then paste your MinGW path (it should be C:\MinGW\bin if you chose the default location).

Afterwords open up a command prompt and type “path” to make sure it worked (you should see minGW somewhere in the print out, probably near or at the end).
Programs will need to be restarted for this change to take effect.

Step 3: Install Code::Blocks
Code::Blocks is an IDE (integrated development environment). Head to their website and download the latest version (codeblocks-10.05-setup.exe) http://www.codeblocks.org/downloads/binaries

Install it to the default location

When the installer finished click yes to run Code::Blocks
then go to Settings -> Compiler and Debugger
Under the Toolchain Executables select GNU GCC Compiler from the drop down and then press AutoDetect
verify that Code::Blocks has found MinGW

If you like now might be a good time to test your Code::Blocks and MinGW setup with a simple Hello World C++ program.

Step 4: Install OpenCV
OpenCV is a library of Computer Vision functions. Head to their website and download the latest version (2.4.2 for Windows) http://opencv.org/downloads.html

Click on the OpenCV-2.4.2.exe and choose C:\ as the extract directory

opencv

OpenCV is now installed – but not configured with Code::Blocks

** Update **
If this is your first time through the tutorial doing a clean install then skip this step first and see if the supplied pre-built binaries will work for you, if you’ve already tried and had issues or if you really want to build your own then continue with this section.

First you’ll need to download and install cmake

Open cmake and select C:\opencv as the source directory and C:\opencv\build\x86\mingw as the directory to build the binaries (you could select any directory but choosing this one will overwrite the pre-built OpenCV binaries and then the
rest of the tutorial is the same. Click configure choose minGW makefiles wait and then click generate.

cmake_config

When cmake is done we need to open a command prompt in the build directory, so navigate to C:\opencv\build\x86\mingw then shift right click and choose open command window here then type “mingw32-make”. Mingw will now start compiling OpenCV, this will take a bit so feel free to do something else, when you come back type “mingw32-make install” and continue with the rest of the tutorial as is.

Step 5: Add OpenCV to the system path
C:\opencv\build\x86\mingw\bin (use the same process as above)

opencv_path

Note: Add x86 binaries regardless of your system type (32bit or 64bit) because minGW is 32bit.

verifiy

Verify that both MinGW and OpenCV are in your system path Make sure you restart Code::Blocks before continuing if you have it open.

Step 6: Configuring Code::Blocks with OpenCV
Make a new Code::Blocks Project:

codeblocks1

right click on your project and choose build options:

codeblocks2

You can also change the global compiler settings from the menu bar at the top right.

codeblocks3

codeblocks4

Again Note – we are using 32-bit binaries even though the system is 64-bit because the compiler is 32-bit.

codeblocks5

Now run this simple OpenCV “Hello World” program to test that the install has worked.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main()
{
    Mat image;// new blank image
    image = cv::imread("test.png", 0);// read the file
    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// create a window for display.
    imshow( "Display window", image );// show our image inside it.
    waitKey(0);// wait for a keystroke in the window
    return 0;
}

Download any image you want, rename it “test.png” or hard code its name and place it in the top of the project directory.

Note – if you run your .exe from outside of code blocks the image needs to be in the same directory as the .exe.

As I mentioned earlier you can also configure OpenCV using the global compiler and debugger settings and the steps are the same, this means that every new project is ready to go with OpenCV. You can also choose file -> save project as template, This allows you to choose the option new from template and avoid the configuration each time.

102 comments

  1. thanks a lot kevin!
    after dozens of websites n hours of waste, your guide came to rescue..

    amazing set of guidelines!
    thanx again.

  2. Many thanks, it works … without pain. All other method on the web seems forget “mingw32-make” and “mingw32-make install” steps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s