当前位置: 首页 > article >正文

OpenCV C++ 学习笔记(四):图像/视频的输入输出(highgui模块 高层GUI和媒体I/O)

文章目录

  • 图片读取
  • 创建窗口
  • 图片显示
  • 图片保存
  • 视频输入输出


图片读取

cv::Mat imread( const String& filename, int flags = IMREAD_COLOR );
enum ImreadModes {IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};

创建窗口

void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);

图片显示

void imshow(const String& winname, InputArray mat);

输入窗口名称和要显示的图像

若窗体未创建,会自动进行创建

CV_EXPORTS_W int waitKey(int delay = 0);

控制图片的展示时间,如设置delay=0,则表示一直展示,按SPACE停止展示。如设置delay不为0,则表示停留delay毫秒。

图片保存

bool imwrite( const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>());

视频输入输出

explicit VideoCapture::VideoCapture(const String& filename, int apiPreference = CAP_ANY);
explicit VideoCapture::VideoCapture(const String& filename, int apiPreference, const std::vector<int>& params);
explicit VideoCapture::VideoCapture(int index, int apiPreference = CAP_ANY);
explicit VideoCapture::VideoCapture(int index, int apiPreference, const std::vector<int>& params);
cv::VideoWriter videoWriter;VideoWriter::VideoWriter(const String& filename, int fourcc, double fps,Size frameSize, bool isColor = true);
VideoWriter::VideoWriter(const String& filename, int fourcc, double fps, const Size& frameSize,const std::vector<int>& params);
VideoWriter::VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,const Size& frameSize, const std::vector<int>& params);//构造函数
VideoWriter(const string& filename, 	 //输出文件名int fourcc, 				//编码形式。使用CV_FOURCC()宏double fps,					//输出视频帧率cv::Size frameSize,			 //单帧图片大小 bool isColor = true);			 //是否保存为彩色图像,false可传入灰度图像
//Open方法
virtual bool open(const string& filename, int fourcc, double fps,Size frameSize, bool isColor = true);
//写入帧
virtual void write(const Mat& image);
virtual VideoWriter& operator << (const Mat& image);//原帧无压缩
videoWriter.open("d:/output.avi", CV_FOURCC('D', 'I', 'B', ' '), 30, cv::Size(1024, 1024), false);	
//MPEG-4 编码压缩
videoWriter.open("d:/output1.avi", CV_FOURCC('D', 'I', 'V', 'X'), 30, cv::Size(1024, 1024), false);	

参数说明:

  • filename

    • 影片档案名称(例如video.avi)
    • 图片序列(例如img_00.jpg, img_01.jpg, img_02.jpg, …)
    • 视频流的网址。每个视频流或IP摄像机源均具有其自己的URL方案。请参考源流的文档以了解正确的URL。
  • index

    • 要打开的视频捕获设备的ID。要打开默认摄像头,只需传递0。
    • apiPreferenceCAP_ANY时,使用camera_id + domain_offset(CAP_ *)向后兼容有效。
  • apiPreference(not important)

    • 首选使用的Capture API后端。如果有多个可用的读取器实现,则可以用于实施特定的读取器实现。
    • 设置读取的摄像头编号,默认CAP_ANY=0,自动检测摄像头。多个摄像头时,使用索引0,1,2,…进行编号调用摄像头。 apiPreference = -1时单独出现窗口,选取相应编号摄像头。
  • fps:帧率

  • frameSize:输出视频中每一帧的尺寸

  • fourcc
    用于编码视频文件的编码器,通过VideoWriter::fourcc函数获得

CV_WRAP static int fourcc(char c1, char c2, char c3, char c4);
代码含义
VideoWriter::fourcc('M', 'P', '4', 'V')MPEG-4编码,输出文件拓展名mp4
VideoWriter::fourcc( 'X' , '2' , '6' , '4' )MPEG-4编码,输出文件拓展名mp4
VideoWriter::fourcc('P','I','M','1')MPEG-1编码,输出文件拓展名avi
VideoWriter::fourcc('X','V','I','D')MPEG-4编码,输出文件拓展名avi
VideoWriter::fourcc('I','4','2','0')YUV编码,输出文件拓展名avi
VideoWriter::fourcc('T','H','E','O')ogg vorbis编码,输出文件拓展名ogv
VideoWriter::fourcc('F',L','V','1')flash video编码,输出文件拓展名flv

示例

VideoCapture video("demo.mp4");
Mat fps;
video.read(fps);
VideoWriter video_out("demo_out.avi", VideoWriter::fourcc('P','I','M','1'), 30, fps.size());while (1){Mat fps;video>>fps;//video.read(fps);fps>>video_out;//video_out.write(fps);imshow("video", fps);waitKey(10);//控制帧率}
http://www.lryc.cn/news/2401731.html

相关文章:

  • 我的创作纪念日——聊聊我想成为一个创作者的动机
  • 蓝桥杯国赛训练 day1 Java大学B组
  • PyTorch——非线性激活(5)
  • OPenCV CUDA模块目标检测----- HOG 特征提取和目标检测类cv::cuda::HOG
  • MATLAB读取文件内容:Excel、CSV和TXT文件解析
  • Spring MVC 之 异常处理
  • 缓存控制HTTP标头设置为“无缓存、无存储、必须重新验证”
  • ubuntu24.04 使用apt指令只下载不安装软件
  • macOS 上使用 Homebrew 安装redis-cli
  • 计算机网络安全问答数据集(1788条) ,AI智能体知识库收集! AI大模型训练数据!
  • WinCC学习系列-高阶应用(WinCC REST通信)
  • 八、Python模块、包
  • 使用交叉编译工具提示stubs-32.h:7:11: fatal error: gnu/stubs-soft.h: 没有那个文件或目录的解决办法
  • macOS 连接 Docker 运行 postgres,使用navicat添加并关联数据库
  • 指针的使用——基本数据类型、数组、结构体
  • TK海外抢单源码/指定卡单
  • Docker MCP 目录和工具包简介:使用 MCP 为 AI 代理提供支持的简单安全方法
  • 【Linux】Linux 环境变量
  • OpenCV在图像上绘制文字示例
  • Java 抗量子算法:构建后量子时代的安全基石
  • Kubernetes 集群到 Jumpserver
  • Android7 Input(十)View 处理Input事件pipeline
  • 图像数据如何表示为概率单纯形
  • (11)Service Mesh架构下Java应用实现零信任安全模型
  • 什么是内网映射?如何将内网ip映射到外网访问?
  • 为什么要选择VR看房?VR看房有什么优点?
  • linux 串口调试命令 stty
  • C++STL-vector的使用
  • 图简记。。
  • pytorch基本运算-范数