[OpenCV in C++] Lesson 4 - Common operations on OpenCV images (1): Deep learning of Mat objects, grayscale, ROI

1. Deep learning of the image object itself

1.1 Mat object and ROI

    This is a taste of technical experience, so it is not a specific function, but a deepening and review of the content that you have been exposed to in the previous part, to help you enter the field of visual processing more deeply.

1.1.1 Create an explicit Mat object

  • Deeper understanding of Mat objects

    We have multiple ways of acquiring digital images from the real world: digital cameras, scanners, computed tomography and magnetic resonance imaging, to name a few. In any case, what we (humans) see are images. However, when converting this to a digital device, what we record is the numerical value of each point in the image.

insert image description here
    For example in the image above you can see that the mirror image of the car is nothing more than a matrix containing all the intensity values ​​of the pixels. How we fetch and store pixel values ​​may vary depending on our needs, but in the end, all images within the computer world may be reduced to a matrix of numbers and other information describing the matrix itself . OpenCV is a computer vision library whose main focus is processing and manipulating this information . So, the first thing you need to be familiar with is how OpenCV stores and processes images.
    So it can be simply understood that an image is an n-dimensional matrix .

insert image description here
Among them, the commonly used constructors mainly include the following types:
1. Mat (int rows, int cols, int type, const Scalar &s)

  • Parameter explanation:
//This is an overloaded member function, 
//provided for convenience. 
//It differs from the above function only in what argument(s) it accepts.
// 这是一个为了更加便捷而提供的重载成员函数,
//和上述构造函数的区别仅仅在于所接受的参数个数的不同

//Parameters  成员参数:
//rows:	一个2维数组的行数
//cols:	 一个2维数组的列数
//type:	这是一个重要的参数,关于这个参数的值(宏),解释放在下边,大家一定要引起重视:
	//原文:Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
		// or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.
	//翻译:该参数表示数组的类型(矩阵的类型),
		//由CV_8UC1...CV_64FC4来创建1~4通道的矩阵,
		//或者使用CV_8UC(n), ..., CV_64FC(n)来创建多通道的矩阵。
	/*
	CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]
		其中,The number of bits per item为每个矩阵数字的位数
				(实则规定十进制数字的大小,这里需要大家对二进制转换10进制比较了解才可);
		Signed or Unsigned为是否有符号(数据的类型);
		The channel number为通道的个数
		Type Prefix为前缀
	例如:CV_8UC3意味着我们使用8位长的无符号字符类型,每个像素有三个通道。这是最多四个通道号预定义的。
	*/
//s:	一个用于初始化矩阵元素的初始化的可选值. 旨在在构造之后完成对所有矩阵元素的值设定为特定值的参数, 使用赋值操作来完成这个动作.
  • Remarks: The Size object in its variant cv::Mat::Mat(Size size,int type,const Scalar & s ) is also an expression of a size object, which can be simply understood as a Size object composed of length, width and height (of course, for complex As far as the image is concerned, the way of understanding is correct, but the specific expression needs to be modified after understanding)

2、Mat (int ndims, const int *sizes, int type, const Scalar &s)

  • Parameter explanation:
//ndims:维度
//sizes:是一个整型数组,且数组中的元素不可变,维度是n就有n个元素,
	//每个元素的大小表示每个维度上的尺寸。
//type: 同上
//s:同上
	/*
	例如:
    int sz[3] = {2,2,2};
    Mat L(3,sz, CV_8UC(1), Scalar::all(0));
    这个对象就是一个3维的,每个维度大小为2,且每一个维度8位无符号的图像
	*/

3、Mat (const Mat &m)

  • Parameter explanation:
// m:另外一个Mat对象,拷贝构造函数。

4、Mat (const Mat &m, const Rect &roi)

  • Parameter explanation:
// 这里涉及到一个非常重要的参数:
// roi:感兴趣的区域,其类型为Rect,这个类型的对象就是一个矩形对象(方框)。
	//当然,感兴趣的区域不一定是方形的(可以是圆的...等),注意融会贯通。

Other constructors can be integrated as long as they understand the above four.

1.1.2 Region of interest ROI

  • What is ROI: (region of intrest) area of ​​interest, what is the purpose? It is designed to select the region of interest on the image for other operations. is a flexible technique.
  • how to use?
    In fact, a way has been quietly demonstrated to you above:
// 利用Rect来完成对Mat对象上感兴趣部分的截取:
Mat (const Mat &m, const Rect &roi) // 就是利用这个构造函数即可,其余的方式在后续的实战中慢慢积累,但是基础很重要,了解本质即活学活用了。
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
	//其中Rect中的参数可以理解为x,y(正方形的起始点坐标【左上顶点】,两个10的含义),正方形区域的长宽(两个100的含义)
Mat E = A(Range::all(), Range(1,3)); // 利用矩阵行列也可以选择矩形区域,用的没有Rect多

2. Image grayscale processing

2.1 Concept

  • What is grayscale processing?
    Taking RGB color pictures as an example, the processing method that makes the three-channel colors equal (R=G=B) is called grayscale processing. If RGB is (0,0,0) it is pure black (0 means no color, that is, every Each channel is black, so it is black in the end), if it is (255,255,255) it is white (the brightest)
  • Why do grayscale processing?
    Generally, grayscale processing is used for image processing by default, because color processing is very difficult and computationally intensive, and it is prone to errors and time-consuming. After the processing is completed, it can be switched back to color.

2.2 cvtColor() function

  • Function prototype:

void cv::cvtColor	(	InputArray 	src,
						OutputArray 	dst,
						int 	code,
						int 	dstCn = 0 
					)	
  • Function function: The color space conversion format of the image is not limited to grayscale conversion, and has a wide range of uses.
  • Parameter explanation:
    • src : original image
    • dst : the processed image
    • code : the conversion mode of the color space
    • dstCn: the number of channels of the target image, the default is 0 (the value does not need to be set if it is not changed), if it is 0, it means that the number of channels of src is automatically used
  • Explanation on the code parameter value: it determines the mode of color conversion, there are many ways, the original URL is attached for reference: The red box in the code parameter value list
    insert image description here is the parameter for grayscale conversion of the BGR image, the following is similar, everyone according to the actual situation The situation can be selected.

2.3 Examples

Mat img = imread("image.jpg");
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", grey);
waitKey();

Follow-up, we continue to learn. Continually updated.

Guess you like

Origin blog.csdn.net/weixin_43520503/article/details/129156752