Opencv VideoCapture打开摄像头接口讲解

VideoCapture打开摄像头接口讲解

  1. VideoCapture
  2. bool open (int index)
  3. VideoCapture cap(index)
  4. open(int cameraNum, int apiPreference)

源码
cap_ffmpeg_impl.hpp

VideoCapture打开视频流文件

  1. bool open (const String& filename)
  2. VideoCapture cap(const String & filename)
  3. bool open(const String & filename, int apiPreference)

VideoCapture 关闭和空间释放

  1. 关闭和空间释放
  2. ~VideoCapture
  3. release()

读取一帧视频

read(OutputArray image)//cap.cpp
bool grab()   //读取并解码
virtual bool retrieve(OutputArray image, int flag = 0);//图像色彩转化
vc >> mat

bool VideoCapture::read(OutputArray image)
{
    CV_INSTRUMENT_REGION()

    if(grab())
        retrieve(image);
    else
        image.release();
    return !image.empty();
}


#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;

int main(int argc, char *argv)
{
    VideoCapture video;
    video.open("1.mp4");
    if (!video.isOpened())
    {
        cout << "open video failed!" << endl;
        getchar();
        return -1;
    }
    cout << "open video success!" << endl;
    namedWindow("video");
    Mat frame;
    for (;;)
    {
        if (!video.read(frame))
        {
            break;
        }
        if (frame.empty()) break;
        imshow("video", frame);
        waitKey(5);
    }
    getchar();


    return 0;
}


#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;

int main(int argc, char *argv)
{
    VideoCapture video;
    video.open("1.mp4");
    if (!video.isOpened())
    {
        cout << "open video failed!" << endl;
        getchar();
        return -1;
    }
    cout << "open video success!" << endl;
    namedWindow("video");
    Mat frame;
    for (;;)
    {
        //if (!video.read(frame))
        //{
        //  break;
        //}
        //读帧与解码
        if (!video.grab())
        {
            break;
        }
        //转换颜色格式
        if (!video.retrieve(frame))
        {
            break;
        }
        if (frame.empty()) break;
        imshow("video", frame);
        waitKey(5);
    }
    getchar();


    return 0;
}
发布了38 篇原创文章 · 获赞 29 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ruotianxia/article/details/79969592