在Ubuntu和debian上安装OpenCV

时间:2023-01-16 06:25:53

参考:http://www.douban.com/note/478450231


http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation

依赖包:

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
可以使用如下命令安装依赖包:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

下载Opencv源码
https://github.com/Itseez/opencv.git


创建临时目录cmake_binary_dir,用来存放cmake所产生的Makefile文件、项目文件、目标文件和输出文件。

然后进入cmake_binary_dir,按命令格式cmake [some optional parameters] <path to the OpenCV source directory>生成makefile文件。

如:

cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..


进入创建的临时目录cmake_binary_dir,输入命令:

make
sudo make install


make过程中出现错误:错误提示在:opencv-2.4.10/modules/core/src/system.cpp文件280行处;

修改如下:https://github.com/Itseez/opencv/commit/ea50be0529c248961e1b66293f8a9e4b807294a6

       #ifdef __x86_64__
asm __volatile__
(
"movl $7, %%eax\n\t"
"movl $0, %%ecx\n\t"
"cpuid\n\t"
:[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3])
:
: "cc"
);
#else
+ // We need to preserve ebx since we are compiling PIC code.
+ // This means we cannot use "=b" for the 2nd output register.
asm volatile
(
"pushl %%ebx\n\t"
"movl $7,%%eax\n\t"
"movl $0,%%ecx\n\t"
"cpuid\n\t"
+ "movl %%ebx,%1\n\t"
"popl %%ebx\n\t"
- // : "=a"(cpuid_data[0]), "=b"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3])
+ : "=a"(cpuid_data[0]), "=r"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3])
:
: "cc"
);


参考:http://www.douban.com/note/478450231
配置openCV参数设置
在/etc/ld.so.conf.d/opencv.conf文件中加入一行:/usr/local/lib ,
可能会没有opencv.conf这个文件,那我们就自己创建一个:
sudo gedit/etc/ld.so.conf.d/opencv.conf
使用下面这条命令:
sudo ldconfig
在 /etc/ bash.bashrc(sudo gedit/etc /bash.bashrc以root进入才能修改)中加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH



另一种安装方法:

http://milq.github.io/install-opencv-ubuntu-debian/点击打开链接

Note: This tutorial has been tested on Debian 8.0 "Jessie" with OpenCV 3.0.0 and Ubuntu 14.04 LTS with OpenCV 3.0.0 alpha. If you have successfully installed OpenCV following this tutorial with other OS or OpenCV version, please share your configuration. Thanks!

Install OpenCV on Ubuntu or Debian is a bit long but very easy. You can install OpenCV from the repository or manually.

INSTALL OPENCV FROM THE REPOSITORY

You can install OpenCV from the 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).

INSTALL OPENCV MANUALLY

To install the latest version of OpenCV be sure that you have removed the library from the repository withsudo apt-get autoremove libopencv-dev python-opencv andfollow the steps below.

1. KEEP UBUNTU OR DEBIAN UP TO DATE

Open your terminal and execute:

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove

2. INSTALL THE DEPENDENCIES

Now install the dependencies:

Build tools:

sudo apt-get install build-essential cmake

GUI:

sudo apt-get install qt5-default libvtk6-dev

Media I/O:

sudo apt-get install zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev

Video I/O:

sudo apt-get install libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev

Parallelism and linear algebra libraries:

sudo apt-get install libtbb-dev libeigen3-dev

Python:

sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy

Java:

sudo apt-get install ant default-jdk

Documentation:

sudo apt-get install doxygen sphinx-common texlive-latex-extra

3. DOWNLOAD AND DECOMPRESS OPENCV

Enter the OpenCV official website and download the latest version for Linux. Then decompress the downloaded file.

4. COMPILE AND INSTALL OPENCV

In your terminal, make sure you are within the OpenCV directory and run the following commands:

mkdir build
cd build
cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON ..
make -j4
sudo make install

Now, configure OpenCV:

sudo ldconfig

Now, you have installed OpenCV. You can execute some examples builded and/or compile one demonstration.

5. EXECUTE SOME OPENCV EXAMPLES

Go to your OpenCV directory and execute a C++ example:

cd build/bin
./cpp-example-edge ../../samples/data/fruits.jpg

Now, go to your OpenCV directory and execute a Python example:

cd samples/python2
python video.py

Finally, go to your OpenCV directory and execute a Java example:

cd samples/java/ant
ant -DocvJarDir=../../../build/bin -DocvLibDir=../../../build/lib

6. COMPILE A DEMONSTRATION

Create a source file named demo.cpp with the following code:

#include <opencv2/core/core.hpp>
#include <opencv2/viz/vizcore.hpp>

int main()
{
cv::viz::Viz3d window = cv::viz::Viz3d("Viz demonstration");

cv::Point3d min(0.25, 0.0, 0.25);
cv::Point3d max(0.75, 0.5, 0.75);

cv::viz::WCube cube(min, max, true, cv::viz::Color::blue());
cube.setRenderingProperty(cv::viz::LINE_WIDTH, 4.0);

window.showWidget("Axis widget", cv::viz::WCoordinateSystem());
window.showWidget("Cube widget", cube);

while(!window.wasStopped()) {
window.spinOnce(1, true);
}

return 0;
}

Create a text file named CMakeLists.txt with the following code:

cmake_minimum_required(VERSION 2.8.12)

project(demo)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(demo demo.cpp)
target_link_libraries(demo ${OpenCV_LIBS})

Finally, build and execute:

mkdir build && cd build && cmake .. && make
./demo

And that's it! You have installed OpenCV, run some examples, and compiled OpenCV code!