ubuntu编译安装Opencv

来源:互联网 发布:python 支持向量机 编辑:程序博客网 时间:2024/05/21 16:23

直接安装opencv库

sudo apt-get install libopencv-dev

如果需要从源码安装,步骤如下:


Install Dependencies


Essentials
These are libraries and tools required by OpenCV.
sudo apt-get install build-essential checkinstall cmake pkg-config yasm

Image I/O
Libraries for reading and writing various image types. If you do not install then the versions supplied by OpenCV will be used.
sudo apt-get install libtiff4-dev libjpeg-dev libjasper-dev

Video I/O
You need some or all of these packages to add video capturing/encoding/decoding capabilities to the highgui module.
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev

Python
Packages needed to build the Python wrappers.
sudo apt-get install python-dev python-numpy

Other third-party libraries
Install Intel TBB to enable parallel code in OpenCV.
sudo apt-get install libtbb-dev

GUI
The default back-end for highgui in Linux is GTK. You can optionally install QT instead of GTK and later enable it in the configuration (see next section). 
sudo apt-get install libqt4-dev libgtk2.0-dev

Compile and Install

Get a copy of the source code here, extract and create a build directory:
tar -xvf OpenCV-2.4.0.tar.bz2cd OpenCV-2.4.0/mkdir buildcd build
Configure using CMake. You have a lot of options in this step. This is what I use: 
cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
Notice, that by adding the -D WITH_QT=ON, the highgui module will use QT instead of GTK. If you want to go with GTK just remove this. For more information on the options, look at the CMakeLists.txt file. When you are happy with the configuration you have, you can start compiling:
make
If compilation finishes without errors, you can install by saying: 
sudo make install
Finally, make sure that your programs can link to the OpenCV library in run-time by adding the following line at the end of your /etc/ld.so.conf
/usr/local/lib
And then configure dynamic linker run-time bindings:
sudo ldconfig

Testing

An easy way to test that the compilation went well is to use the OpenCV test utilities. For example, to test the core module go to OpenCV-2.4.0/build/bin and run:
 ./opencv_test_core
You should see something like that:


Note that some failures when testing other modules may come from missing image files. To correctly run these tests or samples you should move the corresponding image files from OpenCV-2.4.0/samples to OpenCV-2.4.0/build/bin.

For testing that you can compile your own programs and link against the installed OpenCV libraries I have packaged the face detection sample with all the necessary files and a simple Makefile. Download it here, extract and type:
make
This should compile and run with a test image, so you should see something like this:

0 0