Post reference: Splitting of an RGB image to R G B using OpenCv Python I'm quite new to using opencv and numpy and simply don't understand how Method #1 works to both copy the image and change channels to black. This is a copy of the code section I don't understand:
r = img.copy()
r[:,:,0] = r[:,:,1] = 0
g = img.copy()
g[:,:,0] = g[:,:,2] = 0
b = img.copy()
b[:,:,1] = b[:,:,2] = 0
Could someone post an explanation that a 75+ years -old can understand? Thank you for any assistance!
The code clearly works...I'm simply trying to understand how the colors are separated / merged within the numpy array
OpenCV stores images as Numpy arrays, like this:
with the channels in B, G, R order. So channel 0 is blue, channel 1 is green and channel 2 is red.
So this command will set the blue channel to 0 for every row and every column, i.e. it will zero out the blue component:
This will zero out the green everywhere:
So, you'll be left with a 3-channel image in which the red component is the only one with anything in it, since both blue and green will be empty.
So, as an example, if you start with this:
and you do this:
you'll get:
You could do both in one go, by the way, using "fancy indexing":