OpenCV-Python Development Guide (1)---How to brighten the image

Basic representation of images

Before realizing how to brighten the image, we need to understand the basic representation of the image. In computers, images are divided into three types: binary images, grayscale images and color images.

The binary image refers to an image that only contains two colors of black and white. For example, in the program, in order to represent the number A, we can use the following grid-like data set to represent it, as shown in the following figure:
Binary image
where 0 represents black , 1 represents white, so we can determine what we want to display, but it has no color, no shade, and only shape.

The second type of image is a grayscale image, which can draw rough character shapes, content styles, etc. through shades, but it has no color. The effect is the same as above, except that the value of each square is not 0 and 1, but [0,255].

The last type is a color image, which adds color to the gray image. Although it can also be represented by the binary image above, one of the squares is not 0 and 1, nor is it [0,255], but [[ 0,255],[0,255],[0,255]]. In this way, it becomes a three-dimensional matrix.

For the basic knowledge of images, we will stop here. Below, each of our blog posts will be a combination of actual combat and theory. This article mainly explains the addition of images and realizes the brightness adjustment of the images.

Two addition operations for images

For image processing, addition operations are relatively basic operations. The complex image processing is done through these basic knowledge. So, for the opening chapter of OpenCV, we will introduce its addition operation in detail.

In the process of image processing, OpenCV provides us with two addition operations to the image. One is to process directly through the "+" sign, and the other is to process through the add function.

"+" and add function

Before understanding the "+" operator, we need to understand the composition of RGB image colors. As we all know, whether it is red, blue or green, the maximum value set by the program is 255. So now there is a tricky question, what if the addition is greater than 255?

For the "+" operator, if it is greater than 255, you need to take the remainder. For example, if the green value is 222, 55 should be added, so the final value is not 277, but 21.

This is not the case with the add function. The addition of images is realized through add. There is no saying that the remainder is taken. Anything greater than 255 after addition is set to 255 uniformly. Knowing this, we can use the addition operation very flexibly.

Get an image

Now that we understand the addition operation of images, let's get an image for operation. The code is as follows:

import cv2
img = cv2.imread("1.jpg", 1)

Here, we import the OpenCV library through import cv2, and then get the image, here we need to pay attention to the second parameter. The blogger here specifically lists a table for everyone to understand the meaning of the second parameter.

Numerical value meaning
-1 Keep the original format unchanged (that is, if it is a grayscale image, then it is a grayscale image, if it is a color image, then it is a color)
0 Adjust the image to a single-channel grayscale image
1 Adjust the image to a 3-channel BGR image, this is the more five-year value
2 When the depth of the loaded image is 16-bit or 32-bit, the corresponding depth image is returned; otherwise, it is converted to an 8-bit image
4 Read the image in any possible color format
8 Load image using gdal driver

The above are the parameter values ​​that we commonly use. Of course, not only these parameters are the only ones, but there are also a lot of parameters for reducing the size of the image after obtaining the image. If you are interested, you can consult the development documentation.

Next, we will display the acquired image, the specific code is as follows:

import cv2

img = cv2.imread("1.jpg", -1)
cv2.imshow("图片", img)#显示图像,参数1为窗口标题,2为获取的图像,中文乱码后续讲解
cv2.waitKey()#等待用户按下
cv2.destroyAllWindows()#释放所有窗口

The above comments are detailed enough, so I won’t repeat them here. After running, the displayed effect is as follows:
Display image

Adjust image brightness

To adjust the image brightness, we generally use the add function, but in order to distinguish the difference between the two additions, we calculate and display the image at the same time. The specific code is as follows:

import cv2

img = cv2.imread("1.jpg", -1)
symbol_img=img+img
add_img=cv2.add(img,img)
cv2.imshow("图片1", symbol_img)
cv2.imshow("图片2", add_img)
cv2.waitKey()
cv2.destroyAllWindows()

After running, the displayed effect is as shown in the figure below (left "+", right add):
Compared

The remainder of the left image is greater than 255, which causes the originally bright pixels to become darker. And because the image on the right uses the add function without taking the modulus, the value greater than 255 is uniformly assigned to 255, so every detail will only be brighter than the original image.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113760323