載入視訊

展示如何使用 cv::VideoCapture。以下是從檔案載入視訊的示例:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>

using namespace cv;
VideoCapture videoSource;
Mat frame;
#define VIDEO_PATH "video.avi"

int main() 
{
    //Open video
    if (!videoSource.open(VIDEO_PATH))
    {            
        std::cout<<"Video not found at "<<VIDEO_PATH<<std::endl;
        return 1;     // Exit if fail
    }
    videoSource.set(CV_CAP_PROP_CONVERT_RGB, 1);
    
    int cameraWidth = videoSource.get(CV_CAP_PROP_FRAME_WIDTH);
    int cameraHeight = videoSource.get(CV_CAP_PROP_FRAME_HEIGHT);
    float cameraAspectRatio = cameraWidth / cameraHeight;

    std::cout <<"Camera resolution: " << cameraWidth<<", "<<cameraHeight<<" aspect ratio: "<<cameraAspectRatio<< std::endl;
    
    while(true)
    {
        videoSource >> frame;
        if(frame.empty())
            break;  
        //Resize frame
        cv::resize(frame, frame, cv::Size(320, 320 / cameraAspectRatio));
        imshow("frame", frame);
        waitKey(20);
    }
   waitKey(0);
   return 0;
}