3. OpenCV image processing

3. OpenCV image processing3.1. Image grayscale3.2. Image binarization3.3. Image edge detection3.4. Line Segment drawing3.5. Draw Rectangles3.6. Draw the circle3.7. Draw the ellipse3.8. Draw polygons3.9. Draw text

Before running the sample program, you need to switch to the directory where the code is located. SSH connects the car, runs at the terminal,

3.1. Image grayscale

Gray-scale processing is the process of transforming a color image into a gray-scale image. Color image is divided into R, G, B three components, respectively, showing a variety of colors such as red, green and blue, grayscale is the process of making the color R, G, B components equal. Pixels with large gray values are brighter (the maximum pixel value is 255, which is white), and darker (the lowest pixel is 0, which is black). The core idea of image grayscale is R = G = B, which is also called grayscale value.

1) Maximum method: the value of R, G, B after conversion is equal to the largest one of the three values before conversion, that is: R=G=B=max (R, G, B). The brightness of the gray image converted by this method is very high.

2) Average value method: the value of R,G,B after conversion is the average value of R,G,B before conversion. That is, R=G=B=(R+G+B)/3. The gray image produced by this method is relatively soft.

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685006656772

3.2. Image binarization

The core idea of binarization is to set a threshold, greater than the threshold is 0 (black) or 255 (white), so that the image is called black and white. The threshold can be fixed or adaptive. The adaptive threshold is generally the comparison of a point pixel with the average value of a region's pixels or a weighted sum of Gaussian distributions, where a difference can be set or no difference can be set.

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685006697718

3.3. Image edge detection

Image edge detection can significantly reduce the data size of the image while retaining the original image attributes. Among the commonly used edge detection methods, Canny edge detection algorithm is one of the methods with strict definition and can provide good and reliable detection. Because it satisfies the three standards of edge detection and has the advantages of simple implementation process, it has become one of the most popular algorithms for edge detection.

Canny edge detection algorithm can be divided into the following 5 steps:

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685006729232

3.4. Line Segment drawing

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685006812102

3.5. Draw Rectangles

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685006872474

3.6. Draw the circle

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685007033215

3.7. Draw the ellipse

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685007360141

3.8. Draw polygons

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685007398443

3.9. Draw text

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685007435120