During our computer vision course this semester we were strictly required to use OpenCV version 1.1 in our programming assignments. Since this is a very outdated version, there are some problems with it especially with the newer ubuntu versions. Newer OpenCV versions such as 2.x can be easily installed from the repositories without any problems. I have no clue about the exact differences between OpenCV2.X and OpenCV1.1 and whether or not it is that necessary to use the latter outdated version in order to mach the university setups or not. That's why I didn't go with OpenCV2.x which is the current mainstream version.
After some Googling and intense hacking around. I managed to hack my way through all difficulties and error messages and finally install OpenCV 1.1 on my 10.10 Ubuntu maverick.
Therefore, in this tutorial I will guide you in details through compiling OpenCV 1.1 from source and getting it to work on Ubuntu 10.04 and higher as well as configuring it to work with eclipse.
Note: If you have any version of OpenCV previously installed, make sure you completely purge it along with it's configuration files and libraries before you proceed with this tutorial. Two OpenCVs at once = Doom's day. You have been warned.
After some Googling and intense hacking around. I managed to hack my way through all difficulties and error messages and finally install OpenCV 1.1 on my 10.10 Ubuntu maverick.
Therefore, in this tutorial I will guide you in details through compiling OpenCV 1.1 from source and getting it to work on Ubuntu 10.04 and higher as well as configuring it to work with eclipse.
Note: If you have any version of OpenCV previously installed, make sure you completely purge it along with it's configuration files and libraries before you proceed with this tutorial. Two OpenCVs at once = Doom's day. You have been warned.
Part 1: Installing OpenCV 1.1
1. OpenCV depends on some packages as well as dev libraries, so let's install these (Make sure you have the 'universe' and 'multiuniverse' repositories enabled in your ubuntu software sources):
1
2
| # OpenCV dependencies sudo apt-get install gcc g++ pkg-config libgtk2.0-dev libjpeg62-dev libtiff4-dev libjasper-dev libpng12-dev zlib1g-dev build-essential |
There are some other optional dependencies you might wanna install depending on what u r going to be doing using OpenCV such as (openexr for HDR images, ffmpeg and libgstreamer for videos, libv4l for webcam support, ... etc). Note: Some of these might cause some bugs if u follow the same instructions in this tutorial as u would need to pass extra parameters to the ./configure script to generate the make file.
2. Download the OpenCV 1.1 source tarball from here somewhere you know. Say in /home/user_name/Downloads. Where 'user_name' is your ubuntu user name.
3. Extract the content of the downloaded file.
2. Download the OpenCV 1.1 source tarball from here somewhere you know. Say in /home/user_name/Downloads. Where 'user_name' is your ubuntu user name.
3. Extract the content of the downloaded file.
1
2
3
4
| # Navigate to where you downloaded it. cd ~ /Downloads # Extract in Current dir. tar -zxvf opencv-1.1pre1. tar .gz |
4. There is a certain C header file that will cause a problem if you try to './configure' due to a declaration error in the code. Let's fix that:
1
| gedit ~ /Downloads/opencv-1 .1.0 /cxcore/include/cxmisc .h |
At line 133, change "#elif" to "#else" then save like the screenshot below:
5. Now, lets configure and generate the make file that we are going to build:
1
2
3
4
5
6
| # Gain root privilege. sudo -s # Navigate to OpenCV extracted dir cd ~ /Downloads/opencv-1 .1.0/ # Configure . /configure --disable-apps --without-v4l --with-gtk |
Basically the parameters we are passing to the ./configure disables adding the sample apps path in the generated make file (so they do not take time in building), --without-v4l to disable 'video4linux' support which is basically web-cam support in opencv as there is some buggy code in that part of the configure script and finally --with-gtk to force using gtk window management library. (necessary by functions such as cvNamedWindow(..) and such)
6. After the configure script (step 5) finishes, we need to build the source package using the generated make file:
6. After the configure script (step 5) finishes, we need to build the source package using the generated make file:
1
2
| # Make sure you are still root and inside the opencv dir! make |
The last line of the output of that command should be something like:
Leaving directory `/home/user_name/Downloads/opencv-1.1.0' where user_name is your ubuntu user name. Otherwise, know that the make command did not succeed and failed somewhere in the process.
7. Next, we need to install:
Leaving directory `/home/user_name/Downloads/opencv-1.1.0' where user_name is your ubuntu user name. Otherwise, know that the make command did not succeed and failed somewhere in the process.
7. Next, we need to install:
1
2
| # Make sure you are still root and inside the opencv dir! make install |
Also, The last line of the output of that command should be something like:
Leaving directory `/home/user_name/Downloads/opencv-1.1.0' where user_name is your ubuntu user name. Otherwise, know that the installation did not succeed and failed somewhere in the process.
8. After that, we need to configure opencv with ld such that it "feels" the new libraries:
Leaving directory `/home/user_name/Downloads/opencv-1.1.0' where user_name is your ubuntu user name. Otherwise, know that the installation did not succeed and failed somewhere in the process.
8. After that, we need to configure opencv with ld such that it "feels" the new libraries:
1
2
| # create/edit opencv.conf sudo nano /etc/ld .so.conf.d /opencv .conf |
Add "/usr/local/lib" without the quotes in a new line then press CTRL+o in order to save your changes and create the file if it's not there.
9. Then, we need to poke ldconfig =D. So:
9. Then, we need to poke ldconfig =D. So:
1
2
| # wake that lazy bitch up! sudo ldconfig |
10. Finally, we need to add the pkgconfig system path:
1
| sudo gedit /etc/bash .bashrc |
Then append the following lines to the end of it and save:
1
2
3
| # Added during OpenCV installation PKG_CONFIG_PATH=$PKG_CONFIG_PATH: /usr/local/lib/pkgconfig export PKG_CONFIG_PATH |
11. Logout the log back in.
12. Now, we need to make sure that the libraries and and paths are in-place and we are set to go:
12. Now, we need to make sure that the libraries and and paths are in-place and we are set to go:
1
2
3
4
| # Verify includes pkg-config --cflags opencv # Verify libraries pkg-config --libs opencv |
The output of these two command should be:
-I/usr/local/include/opencv
and
-L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml
Respectively.
13. Now if it all went okay then, it's time to test some code and see some output. So, download the following file into say your /home/user_name/Downloads directory. where user_name is ur ubuntu user name.
-I/usr/local/include/opencv
and
-L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml
Respectively.
13. Now if it all went okay then, it's time to test some code and see some output. So, download the following file into say your /home/user_name/Downloads directory. where user_name is ur ubuntu user name.
Then compile and run it by typing the following commands:
1
2
3
4
5
6
| # Navigate to Download dir cd ~ /Downloads # Build binary g++ -I /usr/local/include/opencv -L /usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml hello.cpp # Run it . /a .out /path/to/an/image .jpg |
Where /path/to/an/image.jpg is a jpg image which you know it's location.
If it all goes well, you should see "Hello" printed in the terminal and a window containing your image poping-up and showing the gray-scale version of it like the screenshot below.
If it all goes well, you should see "Hello" printed in the terminal and a window containing your image poping-up and showing the gray-scale version of it like the screenshot below.
Part 2: Configuring Eclipse with OpenCV
a. Download the linux version of Eclipse for C/C++ developers from here. Extract the downloaded file and run the file named "Eclipse" from inside the extracted folder in order to launch eclipse.
b. File -> New -> C++ Project -> Enter a name -> Finish.
c. Right click on created project folder -> new -> Source File -> hello.cpp -> finish. Then, copy and paste the code that you downloaded in step 13.
d. Right click on project folder -> properties -> C/C++ build -> settings -> G++ compiler -> includes. Then in the include paths section (-I) click the tiny add button which has a green + sign on it and add the following path: /usr/local/include/opencv Like the screenshot below:
b. File -> New -> C++ Project -> Enter a name -> Finish.
c. Right click on created project folder -> new -> Source File -> hello.cpp -> finish. Then, copy and paste the code that you downloaded in step 13.
d. Right click on project folder -> properties -> C/C++ build -> settings -> G++ compiler -> includes. Then in the include paths section (-I) click the tiny add button which has a green + sign on it and add the following path: /usr/local/include/opencv Like the screenshot below:
e. From the same window as in (d), navigate to G++ Linker -> Libraries and add libraries (-l) and library search paths (-L) as in the following screenshot:
f. Modify the 1st two lines of code as follows:#include <cv.h> --> #include <opencv/cv.h>
#include <highgui.h> --> #include <opencv/highgui.h>
g. Right click -> refresh your project. There shouldn't be any warnings or errors.
h. Build then Run using the eclipse toolbar. You will see this output displayed in console:
Hello
Enter filename
File not found