Ask Your Question

why load an image with input 0 gives a different result that converting the same image with input 1 to grey scale with opencv crtcolor method?

asked 2018-11-21 21:58:40 -0500

John O gravatar image

In Open CV with python, I wrote the following script:

import numpy as np

import cv2

'''Compare imread( 'detect_blob.png',0) with imread('detect_blob.png',1) and convert to gray scale'''

Method 1

img = cv2.imread('detect_blob.png',0)

width = img.shape[1]



Method 2

gray = cv2.cvtColor(cv2.imread('detect_blob.png',1), cv2.COLOR_RGB2GRAY)




'''When display, Method 1 has lighter grays than the Method 2,

does this means that this two conversion of a RGB image to Gray Scale image, handle the conversion in different ways?

If so, why, and should I learn more about it?'''

Thank you so much

cv2.waitKey(0) cv2.destoryAllWindows()

edit retag flag offensive close merge delete


Your answer is in the doc

When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available. Results may differ to the output of cvtColor()
LBerger gravatar imageLBerger ( 2018-11-22 04:30:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-21 23:38:14 -0500

berak gravatar image

opencv uses BGR pixel order, not rgb, as you assume.

so the correct conversion flag must be cv2.COLOR_BGR2GRAY

if you load an image as grayscale, using cv2.IMREAD_GRAYSCALE, the internal conversion from libpng or libjpg will be used, so again results might differ.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-11-21 21:57:50 -0500

Seen: 327 times

Last updated: Nov 21 '18