Splitting Image using OpenCV in python
That is as simple as loading an image using cv2.imread
and then use cv2.split
:
>>> import cv2
>>> import numpy as np
>>> img = cv2.imread("foo.jpg")
>>> b,g,r = cv2.split(img)
OpenCV documentation is available from docs.opencv.org
As mentioned in the documentation tutorial, the cv2.split() is a costly operation in terms of performance(time) if you don't want to operate on all the channels but only one/two, so the numpy indexing is preferred:
import cv2
import numpy as np
img = cv2.imread("foo.jpg")
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
Remember that opencv reads the images as BGR instead of RGB
Edit: @Mitch McMabers, Thanks for pointing this out. Please use this method for max efficiency if you want to work on just one/two channels separately. If you want to operate on all three channels, access the channels using cv2.split() as mentioned in @jabaldeno's answer.