OpenCV的Ubuntu14配置

You can install OpenCV from the Ubuntu or Debian repository,
INSTALL OPENCV FROM THE UBUNTU OR DEBIAN REPOSITORY
$ sudo apt-get install libopencv-dev python-opencv
However, you will probably not have installed the latest version of OpenCV and you may miss some features (for example: Python 3 bindings do not exist in the repository).

SO, BETTER IS INSTALL OPENCV FROM THE OFFICIAL SITE,
To install the latest version of OpenCV be sure that you have removed the library from the repository .

There are 2 methods of removing your old installation of OpenCV, and they depend on how have you had OpenCV installed in first place!

1- If you have installed from Ubuntu’s repository (or package managers like apt or the package manager of any other distros):
In this case, it is as simple as removing OpenCV’s package using your package manager. For example, on Ubuntu based Linux systems you can write the following commands in your favorite terminal. P
$ sudo apt-get autoremove libopencv-dev python-opencv

2-If you have installed from source (using make/make install):
In this case, the make command should have created an uninstall profile for you. So to remove OpenCV, go to the folder that you have compiled OpenCV (the place you had called make/make install) and execute the following command:
$ sudo make uninstall

3- NOTE: If you do not remember how have you installed OpenCV, or none of the above method works for you, you can use the following command to delete any file that has something to do with OpenCV. Please note that removing files can be dangerous so do this on your own risk! I take no responsibility!
$ sudo find / -name “*opencv*” -exec rm -i {} ;
$ sudo ldconfig && sudo ldconfig -vp

4- and follow the steps below.

PART – I PREPARE

1. make sure that everything in the system is updated and upgraded
$ sudo apt-get update
# $ sudo apt-get upgrade

2. install dependencies
依赖包安装

2.1 Build tools
$ sudo apt-get install build-essential
主要为build-essential软件包.
为编译程序提供必要的软件包的列别信息,这样软件包才知道头文件、库函数的位置。
此外,它还会下载依赖的软件包,安装gcc/g++/gdb/make等基本编程工具,最后组成一个开发环境。

$ sudo apt-get install cmake
安装cmake用于编译源码

2.2 GUI & OpenGLs
# $ sudo apt-get install qt5-default libvtk6-dev
# sudo apt-get install qt4-default libqt4-opengl-dev libvtk5-qt4-dev libgtk2.0-dev libgtkglext1 libgtkglext1-dev -y

2.3 Media I/O
安装能够支持图像读写以及视频读写的相关依赖包
$ sudo apt-get install libgtk2.0-dev
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjasper-dev

2.4 Video I/O
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# sudo apt-get install libavformat-dev libavutil-ffmpeg54 libavutil-dev libxine2-dev libxine2 libswscale-dev libswscale-ffmpeg3 libdc1394-22 libdc1394-22-dev libdc1394-utils -y

2.4 codec
#sudo apt-get install libavcodec-dev -y
#sudo apt-get install libfaac-dev libmp3lame-dev -y
#sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev -y
#sudo apt-get install libtheora-dev libvorbis-dev libxvidcore-dev -y
#sudo apt-get install ffmpeg x264 libx264-dev -y
#sudo apt-get install libv4l-0 libv4l v4l-utils -y

2.5 Python
$ sudo apt-get install python-dev python-numpy
# $ sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy

2.5 Parallelism and linear algebra libraries, multiproccessing library
$ sudo apt-get install libtbb-dev
# $ sudo apt-get install libtbb2
# libeigen3-dev

2.6 安装pkg-config
一个统一接口计算机软件,用于从源码中编译软件时查询已安装的库
$ sudo apt-get install pkg-config

2.6
$ sudo apt-get install libdc1394-22-dev
2.7
$ sudo apt-get install libopencv-dev
# $ sudo apt-get install checkinstall yasm libqt4-dev libqt4-opengl-dev

PART – II make & install

1. doanload and unzip
下载并解压OpenCV
$ mkdir ~/opencv-src
$ cd ~/opencv-src
$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.11/opencv-2.4.11.zip
$ unzip opencv-2.4.11.zip

2. cmake
generate the Makefile by using cmake, we can define which parts of OpenCV we want to compile,
For example, we want to use:
the viz module, Python, Java, TBB, OpenGL, Qt, work with videos, etc.

2.1 切换到解压后的OpenCV路径
$ cd opencv-2.4.11
$ cmake .
NOTE 后面的.表示找CMakeLists.txt文件

or, ABOVE is not very good, so:

2.2在另外一个文件夹,常为其子文件夹,构建makefile,同时进行一些参数配置
execute the following line at the terminal to create the appropriate Makefile:
$ cd ~/opencv-src/opencv-2.4.11
$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/dehaou1404/opencv-src/opencv-2.4.11/modules ..

-D CMAKE_INSTALL_PREFIX=/usr/local
-D CMAKE_BUILD_TYPE=RELEASE
-D BUILD_NEW_PYTHON_SUPPORT=ON
-D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON
-D BUILD_TIFF=ON
-D WITH_TBB=ON
-D WITH_V4L=ON
-D WITH_QT=ON
-D WITH_OPENGL=ON
-D WITH_VTK=ON
-D OPENCV_EXTRA_MODULES_PATH=/home/dehaou1404/opencv-src/opencv-2.4.11/modules
# -D WITH_IMAGEIO=ON
# -D BUILD_TIFF=ON
# -D BUILD_EXAMPLES=ON
-D OPENCV_EXTRA_MODULES_PATH=/home/dehaou1404/opencv-src/opencv-2.4.11/modules

After I set WITH_V4L = OFF but still keep WITH_LIBV4L=ON, the configuring skipped the searching of ‘sys/videoio.h’, and the compilation worke$

during configuration, remove the packages that use ‘sys/videoio.h’; this just evades the issue, but it works well; hopefully it can be solved i$

NOTE1: Use cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local .. , without spaces after -D if step 2 do not work.

NOTE2: There are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent).
“CMAKE_INSTALL_PREFIX=/usr/local”路径可以自定义修改。

IF errors occure, to see CMakeFiles/CMakeError.log file.

For me the one is:
fatal error: linux/videodev.h: No such file or directory
SOLUTION:
$ sudo apt-get install libv4l-dev
Installing libv4l-dev creates a /usr/include/linux/videodev2.h , but want to find linux/videodev.h.
The library does ship header files for compatibility, but fails to put them where applications will look for them.
$ cd /usr/include/linux
$ sudo ln -s ../libv4l1-videodev.h videodev.h
This provides a linux/videodev.h, and of the right version (1).

The another error :
fatal error: sys/videoio.h: No such file or directory

fatal error: ffmpeg/avformat.h: No such file or directory
#include
^
compilation terminated.
SO:

FInally,
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_PYTHON_EXAMPLES=ON ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON ..
-D INSTALL_C_EXAMPLES=ON ..

-D WITH_V4L=ON

-D INSTALL_PYTHON_EXAMPLES=ON <---- result in errors. HINTS: if once cmake error. need to remake folder as release then by the process again. this is ended: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_V4L=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON .. 2.3 Check Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Check that Java, Python, TBB, OpenGL, V4L, OpenGL and Qt are all detected correctly. If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again. 3. 编译 $ cat /proc/cpuinfo | grep processor -- 4 核 $ make -j4 4. 安装 $ sudo make install PART - III CONFIGURE 动态链接库和头文件配置 1. 配置相关信息是OpenCV动态库被共享 在/ect/ld.so.conf.d目录添加opencv.conf文件 $ sudo gedit /etc/ld.so.conf.d/opencv.conf 文件内容: # opencv.conf /usr/local/lib 使用动态库管理命令ldconfig,使opencv的相关链接库文件被系统共享: $ sudo ldconfig -v 2. 添加OpenCV的头文件位置 $ sudo vi /etc/bash.bashrc Add these two lines at the end of the file and save it: PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH pkg-config维护opencv的相关配置文件,可以在/usr/local/lib/pkgconfig目录下看到opencv.pc文件,此文件主要记录opencv的动态库信息和头文件信息。 使用pkg-config命令列出opencv的配置信息: 换路径: $ cd /urs/local/lib/pkgconfig 命令: $ pkg-config --libs opencv 查看opencv相关配置信息 注意:在更改相关文件时,可能文件的权限首先,故需现更改相关的权限 3. OK Now you have OpenCV 2.4.9 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support. PART - IV TEST OpenCV 1. build some samples included in OpenCV: 切换到opencv下载解压后的文件夹目录下,然后进入sample/c/目录下,编译样例文件 $ cd ~/opencv-2.4.9/samples/c $ chmod +x build_all.sh $ ./build_all.sh 执行完成后,会生成对应的可执行文件 2. 运行其中一个样例 $ ./find_obj 显示执行结果即可。 3. These examples use the old C interface: $ ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg $ ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg 4. The following examples use the new C++ interface: $ ~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg $ ~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial 5. run some Python code: $ python ~/opencv-2.4.9/samples/python2/turing.py 6. read a video and use OpenGL with Qt through this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses OpenGL to draw in 3D (great sample Javier): $ cd ~/opencv-2.4.9/samples/cpp/Qt_sample $ mkdir build $ cd build $ cmake .. $ make $ ./OpenGL_Qt_Binding 7. build a sample using the 3D visualization module viz: $ cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz $ g++ -o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs` $ ./widget_pose 8. hello word $ mkdir ~/prj-opencv $ cd ~/prj-opencv $ vi testopencv.c #include
#include

using namespace cv;

int main(int argc, char* argv[])
{
Mat image;
image = imread(argv[1], 1);

if (argc != 2 || !image.data)
{
printf(“No image datan”);
return -1;
}

namedWindow(“Display”, CV_WINDOW_AUTOSIZE);
imshow(“Display”, image);
waitKey(0);
return 0;
}

then,
$ vi CMakeLists.txt
project(testopencv)
find_package(OpenCV REQUIRED)
add_executable(testopencv testopencv)
target_link_libraries(testopencv ${OpenCV_LIBS})
cmake_minimum_required(VERSION 2.8)

$ cmake .
$ make

$ ./testopencv one.jpg

PART – V CONCLUSION

now you can use OpenCV with C++, C, Python, and Java. The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module. Multi threading functionality is enabled using TBB. Also, video support is enabled as well.

REF: http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

0 回复

发表评论

Want to join the discussion?
Feel free to contribute!

发表评论