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.

126 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.

  3. Hi.Thanks for this. I have some questions.

    – I dont understand your;

    “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.” part. What is that mean?I try open cmd and some command. Like “gcc C:\Opencv\opencv\build\x86\mingw” but return error ( http://postimg.org/image/rnqpzazvt/ ).

    I tried some combination. And i finish every step.When i compile your simple projects code, it telling me missing dll file. It said that the libopencv_core243.dll file is missing.

    What should i do?

    1. I have the same problem (Codeblocks 13.12, OpenCV 2.4.9, Windows 7) I have searched my computer for the file “libopencv_core249.dll (my error is with 249 not 243 but this is because of the version I think). the file opencv_core249.dll DOES exist in my lib folder. it seems like somewhere in the code there is a missing “\” between lib and opencv when the dll file is referenced. I looked around in some of the header files to see if I could figure out where the reference was but nothing ( I didn’t try very hard). sorry I don’t have a solution, I feel you pain. Any help with this issue?

  4. Hi.Thanks for this. I have some questions.

    – I dont understand your;

    “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.” part. What is that mean?I try open cmd and some command. Like “gcc C:\Opencv\opencv\build\x86\mingw” but return error ( http://postimg.org/image/rnqpzazvt/ ).

    I tried some combination. And i finish every step.When i compile your simple projects code, it telling me missing dll file. It said that the libopencv_core243.dll file is missing.

    What should i do?

    1. I think you are either missing mingw or you didn’t add it to the path. Plus you need to type mingw32-make and not gcc

  5. I’ve been having this error and I can’t find a way to fix it:

    “The procedure entry point __gxx_personality_v0 could not be located in the dynamic link library D:\opencv\build\x86\mingw\bin\libopencv_core249.dll.”

    I’ve looked everywhere for a solution and I need this working soon. I hope you can help me.

    1. hi,
      so pleaze, did you solve this problem ?! coz, actually, i’m facing it right now, and i don’t really know how to fix it :/
      i would be more than grateful of you can answer me asap..
      thank you in advance

  6. I get this weird error and I can’t find a way to fix it. I looks alike something that other people had.

    “The procedure entry point __gxx_personality_v0 could not be located in the dynamic link library D:\opencv\build\x86\mingw\bin\libopencv_core249.dll.”

    I don’t know how to fix it and I need this working soon. I hope you can help me.

  7. huh don’t know what to tell you. Worked for me last time I did it (note that I don’t recommend doing this on windows for several reasons) Try rebuilding them?

  8. Pingback: Anonymous
  9. CMake Error: The source directory “C:/Libs/opencv” does not appear to contain CMakeLists.txt. When clicking configure. Maybe you should address this?

  10. I am getting the following during making mingw32-make.

    [ 44% ] mingw32-make[2]: *** Access is denied.
    . Stop.
    mingw32-make[2]: *** Waiting for unfinished jobs…

    What could be the problem?
    Thanks.

    1. Try making it again.The same thing happened to my friend and it worked.
      Incidentally his name is the same as yours. :D

  11. So this is a two part question. The first part being how do you add the picture to the top of the file directories in the sample script/ I tried just creating a folder for it and just doing “C:\test”. The issues is with both approachs I get “an unexpected error has caused “name_of_script.exe” to stop working.” I checked and found that I have everything linked right so it leads me to the conculsion of it being an error in finding the test.png photo.

    1. If I recall correctly images go in the Code Blocks project root. To determine if this is the issue or if it is indeed linking you can do 2 things – first just put the image all over your code blocks project directory and see if it works or write a new hello world program that doesn’t load from file but instead does something like creates an identity matrix and prints it out. Good luck!

  12. Thanks for the guidance. Program is compiled successfully but is having a message on my windows

    Process returned -1073741634 (0xC00000BE) execution time : 4.602 s
    Press any key to continue.

  13. hey.bro I followed exactly same steps you have mentioned.My codeblocks is compiling open cv programs properly but It shows a entry point error missing libstdc++-6.dll missing.
    And also I can’t create project It shows project directory can’t be created.

    Reply soon I’m really worried.

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