【OpenCV】提取視訊中指定幀
阿新 • • 發佈:2019-01-26
在進行專案的過程中,需要對一個視訊的指定幀圖片進行儲存,在下面的程式中,想要的幀數為140,200,260,320,380,440,500,560,720,780,800
使用OpenCV進行實現的相關函式如下:
上面的程式中,要特別注意 m>>img這個語句//將視訊中的指定幀存為圖片 void Vedio2Imag(string VideoName) { VideoCapture m; string s; Mat img; //VideoWriter out("out.mp4", VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, Size(1920, 1080)); m.open(VideoName); if (!m.isOpened()) { cout << "視訊讀入錯誤" << endl; } //m.set(CAP_PROP_FPS, 15);//設定視屏幀率 int j; for (size_t i = 0; i < m.get(CV_CAP_PROP_FRAME_COUNT); i++) { j = i; if (j > 139 && j % 60 == 20 && j < 801) { m >> img; s = to_string(j) + ".jpg"; std::cout << "第" << j << "幀" << endl; imwrite(s, img); } else { m >> img; continue; } } }
一定要謹記謹記