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

发布于:2025-06-07 ⋅ 阅读:(21) ⋅ 点赞:(0)


图片读取

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);//控制帧率
    }

网站公告

今日签到

点亮在社区的每一天
去签到