Introduction
Hello and welcome to my blog! A lot of the content I'll be adding here in the future will involve the OpenCV library, so I thought what better a way to begin than with a handy installation guide for setting it up on your Linux (Ubuntu) system.
This guide will be split into three sections as follows:
- Install all prerequisite libraries
- Compile and install FFMPEG
- Compile and install OpenCV
So, let's begin.
1 - Install all prerequisite libraries
First of all it's a good idea to update your package lists. You can do this by opening a terminal and simply typing the following:
sudo apt-get update
Next, install Synaptic Package Manager from the Ubuntu Software Centre (or your distribution's equivalent). Synaptic will automatically add a package's dependencies and the dependencies of those dependencies (and so on) to your system, which can massively simplify the installation process.
To mark a package for installation simply search for its name in the "Quick filter" box and click the box to the left of the relevant search result, choosing "Mark for Installation" as shown below:
(Click to Enlarge) - Searching for packages using Synaptic |
- build-essential For building debian packages
- cmake For generating configured makefiles
- libeigen3-dev C/C++ lib for matrix/vector maths
- libfaac-dev Audio encoder
- libgtk3-dev For building GUI interfaces
- libjasper-dev For general image manipulation
- libjpeg62-dev For handling JPEG images
- libopencore-amrnb-dev Speech codec - Narrow band
- libopencore-amrwb-dev Speech codec - Wide band
- libopenexr-dev For handling EXR images
- libtbb-dev Multi-core performance lib
- libtheora-dev For handling Ogg Theora videos
- libtiff5-dev For handling TIFF images
- libvorbis-dev For handling Ogg Vorbis audio
- libxvidopencore-dev Video codec
- libx264-dev Video codec
- python3.3-dev For building Python modules etc
- python-numpy Extras for Python
- yasm Assembler
Note that you can get away with not installing some of these if you know you're not going to be using them with OpenCV / FFMPEG, it's your call. This also goes the other way - If you know you're going to be using a file format or codec that hasn't been listed above, you'll want to search for its development package (the -dev suffix) and install it.
Once you've marked all of the above files for installation, click the "Apply" button and then expand the "To be Installed" drop-down that appears. Check that all of the above packages are on the list (there will be a lot more than just them - those are the dependencies). I've done the list in alphabetical order to make it a bit easier - If you've missed one simply click cancel, mark that package for installation and then click "Apply" again.
After checking that all of the packages are on the list, confirm that you wish to install them.
2 - Compile and Install FFMPEG
Now for FFMPEG. At the time of writing, FFMPEG is on version 1.1 - To get the latest version go here and scroll to the bottom, downloading the corresponding .tar.bz2 file. Alternatively to get the same version as me type the following into a Terminal window:
cd ~
wget ffmpeg.org/releases/ffmpeg-1.1.tar.bz2
tar -xvf ffmpeg-1.1.tar.bz2
cd ffmpeg-1.1
If you downloaded a different version you'll need to extract it yourself and navigate to the created folder. If you didn't install some of the prerequisites or added some extra file formats and codecs yourself you should now type this and look for the relevant options to toggle:
./configure --help
However, if you did the same prerequisites as me you can simply add each of the following options:
./configure --enable-gpl --enable-version3 --enable-nonfree
--enable-shared --enable-x11grab --enable-libfaac
--enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libtheora --enable-libvorbis --enable-libx264
--enable-libxvid
Regardless, the most important option here is the --enable-shared - If you don't have this enabled, OpenCV will not be able to use FFMPEG for opening and closing video files! You can check that FFMPEG will be shared by scrolling up after running the configuration script and ensuring it says "shared yes" as shown below:
(Click to Enlarge) - Ensure FFMPEG is shared |
With FFMPEG correctly configured, type this into the Terminal:
make
sudo make install
Assuming there were no errors FFMPEG has now been installed. If something went wrong, check you've set the ./configure options correctly, especially if you added/removed some of them as discussed earlier.
3 - Compile and Install OpenCV
Finally it's time to install OpenCV. As with before there may be a newer version available than what I'm using (2.4.3) - You can get the latest version here. Alternatively to get the same version as me type the following into a Terminal window:
cd ~
wget sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2
tar -xvf OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3
Once inside the extracted archive you'll want to create a new folder that will contain the compiled code:
mkdir build
cd build
Now to configure OpenCV for installation - You may have to tailor these options to suit your needs (removing -D INSTALL_PYTHON_EXAMPLES=ON if you're not interested in using OpenCV with Python, etc). Type this:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON
-D WITH_TBB=ON -D WITH_FFMPEG=ON ..
Note the .. at the end of that command. Also, if you want to build OpenCV to have OpenCL support (assuming you've already installed OpenCL) then you should add the option -D WITH_OPENCL=ON after -D WITH_FFMPEG=ON.
Check the output of cmake to ensure your options have been set correctly (notably that the entry FFMPEG says YES) as shown below:
(Click to Enlarge) - Ensure FFMPEG is being used |
If everything looks OK, type the following:
make
sudo make install
Once OpenCV has been installed there's still a few things left to do. Firstly, you'll need to tell the system where to find the OpenCV library:
sudo nano /etc/ld.so.conf.d/opencv.conf
With the file open (it might be empty, don't worry) add the following line and save: (press Ctrl-O to save, hit enter to confirm the file name and then hit Ctrl-X to exit)
/usr/local/lib
And finally configure the shared libraries:
sudo ldconfig
Now you'll need to restart your machine. Once you're back open a Terminal window and type the following:
echo $(pkg-config opencv --cflags --libs)
You should see something like this:
(Click to Enlarge) - Make sure you get something similar |
If you do, you've now successfully installed OpenCV with FFMPEG support! If not you'll want to reconfigure, remake and reinstall OpenCV before checking again. You won't be able to compile your code properly until you see an output like above.
In order to compile your code, you'll want to use that same string as a compiler argument. For example, to compile the file main.cpp that includes the file #include <cv.h> you would type the following into a Terminal window:
g++ -o EXAMPLE main.cpp $(pkg-config opencv --cflags --libs)
That concludes this guide - Thanks for reading and feel free to leave a comment if you need anything clarifying or explaining.
No comments:
Post a Comment