Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Cách tải và phát video từ file trong OpenCV C++


Giới thiệu

Phát video từ file tương tự camera, nhưng VideoCapture nhận đường dẫn file thay vì index camera.

Chương trình phát video từ file

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main() {
    Mat myImage;
    namedWindow("Video Player");
    VideoCapture cap("video.mp4");
    if (!cap.isOpened()) {
        cout << "Không phát hiện luồng video" << endl;
        system("pause"); return -1;
    }
    while (true) {
        cap >> myImage;
        if (myImage.empty()) break;
        imshow("Video Player", myImage);
        if (waitKey(25) == 27) break;
    }
    cap.release();
    return 0;
}

Kết quả

Cửa sổ "Video Player" phát file video.mp4, nhấn ESC để thoát.