本文记录了Ubuntu 14.04下使用源码手动安装OpenCV 3.0的过程。此外记录了在Python中安装及载入OpenCV的方法。
1、安装OpenCV所需的库(编译器、必须库、可选库)
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
2、从官网下载最新opencv源码(2.4以上)http://sourceforge.net/projects/opencvlibrary/
或者github
======================2016.8.13更新===================
遇到类似这样的问题:
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
需要自己编译ffmpeg 加上shared选项
sudo apt-get remove ffmpeg
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic
make -j16
sudo make install
======================================================
3、编译opencv
将opencv放至任意目录,解压
unzip opencv-3.0.0-rc1.zip
创建编译目录,编译
cd ~/opencv-3.0.0-rc1
mkdir release
cd release
cmake -D WITH_CUDA=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/net/wanggu/local ..
(推荐用ccmake .. 可以手动配置更多的选项,比如CMAKE_INSTALL_PREFIX, "CUDA_GENERATION"= Auto)
-----------------------
CMAKE_INSTALL_PREFIX /net/wanggu/software/opencv-master/install
遇到cuda问题:
CUDA_GENERATION Auto
-----------------
(2016.8.24 update)
nonfree里面包含features2d, 要启用的话
在opencv3.x中要先下载 https://github.com/opencv/opencv_contrib
然后ccmake ..
设置以下:
OPENCV_ENABLE_NONFREE ON
OPENCV_EXTRA_MODULES_PATH /net/wanggu/software/opencv-master/opencv_contrib/modules
----------------
/usr/local 改成其他的,不然要sudo权限。
(opencv-3.0.0-alpha这个版本不靠谱)
![](https://i-blog.csdnimg.cn/blog_migrate/d16d88375578d2b62ef19c628d15c507.png)
make -j8
sudo make install -j8
-----------------------------------
添加cv的python环境变量到.bashrc
export CVPATH=/usr/local/lib/python2.7/site-packages
export PYTHONPATH=$CVPATH:$PYTHONPATH
4、测试opencv
1) 创建工作目录
mkdir ~/opencv-lena
cd ~/opencv-lena
gedit DisplayImage.cpp
2) 编辑如下代码
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
3) 创建CMake编译文件
gedit CMakeLists.txt
写入如下内容
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
4) 编译
cd ~/opencv-lena
cmake .
make
5) 执行
此时opencv-lena文件夹中已经产生了可执行文件DisplayImage,下载lena.jpg放在opencv-lena下,运行
./DisplayImage lena.jpg
6) 结果
5、安装python-opencv
可直接使用apt安装
sudo apt-get install python-opencv
sudo apt-get install python-numpy
测试:
打开python,import cv模块成功即可。
import cv
import cv2
相关文章:
Ubuntu 12.04下安装OpenCV 2.3.1,图像二值化:http://www.linuxdiyf.com/linux/8615.html
Linux环境下qt/qt creator添加OpenCV的配置:http://www.linuxdiyf.com/linux/9314.html
Ubuntu中安装OpenCv2.1九步曲:http://www.linuxdiyf.com/linux/9848.html