CV - 色彩空间 转换/分离/合并


一、颜色空间转换

cvtColor(src, code[, dst[, dstCn]]) -> dst

  • src:原始图像
  • code:转换的代码或者标识
  • 返回值:已完成转换的图像

lena = cv2.imread('lena.jpg')

lena_gray = cv2.cvtColor('lena.jpg', cv2.COLOR_BAYER_RG2GRAY)
cv_show(lena_gray)

img.shape # (H, W, C)    
#  (200, 200, 3)

二、分离 split

b,g,r = cv2.split(img)
r
array([[223, 222, 223, ..., 235, 232, 202],
       [225, 224, 223, ..., 236, 234, 198],
       [225, 226, 226, ..., 233, 222, 178],
       ...,
       [ 90,  92,  93, ..., 148, 153, 158],
       [ 87,  91,  89, ..., 156, 165, 165],
       [ 82,  94,  92, ..., 159, 172, 177]], dtype=uint8)
cv_show(b)
cv_show(g)
cv_show(r)
r.shape
# (200, 200)

img=cv2.merge((b,g,r))
img.shape # (200, 200, 3)
# 只保留 R 通道

cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
# cv_show('R',cur_img)

cur_img
'''
array([[[  0,   0,  91],
        [  0,   0,  91],
        [  0,   0,  91],
'''

array([[[  0,   0,  91],
        [  0,   0,  91],
        [  0,   0,  91],
        ...,
        [  0,   0, 122],
        [  0,   0, 122],
        [  0,   0, 122]],

        ...,
        [  0,   0, 120],
        [  0,   0, 121],
        [  0,   0, 121]]], dtype=uint8)
# 只保留 G
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0

cv_show('G',cur_img)
# 只保留 B
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0

cv_show('B',cur_img)

三、合并 merge


img2 = cv2.merge([b, g, r])

posted @ 2021-02-04 10:35  月思  阅读(183)  评论(0编辑  收藏  举报