Ask Your Question
0

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 -0600

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]

cv2.imshow("Image_Use0",img)

cv2.moveWindow("Image_Use0",0,0)

Method 2

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

cv2.imshow("mage_cvtColor",gray)

cv2.moveWindow("Image_cvtColor",width,0)

Note:

'''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

Comments

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

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

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

Stats

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

Seen: 517 times

Last updated: Nov 21 '18