加强版十六章视频读写

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();

}

相关推荐
会飞锦鲤7 小时前
基于 RT-DETR 的 PCB 缺陷检测
深度学习·目标检测·计算机视觉·pyqt5·rt-detr·工业质检·pcb缺陷检测
硅谷秋水5 天前
MorphMimic 方法分析
人工智能·机器学习·计算机视觉·机器人
jay神5 天前
基于 YOLOv8 的传送带异物检测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机视觉·计算机毕业设计
Rattenking7 天前
OpenCV 轨迹栏操作和键盘响应操作
人工智能·opencv·计算机外设
jay神8 天前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
wangxiaoxuan2178 天前
TF2 的原理与使用:把坐标、运动和观测时间一起算清楚
计算机视觉·机器人·科沃斯八界
硅谷秋水8 天前
EgoHumanoid‑V2 方法分析
人工智能·机器学习·计算机视觉·语言模型·机器人
BYSJMG8 天前
计算机毕业设计选题推荐:【基于深度学习的车辆损坏严重程度评估方法研究】ResNet50 图像分类
人工智能·深度学习·目标检测·机器学习·计算机视觉·分类·课程设计
BYSJMG8 天前
计算机毕业设计选题推荐|【基于深度学习的电梯电动车违规进入检测研究】YOLO26m+可视化大屏
人工智能·深度学习·神经网络·目标检测·机器学习·计算机视觉·课程设计
硅谷秋水9 天前
将闲科技(LiberAI)提出世界动作模型Liber-0
机器学习·计算机视觉·语言模型·机器人