1. 程式人生 > >VideoCapture 讀取視訊檔案,顯示視訊(幀)資訊

VideoCapture 讀取視訊檔案,顯示視訊(幀)資訊

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
    // 定義相關VideoCapture物件
	VideoCapture capture;
    // 讀取視訊檔案
	capture.open("..\\images\\car.avi");
    //  判斷視訊流讀取是否正確
	if(!capture.isOpened())
	{
		std::cout << "fail to open video!" << std::endl;
		return -1;
	}
	// 獲取視訊相關資訊-幀數
	long nTotalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
	std:: cout << "nTotalFrame = " << nTotalFrame << std::endl; 
	// 獲取視訊相關資訊-幀畫素寬高 
	int  frameHeight= capture.get(CV_CAP_PROP_FRAME_HEIGHT);  
    int  frameWidth  = capture.get(CV_CAP_PROP_FRAME_WIDTH); 
    std::cout << "frameHeight = " << frameHeight << std::endl;
    std::cout << "frameWidth = " << frameWidth << std::endl;
    // 獲取視訊相關資訊-幀率
    double FrameRate = capture.get(CV_CAP_PROP_FPS);  
    std::cout<< "FrameRate = " << FrameRate << std::endl;  
    cv::Mat frameImg;   
    // read方法獲取顯示幀
    long nCount = 1;
    while (true)
    {
        std::cout << " Current frame: " << nCount << std::endl;
        capture >> frameImg;
        // 判斷當前讀取檔案   
        if (!frameImg.empty())
        {
            imshow("frameImg", frameImg);
        }else
        {
            break;
        }
        // 按下鍵盤上q鍵退出
        if (char(waitKey(1)) == 'q')
        {
            break;
        }
        nCount++;
    }
    // 視訊釋放
    capture.release();  
	return 0;

}

轉載:http://blog.csdn.net/zhuwei1988