opencv显示一张图片

本文介绍OpenCV库的基本使用,包括如何利用Mat数据结构读取和保存图片,以及通过imread函数读入图片的方法。同时,详细解释了imread函数的参数设置,如IMREAD_COLOR、IMREAD_GRAYSCALE等,并展示了使用imshow函数显示图片的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天开始学习opencv,简单记录一下学习过程。学习opencv就由显示一张图片开始吧。保存一张图片用数据结构Mat,读入一张图片用函数imread。详细定义如下,一参为图片的路径,二参为枚举常量,枚举常量可以取的值有13个。

Mat imread( const String& filename, int flags = IMREAD_COLOR );
//! Imread flags
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 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.
     };

显示一张图片,用函数imshow。在opencv源码中函数的声明如下,InputArray可以传入一个Mat的类型。

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

 以下是一个用法的示例。

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

void main() {
	//读入一张图片,一参为图片的绝对路径,二参为枚举常量,有 IMREAD_COLOR、IMREAD_GRAYSCALE等
	Mat srcImg = imread("d:/data/test.png",IMREAD_LOAD_GDAL);
	imshow("源图像", srcImg);
	waitKey();//暂停窗口
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值