使用OpenCV與gcc和CMake

2018-08-25 18:08 更新
注意
我們假設(shè)您已經(jīng)在工作站中成功安裝了OpenCV。
  • 在代碼中使用OpenCV的最簡(jiǎn)單方法是使用CMake。幾個(gè)優(yōu)點(diǎn)(從維基獲?。?/li>
  1. 在Linux和Windows之間移植時(shí),無需更改任何內(nèi)容
  2. 可以輕松地與CMake(即Qt,ITK和VTK)的其他工具組合使用
  • 如果您不熟悉CMake,請(qǐng)?jiān)谄渚W(wǎng)站上查看教程。

Steps

使用OpenCV創(chuàng)建程序

讓我們使用一個(gè)簡(jiǎn)單的程序,如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;

創(chuàng)建一個(gè)CMake文件

現(xiàn)在你必須創(chuàng)建你的CMakeLists.txt文件。它應(yīng)該是這樣的:

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} )

生成可執(zhí)行文件

這部分很簡(jiǎn)單,就像使用CMake的任何其他項(xiàng)目一樣:

cd <DisplayImage_directory>
cmake .
make

結(jié)果

現(xiàn)在你應(yīng)該有一個(gè)可執(zhí)行文件(在這種情況下叫做DisplayImage)。你只需要運(yùn)行它給出一個(gè)圖像位置作為參數(shù),即:

./DisplayImage lena.jpg

你應(yīng)該得到一個(gè)漂亮的窗口,如下所示:

GCC_CMake_Example_Tutorial

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)