opencv入门7:拆分和合并渠道-splitting and merging channels

彩色图像由多个通道组成:红色,绿色和蓝色组件
拆分图像颜色通道使用cv2.split(image)方法
合并 使用 cv2.merge([B,G,R]) 方法

拆分和合并渠道splitting_and_merging.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
26
27
28
29
import numpy as np 
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required =True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
(B,G,R) = cv2.split(image)
# 我们在RGB色彩空间中考虑图像 - 红色像素第一,绿色像素第二,蓝色像素第三。
#但是,OpenCV以反向通道顺序将RGB图像存储为NumPy数组。而不是以RGB顺序存储图像,而是以BGR顺序存储图像;因此我们按照相反的顺序解开元组
cv2.imshow("Red",R)
cv2.imshow("Green",G)
cv2.imshow("Blue",B)

merged = cv2.merge([B,G,R])
cv2.imshow("Merged",merged)
cv2.waitKey(0)
cv2.destroyAllWindows()

zeros = np.zeros(image.shape[:2],dtype="uint8")
cv2.imshow("Red",cv2.merge([zeros,zeros,R]))
cv2.imshow("Green",cv2.merge([zeros,G,zeros]))
cv2.imshow("Blue",cv2.merge([B,zeros,zeros]))
cv2.waitKey(0)
# 为了显示频道的实际“颜色”,我们首先需要使用cv2.split来分割图像。
# 然后,我们需要重新构建图像,
# 但这次设置所有像素为0,除了当前通道


把时间当朋友。
坚持原创技术分享,您的支持将鼓励我继续创作!