视频读写
- 视频格式与相关API介绍
- 格式支持
- VideoCapture
- read
- isOpened:
- release:释放资源
>>
操作符重载:读取到每一帧
- 从视频文件中读写视频
- 从摄像头中读写视频
- VideoWriter -代码演示
- www.fourcc.org
- CV_FOURCC(‘D’, ‘I’, ‘V’, ‘X’) = MPEG-4 codec
- CV_FOURCC(‘P’,‘I’,‘M’,‘1’) = MPEG-1 codec
- CV_FOURCC(‘U’, ‘2’, ‘6’, ‘3’) = H263 codec
- -1 打开对话框选择
代码:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
VideoCapture capture;
capture.open("E:/Experiment/OpenCV/src/video_003.avi");
if (!capture.isOpened()){
printf("Video could not load ....");
return -1;
}
Mat frame;
namedWindow("test-show", CV_WINDOW_AUTOSIZE);
while (capture.read(frame)){
imshow("test-show", frame);
char c = waitKey(100);
if (c == 27){
break;
}
}
capture.release();
waitKey(0);
return 0;
}
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
VideoCapture capture(0);
if (!capture.isOpened()){
printf("Video could not load ....");
return -1;
}
double fps = capture.get(CV_CAP_PROP_FPS);
printf("FPS:%F", fps);
Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter writer("E:/Experiment/ww_demo.mp4", CV_FOURCC('D', 'I', 'V', 'X'), 15, size, true);
Mat frame;
namedWindow("test-show", CV_WINDOW_AUTOSIZE);
while (capture.read(frame)){
imshow("test-show", frame);
writer.write(frame);
char c = waitKey(100);
if (c == 27){
break;
}
}
capture.release();
waitKey(0);
return 0;
}
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
VideoCapture capture;
capture.open("E:/Experiment/ww_demo.mp4");
if (!capture.isOpened()){
printf("Video could not load ....");
return -1;
}
Mat frame, gray, binary;
namedWindow("test-show", CV_WINDOW_AUTOSIZE);
namedWindow("gray-show", CV_WINDOW_AUTOSIZE);
namedWindow("binary-show", CV_WINDOW_AUTOSIZE);
namedWindow("bitwise_not-show", CV_WINDOW_AUTOSIZE);
while (capture.read(frame)){
imshow("test-show", frame);
cvtColor(frame, gray, COLOR_BGR2GRAY);
imshow("gray-show", gray);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary-show", binary);
bitwise_not(frame, frame);
imshow("bitwise_not-show", frame);
char c = waitKey(100);
if (c == 27){
break;
}
}
capture.release();
waitKey(0);
return 0;
}