红联Linux门户
Linux帮助

基于ubuntu16.04系统下OpenCV源码安装及无法import cv2

发布时间:2017-01-14 09:45:27来源:blog.csdn.net/tengxing007作者:tengxing007
安装准备
系统:Ubuntu16.04
OpenCV的相关依赖:
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev  cmake libswscale-dev libjasper-dev
cmake 是一个跨平台的编译工具,对C++支持的特别好。
 
下载
地址:https://sourceforge.net/projects/opencvlibrary/files/latest/download?source=typ_redirect 我下载的是3.2(最新)版本的
 
编译安装
打开终端,进入下载目录,并解压进入目录:
cd python
zip opencv-3.2.0
cd opencv-3.2.0
执行如下命令:
cmake . 
#注意:不要弄成cmake/ . 
注意:如果您遇到散列不匹配错误,请从sourceforge手动下载文件ippicv_linux_20140513.tgz
下载文件后,将内容解压缩到opencv-3.0.0-alpha文件夹中的ippicv文件夹。
继续,报错:
Checking for module 'gtk+-3.0'
--   No package 'gtk+-3.0' found
-- Checking for module 'gstreamer-base-1.0'
--   No package 'gstreamer-base-1.0' found
少库,安装
sudo apt-get install libgt.......
#少多少就加多少,报错一直安装,直到如下报错
checking for module 'gstreamer-base-1.0'
--   package 'gstreamer-base-1.0' not found
-- checking for module 'gstreamer-video-1.0'
--   package 'gstreamer-video-1.0' not found
-- checking for module 'gstreamer-app-1.0'
--   package 'gstreamer-app-1.0' not found
-- checking for module 'gstreamer-riff-1.0'
--   package 'gstreamer-riff-1.0' not found
-- checking for module 'gstreamer-pbutils-1.0'
于是我
sudo apt-get install libgstreamer1.0-dev
结果无法解决,网上没有,去官网https://gstreamer.freedesktop.org/download/找了说,
Download GStreamer
In general, you should find packages that were specifically made for your distribution. Do not compile from source yourself unless you are certain you have to or want to. Do not compile from Git unless you really need some specific feature that is not released yet, or want to help out developing GStreamer.
Linux
Most, if not all, Linux distributions provide packages of GStreamer. You should find these in your distribution’s package repository.
Note that some distributions split the GStreamer plugins up further than the upstream sources. Additionally, some distributions do not include the gst-plugins-bad, gst-plugins-ugly, and gst-libav packages in their main repository, for legal reasons.
OpenBSD
Packages for various architectures are available, see the OpenBSD ports page.
Windows
MSI installers for Windows can be found here. They are provided and maintained by the GStreamer project.
Mac OS X
A .framework for Mac OS X can be found here. It is provided and maintained by the GStreamer project.
Android
Binaries for GStreamer on Android that integrate into the Android NDK can be found here. They are provided and maintained by the GStreamer project.
iOS
Binaries for GStreamer on iOS that integrate into XCode can be found here. They are provided and maintained by the GStreamer project.
Symbian
The sources of the initial port of GStreamer are available for download.
Releated binary packages are also available.
Source
Check our modules page, or Go straight to our source download directory.
ubuntu 直接通过apt-get 安装,但是找不到,只有去Sources download。然后下载编译一样不行。
 
成功的分界线
通过搜索在stackoverflow找到了一篇nice的文章。成功的解决了我的问题,具体步骤如下:
# step 1 
# 安装依赖 (太多了,自己相应替换)
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
# step 2
# 下载解包
wget https://github.com/Itseez/opencv/archive/3.0.0-alpha.zip
unzip opencv-3.0.0-alpha.zip
# step 3
# 编译
cd opencv-3.0.0-alpha
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 WITH_QT=ON -D WITH_OPENGL=ON ..
# step 4
#安装
make 
sudo make install
# step 5
# 安装完成,进行整理 
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
sudo apt-get update
写个Python文件:
import cv2 
#没有报错了
不仅解决了安装opencv的问题还解决了import cv2 的问题,因为在网上都说还得把一个包加入到另一个地方,就是将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下。
不得不说stackoverflow真的是个好东西,搜索对于开发人员来说真的很重要,最主要的还是对于bug的理性认知,和分析,解决。 
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27787.html