Code::Blocks is an GPL based and cross-platform IDE. This is the tutorials using Code::Blocks with OpenCV.
There are 2 different ways to configure OpenCV (shown below), but first you should create a new project:
Create a simple console project.
We can use project wizard to create a simple console project. Here is the steps
Give this project name of "test_opencv"
Then copy some sample code to main.cpp, such as the contents of "C:\OpenCV\samples\cpp\demhist.cpp"
Configuring Code::Blocks for OpenCV v2.4
Now you need to configure the compiler to find the OpenCV header files and libraries. There are 2 different ways you can configure Code::Blocks for OpenCV v2.4. OpenCV was made of just 4 libraries originally (until v2.1), but now there are many more library files, so you are highly recommended to use the tool "pkg-config" (as mentioned on CompileOpenCVUsingLinux), but if you are using MinGW on Windows then you might prefer the manual method.
Automatically with the pkg-config tool (easier with Linux or Mac).
Manually adding the OpenCV library (easier with MinGW on Windows).
Configuring Code::Blocks for OpenCV using pkg-config
pkg-config is a free command-line tool (available on Windows, Mac and Linux) that should have been automatically setup correctly if you built OpenCV with CMake. Open a command-line and enter pkg-config opencv --cflags or pkg-config opencv --libs, it will display the compiler and linker arguments to successfully compile your own OpenCV projects without worrying about where OpenCV is installed or worrying about which version you have installed, or how to link to the library in each Operating System, etc. If you get an error that it does not know what pkg-config is, then install pkg-config yourself (Linux or Mac can use the official release, but for Windows (MinGW) you should use the tool pkg-config-lite instead).
If you were compiling your project on the command line you could type:
cc `pkg-config opencv --cflags` -c main.cpp
cc `pkg-config opencv --libs` -o test_opencv main.o(Note: pkg-config is surrounded by back-tick characters, not quote or apostrophe characters (its usually the same key as the Tilda key ~, next to the 1 and Esc keys).
To setup Code::Blocks to use pkg-config, first you should right-click on your project and open the "Build options ..." dialog.
Now you can simply put this into "Linker settings -> Other linker options":
`pkg-config opencv --libs`
And put this into "Compiler-> Other options":
`pkg-config opencv --cflags`
(Remember to include the back-tick characters, by copy-pasting those lines directly into Code::Blocks).
The beauty of this method is that it should work for Linux, Windows and Mac, and for all versions of OpenCV, whereas the old manual method has different lib filenames for different Operating Systems and different versions of OpenCV.
Troubleshooting
If you have tried the pkg-config method above but Code::Blocks does not find OpenCV headers and libs but you have verified that pkg-config works for OpenCV on a command-line, then Code::Blocks probably doesn't know where pkg-config is installed. So you should place a link into "/usr/bin/" (Linux & Mac) or "C:\Windows\" (Windows) so that it will be found. For Linux or Mac, you can find the path to pkg-config if you type this into a terminal:
which pkg-config
This might print something like "/opt/local/bin/pkg-config".
So then type this in a terminal:
cd /usr/bin
ln -s /opt/local/bin/pkg-config pkg-configThis allows accessing pkg-config from the most common program folder "/usr/bin/" as well as it's actual location.
Configuring Code::Blocks for OpenCV v2.4 manually
Updated by Walker Xian (osce <at> yahoo.cn) on 21st July 2012
In this tutorial I will be using OpenCV v2.4.2 and Code Blocks v10.05 with GNU compiler (MinGW) on Windows 7. To work on OpenCV with Code Blocks you just need to add some OpenCV libraries and the folder with OpenCV header files. Following are some simple steps that I followed:
1. First of all I assume you have downloaded the OpenCV pre-built package and installed it on your system. (Not exactly install, just extract) http://sourceforge.net/projects/opencvlibrary/files/opencv-win/, I tested with v2.4.2.
2. Make sure the environment variable PATH is set. This could be done during the installation procedure itself. It will prompt you with this question. If you missed that then you have to manually add it to your system environment.
3. (These instructions assume you installed OpenCV to "C:\OpenCV", so adjust the paths if you installed somewhere else)。In Code::Blocks, goto the menu "Settings > Compiler and Debugger > Search Directories". Then goto "Add" and add the directory "C:\OpenCV\include\opencv". This will let your project find the OpenCV header include files when compiling (before linking):
4. In the Linker tab, add the directory "C:\OpenCV\build\x86\mingw\lib":
5. Now click on "Linker Settings". Add all the .lib files from "C:\OpenCV\build\x86\mingw\lib" (many files). This will let you project link to OpenCV libraries:
6. That's it!! Now you are ready to run your first OpenCV program. I tested a sample program "demhist" in the folder "C:\OpenCV\samples\cpp\demhist.cpp".
Alternative method, for OpenCV 2.2
In order to make OpenCV 2.2 (containing C++ code) work under Windows with Code::Blocks and MinGW, Matthew (mazeus12 on the mailing list) did the following:
As mentioned in "OpenCV-2.2.0-win-README.txt", "OpenCV-2.2.0-win32-vs2010.exe" does not contain binaries for MinGW so they need to be built from the contents in "OpenCV-2.2.0-win.zip".
Steps to build OpenCV 2.2 with Code::Blocks and MinGW:
1. Install Code::Blocks (10.05) with the (MinGW) C++ compiler option. This should among other install the C++ compiler and mingw32-make to "C:\Program Files\CodeBlocks\MinGW\bin" (I also tried to install the latest MinGW using "mingw-get install gcc g++ mingw32-make" from www.mingw.org but I got an error in extracting some files…)
2. Add "C:\Program Files\CodeBlocks\MinGW\bin" to system PATH (at your own judgment: remove any other paths to MinGW (Somehow DevCpp MinGW paths with probable different versions messed up the build process))
3. Install Cmake (2.8)
4. Extract "OpenCV-2.2.0-win.zip" to "C:\OpenCV-2.2.0-win" (It creates a second folder so the final destination looks like that: "C:\OpenCV-2.2.0-win\OpenCV-2.2.0")
5. Run Cmake (cmake-gui)
6. Set the source code: "C:\OpenCV-2.2.0-win\OpenCV-2.2.0"
7. Set where to build the binaries: e.g. "C:\OpenCV2.2MinGW"
8. Press configure
9. Let Cmake create the new folder
10. Specify the generator: MinGW Makefiles
11. Select "Specify native compilers" and click next
12. For C set: C:/Program Files/CodeBlocks/MinGW/bin/gcc.exe
13. For C++ set: C:/Program Files/CodeBlocks/MinGW/bin/g++.exe
14. Click finish
15. In the configuration screen type in "RELEASE" (or "DEBUG" if you want to build a debug version) for "CMAKE_BUILD_TYPE". Select BUILD_EXAMPLES if you want (I didn't change anything else here like "WITH_TBB" or "WITH_QT" etc. I'll try that when time comes to use TBB or Qt)
16. Click configure again
17. Click generate
18. Close cmake
19. Go to the command prompt and inside the folder "C:\OpenCV2.2MinGW" type "mingw32-make" and hit enter (takes some time)
20. Then type "mingw32-make install" and hit enter again
21. Open Code::Blocks and create a new C++ project (Configuration similar to the guide: CodeBlocks).
22. In menu: "Project/Build options/Linker settings/Link libraries" add "C:\OpenCV2.2MinGW\lib\libopencv_calib3d220.dll.a" and all the other *.dll.a files in this folder
23. In menu: "Project/Build options/Search directories/Compiler" add "C:\OpenCV2.2MinGW\include" (includes in a new program schould look like this: "#include <opencv2/core/core.hpp>, #include <opencv2/imgproc/imgproc.hpp>, #include <opencv2/highgui/highgui.hpp> etc.")
24. In menu: "Project/Build options/Search directories/Linker" add "C:\OpenCV2.2MinGW\lib" The options in steps 22 - 24 can also be added as global options in menu: Settings/Compiler an Debugger/Global compiler settings/..., so they will apply to any project and opened *.cpp file.
25. If necessary, in menu: Settings/Compiler an Debugger/Global compiler settings/Toolchain executables" specify "C:\Program Files\CodeBlocks\MinGW" for the compiler's installation directory.
26. Open a sample file (or import it into the project), e.g. "C:\OpenCV2.2MinGW\samples\cpp\dft.cpp" and built it. (If the *.cpp files do not exist in this folder, you can find them in the initial folder where you extracted "OpenCV-2.2.0-win.zip" i.e. "C:\OpenCV-2.2.0-win\OpenCV-2.2.0\samples\cpp")
27. Add "C:\OpenCV2.2MinGW\bin" to the system path
28. Run the program
Configuring Code::Blocks using old method for OpenCV v1.1
Set the include header file path (OpenCV v1.1):
Set the library path (OpenCV v1.1):
Add the libraries (OpenCV v1.1) directive:
Here is another way you can add the include path and lib path in Code::Blocks
First, you need to add a global variable in Codeblocks, see here: You can open this dialog in:
Codeblocks Menu->settings->global variables, than add a variable named "cv", and choose the "base path" of the "cv"
Then you can define the #cv.include and # cv.lib path, in my system,
fill the include edit bar with(this is where your opencv include path locates) : $(#cv)\OpenCV-2.1.0\include\opencv
fill the lib path with(this is where your opencv libraries locate) : $(#cv)\opencv_build\lib
Later, in your Opencv project, you can change the build options like below:
Also, you can add the libraries by using these linker options:
-lcxcore210 -lhighgui210 -lcv210
Build and Setting input image
Run the build program
Other links
A Chinese version (中文) can be found here:http://www.opencv.org.cn/index.php/Codeblocks_MinGW_openCV