Ubuntu16.04安装opencv for python/c++
网上关于opencv的安装已经有了不少资料,但是没有一篇资料能让我一次性安装成功,因此花费了大量时间去解决各种意外,希望这篇能给一些人带去便利,节省时间。
1.安装OpenCV所需的库
1 sudo apt-get install build-essential 2 sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 3 sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
maybe running the third command line will encounter some error,like as "please indicate the libtiff-dev explicitly, libtiff5-dev or libtiff4-dev...",ok,just do as the blog 点击打开链接,change the parameter "libtiff-dev" of the third command line to 'libtiff5 -dev',because my ubuntu is 64-system.(fucking sogou of ubuntu,I have paid half of day to install it yesterday,finally,I find it make some fanren mistakes ,such like I press shift+2 and don't get @ symbol but " symbol .... fucking this ,that means I have to write my blogs in English ,fucking this!!!!! I hope someone who know how to solve the problem can give me some advice,T T )
2.下载最新opencv源码
链接:点击打开链接
remainer:when you open the link ,you shall go into a github repository of official opencv,you click the branch buttom like this
chose version that you need.and click the 'clone and download'.
将opencv-3.2.0.zip解压,然后进入该目录。
1 unzip opencv-3.2.0.zip 2 cd ~/opencv-3.2.0
3.编译opencv
1 cd ~/opencv-3.2.0 2 mkdir release 3 cd release 4 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 5 sudo make install
4、测试opencv(C++)
这时,已经可以通过C++使用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 (paying more attention here ,there is CMakeLists.txt not CMakeList.txt ,don't forget the character s of the end of 'CMakeList',I wasted one and half hour here,shit!)
写入如下内容
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 . (this dot represents current directory)
make
5) 执行
此时opencv-lena文件夹中已经产生了可执行文件DisplayImage,下载lena.jpg放在opencv-lena下,运行
./DisplayImage lena.jpg
5、测试opencv(python)
如果你的电脑已经装了anaconda,则只需如下命令
pip install opencv-python
否则,使用
pip install python-opencv
在python中import cv2,没有提示错误表示安装成功。
reference: