OpenCv3.0 计算机视觉教程

Liniux系统安装编译OpenCV(1)

2018-08-15  本文已影响0人  很优秀的你

一、安装

以下步骤针对Ubuntu 10.04进行了测试,对于其他发行版操作步骤相同。

Required Packages

必须安装
可选安装

Required Packages

你可以使用最新的稳定OpenCV版本,也可以从我们的Git存储库中获取最新的源码

Getting the Latest Stable OpenCV Version
Getting the Cutting-edge OpenCV from the Git Repository

启动Git客户端并克隆OpenCV存储库。 如果你需要来自OpenCV contrib repository里的模块,那么也可以克隆它。
For example:

cd ~/<my_working_directory>
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Building OpenCV from Source Using CMake

  1. 创建一个临时目录,我们将其表示为<cmake_build_dir>,您要在其中放置生成的Makefile,项目文件以及目标文件和输出二进制文件并进入那里。
    For example:

     cd ~/opencv
     mkdir build
     cd build
    
  2. 配置cmake运行路径:[<some optional parameters>] <path to the OpenCV source directory>
    For example:

     cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ...
    

或者make-gui

  1. 参数使用说明

如果创建的库的太大在一些硬件环境中(例如在Android构建的情况下),你可以使用install / strip命令来获得最小的大小。 此时版本所需空间将会比原先版本较少两倍。 然而除非那些额外的大文件确实很重要,否则我们不建议使用它。

二、使用gcc和cmake完成第一个opencv程序

Note

如果上边OpenCV已经完成了安装,下来我们开始写第一个Opencv程序。

  • 在代码中使用OpenCV的最简单方法是使用CMake,且有一些无法取代的优点(出自Wiki):
    1. 在Linux和Windows之间移植时无需更改任何内容
    2. 可以很容易地与CMake的其他工具结合使用(即Qt,ITK和VTK)

步骤

Create a program using OpenCV

我们写一个简单的程序,如:DisplayImage.cpp。

#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;
}
Create a CMake file

现在您必须创建CMakeLists.txt文件,如下所示:

 cmake_minimum_required(VERSION 2.8)
 project( DisplayImage )
 find_package( OpenCV REQUIRED )
 include_directories( ${OpenCV_INCLUDE_DIRS} )
 add_executable( DisplayImage DisplayImage.cpp )
 target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Generate the executable(生成可执行文件)

这部分很简单,就像使用CMake的任何其他项目一样:

cd <DisplayImage_directory>
cmake .
make
Result

到目前为止,您应该有一个可执行文件(在本例中称为DisplayImage)。 你必须运行它,给出一个图像位置作为参数,即:

./DisplayImage lena.jpg

您应该得到一个漂亮的窗口,如下所示:


GCC_CMake_Example_Tutorial.jpg
上一篇 下一篇

猜你喜欢

热点阅读