C++部分`#include<opencv2/opencv.hpp>
#include<stdlib.h>
using namespace cv;
void main()
{
Size videoSize(680, 480);
VideoCapture cap(CV_CAP_DSHOW + 0);
cap.open(1); //打开摄像头
if (!cap.isOpened())//如果视频不能正常打开则返回
return;
cvWaitKey(30);
int fps = cap.get(CV_CAP_PROP_FPS);
printf("current fps : %d \n", fps);
VideoWriter wri("D:\\C++project\\oua.mp4", CV_FOURCC('M', 'P', '4', '2'), 25, videoSize, true);
Mat frame;//用于保存每一帧图像
while (cap.isOpened())
{
cap.read(frame);
//cap >> frame; //等价于 cap.read(frame);
if (frame.empty()) //如果某帧为空则退出循环
break;
if (!wri.isOpened())
{
assert("track writer open failed!\n");
}
imshow("raw", frame); //显示原始图像 原始分辨率为 640 * 480
wri.write(frame);
//wri << frame;
char c = cvWaitKey(30);
if (c == 27)//Esc 键退出
{
break;
}
}
wri.release();
cap.release();//释放资源
}
python
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
cap.set(4, 720)
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
out = cv2.VideoWriter('out1.mp4', fourcc, 25, (640, 480))
while(True):
ret,frame=cap.read()
cv2.imshow('frame',frame)
out.write(frame)
if cv2.waitKey(1)==ord('1'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
|