OpenCV-python之通道的分离和合并

1.分离

'''法一:
用opencv自带函数split进行分离'''

import cv2
import numpy as np

img = cv2.imread("./bd.jpg")

b, g, r = cv2.split(img) 
'''其中split返回RGB三个通道,如果只想返回其中一个通道,可以这样:
   b = cv2.split(img)[0]
   g = cv2.split(img)[1]
   r = cv2.split(img)[2]
'''

cv2.imshow("Blue", r)
cv2.imshow("Red", g)
cv2.imshow("Green", b)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''法二:
用numpy '''

b = np.zeros((img.shape[0] ,img.shape[1]), dtype=img.dtype)
g = np.zeros((img.shape[0] ,img.shape[1]), dtype=img.dtype)
r = np.zeros((img.shape[0] ,img.shape[1]), dtype=img.dtype)
b[: ,:] = img[: ,: ,0]
g[: ,:] = img[: ,: ,1]
r[: ,:] = img[: ,: ,2]


2.合并

'''法一'''
merged = cv2.merge([b, g, r])
print("Merge by OpenCV")
print(merged.strides)
print(merged)
'''法二'''
mergedByNp = np.dstack([b, g, r])
print("Merge by NumPy ")
print(mergedByNp.strides)
print(mergedByNp)

cv2.imshow("Merged", merged)
cv2.imshow("MergedByNp", merged)

cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV-python之通道的分离和合并_第1张图片

说明:这些知识也比较基础,好好学习,继续努力!

你可能感兴趣的:(OpenCv)