Opencv matrix mask operation

Preface

This article introduces the matrix mask operation of opencv.

1. Basic operation of image:

1. Definition of image: An image refers to the actual scene picture captured by the input device or any picture (composed of pixels) stored in digital form. Pixel is the smallest unit that makes up an image, and each pixel is composed of multiple (usually 3) points of different colors (usually red, green, and blue)
Insert picture description here

2. There are many basic operations on images, such as: +, -, *, /, bit operations, square root, logarithm, absolute value, etc.;
3. The image can also be enlarged, reduced, rotated, and part of it can be intercepted as ROI (Region of Interest) for operation;
4. Each color channel can be extracted separately and various operations can be performed on each color channel* (for example, RGB has 3 color channels, all of which can be operated).Insert picture description here

2. What is a mask?

1. The composition of the mask: The mask is a binary image composed of 0 and 1.
It can be simply understood as a matrix composed of 0 and 1.

2. Definition of mask: Use selected images, graphics or objects to occlude the processed image (full or partial) to control the area or process of image processing.

       In OpenCV, the mask operation is relatively simple. The rough idea is to recalculate the value of each pixel in the image through a mask matrix. The mask matrix controls the influence of the current position of the old image and surrounding pixels on the current position of the new image. In mathematical terms, we define a weight table.

3. Mask operation example: We simply use the AND operation (&) to give an example:
Operation method: Each pixel in the original image and each corresponding pixel in the mask are ANDed.
Insert picture description here

3. Mask realizes the adjustment of image contrast:

1. Mask operation calculation formula: Contrast adjustment calculation formula
2. Opencv syntax:

  2.1 Get the row pointer ptr function of the matrix const uchar *current = src.ptr(row);
FF
  2.2 Get the number of channels per pixel of the matrix int offsetx = src.channels();

  2.3 Pixel point value processing function, saturate_cast (uchar value) function is to ensure that the RGB value range is between 0 and 255.
     Pixel range processing:
     saturate_cast (-1), less than 0 returns 0
     saturate_cast (256), greater than 255 returns 255
     saturate_cast (100), returns 100

  2.4 Function call filter2D function
      definition mask:
      Mat kernel = (Mat_(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
      filter2D( src, dst, src.depth(), kernel ); Among them, src and dst are Mat type variables, src.depth() represents the depth of the bitmap, there are 32, 24, 8, etc., and the kernel is a Mat object.

3. Code implementation:

  3.1 Custom flooding operation

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

using namespace std;
using namespace cv;
int main() {
    
    
	Mat src, dst;
	src = imread("C:\\Users\\ASUS\\Desktop\\1.jpg");
	if (!src.data) {
    
    
		cout << "could not  load image!";
		return 0;
	}
	namedWindow("my_screen", WINDOW_AUTOSIZE); //创建一个窗口,自动大小不可人为改变
	imshow("my_screen", src);

	int cols = src.cols * src.channels();    //cols获取图像的列,因为每个像素有3个像素通道,channels获取像素通道
	int offsetx = src.channels();            //channels获取像素通道
	int rows = src.rows;                     //rows获取像素的行
	dst = Mat(src.size(), src.type());       //创建一个原图像大小,形式的空间
	for (int row = 1; row < rows - 1; row++) {
    
      
		const uchar* current = src.ptr<uchar>(row - 1); //获取上一行的指针
		const uchar* previous = src.ptr<uchar>(row);
		const uchar* next = src.ptr<uchar>(row + 1);  //获取下一行的指针
		uchar* output = dst.ptr<uchar>(row);
		for (int col = offsetx; col < cols; col++) {
    
    
			output[col] = saturate_cast<uchar>(5 * current[col] -
				(current[col - 1 - offsetx] + current[col + offsetx] + previous[col] + next[col]));
			//利用像素处理函数,处理越界数值
		}
	}

	namedWindow("contrast image demo", WINDOW_AUTOSIZE);
	imshow("contrast image demo", dst);
	waitKey(0);

	return 0;
}

  3.2 Function call filter2D function operation:

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

using namespace std;
using namespace cv;
int main() {
    
    
	Mat src, dst;
	src = imread("C:\\Users\\ASUS\\Desktop\\1.jpg");
	if (!src.data) {
    
    
		cout << "could not  load image!";
		return 0;
	}
	namedWindow("my_screen", WINDOW_AUTOSIZE); //创建一个窗口,自动大小不可人为改变
	imshow("my_screen", src);

	//用opencv的API  filter2D
	double t = getTickCount();
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	filter2D(src, dst, src.depth(), kernel);
	double timeconsums = (getTickCount() - t) / getTickFrequency();
	cout << "time consume " << timeconsums; //用来计算时长

	namedWindow("contrast image demo", WINDOW_AUTOSIZE);
	imshow("contrast image demo", dst);
	waitKey(0);

	return 0;
}

4. Processing result:Insert picture description here

Four, summary:

The matrix mask in this article is a variety of bit operations between two images.

If there is an error, please advise!

Guess you like

Origin blog.csdn.net/ivan_9/article/details/113059961