opencv入门(2)图像读取和显示


1 包含API

读取图像 imread
显示图像 imshow

2. 读取图像 imread

imread
在这里插入图片描述
第一个参数是文件地址,第二个参数是显示模式,默认是IMREAD_COLOR模式
修改该参数

//! Imread flags
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.
     };

如果有透明通道,使用IMREAD_UNCHANGED

3. 显示图像 imshow

在这里插入图片描述
input

src读进来的是是8位通道的,位图和深度是3 * 8 = 24,API里用枚举显示图片的深度
src.depth(); 这个值调用出来是1,是正常的,枚举中24的值等于1

4 阻塞函数 waitKey

在这里插入图片描述

waitKey(0); 阻塞等待
waitKey(1);停顿1毫秒

5 关闭之前创建的所有窗口 destroyAllWIndows

在这里插入图片描述

6 创建一个窗口 nameWindow

namedWindow(“inputwindow”,WINDOW_FREERATIO);
imshow(“inputwindow”, src);
和imshow配合,让imshow图片显示在inputwindow中
在这里插入图片描述
WINDOW_FREERATIO并且图片能自动适应窗口大小

7 代码

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	Mat src = imread("G:/test/NoRecord.png",IMREAD_UNCHANGED);
	
	if (src.empty())
	{
		print("could not load img");
		return -1;

	}
	namedWindow("inputwindow",WINDOW_FREERATIO);
	imshow("inputwindow", src);
	waitKey(0);
	destroyAllWindows();


	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值