opencv读取图片和视屏

本文介绍了如何使用OpenCV在C++中读取和显示图片及视频。通过`imread`函数读取图片,利用`imshow`函数显示图片,详细解析了这两个函数的使用方法。对于视频,文章展示了使用`VideoCapture`类读取并逐帧显示视频的过程,同时提到程序运行时只能通过停止按钮结束,关闭窗口无效。
摘要由CSDN通过智能技术生成

显示一幅图片

我们首先将图片文件用imread读取然后存储到Mat类的对象中。

#include <iostream>  
#include <opencv2\opencv.hpp>
using namespace cv;
int main() {  
    Mat img = imread("avatar.jpg");
    // 在窗口中显示avatar  
    imshow("avatar", img);
    // 等待按下任意键  
    waitKey(0);
}

Mat类是用于保存图像以及其他矩阵数据的数据结构,默认情况下大小为0.我么可以为其设置初始值,例如

Mat picture(320, 640, Scalar(100));

imread函数

关于imread函数,函数原型如下:

Mat imread(const string& filename, int flag = 1);

它的第一个参数filename,填入我们需要读取图片的路径。在windows下支持如下类型的加载:

  • Windows bitmaps - .bmp, .dib (always supported)
  • JPEG files - .jpeg, .jpg, .jpe (see the *Notes section)
  • JPEG 2000 files - .jp2 (see the *Notes section)
  • Portable Network Graphics - .png (see the *Notes section)
  • WebP - .webp (see the *Notes section)
  • Portable image format - .pbm, .pgm, .ppm .pxm, *.pnm (always supported)
  • Sun rasters - .sr, .ras (always supported)
  • TIFF files - .tiff, .tif (see the Notes section)
  • OpenEXR Image files - .exr (see the *Notes section)
  • Radiance HDR - .hdr, .pic (always supported)
  • Raster and Vector geospatial data supported by Gdal (see the Notes section)

第二个参数flag的设置,我们通过查看imgcodecs.hpp(opencv3.3)可以知道如下的枚举变量,默认情况下设置为1(IMREAD_COLOR)。

enum ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image.
       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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值