Python OpenCV 灰階轉彩色(Gray to RGB/BGR)

本篇將介紹如何使用 OpenCV 與 Python 來灰階轉彩色(Gray to RGB 或 Gray to BGR),在寫 Python 影像處理程式時常會用到 OpenCV cvtColor 作顏色空間轉換的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來進行 Gray to RGB/BGR 灰階轉彩色。

什麼情況會用到灰階轉彩色 COLOR_GRAY2RGB?
當你的原圖是灰階單一channel,但又想在上面畫有色彩的東西時,這時就可以將原本的灰階單一channel轉成三個channel,再去作處理。

範例. 灰階轉彩色

灰階單個channel 轉RGB三個channel

使用 cv2.cvtColor 轉換顏色空間時,第二個參數與灰階轉彩色相關的有:
COLOR_GRAY2RGB
COLOR_GRAY2BGR

opencv-gray-to-rgb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

gray = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
# (1 channel)
print(gray.shape)
print(gray.ndim)

gray_three_channel = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# (3 channel)
print(gray_three_channel.shape)
print(gray_three_channel.ndim)

# Fill a contour on both the single channel and three channel image
# (left up, right up, right down, left down)
contour = np.array([[200, 200], [400, 200], [400, 400], [200, 400]], np.int32)
yellow_color = [0, 255, 255] # BGR
cv2.polylines(gray, pts=[contour], isClosed=True, color=yellow_color, thickness=3)
cv2.polylines(gray_three_channel, pts=[contour], isClosed=True, color=yellow_color, thickness=3)

cv2.imshow('Input', gray)
cv2.imshow('Result', gray_three_channel)
cv2.waitKey(0)

輸出

1
2
3
4
(512, 512)
2
(512, 512, 3)
3

結果如下圖所示:

cv2.cvtColor 參數的詳細細節請參考這裡

範例2. 在單channel與三channel填色的差異

使用 cv2.cvtColor 將單個 channel 轉換成三個channel,
或者使用 cv2.merge 將單個 channel 合併成三個channel,

opencv-gray-to-rgb2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# Create random color image
image = (np.random.standard_normal([200, 200, 3]) * 255).astype(np.uint8)

# Convert to grayscale (1 channel)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray_three_channel = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# Merge channels to create color image (3 channels)
#gray_three_channel = cv2.merge([gray,gray,gray])

# Fill a contour on both the single channel and three channel image
contour = np.array([[10, 10], [110, 10], [110, 110], [10, 110]])
yellow_color = [0, 255, 255] # BGR
cv2.fillPoly(gray, [contour], yellow_color)
cv2.fillPoly(gray_three_channel, [contour], yellow_color)

cv2.imshow('Input', image)
cv2.imshow('Gray', gray)
cv2.imshow('Gray 3 channel', gray_three_channel)
cv2.waitKey()

結果如下圖所示:

參考
How does one convert a grayscale image to RGB in OpenCV (Python) for visualizing contours after processing an image in binary? - Stack Overflow
https://stackoverflow.com/questions/21596281/how-does-one-convert-a-grayscale-image-to-rgb-in-opencv-python-for-visualizing cv2.COLOR_GRAY2RGB Python Example
https://www.programcreek.com/python/example/81595/cv2.COLOR_GRAY2RGB

其它相關文章推薦
Python OpenCV 彩色轉灰階(RGB/BGR to GRAY)
Python OpenCV 彩色轉HSV(RGB/BGR to HSV)
Python OpenCV 彩色轉YCbCr(RGB/BGR to YCbCr)
Python OpenCV 影像二值化 Image Thresholding
Python OpenCV 影像平滑模糊化 blur
Python OpenCV 影像邊緣偵測 Canny Edge Detection
Python OpenCV resize 圖片縮放
小專案 Python OpenCV 圖片轉字元圖畫