This is the sixth day of my participation in the August Text Challenge.More challenges in August

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.

Unexpectedly, what I learned today is the conversion of color image and gray image. After the previous learning, have you mastered two ways of converting color images into grayscale images, like me? On the first day of 2021, I will learn one way.

Color images are converted to grayscale images

In the first way, the parameter is directly set to 0 when the image is read by imread, and the color image is automatically converted to gray image. In the second way, channel separation can be carried out through split, or called reading a single channel, or a color image can be separated into three single channel gray image

The way to do this today is through a method called cvtColor.

The cv2.cvtColor() method is used to convert an image from one color space to another. OpenCV provides more than 150 color-space conversion methods. Too many to use

The syntax format for this method is:

cv2.cvtColor(src, code[, dst[, dstCn]])
Copy the code

Parameters:

  • SRC: This is the image whose color space is to be changed.
  • Code: This is the color space conversion code.
  • DST: This is an output image of the same size and depth as the SRC image. This parameter is optional.
  • DstCn: This is the number of channels in the target image. If the argument is 0, the number of channels is automatically derived from SRC and the code, optional.

Parameters translated into Chinese, also found

CvtColor (SRC, DST,code,dstCn) ===> (original image, color conversion code, output image, output channel)Copy the code

The gray scale conversion code is as follows:

import cv2

# path
path = './7_1.jpg'

# fetch image
src = cv2.imread(path)

# Picture display window name
window_name = 'Image'

# BGR to grayscale
image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

cv2.imshow(window_name, image)

cv2.waitKey()
Copy the code

Also see the HSV format image, the conversion result is as follows, which is a little scary, by the way, I converted some other formats:

import cv2
import matplotlib.pyplot as plt
# path
path = './7_1.jpg'

# fetch image
src = cv2.imread(path)

# Picture display window name
window_name = 'Image'

# BGR converts to RGB
image1 = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)

# BGR converts to Gray
image2 = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

# BGR to HSV
image3 = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

plt.subplot(1.3.1)
plt.imshow(image1)
plt.title("RGB")

plt.subplot(1.3.2)
plt.imshow(image2,"gray")
plt.title("GRAY")

plt.subplot(1.3.3)
plt.imshow(image3,"hsv")
plt.title("hsv")

plt.show()
Copy the code

False color image

Color images can be turned into grayscale images, and the corresponding grayscale images can also be turned into color, of course, here is the false color image.

This part of the content because of the current application scenario is not clear, I will post you a blog I see in the learning process.

Blog 1 blog 2 Blog 3

The description of false color image can be directly referred to in Baidu Encyclopedia.

Thank you for your direction

OpenCV end

An hour has passed. Have you mastered the Python OpenCV related knowledge points?

In your spare time, you can subscribe to eraser’s crawler course to learn crawler knowledge.

Blogger ID: Dream eraser, hope you like, comment, favorites.