简体   繁体   English

如何在 Ubuntu 16.04 上为 Python 2.7 安装 openCV 2.4.13?

[英]How to install openCV 2.4.13 for Python 2.7 on Ubuntu 16.04?

I have tried a lot of online posts to install opencv but they are not working for Ubuntu 16.04.我尝试了很多在线帖子来安装 opencv,但它们不适用于 Ubuntu 16.04。 May anyone please give me the steps to install openCV 2.4.13 on it?任何人都可以给我安装openCV 2.4.13的步骤吗?

There's a script I had made a while back, for installing the latest version of OpenCV (4.2 as of the last update to this answer) Here's the link to it我之前制作了一个脚本,用于安装最新版本的 OpenCV(截至本答案的最后更新时为 4.2)这是它的链接

https://github.com/rsnk96/Ubuntu-Setup-Scripts/blob/master/Build-OpenCV.sh https://github.com/rsnk96/Ubuntu-Setup-Scripts/blob/master/Build-OpenCV.sh

NOTE : For Ubuntu 16.10+, there are some minor dependency changes you will have to make.注意:对于 Ubuntu 16.10+,您必须进行一些小的依赖更改。 Have a look at the comments for the same.看看相同的评论。

Since you do not want to install opencv contrib, and you specifically want opencv 2.4.13, I have modified the script below.由于您不想安装 opencv contrib,并且您特别想要 opencv 2.4.13,我修改了下面的脚本。 I would suggest you go through the script and understand what is happening before you execute it我建议您在执行脚本之前先阅读脚本并了解发生了什么

sudo apt-get update
sudo apt-get upgrade -y

sudo apt-get install build-essential -y
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -y

sudo apt-get install python3-numpy python3-pip python3-scipy python3-matplotlib python-dev python-matplotlib python-numpy python-scipy -y

sudo apt-get install python-pip python-tk libqt4-dev libqt4-opengl-dev  libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant -y


echo "GUI and openGL extensions"
sudo apt-get install qt4-default libqt4-opengl-dev libvtk5-qt4-dev libgtk2.0-dev libgtkglext1 libgtkglext1-dev -y

echo "image manipulation libraries"
sudo apt-get install libpng3 pngtools libpng12-dev libpng12-0 libpng++-dev -y
sudo apt-get install libjpeg-dev libjpeg9 libjpeg9-dbg libjpeg-progs libtiff5-dev libtiff5 libtiffxx5 libtiff-tools libjasper-dev libjasper1  libjasper-runtime zlib1g zlib1g-dbg zlib1g-dev -y

echo "video manipulation libraries"
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

echo "codecs"
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

echo "multiproccessing library"
sudo apt-get install libtbb-dev -y

echo "finally download and install opencv"
mkdir opencv
cd opencv
wget "https://github.com/opencv/opencv/archive/2.4.13.2.zip"
unzip opencv-2.4.13.2.zip

cd opencv-2.4.13.2
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE \
 -DCMAKE_INSTALL_PREFIX=/usr/local \
 -DINSTALL_C_EXAMPLES=ON \
 -DINSTALL_PYTHON_EXAMPLES=ON \
 -DBUILD_EXAMPLES=ON \
 -DBUILD_opencv_cvv=OFF \
 -DBUILD_NEW_PYTHON_SUPPORT=ON \
 -DWITH_TBB=ON \
 -DWITH_V4L=ON \
 -DWITH_QT=ON \
 -DWITH_OPENGL=ON \
 -DWITH_VTK=ON ..

echo "making and installing"
make -j8
sudo make install

echo "finishing off installation"
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

echo "Congratulations! You have just installed OpenCV. And that's all, folks! :P"

PS Create a script file out of this and then just execute the script file, rather than copy-pasting it line by line into the terminal. PS 从中创建一个脚本文件,然后执行该脚本文件,而不是将其逐行复制粘贴到终端中。 You can do that by copying all of it, placing it in a file with the extension .sh , and then simply running that .sh file from the terminal using $ ./filename.sh您可以通过复制所有文件,将其放入扩展名为.sh的文件中,然后使用$ ./filename.sh从终端简单地运行该 .sh 文件来完成此$ ./filename.sh

According to this source , from Ubuntu 16.04, you can now just do this to install OpenCV for Python2.7:根据这个来源,从 Ubuntu 16.04 开始,您现在可以这样做来为 Python2.7 安装 OpenCV:

pip install opencv-python

or for python3:或者对于python3:

pip3 install opencv-python

For more information, see here .有关更多信息,请参阅此处

I tested this on my machine and it works, great stuff this :).我在我的机器上测试了这个,它可以工作,很棒的东西:)。

This is much easier than all other methods I have come across thus far.这比我迄今为止遇到的所有其他方法要容易得多。

sudo apt-get install build-essential cmake git pkg-config
sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev
sudo pip install numpy
sudo apt-get install python-opencv

Then you can have a try:那你可以试试:

$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
>>> import cv2

If you have more than one opencv installation on your machine, now I tell you the most direct, effective and easy way to choose opencv version in python.如果你的机器上安装了不止一个opencv,现在我告诉你在python中选择opencv版本最直接、最有效、最简单的方法

You can type the following command to check the current opencv version in python您可以在python中键入以下命令来检查当前的opencv版本

import cv2
cv2.__version__

默认opencv版本

opencv path in python is set in system variable PYTHONPATH , you can echo this variable with echo $PYTHONPATH python中的opencv路径在系统变量PYTHONPATH设置,您可以使用echo $PYTHONPATH回显此变量
当前的 Python 路径

If I want to use opencv3.x in python, then using vi or gedit open .bashrc in the home folder, add this content to the end如果我想在python中使用opencv3.x,那么在home文件夹中使用vi或gedit打开.bashrc ,将这个内容添加到最后

export PYTHONPATH=/home/ismart/ipa2/devel/lib/python2.7/dist-packages:/home/ismart/catkin_ws/install_isolated/lib/python2.7/dist-packages:/usr/local/opencv3.x/lib/python2.7/dist-packages

Replace the path with yours用你的路径替换路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM