Sunday, October 11, 2015

Python + OpenCV: split and merge - to exchange color channel

Example to use OpenCV split() and merge() to separate color channel, and re-construct image with red and blue color channel exchanged.


import sys
import cv2

print("Python version: \n" + sys.version)
print("cv2 version: " + cv2.__version__)

#img1 and img2 must be in same size
img1 = cv2.imread('Raspberry_Pi_Logo.png', 1)
b,g,r = cv2.split(img1)
img2 = cv2.merge((r,g,b))

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('r', r)
cv2.imshow('g', g)
cv2.imshow('b', b)
cv2.waitKey(0)
cv2.destroyAllWindow()


No comments: