Splitting of RGB color space - using OpenCV split and arrays

Splitting of RGB color space - using OpenCV split and arrays

Generate RGB graphics

In principle, the RGB color image matrix is ​​stored in the form of pixels. The following example will draw three circles in the three primary colors of red, green and blue. It should be paid special attention that the format of pyplot display image is RGB(A), so color space conversion is required cv2.cvtColor(image,cv2.COLOR_RGB2BGRA)

Reference Code

from matplotlib import pyplot as plt
import cv2
import numpy as np
# 生成一个 640*480 的 BGR 三个通道的彩色图片,底色全为0
canvas = np.zeros((480, 640, 3), dtype='uint8')
image_path='../data/diy.jpg'
# 圆半径为 90 pixel
radius = 90
# 配色为 BGR
blue = (255, 0, 0)
green = (0, 255, 0)
red = (0, 0, 255)
# 圆边厚度为 10 pixel
thickness = 10
# 画出 红绿蓝三个图片
image = cv2.circle(canvas, (320, 200), radius, red, thickness)
image = cv2.circle(canvas, (220, 300), radius, green, thickness)
image = cv2.circle(canvas, (420, 300), radius, blue, thickness)
# 因为 pyplot 显示图片的格式为 RGB(A) 所以要进行颜色空间转换
toGBR = cv2.cvtColor(image,cv2.COLOR_RGB2BGRA)
# the image in the BGR 8-bit format
cv2.imwrite(image_path,canvas)
plt.figure(figsize=(8,12))
plt.subplot(211), plt.title('BGR format')
plt.imshow(image) # image 为 BGR 8-bit 格式,而 plt 以 RGB 方式显示,所以颜色会不对
plt.subplot(212), plt.title('RGB(A) format')
plt.imshow(toGBR) # toGBR 为 RGB(A) 格式,这才是正确的配色
plt.show()

insert image description here
Figure 1. Showing the difference between BGR and RGB(A) formats, the color matching of the graphics on the right hand side is correct

Split RGB channels to display in grayscale

Divide the color picture according to the BGR channel. The following example displays a separate picture for each channel of the diy.jpg picture generated above. From the order of display, it can be found that the channels are red (0), green (1), blue (2).

Reference Code

from matplotlib import pyplot as plt
import cv2
#IMREAD_COLOR 会将图片以 BGR 8-bit 格式读入
img1 = cv2.imread('../data/diy.jpg',cv2.IMREAD_COLOR)
# 将图片依 BGR 分割成三个通道
channels = cv2.split(img1)
# color in BGR
titles=['Blue', 'Green', 'Red']
figure, plots = plt.subplots(ncols=3, nrows=1,figsize=(12,6))
for i, subplot in zip(range(3), plots):
    subplot.imshow(channels[i],'gray')
    subplot.set_axis_off()
    subplot.set_title(titles[i])
plt.show()

insert image description here
Figure 2. Split BGR color space

Split RGB channels to display in color

The processing steps are:

  1. Load a color image and read it in BGR 8-bit format (opencv)
  2. Split independent color channels (opencv)
  3. Create a blank image (np)
  4. Give the color of a specific channel to a blank image
  5. Convert to color space of plt (opencv)
  6. Display results using plt (matplotlib)

Reference Code

from matplotlib import pyplot as plt
import numpy as np
import cv2
#加载彩色图像
img = cv2.imread('../data/diy.jpg',cv2.IMREAD_COLOR)
# 分割独立的颜色通道
channels = cv2.split(img)
# color in BGR
titles=['Blue', 'Green', 'Red']
figure, plots = plt.subplots(ncols=3, nrows=1,figsize=(12,6))
for i, subplot in zip(range(3), plots):
    # 创建空白图片
    temp = np.zeros(img.shape, dtype='uint8')
    # 将特定通道的颜色给予空白图片
    temp[:,:,i] = channels[i] # img[:,:,i]
    # 转换成 plt 的颜色空间
    toGBR = cv2.cvtColor(temp,cv2.COLOR_RGB2BGRA)
    subplot.imshow(toGBR)
    subplot.set_title(titles[i])
    subplot.set_axis_off()
plt.show()

insert image description here
Figure 3. Respective RGB channels displayed in color

If it is for a picture with a white background, such a display method usually cannot achieve the desired effect, as shown in the code below, because the white background will cause the entire single-channel display to show the background color of a single channel, which is not easy to highlight out the target. In addition, the red, blue and green in human eyes is often the result of mixing, so if you want to find out the red, blue and green colors separately, you usually use the HSV color space.

from matplotlib import pyplot as plt
import numpy as np
import cv2
#加载彩色图像
img = cv2.imread('../data/b.png',cv2.IMREAD_COLOR)
channels = cv2.split(img)
# color in BGR
titles=['Blue', 'Green', 'Red']
figure, plots = plt.subplots(ncols=4, nrows=1,figsize=(12,4))
for i, subplot in zip(range(4), plots):
    if i < 3:
        temp = np.zeros(img.shape, dtype='uint8')
        temp[:,:,i] = channels[i] # img[:,:,i]
        toGBR = cv2.cvtColor(temp,cv2.COLOR_RGB2BGRA)
        subplot.imshow(toGBR)
        subplot.set_title(titles[i])
        subplot.set_axis_off()
    else:
        toGBR = cv2.cvtColor(img,cv2.COLOR_RGB2BGRA)
        subplot.imshow(toGBR)
        subplot.set_title('Origin')
        subplot.set_axis_off()
plt.show()

insert image description here
Figure 4. Respective RGB channels displayed in color

References

  • OpenCV API, https://docs.opencv.org/
  • Getting Started with Images, https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html
  • matplotlib.pyplot.subplot, https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot
  • matplotlib.pyplot.imshow, https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html

Guess you like

Origin blog.csdn.net/m0_50614038/article/details/129899226