1. 程式人生 > >OpenCV讀取視訊並跳幀儲存為圖片(C++)

OpenCV讀取視訊並跳幀儲存為圖片(C++)

網上看了很多部落格,基本都是逐幀儲存,且好多轉載不附原文的文章。本文根據上述原文修改。

執行環境:Visual studio 2015 + opencv 3.4.1

幾個有關視訊讀取的函式:

VideoCapture::VideoCapture

​ VideoCapture可以從檔案中或者攝像頭中讀取視訊,這是提供給C++的介面的,C的介面是CvCapture結構。

 VideoCapture::VideoCapture(conststring& filename)
 VideoCapture::VideoCapture(int device)

上面是初始化VideoCapture物件的,第一個是從檔案中讀取,第二個是從裝置中讀取,預設裝置在這裡是0;

例子:

VideoCapturecapture(“sample.avi”); /*讀取sample.avi檔案*/
VideoCapturecapture(0); /*如果只有一個攝像頭,capture會得到攝像頭的視訊*/

VideoCapture::Open

​ 開啟視訊檔案或者視訊裝置(例如攝像頭)。

bool VideoCapture::open(const string& filename)
bool VideoCapture::open(int device)

例子:

VideoCapturecapture;
capture.open("sample.avi"
); /*這裡的兩句等效於上面的VideoCapturecapture("sample.avi"),capture.open(0)*/

上面的視訊檔案開啟,用完後,最好呼叫 capture.release() 來關閉剛剛開啟的檔案

VideoCapture::release

void VideoCapture::release()

例子:

capture.release(); /*釋放開啟的視訊*/

VideoCapture::isOpened

判斷視訊是否被開啟,成功開啟返回ture,否則false。

bool VideoCapture::open(const string
& filename) bool VideoCapture::open(int device)

VideoCapture::grab

從視訊檔案中或者裝置中獲取下一幀,該方法成功呼叫返回ture;主要用於多攝像頭情況下,特別當那些攝像頭沒有實現硬體同步。

bool VideoCapture::grab()

grab之後,需要呼叫retrive對獲取的幀進行解碼。

VideoCapture::retrieve

對grab()得到的幀進行解碼。

bool VideoCapture::retrieve(Mat& image, int channel=0)

VideoCapture::read

獲取、解碼,這個方法結合了grab和retrieve,比較方便,

VideoCapture& VideoCapture::operator>>(Mat& image)
bool VideoCapture::read(Mat& image)

例子1:

// 方法一
if(!capture.read(frame))
{
    cout << "讀取視訊失敗" << endl;
    return-1;
}
//方法二
capture >>frame;

這兩個方法都可以,不過第一個能夠判斷,建議使用第一個,程式更健壯。

VideoCapture::get

返回VideoCapture的一些屬性

double VideoCapture::get(int propId)

probId可以是下面的:

· CV_CAP_PROP_POS_MSEC Currentposition of the video file in milliseconds or video capture timestamp.

· CV_CAP_PROP_POS_FRAMES 0-basedindex of the frame to be decoded/captured next.

· CV_CAP_PROP_POS_AVI_RATIO Relativeposition of the video file: 0 - start of the film, 1 - end of the film.

· CV_CAP_PROP_FRAME_WIDTH Width of theframes in the video stream.

· CV_CAP_PROP_FRAME_HEIGHT Height ofthe frames in the video stream.

· CV_CAP_PROP_FPS Frame rate.

· CV_CAP_PROP_FOURCC 4-charactercode of codec.

· CV_CAP_PROP_FRAME_COUNT Number offrames in the video file.

· CV_CAP_PROP_FORMAT Format ofthe Mat objects returned by retrieve() .

· CV_CAP_PROP_MODE Backend-specificvalue indicating the current capture mode.

· CV_CAP_PROP_BRIGHTNESS Brightnessof the image (only for cameras).

· CV_CAP_PROP_CONTRAST Contrast ofthe image (only for cameras).

· CV_CAP_PROP_SATURATION Saturationof the image (only for cameras).

· CV_CAP_PROP_HUE Hue of theimage (only for cameras).

· CV_CAP_PROP_GAIN Gain of theimage (only for cameras).

· CV_CAP_PROP_EXPOSURE Exposure(only for cameras).

· CV_CAP_PROP_CONVERT_RGB Booleanflags indicating whether images should be converted to RGB.

· CV_CAP_PROP_WHITE_BALANCE Currentlynot supported

· CV_CAP_PROP_RECTIFICATION Rectificationflag for stereo cameras (note: only supported by DC1394 v 2.x backendcurrently)

VideoCapture::set

設定VideoCapture的屬性:

bool VideoCapture::set(int propId, double value)

設定的probId和上面的get一樣。

例項:

下面的是視訊讀取和將視訊內的畫面轉化為圖片的程式碼,裡面的註釋應該足夠解釋了。

#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( "E:/Data/警戒線/@20180727105959_20180727115959_2502.avi" );
    //檢測是否正常開啟:成功開啟時,isOpened返回ture
    if ( !capture.isOpened( ) )
        cout << "fail toopen!" << endl;

    //獲取整個幀數
    long totalFrameNumber = capture.get( CV_CAP_PROP_FRAME_COUNT );
    cout << "整個視訊共" << totalFrameNumber << "幀" << endl;


    //設定開始幀()
    long frameToStart = 1;
    capture.set( CV_CAP_PROP_POS_FRAMES, frameToStart );
    cout << "從第" << frameToStart << "幀開始讀" << endl;

    //設定結束幀
    int frameToStop = 300;

    if ( frameToStop < frameToStart )
    {
        cout << "結束幀小於開始幀,程式錯誤,即將退出!" << endl;
        return -1;
    }
    else
    {
        cout << "結束幀為:第" << frameToStop << "幀" << endl;
    }

    //獲取幀率
    double rate = capture.get( CV_CAP_PROP_FPS );
    cout << "幀率為:" << rate << endl;


    //定義一個用來控制讀取視訊迴圈結束的變數
    bool stop = false;

    //承載每一幀的影象
    Mat frame;

    //顯示每一幀的視窗
    //namedWindow( "Extractedframe" );

    //兩幀間的間隔時間:
    //int delay = 1000/rate;
    double delay = 1000 / rate;


    //利用while迴圈讀取幀
    //currentFrame是在迴圈體中控制讀取到指定的幀後迴圈結束的變數
    long currentFrame = frameToStart;


    while ( !stop )
    {
        //讀取下一幀
        if ( !capture.read( frame ) )
        {
            cout << "讀取視訊失敗" << endl;
            return -1;
        }


        //cout << "正在讀取第" << currentFrame << "幀" << endl;
        //imshow( "Extractedframe", frame );

        //此處為跳幀操作
        if ( currentFrame % 50 == 0 ) //此處為幀數間隔,修改這裡就可以了
        {
            cout << "正在寫第" << currentFrame << "幀" << endl;
            stringstream str;
            str << "G:/test_pic/" << currentFrame << ".png";        /*圖片儲存位置*/

            cout << str.str( ) << endl;
            imwrite( str.str( ), frame );
        }

        //waitKey(intdelay=0)當delay≤ 0時會永遠等待;當delay>0時會等待delay毫秒
        //當時間結束前沒有按鍵按下時,返回值為-1;否則返回按鍵
        int c = waitKey( delay );
        //按下ESC或者到達指定的結束幀後退出讀取視訊
        if ( ( char )c == 27 || currentFrame > frameToStop )
        {
            stop = true;
        }
        //按下按鍵後會停留在當前幀,等待下一次按鍵
        if ( c >= 0 )
        {
            waitKey( 0 );
        }
        currentFrame++;

    }

    //關閉視訊檔案
    capture.release( );
    waitKey( 0 );
    return 0;
}

結果:

讀取後的圖片,因為圖片很多,上面程式碼在設定的時候,只讀取了300幀
mark