Opencv C++ introductory learning 1. Reading, displaying and saving images

Resources required for this article: Link: https://pan.baidu.com/s/1KhVOe_J25R_fmfXLldaQjA?pwd=1024 
Extraction code: 1024 
This series of articles is a learning essay for me to learn Opencv for the first time. If there are any omissions, please forgive me.

This series of articles has no commercial purpose and is for educational purposes only.

1. Reading, displaying and saving images

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

using namespace cv;
using namespace std;

int main()
{
	Mat image;   //创建一个空图像image
	image = imread("D://lena.png", IMREAD_COLOR);  //读取文件夹中的图像  此处IMREAD_COLOR为加载彩色影像,默认可不写。

	//检测图像是否加载成功
	if (image.empty())  //检测image有无数据,无数据 image.empty()返回 真
	{
		cout << "Could not open or find the image" << endl;
		return -1;
	}

	namedWindow("输入的图片", WINDOW_NORMAL);  //创建显示窗口,不加这行代码,也能显示,只不过窗口大小不能改变,这里的WINDOW_NORMAL参数使得我们可以随意调节窗口大小
	imshow("输入的图片", image);  //在窗口显示图像

	imwrite("1.png", image); //保存图像为png格式,文件名称为1

	waitKey(0);  //暂停,保持图像显示,等待按键结束

	return 0;

}

8ff8d8323b7d4d7ebf8c70a1fe71c402.png

The result of the above code is as shown above.

It should be noted here if

96f7f9e53b314ef9bfac3ec8fc27382f.png

The names in namedWindow and imshow are inconsistent, which will lead to the following situations:

fe8225cba2724c76996996b6cccb62b1.pngThe outer IMAGE window can be scaled freely, but lena's picture is in non-zoomable mode by default.

Function introduction:

1.1 imread function

Use imread() to read images. imread contains two parameters:

imread(image path, image form);

There are three image forms:

1. Load color images (default loading form)

imread(image path, IMREAD_COLOR);

or:

imread(image path, 1);

2. Load grayscale mode image

imread(image path, IMREAD_GRAYSCALE);

or:

imread(image path, 0);

3. Load the image, including alpha channel

imread(image path, IMREAD_UNCHANGED);

or:

imread(image path, -1);

1.2 namedWindow function

Function: The function of namedWindow() is to create a new display window to display images.

namedWindow() contains two parameters:

namedWindow(window name, window form)

There are two commonly used window forms:

1. The displayed image size cannot be changed (default form)

namedWindow(window name, WINDOW_AUTOSIZE)

2. The image size can be adjusted

namedWindow(window name, WINDOW_NORMAL)

1.3 imshow function

Function: The imshow function is used to display images.

The imshow() function contains two parameters:

imshow(window name, image name)

1.4 imwrite function

Function: The imwrite function is used to display images.
The imwrite() function contains two parameters:
imwrite (save image name and format, image name)

Guess you like

Origin blog.csdn.net/w2492602718/article/details/133976963