OpenCV--VideoCapture類


讀取本地視頻&打開攝像頭


OpenCV中VideoCapture中有三個構造函數

  • VideoCapture();
  • VideoCapture(const String& filename, int apiPreference = CAP_ANY);
  • VideoCapture(int index, int apiPreference = CAP_ANY);

1.VideoCapture();

默認構造函數

2.VideoCapture(const String& filename, int apiPreference = CAP_ANY);

簡單打開一個視頻文件 或者一個捕捉設備 或者IP視頻流(具體怎么讀可以參考https://www.cnblogs.com/arkenstone/p/7194226.html

filename可以是

  • 視頻文件名
  • 圖片名
  • 視頻流的URL

3.VideoCapture(int index, int apiPreference = CAP_ANY);

打開攝像頭用來捕捉視頻
index默認自帶攝像頭0,其他的外接攝像頭一般是1.

具體定義如下

class CV_EXPORTS_W VideoCapture
{
public:
        CV_WRAP VideoCapture();
        CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY);
        CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY);
        virtual ~VideoCapture();
    
    CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY);

    CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY);
    
    CV_WRAP virtual bool isOpened() const;
    
    CV_WRAP virtual void release();
    
    CV_WRAP virtual bool grab();
    
    CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0);
    
    virtual VideoCapture& operator >> (CV_OUT Mat& image);

    virtual VideoCapture& operator >> (CV_OUT UMat& image);

    CV_WRAP virtual bool read(OutputArray image);

    CV_WRAP virtual bool set(int propId, double value);

    CV_WRAP virtual double get(int propId) const;

    CV_WRAP String getBackendName() const;

protected:
    Ptr<CvCapture> cap;
    Ptr<IVideoCapture> icap;
}

eg. 讀取本地視頻

void VideoRead()
{
	VideoCapture capture("1.mp4");
	/*
	VideoCapture capture;
	captrue.open("1.mp4");
	*/
        while (1)
	{
		//frame存儲每一幀圖像
		Mat frame;
		//讀取當前幀
		capture >> frame;
		//播放完退出
		if (frame.empty()) {
			printf("播放完成\n");
			break;
		}
		imshow("讀取視頻",frame);
		//延時30ms
		waitKey(30);
	}
}


免責聲明!

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



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