- Download the OpenCV source code from its Github repository. I prefer to download the releases, which are more stable compared to the main branch.
Create a build directory:
$ mkdir build $ cd build
- Configure the features you want OpenCV to be compiled with. I prefer the CMake GUI for this:
$ cmake-gui ..
Click Configure. Go down the list and look for the items named
WITH_*. Each of these is a feature that can be compiled into OpenCV. Choose what you would like and uncheck those not needed.
BUILD_*items: These are related to external libraries. If you enable one of them, during the build, the source code of that external library will be downloaded and built. You typically do not want to do this, since you can install the pre-built library directly by using
apt-get. For example, instead of using
BUILD_JPEG, you can just install the
libjpeg-devpackage from Ubuntu repositories.
BUILD_opencv_*items: These are the features of OpenCV. Choose which of these features you need for your work. These features might be interdependent and it is your responsibility to ensure that all the dependent features are also enabled. You can see if the dependencies for the feature you need were met in the output you get after pressing the Configure button.
WITH_*items: These are typically related to external libraries. This is different from
BUILD_*items, since choosing this indicates that you want OpenCV to link with this external library. For example, choosing
WITH_TBBlinks with the Intel TBB library and certain OpenCV features compute faster due to the added parallelism available from Intel TBB.
After you have chosen the features you need, click Configure again. Examine the output at the bottom and if you are satisfied, click Generate. This generates the Makefile.
Build OpenCV from the Makefile:
The build can take a long time, so you might want to run parallel builds:
$ make -j
- If you find that a feature you want is not being built, then check the output that is at the bottom of the CMake GUI after you press Configure.
Ensure there is nothing displayed in red color. If there is, note down the header file or library that is required and install those packages. Also ensure that all the options you chose have been chosen for build. Some of the features are inter-dependent and may not be built if their dependencies are not chosen.
Another technique is to go to the subdirectory containing the feature and look at the
CMakeLists.txt file there. Look for the text
REQUIRED_DEPS that shows what other OpenCV features need to be present for this feature to be built. Ensure those features are also chosen.
- To install OpenCV that you have built:
$ sudo make install
Tried with: OpenCV 2.4.9 and Ubuntu 14.04