歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV3讀取視頻或攝像頭

OpenCV3讀取視頻或攝像頭

日期:2017/3/1 9:15:52   编辑:Linux編程

我們可以利用OpenCV讀取視頻文件或者攝像頭的數據,將其保存為圖像,以用於後期處理。下面的實例代碼展示了簡單的讀取和顯示操作:

// 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;
}

在我們解釋如何讀取視頻文件或者攝像頭輸入之前,我們需要首先介紹一個非常有用的新類,它可以用於幫助我們管理輸入的命令行參數,這個類在OpenCV 3.0中被引入,被稱為CommandLineParser類。

// 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}"
};

  對於一個命令行解析器(command-line parser)而言,我們首先需要做的事情就是在一個常量字符串向量中定義我們需要或者允許出現的參數列表。其中的每一行都有一個固定的模式:

{ 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 http://www.linuxidc.com/Linux/2015-08/121400.htm

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

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

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

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

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

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

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

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

Copyright © Linux教程網 All Rights Reserved