Basic representation of images

Before realizing how to lighten an image, we need to understand the basic representation of an image. In the computer, images are divided into three types: binary images, gray images and color images.

Where, binary image refers to the image containing only black and white colors. For example, in the program, in order to represent the number A, we can represent it by the following data set arranged in A grid, as shown in the figure below:

Where 0 is black, 1 is white, so we can be sure what we want to display, but it doesn’t have color, it doesn’t have shade, it just shows shape.

The second type of image is the grayscale image, which can be drawn in shades to give general shapes, content styles, etc., but it has no color. The effect is the same as above, except that instead of zeros and ones, each square has a value of [0,255].

Finally, there is the color image, which is the addition of color to the grayscale image, although it can also be represented as the binary image above, but one of the squares is not 0 and 1, nor [0,255], but [[0,255],[0,255],[0,255]. So this is going to be a three-dimensional matrix.

So much for the basics of images. Next, each of our blog posts will be a combination of practical and theoretical, focusing on image addition and brightness adjustment.

Two kinds of graph addition operations

For image processing, the addition operation is the basic operation. And complex image processing, are through these basic knowledge to complete. So, for the opening of OpenCV, we’ll go into the details of addition.

In the process of image processing, OpenCV provides two kinds of image addition operations. One is handled directly by the “+” sign, and the other is handled by the add function.

+ and the add function

Before we can understand the “+” operator, we need to understand the color composition of an RGB image. As we all know, whether it’s red, blue or green, the maximum value is set programmatically to 255. So now we have a tricky problem, what if the addition is greater than 255?

For the + operator, if it is greater than 255, the remainder is taken. For example, the green value was 222, and now you add 55, so instead of 277, you end up with 21.

But the add function is not like this, through add to achieve the image addition operation, there is no saying to take the remainder, after the addition of more than 255, the unified set to 255. Now that we know that, we can be very flexible with addition.

Get an image

Now that we know the image addition operation, let’s get an image to operate, as follows:

import cv2
img = cv2.imread("1.jpg".1)
Copy the code

In this case, we import the OpenCV library by import cv2, and then we get the image. Note the second parameter here. The blogger has a table to show you what the second parameter means.

The numerical meaning
– 1 Keep the original format (i.e., if it’s grayscale then it’s grayscale, if it’s color then it’s color)
0 The image is adjusted to a single channel gray image
1 Adjust the image to a 3 channel BGR image, this is the more 5 year value
2 When the depth of the loaded image is 16 or 32 bits, the corresponding depth image is returned; Otherwise, convert it to an 8-bit image
4 Read the image in any possible color format
8 Load the image using the GDAL driver

These are the parameters we often use. Of course, there are not only these parameters, but also a lot of parameters to reduce the size of the image. You can query the development document if you are interested.

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

import cv2

img = cv2.imread("1.jpg", -1)
cv2.imshow("Image", img)# display image, parameter 1 is the title of the window, parameter 2 is the image obtained, Chinese garbled characters will be explained later
cv2.waitKey()Wait for the user to press
cv2.destroyAllWindows()Release all Windows
Copy the code

The above comments are detailed enough that they won’t be repeated here. After running, the display looks like this:

Adjust image brightness

We generally use the add function to adjust the image brightness, but in order to distinguish the difference between the two kinds of addition, 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("Picture 1", symbol_img)
cv2.imshow("Photo 2", add_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the display effect is as shown in the following figure (left “+”, right add) :

The remainder of the left image greater than 255 is taken, resulting in the originally bright pixels becoming darker. The image on the right will only be brighter than the original image because the add function does not need to take the modulus, and the value is assigned to 255 if the value is greater than 255.