首页 > 代码库 > 安装 openCV 2.4.10

安装 openCV 2.4.10

近期试验了一下 ubuntu 12.06 (x86) 安装。openCV 

安装脚本

最好的文章是 https://help.ubuntu.com/community/OpenCV。 它提供一个脚本(opencv.sh) 做了全部的安装工作

version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)"
echo "Installing OpenCV" $version
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -qq remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg
echo "Downloading OpenCV" $version
wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
echo "Installing OpenCV" $version
unzip OpenCV-$version.zip
cd opencv-$version
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j2
sudo checkinstall
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV" $version "ready to be used"

略微阅读脚本。就知道它做的事情, 有以下几点值得关于

1)它没实用 make install, 而是用 checkinstall

CheckInstall keeps track of all files installed by a "make install" or equivalent, creates a Slackware, RPM, or Debian package with those files, and adds it to the installed packages database, allowing for easy package removal or distribution.

CheckInstall 是一款优秀的源代码安装软件。它使得 Linux 软件管理更加方便。特别是在源代码须要常常反复编译的情况下。CheckInstall 能够让你丝毫不破坏系统一致性的前提下全然的卸载程序。并且。你还能够使用这些编译好的安装包直接在其它的机器上安装无须再又一次编译。


2) 没有设置 LD_LIBRARY_PATH, 而是用 ldconfig

ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需执行动态链接 库的管理命令--ldconfig ldconfig 命令的用途,主要是在默认搜寻文件夹(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的文件夹下,搜索出可共享的动态 链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默觉得 /etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.


编译错误

我在编译中遇到一个错(可能x86才有)

http://git.buildroot.net/buildroot/tree/package/opencv/opencv-0001-core-fix-x86-PIC-code-compilation.patch?id=2b581cc04f3b106928f3d0c3b7fb6c44e50c8a61


使用 opencv

python 的样例

from cv2.cv import *

img = LoadImage("/home/USER/Pictures/python.jpg")
NamedWindow("opencv")
ShowImage("opencv",img)
WaitKey(0)

C++ 样例

#include<opencv2/highgui/highgui.hpp>
using namespace cv;

int main()
{

    Mat img = imread("/home/USER/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR);
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

编译命令

$ g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`
$ ./opencvtest

假设直接写 Makefile 文件

CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`

% : %.cpp
        g++ $(CFLAGS) $(LIBS) -o $@ $<

( ref: http://stackoverflow.com/questions/9870297/makefile-to-compile-opencv-code-in-c-on-ubuntu-linux) 

这也说明 pkg-config 命令的使用方法

安装 openCV 2.4.10