之前在Linux下装过几次opencv,但几乎每次都要查一下怎么安装,这次索性记录一下安装过程,不用每次都看其他人的教程了。

至于安装过程,可以直接参考官方文档。在解压后的文件夹下opencv\build\doc的opencv_tutorials.pdf文件开头就讲怎样在Linux下安装opencv.

以下就是官方文档的原文:

1.1 Installation in Linux
These steps have been tested for Ubuntu 10.04 but should work with other distros as well.
Required Packages
• GCC 4.4.x or later. This can be installed with:
sudo apt-get install build-essential
• 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] libdc1394 2.x;
• [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev.
All the libraries above can be installed via Terminal or by using Synaptic Manager

Building OpenCV from Source Using CMake, Using the Command Line
1. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated
Makefiles, project files as well the object files and output binaries.
2. Enter the <cmake_binary_dir> and type
cmake [<some optional parameters>] <path to the OpenCV source directory>
For example
cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:
make
sudo make install

因为是在虚拟机上操作,虚拟机网速有限,所以我的opencv2.4.9是从本地下载好后通过共享文件夹拷贝到虚拟机上的。

操作一 拷贝OPENCV源文件到虚拟机

先从虚拟机Terminal进入的共享文件夹,假设opencv-2.4.9.zip在共性文件夹内,使用命令复制到Documents文件夹下(其中xxxxxxxxx就是你设置的linux虚拟机的用户名):

cp opencv-2.4.9.zip /home/xxxxxxxxx/Documents/

这样就将 opencv-2.4.9.zip拷贝至虚拟机Documents文件夹下。

下一步就是解压opencv-2.4.9.zip文件:

unzip opencv-2.4.9.zip

操作二 安装Cmake

由于要安装opencv,按照上面官网教程的说明:

在我安装的ubuntu12.04上面测试过。自带的gcc版本:

嗯,4.6.3,比官网上说的4.4大,没问题。

然后按照下一步教程sudo apt-get install build-essential:

接着安装cmake,直接使用sudo apt-get install cmake安装的cmake一般都是ubuntu自带的支持的版本,不是最新的,但是使用该方法安装方便,我就暂且一试,毕竟下载源码自己编译很费事。

所以为图简便,就直接这样安装了,然后查一下版本号:

喔,相对于前面教程里面说的CMake 2.6 or higher,这个版本已经够用了。

操作三 安装必要库

由于Git是用来下载opencv源码用的,因为开始我们已经从本地拷贝过源码了,所以此处可以跳过。

下面是安装一些库和依赖文件:

sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libtiff4-dev libswscale-dev libjasper-dev

安装pkg-config,它是一个提供从源代码中编译软件时查询已安装的库时使用的统一接口的计算机软件。(下面这个在我这里是默认安装好的)

sudo apt-get install pkg-config

操作三 编译安装opencv

没有gui的cmake我用不惯,所以先安装cmake-gui:

sudo apt-get install cmake-qt-gui

安装完成后,就可以使用cmake-gui了,在终端输入cmake-gui就可以打开GUI界面:

然后就是配置了。

选完原文件路径和生成路径,然后点Configure就可以完成配置。注意在生成路径下的static文件夹是我新建的,因为我要生成一份静态库。

注意我用黄色框框起来的部分,因为要生成静态库,所以把右侧的复选框的“√”去掉,然后重新点Configure按钮。

下一步点Generate,Cmake会根据CmakeList.txt生成make文件,接着就需要编译了。

关掉cmake-gui。由终端进入自己新建的那个static文件夹

使用make编译,其中-j4表示4核编译:

make -j4

嗯,大概这次是被我要写笔记吓到了,一次顺利通过,内心喜悦!

使用make install 安装

嗯,又顺利通过!

操作三 测试opencv

1.2 Using OpenCV with gcc and CMake
Note: We assume that you have successfully installed OpenCV in your workstation.
• The easiest way of using OpenCV in your code is to use CMake. A few advantages (taken from the Wiki):
1. No need to change anything when porting between Linux and Windows
2. Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK )
• If you are not familiar with CMake, checkout the tutorial on its website.
Steps
Create a program using OpenCV
Let’s use a simple program such as DisplayImage.cpp shown below.

 #include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -;
}
Mat image;
image = imread( argv[], );
if ( !image.data )
{
printf("No image data \n");
return -;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey();
return ;
}

Create a CMake file
Now you have to create your CMakeLists.txt file. It should look like this:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Generate the executable
This part is easy, just proceed as with any other project using CMake:
cd <DisplayImage_directory>
cmake .
make
Result
By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image
location as an argument, i.e.:
./DisplayImage lena.jpg
You should get a nice window as the one shown below:

嗯,就这样吧。

ubuntu12.04安装Opencv2.4.9的更多相关文章

  1. Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or dir

    问题: Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared obje ...

  2. ubuntu12.04安装svn 1.7(转载)

    ubuntu12.04安装svn 1.7 分类: ubuntu2013-10-22 16:03 239人阅读 评论(0) 收藏 举报 svnubuntu   目录(?)[+]   1.问题     在 ...

  3. ubuntu12.04 安装 php5.4/php5.5

    1:修改源(我使用163的源)直接修改/etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ precise main universe re ...

  4. Ubuntu12.04 安装Samba

    Ubuntu12.04 安装Samba Ubuntu12.04 安装Samba 本教程介绍了在Ubuntu12.04安装Samba文件服务器,以及如何配置它通过SMB协议共享文件,以及如何将用户添加. ...

  5. Ubuntu12.04安装java6

    按照android官方文档 http://source.android.com 下载编译android源代码,jdk安装失败,尝试一下方法成功(2013-11-20) 下面我就把在Ubuntu12.0 ...

  6. [分享]Ubuntu12.04安装基础教程(图文)

    [分享]Ubuntu12.04安装基础教程(图文) 原文地址: http://teliute.org/linux/Ubsetup/lesson21/lesson21.html 1.进入 live cd ...

  7. ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境

    1.Ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境 http://blog.db89.org/ubuntu12-04-install-nginx-php-m ...

  8. ubuntu12.04 安装CS:APP Y86模拟器

    下的第一UBUNTU12.04下Y86模拟器的安装:(參考http://archive.cnblogs.com/a/1865627/ 作适当改动) 1.安装bison和flex词法分析工具 sudo ...

  9. Ubuntu12.04安装vscode i386

    最近在Ubuntu12.04的32位版本上安装vscode,我下载的是32位deb包,  vscode官网 安装命令 sudo dpkg -i vscode-i386.deb 安装完成没有报错,但是点 ...

随机推荐

  1. Python中matplotlib模块的简单使用

    测试代码如下: # encoding:utf-8 import numpy as np import matplotlib.pyplot as plt x = np.array([1,2,3,4,5, ...

  2. Codeforces 832A. Sasha and Sticks

    It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...

  3. 剑指offer 15.链表反转

    15.链表反转 题目描述 输入一个链表,反转链表后,输出新链表的表头. PHead,pre, next分别指向当前结点, 前一个结点, 后一个结点,每次迭代先更新当前结点的指针,记录下个结点的指向,转 ...

  4. (转)git使用规范

    转自:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个 ...

  5. ubuntu系统定时运行 crontab

    1,crontab是个啥? ubuntu系统自带cron工具,cron是一个系统上的定时工具,用它的好处在于,不同的程序可以用同一个计时器,这样就省得不同程序各自sleep了,另外它还支持比较多的个性 ...

  6. 全面了解 Java 原子变量类

    目录   一.原子变量类简介  二.基本类型  三.引用类型  四.数组类型  五.属性更新器类型  参考资料

  7. 10.pandas的替换和部分替换(replace)

    在处理数据的时候,很多时候会遇到批量替换的情况,如果一个一个去修改效率过低,也容易出错.replace()是很好的方法.   源数据 1.替换全部或者某一行 replace的基本结构是:df.repl ...

  8. hdu 5917

    题意:给你一个无向图,问图中有多少个符合条件的集合?条件为这个集合里面存在一个子集(大小>=3)为团或者都是孤立点.答案mod1e9+7: 根据 Ramsey定理,大于等于6个的集合,肯定存在一 ...

  9. 编码 - 调整 gitbash 文本字符集

    概述 gitbash 设置 文本字符集 背景 最近被 编码 的事情搞得乱七八糟 有点没头绪, 所以碰到 编码相关 的东西, 都想看上一看 环境 os win10.1903 git 2.20.1 1. ...

  10. Tomcat/conf/server.xml文件中docBase和path的说明

    Tomcat的项目部署方式有以下三种: 1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法.2.在tomcat安装目录中有一个conf文件夹,打 ...