jashliao 用 VC++ 實現 fanfuhan OpenCV 教學012 ~ opencv-012-視頻讀寫[抓取影片資訊、影片撥放拷貝(讀取影格/顯示圖像(畫面)&寫入檔案)]
jashliao 用 VC++ 實現 fanfuhan OpenCV 教學012 ~ opencv-012-視頻讀寫[抓取影片資訊、影片撥放拷貝(讀取影格/顯示圖像(畫面)&寫入檔案)]
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/03/26/opencv-012/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
https://github.com/jash-git/jashliao-implements-FANFUHAN-OPENCV-with-VC
★前言:

★主題:
OPENCV提供操作影片的函數類別,此範例就是簡單的展示讀取影片內容後把影片畫面顯示出來並轉存到另一實體檔案的簡易範例
★C++
// VC_FANFUHAN_OPENCV012.cpp : 定義主控台應用程式的進入點。
//
/*
// Debug | x32
通用屬性
| C/C++
| | 一般
| | 其他 Include 目錄 -> C:\opencv\build\include
|
| 連結器
| |一一般
| | 其他程式庫目錄 -> C:\opencv\build\x64\vc15\lib
|
| |一輸入
| | 其他相依性 -> opencv_world411d.lib;%(AdditionalDependencies)
// Releas | x64
組態屬性
| C/C++
| | 一般
| | 其他 Include 目錄 -> C:\opencv\build\include
|
| 連結器
| |一般
| | 其他程式庫目錄 -> C:\opencv\build\x64\vc15\lib
|
| |一輸入
| | 其他相依性 -> opencv_world411.lib;%(AdditionalDependencies)
*/
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
void pause()
{
printf("Press Enter key to continue...");
fgetc(stdin);
}
int main()
{
// 打开摄像头
// VideoCapture capture(0);
// 打开视频文件
VideoCapture capture;
capture.open("../../images/vtest.avi");////影片讀取
if (!capture.isOpened())
{
cout << "could not load video.." << endl;
pause();
return -1;
}
else
{
Size S = Size((int)capture.get(CAP_PROP_FRAME_WIDTH), (int)capture.get(CAP_PROP_FRAME_HEIGHT));////影片畫面大小
int fps = capture.get(CAP_PROP_FPS);//抓取影片每秒張數
cout << "capture Width:" << S.width <<"\tHeight:"<<S.height<< endl;
cout << "capture fps: " << fps << endl;
VideoWriter writer("test.mp4", cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), fps, S, true);
Mat frame;
while (capture.read(frame)) {
imshow("input", frame);//顯示影面每一格的畫面
writer.write(frame);//將每一格的畫面寫入檔案
char c = waitKey(50);
if (c == 27) //ESC
{
break;
}
}
capture.release();
writer.release();
waitKey(0);
}
return 0;
}
★Python
import cv2 as cv
import numpy as np
capture = cv.VideoCapture("D:/vcprojects/images/768x576.avi")
# capture = cv.VideoCapture(0) 打开摄像头
height = capture.get(cv.CAP_PROP_FRAME_HEIGHT)
width = capture.get(cv.CAP_PROP_FRAME_WIDTH)
count = capture.get(cv.CAP_PROP_FRAME_COUNT)
fps = capture.get(cv.CAP_PROP_FPS)
print(height, width, count, fps)
out = cv.VideoWriter("D:/test.mp4", cv.VideoWriter_fourcc('D', 'I', 'V', 'X'), 15,
(np.int(width), np.int(height)), True)
while True:
ret, frame = capture.read()
if ret is True:
cv.imshow("video-input", frame)
out.write(frame)
c = cv.waitKey(50)
if c == 27: # ESC
break
else:
break
capture.release()
out.release()
★結果圖:

★延伸說明/重點回顧: