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

Cách lấy vị trí khung hình hiện tại trong OpenCV C++


Giới thiệu

Khung hình hiện tại (current frame) là frame đang phát. Dùng CAP_PROP_POS_FRAMES lấy chỉ số frame (bắt đầu từ 0 hoặc 1 tùy backend).

Chương trình lấy current frame

#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;
        int current_Frame = cap.get(CAP_PROP_POS_FRAMES);
        if (myImage.empty()) break;
        cout << "Số khung hình hiện tại: " << current_Frame << endl;
        imshow("Video Player", myImage);
        char c = (char)waitKey(25);
        if (c == 27) break;
    }
    cap.release();
    return 0;
}

Kết quả

Video phát, console in liên tục số thứ tự frame đang hiển thị.