VideoCapture capture(0)用来打开系统摄像头
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
VideoCapture capture(0);
if (!capture.isOpened()) {
printf("0");
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret)break;
imshow("ret", frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
waitKey(0);
destroyAllWindows();
}
读取摄像头
读取视频文件
获取帧数等信息
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
//VideoCapture capture(0);
VideoCapture capture("C:/newword/image/20.avi");
if (!capture.isOpened()) {
printf("0");
}
namedWindow("frame", WINDOW_AUTOSIZE);
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int heigh = capture.get(CAP_PROP_FRAME_HEIGHT);
int num_of_frame = capture.get(CAP_PROP_FRAME_COUNT);
printf("frame size(f=%d,w=%d,h=%d,m=%d)", fps, width, heigh, num_of_frame);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret)break;
imshow("ret", frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
waitKey(0);
destroyAllWindows();
}
读取视频文件或摄像头,将读取视频的保存
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
VideoCapture capture(0);
//VideoCapture capture("C:/newword/image/20.avi");
if (!capture.isOpened()) {
printf("0");
}
namedWindow("frame", WINDOW_AUTOSIZE);
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int heigh = capture.get(CAP_PROP_FRAME_HEIGHT);
int num_of_frame = capture.get(CAP_PROP_FRAME_COUNT);
int type = capture.get(CAP_PROP_FOURCC);
printf("frame size(f=%d,w=%d,h=%d,m=%d)", fps, width, heigh, num_of_frame);
Mat frame;
VideoWriter writer("C:/newword/image/232320.mp4", type, fps, Size(width, heigh), true);
while (true) {
bool ret = capture.read(frame);
if (!ret)break;
imshow("ret", frame);
writer.write(frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
waitKey(0);
destroyAllWindows();
}