加强版十六章视频读写

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

}

相关推荐
且慢.5891 小时前
Python_day47
python·深度学习·计算机视觉
Unpredictable2222 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
jndingxin2 小时前
OpenCV CUDA模块图像处理------创建一个模板匹配(Template Matching)对象函数createTemplateMatching()
图像处理·人工智能·opencv
吴声子夜歌3 小时前
OpenCV——Mat类及常用数据结构
数据结构·opencv·webpack
一勺汤6 小时前
YOLO12 改进|融入 Mamba 架构:插入视觉状态空间模块 VSS Block 的硬核升级
yolo·计算机视觉·mamba·yolov12·yolo12·yolo12该机·yolo12 mamba
YYXZZ。。7 小时前
PyTorch——优化器(9)
pytorch·深度学习·计算机视觉
新知图书8 小时前
OpenCV为图像添加边框
人工智能·opencv·计算机视觉
carpell10 小时前
【语义分割专栏】3:Segnet原理篇
人工智能·python·深度学习·计算机视觉·语义分割
Hero_HL1 天前
Towards Open World Object Detection概述(论文)
人工智能·目标检测·计算机视觉
东皇太星1 天前
SIFT算法详细原理与应用
图像处理·算法·计算机视觉