Giving this image:
If we do image[:, :, 1]
, we see:
While doing cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
:
They are slightly different, but just slightly.
Is there any specific reason for using the array accessing [:, :, 1]
instead of the cv2.cvtColor()
call?
Actually, what does [:, :, 1]
exactly do? Can you explain in a few words I better understand?
My final purpose is to extract text from some images. Should I stick to some of the two explained methods? Or should they be quite the same?
[:, :, 1]
is a representation of Red Green Blue, so it's just another way of saying rgb(0, 0, 1)
. cv2.cvtColor()
on the other hand is also a change of the colorspaces, but with cv2.COLOR_BGR2GRAY
as the parameter, you actually use grayscales instead.
If you loaded the image with OpenCV , image[:,:,0]
will be the Blue channel, image[:,:,1]
will be the Green channel and image[:,:,2]
will be the Red channel. I am saying OpenCV uses BGR
ordering.
If you opened the image with PIL/Pillow or almost any other module, image[:,:,0]
will be the Red channel, image[:,:,1]
will be the Green channel and image[:,:,2]
will be the Blue channel. I am saying the rest of the world uses RGB
ordering.
Now, look at the red pencil, it will be bright white in the Red channel because there is lots of red in it. Look at the blue pencil, it will be bright white in the Blue channel. Look at the green pencil, it will be bright white in the Green channel.
If you convert to greyscale, the Red, Green and Blue channels are mixed in certain percentages rather than being selected exclusively in their entirety:
grey = 0.30*R + 0.59*G + 0.11*B
So, if you use cvtColor(...,BGR2GRAY)
the greens will show up lightest (because they have a bigger weighting), then the reds, and the blues will be darkest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.