opencv image grayscale

  •  

  • Image grayscale is to map the brightness value (R, G, B) of the image to the gray value between 0-255 in a certain way. In order to make the image look less monotonous, the brightness value of the image needs to be adjusted. transform. The following is a brief introduction to the grayscale function in opencv: 1. First convert the pixel value of the image into three components of R, G, and B, where the R component is used for image grayscale transformation, and the G component is used for color transformation. 2. Then according to the grayscale transformation formula: 3. According to the formula, the grayscale value of each pixel can be calculated. 4. Finally, the grayscale of the image is completed by corresponding these grayscale values ​​to the value of each pixel. 1. First convert the image into three components of R, G, and B. 3. Finally, the gray value of each pixel is mapped to the corresponding color channel.

    • 1. Using the image.palindrome library in opencv, call the sobel function to obtain the pixel value, and then use the grayscale transformation function to calculate the grayscale value.

      1. First use the sobel function in the image.palindrome library to obtain the pixel value, and then use the grayscale transformation function to calculate the grayscale value. 2. In opencv, if you want to perform grayscale transformation on a certain pixel of the image, you can directly use the grid function (this is a function with the same name as the class file name). The grid function realizes the unification of the gray value of multiple pixels in the image. 3. In opencv, we can realize image grayscale through the reverse and convert methods in the openCV library. 4. The specific operation is as follows: 1) First divide the image into multiple pixel groups with the same pixel value, and each pixel group can be regarded as a pixel with the same gray value and a gray value of 1. For example, in the image.palindrome library, We divide the image into four pixel groups: Each pixel group corresponds to a grayscale transformation function, as shown in the above code. 2) Then use the reverse and convert methods to calculate the grayscale transformation value of each pixel group respectively, and obtain the grayscale value of each pixel group by calling the reverse function; 3) Finally, combine the calculated grayscale value with all the grayscale values ​​in the image The gray values ​​of all pixels in the image can be obtained by multiplying the gray values ​​of the pixels. 5. You can directly call the grid function to calculate the gray value of all pixels in the image: 6. However, this method will divide the image into a large gray area, and cannot handle some relatively small areas. For example, there is a point in the picture, which can be regarded as a pixel group with a gray value of 1 in the image, then we can directly use this point as the segmentation boundary: 7. Image segmentation can be realized through the symbols function. The symbols function is the result of calculating the grayscale transformation of an entire image. 8. For the convenience of understanding, we divide the whole image into four parts of the same size, which are red, green, blue and white. These four parts correspond to different color channels: 9. The red channel corresponds to the red gray value; the green channel corresponds to the green gray value; the blue channel corresponds to the blue gray value; the white channel corresponds to the white gray value. The grayscale transformation formula corresponding to the four color channels can be obtained by calculation:

    • 2. Use the opencv-benchmark library in opencv to set the pixel value and the corresponding channel threshold, and then use the gamma function in the opencv-benchmark library to calculate the gray value of each pixel.

      3. In opencv, if you input a picture, you can directly use the sobel function to output a grayscale picture. 4. We can use the gamma function in the opencv-benchmark library to map the brightness value of the image to a 255 gray value. 5. There is a function in the opencv-benchmark library that is a mapping function from one RGB color space to another RGB color space, which can map the grayscaled color channel of the image to an RGB color space.

    • 3. Normalize the gray value of each pixel in opencv, and then use the sobel function to obtain the color channel of each pixel.

      According to the formula, the R component can be transformed into any value between 0-255, that is, the R component can be transformed into any value between 0-255. Because R and G are color components, they can correspond to each pixel. 2. Then normalize the three components of R, G, and B. Normalization refers to the process of mapping the RGB value of an image according to its gray value in opencv. The RGB components in the three RGB pixels are composed of brightness values ​​of three different colors, where the first brightness value R represents the color of the pixel (red, green and blue); the second brightness value G represents the color of the pixel. The color of the pixel (yellow, blue, green); the third brightness value B represents the color of the pixel (black and white). The normalized RGB three pixels can be mapped to three different channels. Since the R component and the G component are mapped to the same color channel, they are relatively independent. 3. Then map these three color channels to any channel between 0-255, that is, map three pixels of RGB to three different color channels. Finally, the grayscaled image is obtained. Calculate which color channel each pixel corresponds to according to the formula, and then connect these three channels to get the final result. The grayscale transformation is complete. 4. We can use opencv to realize the operation of obtaining the gray value after the gray scale transformation of the image. The following is the code for the gray value corresponding to the RGB three color channels after the gray scale is realized by using opencv: 5. Then use sobel The function maps these grayscale values ​​corresponding to three different color channels to the RGB color channel. Because the sobel function obtains which color channel each pixel corresponds to is determined by opencv's automatic calculation, so the sobel function does not need to be implemented by ourselves. 9. Through the above introduction, we can see that the gray value obtained after the gray scale transformation corresponds to the three color channels of RGB are determined by opencv automatic calculation.

    • 4. Use the color channels corresponding to these grayscale values ​​as new color channels, and then perform grayscale transformation processing on the new color channels.

      Grayscale transformation is a basic operation in image processing, which maps the brightness value of a grayscale image to a grayscale value between 0-255. By performing grayscale transformation on the image, grayscale values ​​corresponding to different brightness values ​​can be obtained. In image processing, the grayscale image can be easily converted into a color image by using the grayscale transformation function, so as to facilitate the subsequent processing of the image. After the grayscale transformation of the original image, a new color image can be obtained, and color images are often used by people in real life, such as color printing, color TV, color photos, etc. The grayscale function in opencv can convert a grayscale image into a matrix of color channels (RGB). Grayscale transformation can be performed on one pixel point, or grayscale transformation on multiple pixel points. For example, given an image with a grayscale of 9: By calculating the grayscale values ​​of 9 pixels and the corresponding color channels, 9 new color channels can be obtained. Subsequent processing of these new color channels as a new RGB matrix can obtain the desired color effect.

The following are several commonly used OpenCV image grayscale codes:

1. Use the cvtColor function to convert the image to a grayscale image:
```python
import cv2
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image ', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. Use the cv2.imread function to read the image and use the cv2.cvtColor function to convert the image to a grayscale image:
```python
import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('Gray Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. Use the dot function of numpy to convert the image For grayscale images:
```python
import cv2
import numpy as np
img = cv2.imread('image.jpg')
gray_img = np.dot(img[..., :3], [0.299, 0.587, 0.114])
cv2.imshow('Gray Image', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
above These are several commonly used OpenCV image grayscale codes, and you can choose the appropriate method according to your needs.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130824526