/* Installing OpenCV 2.3 in Ubuntu.I had a lot of problems initially to install and port the OpenCV library to Python. But after reading a lot of blogs I finally found a solution which I have posted below. */
1)Install all pre requisites
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install pkg-config
sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3
sudo apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2
sudo apt-get install zlib1g-dbg zlib1g zlib1g-dev
sudo apt-get install libjasper-dev libjasper-runtime libjasper1
sudo apt-get install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog
sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev
sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev
sudo apt-get install libxine1-ffmpeg libxine-dev libxine1-bin
sudo apt-get install libunicap2 libunicap2-dev
sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils
sudo apt-get install swig
sudo apt-get install libv4l-0 libv4l-dev
sudo apt-get install python-numpy
sudo apt-get install build-essential libgtk2.0-dev libjpeg62-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
2)Install the python development headers
sudo apt-get install python-dev
3)Download the source code:
4)Go to the directory where OpenCV is downloaded via the terminal only and then un zip the package:
(note:- It is recommended that you move the downloaded OpenCV package to the home/ directory
tar -xvf OpenCV-2.3.1a.tar.bz2
5)Now make a new directory called build and go in to it
mkdir build & cd build
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
8) Make it permanent
sudo make install
9)Configuring OpenCV for using shared libraries:
sudo gedit /etc/ld.so.conf.d/opencv.conf
Add the following line at the end of the file (it may be an empty file, that is ok) and then save it:
Close the file and run the following command to configure the library:
OR RUN : export LD_LIBRARY_PATH=/usr/local/lib
10)Open your .bashrc file and add the following:
sudo gedit /etc/bash.bashrc
add the following at the end of the file -
sudo gedit /etc/bash.bashrc
add the following at the end of the file -
Save and close the file
11) Reboot the system
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
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-liteinstead).
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.
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:
This might print something like “/opt/local/bin/pkg-config”.
So then type this in a terminal:
ln -s /opt/local/bin/pkg-config pkg-config
This 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
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:
2. 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
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 “kutayzorlu” in the folder “C:\OpenCV\samples\cpp\kutayzorlu.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 (SomehowDevCpp 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 , #include , #include 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