opencv python (bgr)3通道拆分合并方法合集

 

1、拆分方法1(利用函数)

import cv2

img = cv2.imread("./images/1.png")
(B, G, R) = cv2.split(img)

cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue", B)
cv2.waitKey(0)

 

2、拆分方法2(利用切片)

import cv2

img = cv2.imread("./images/1.png")
B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]

cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue", B)
cv2.waitKey(0)

 

3、拆分结果 

 opencv python (bgr)3通道拆分合并方法合集_第1张图片

 

 4、合并方法1(利用函数)

import cv2

img = cv2.imread("./images/1.png")
(B, G, R) = cv2.split(img)
merged = cv2.merge([B, G, R])
cv2.imshow("Merged", merged)
cv2.waitKey(0)

 

  5、合并方法2(利用切片)

img = cv2.imread("./images/1.png")
(B, G, R) = cv2.split(img)
merged = np.ones(img.shape, dtype=np.uint8)
merged[:, :, 0] = B
merged[:, :, 1] = G
merged[:, :, 2] = R
cv2.imshow("Merged", merged)
cv2.waitKey(0)

 

  6、合并方法3(利用转置)

import numpy as np
import cv2

img = cv2.imread("./images/1.png")
(B, G, R) = cv2.split(img)
merged = np.array([B, G, R])
merged = merged.transpose([1, 2, 0])
cv2.imshow("Merged", merged)
cv2.waitKey(0)

 

  7、合并结果

 opencv python (bgr)3通道拆分合并方法合集_第2张图片

 

 

 

 

 

你可能感兴趣的:(Python)