[OpenCv] c++ image primary operation | image grayscale

1. Image

1. Image information

Q: How are images stored in the computer? A: In the computer, a commonly used matrix is ​​used to represent a digital image with a size of , and the value of the matrix element is the pixel value at the corresponding position of the image.
M x NM x N

For the color images in the local disk of the computer, right-click on a single computer and select "Properties", and you can see the basic information of an image.

2. Types of images

1) Binary image:

Q: What is a binary image? A: A two-dimensional matrix of a binary image consists of only two values ​​of 0 and 1, "0" represents black, and "1" represents white. Binary images can be regarded as a special case of grayscale images. Binary images are usually used for scanning recognition (OCR) of text and line drawings and storage of mask images.

2) Grayscale image:

Q: What is a grayscale image? A: A grayscale image is an image with only one sampled color per pixel, usually displayed as a gray scale from the darkest black to the brightest white. Grayscale images are different from black and white images. In the field of computer graphics, black and white images only have two colors, black and white, but grayscale images have many levels of color depth between black and white.

3) Color map:

Q: What is a color map? A: Each pixel of a color image is usually represented by three components: red (R), green (G), and blue (B). The color value of each pixel of the image (represented by the three primary colors of RGB) is directly stored in the image matrix. Since the color of each pixel needs to be represented by three components of R, G, and B, M and N represent the number of rows and columns of the image respectively. The two-dimensional matrix of each represents the R, G, B three color components of each pixel. The data type of the image is generally an 8-bit unsigned integer, which is usually used to represent and store a true-color image.
RGBM x NRGB

2. Image conversion

1. Separate the three channels of the color image

Color images are generally stored as 3-channel images in computers. Each pixel of an image is quantized by a vector consisting of three numbers, most commonly by three components of R, G, and B. RGBThe values ​​of these three elements of the model represent the brightness of the three primary colors, red, green, and blue, respectively. We can splitseparate these three channels with a function.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;
int main() {
    
    
	Mat srcImage = imread("...\\dog.jpg");
	if (!srcImage.data)
		return 1;
	imshow("srcImage", srcImage);
	vector<Mat> planes;
	split(srcImage, planes);
	imshow("B", planes[0]);
	imshow("G", planes[1]);
	imshow("R", planes[2]);
	waitKey(0);
	return 0;
}

The running result is shown in the figure

In color image processing, the channels are generally separated first, each single channel is processed separately and then merged.

2. Image grayscale processing

#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;
int main() {
    
    
	//读取源图像并转化为灰度图像
	cv::Mat srcImage = imread("...\\dog.jpg");
	if (!srcImage.data)
		return 1;
	//读取源图像并转化为灰度图像
	Mat srcGray;
	cvtColor(srcImage, srcGray, CV_RGB2GRAY);
	//显示源图像及灰度图像
	imshow("srcImage", srcImage);
	imshow("srcGray", srcGray);
	waitKey(0);
	return 0;
}

The running result is shown in the figure :

Guess you like

Origin blog.csdn.net/Ceylan__/article/details/129388555