Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

OpenCV3 讀取視頻與攝像頭並解析命令行列

利用 OpenCV,我們可以輕鬆讀取視頻檔案或攝像頭的影像資料,將其保存為圖像以供後續處理。本文的範例展示了一個簡潔的讀取與顯示流程,同時介紹了 OpenCV 3.0 引入的強大工具——CommandLineParser 類別。

// This is a demo introduces you to reading a video and camera
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp> // for camera
using namespace cv;

// OpenCV command line parser functions
// Keys accepted by command line parser
const char* keys =
{
    "{help h usage ? | | print this message}"
    "{@video | | Video file, if not defined try to use webcamera}"
};

int main(int argc, const char** argv)
{
    CommandLineParser parser(argc, argv, keys);
    parser.about("Reading a video and camera v1.0.0");

    // If requires help show
    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }
    String videoFile = parser.get<String>(0);

    // Check if params are correctly parsed in his variables
    if (!parser.check())
    {
        parser.printErrors();
        return 0;
    }

    VideoCapture cap;
    if (videoFile != "")
    {
        cap.open(videoFile);// read a video file
    }else {
        cap.open(0);// read the default caera
    }
    if (!cap.isOpened())// check if we succeeded
    {
        return -1;
    }

    namedWindow("Video", 1);
    while (1)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        imshow("Video", frame);
        if (waitKey(30) >= 0) break;
    }

    // Release the camera or video file
    cap.release();
    return 0;
}

認識 CommandLineParser

在深入講解如何讀取視頻或攝像頭之前,我們先來認識這個在 OpenCV 3.0 中引入、極其實用的類別。它幫助我們優雅地管理命令列輸入參數。

// OpenCV command line parser functions
// Keys accepted by command line parser
const char* keys =
{
    "{help h usage ? | | print this message}"
    "{@video | | Video file, if not defined try to use webcamera}"
};

使用命令列解析器時,第一步是在一個常量字串清單中定義我們需要或允許出現的參數。每一行都遵循固定模式:

{ name_param | default_value | description }

其中,name_param(參數名稱)前可加上「@」符號,用來把該參數指定為預設輸入。我們可以定義不止一個參數。

CommandLineParser parser(argc, argv, keys);

建構函式會讀取主函式的輸入參數,並搭配先前定義好的參數清單作為預設輸入。

// If requires help show
if (parser.has("help"))
{
    parser.printMessage();
    return 0;
}

成員方法 has() 會檢查指定參數是否存在。在此範例中,我們檢查使用者是否加入了「-h」、"-?"、"--help" 或 "--usage";若有,便用 printMessage() 輸出所有參數說明。

String videoFile = parser.get<String>(0);

透過 get<typename>(name_param) 函式,我們能存取並讀取任何輸入參數。上一行程式碼也可改寫為:

String videoFile = parser.get<String>("@video");

取得所有需要的參數後,我們可以進一步檢查它們是否都被正確解析;若有參數未能成功處理,則顯示錯誤訊息。

// Check if params are correctly parsed in his variables
if (!parser.check())
{
    parser.printErrors();
    return 0;
}

OpenCV官方教程中文版(For Python) PDF  https://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu Linux下安裝OpenCV2.4.1所需包 https://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 https://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 https://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 https://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 https://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 https://www.linuxidc.com/Linux/2011-11/47806.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9  https://www.linuxidc.com/Linux/2014-12/110045.htm

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved