002 :VideoCapture 的使用


class CV_EXPORTS_W VideoCapture
{
public:
    /**1. @brief Default constructor
    @note In @ref videoio_c "C API", when you finished working with video, release CvCapture structure with
    cvReleaseCapture(), or use Ptr\<CvCapture\> that calls cvReleaseCapture() automatically in the
    destructor.
當結束采集時候,調用cvReleaseCapture函數釋放資源
*/ CV_WRAP VideoCapture(); /** 2.@overload @brief Opens a video file or a capturing device or an IP video stream for video capturing with API Preference @param filename it can be: - name of video file (eg. `video.avi`) - or image sequence (eg. `img_%02d.jpg`, which will read samples like `img_00.jpg, img_01.jpg, img_02.jpg, ...`) - or URL of video stream (eg. `protocol://host:port/script_name?script_params|auth`). Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.
打開.avi或者jpg圖片的名 @param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. @sa The list of supported API backends cv::VideoCaptureAPIs
指定使用的讀取的方式
*/ CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY); /** 3. @overload @brief Opens a camera for video capturing @param index id of the video capturing device to open. To open default camera using default backend just pass 0. (to backward compatibility usage of camera_id + domain_offset (CAP_*) is valid when apiPreference is CAP_ANY)
如果只有一個攝像頭,則默認打開0
@param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L. 讀取的參數 @sa The list of supported API backends cv::VideoCaptureAPIs
*/ CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY); /** 4.@brief Default destructor The method first calls VideoCapture::release to close the already opened file or camera.
虛函數,用來調用 VideoCapture::release 來關閉文件或者攝像機
*/ virtual ~VideoCapture(); /**5. @brief Opens a video file or a capturing device or an IP video stream for video capturing. @overload Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY)
參數與VideoCapture相同 @return `true` if the file has been successfully opened The method first calls VideoCapture::release to close the already opened file or camera.
*/ CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY); /** @brief Opens a camera for video capturing @6.overload 參數與 VideoCapture相同 Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY) @return `true` if the camera has been successfully opened. The method first calls VideoCapture::release to close the already opened file or camera. */ CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY); /** 7.@brief Returns true if video capturing has been initialized already. 判斷VideoCapture是否初始化成功 If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true. */ CV_WRAP virtual bool isOpened() const; /** 8.@brief Closes video file or capturing device. The method is automatically called by subsequent VideoCapture::open and by VideoCapture destructor. 該方法在調用VideoCapture::open需要被調用,也被VideoCapture的析構函數調用 The C function also deallocates memory and clears \*capture pointer. */ CV_WRAP virtual void release(); /** 9.@brief Grabs the next frame from video file or capturing device.
獲取視頻的下一幀以及采集設備中的一幀圖像 @return `true` (non-zero) in the case of success. The method/function grabs the next frame from video file or camera and returns true (non-zero) in the case of success. The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time. Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call VideoCapture::grab() first and then call VideoCapture::retrieve() one or more times with different values of the channel parameter. @ref tutorial_kinect_openni
*/ CV_WRAP virtual bool grab(); /** 10.@brief Decodes and returns the grabbed video frame.
解碼和返回抓取到的視頻幀 @param [out] image the video frame is returned here. If no frames has been grabbed the image will be empty. @param flag it could be a frame index or a driver specific flag @return `false` if no frames has been grabbed The method decodes and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns an empty image (with %cv::Mat, test it with Mat::empty()). @sa read() @note In @ref videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
*/ CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0); /**11. @brief Stream operator to read the next video frame.
運算符用於獲取視頻中的某一個幀 @sa read()
*/ virtual VideoCapture& operator >> (CV_OUT Mat& image); /** @overload @sa read() */ virtual VideoCapture& operator >> (CV_OUT UMat& image); /** 13.@brief Grabs, decodes and returns the next video frame.
返回視頻中的幀 @param [out] image the video frame is returned here. If no frames has been grabbed the image will be empty. @return `false` if no frames has been grabbed The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns empty image (with %cv::Mat, test it with Mat::empty()). @note In @ref videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
*/ CV_WRAP virtual bool read(OutputArray image); /**14. @brief Sets a property in the VideoCapture.
設置VideoCapture的屬性 @param propId Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from @ref videoio_flags_others @param value Value of the property. @return `true` if the property is supported by backend used by the VideoCapture instance. @note Even if it returns `true` this doesn't ensure that the property value has been accepted by the capture device. See note in VideoCapture::get()
*/ CV_WRAP virtual bool set(int propId, double value); /** 15@brief Returns the specified VideoCapture property 設置VideoCapture的屬性 @param propId Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from @ref videoio_flags_others @return Value for the specified property. Value 0 is returned when querying a property that is not supported by the backend used by the VideoCapture instance. @note Reading / writing properties involves many layers. Some unexpected result might happens along this chain. @code {.txt} `VideoCapture -> API Backend -> Operating System -> Device Driver -> Device Hardware` @endcode The returned value might be different from what really used by the device or it could be encoded using device dependent rules (eg. steps or percentage). Effective behaviour depends from device driver and API Backend */ CV_WRAP virtual double get(int propId) const; /** @brief Returns used backend API name @note Stream should be opened. */ CV_WRAP String getBackendName() const; protected: Ptr<CvCapture> cap; Ptr<IVideoCapture> icap; };

以上為VideoCapture類的解析

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

// OpenCV command line parser functions
// Keys accecpted 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("Chapter 2. 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; //聲明VideoCapture對象
    if(videoFile != "")
        cap.open(videoFile);
    else
        cap.open("Recording3.webm"); 
    if(!cap.isOpened())  // 判斷VideoCapture是否打開成功
        return -1;
    namedWindow("Video",1);
    for(;;)       ///死循環”有兩種寫法:for(;;)和while(true),
    {
        Mat frame;  ///聲明Mat用於存儲單幀的圖像
        cap >> frame; // 通過運算符>>獲取單幀圖像
        if(frame.empty())
            return 0;
        imshow("Video", frame); ///顯示圖像
        if(waitKey(30) >= 0) break;///兩幀顯示的等待時間
    }
    // Release the camera or video cap
    cap.release();    
    return 0;
}

注意:攝像頭訪問下一幀所需的時間是根據攝像頭的采集速度以及算法時間決定,例如FPS為20,即采集一幀需要50ms,算法處理時間小於10ms,則只需要等到40ms就ok

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM