OpenCV_01 图像的读取与显示

本文详细介绍了OpenCV中用于图像读取的`imread`函数及其参数,包括不同读取模式对图像的影响。同时,讲解了`namedWindow`函数创建和设置图像显示窗口的方法,以及`imshow`函数如何将图像显示在指定窗口。通过示例代码展示了如何使用这些函数完成基本的图像读取与显示操作。
摘要由CSDN通过智能技术生成

OpenCV_01 图像的读取与显示

函数解读

  • 图像读取函数 imread
Mat imread( const String& filename, int flags = IMREAD_COLOR );
//filename: 需要读取的文件名称
//flags: 读取图像形式的标志
       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.

  • 图像窗口函数 namedWindow
void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
//winname:窗口名称;
//flags:窗口属性设置标志
       WINDOW_NORMAL     = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
       WINDOW_AUTOSIZE   = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.
       WINDOW_OPENGL     = 0x00001000, //!< window with opengl support.

       WINDOW_FULLSCREEN = 1,          //!< change the window to fullscreen.
       WINDOW_FREERATIO  = 0x00000100, //!< the image expends as much as it can (no ratio constraint).
       WINDOW_KEEPRATIO  = 0x00000000, //!< the ratio of the image is respected.
       WINDOW_GUI_EXPANDED=0x00000000, //!< status bar and tool bar
       WINDOW_GUI_NORMAL = 0x00000010, //!< old fashious way

  • 图像显示函数 imshow
void imshow(const String& winname, InputArray mat);
//winname: 窗口名称
//mat: 图像矩阵

代码

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


//图像的读取与显示
int main() {
	cv::Mat img = cv::imread("demo.png", cv::IMREAD_UNCHANGED);  
	if (img.empty())
	{
		cout << "请确认图片文件是否正确" << endl;
		return -1;
	}
	cv::namedWindow("show Image", cv::WINDOW_AUTOSIZE);
	cv::imshow("show Image", img);
	cv::waitKey(0);
	return 0;

}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值