2

i am trying to load an grayscale image with opencv2, but somehow the color channel gets completely removed.

When i am doing:

img = cv2.imread(PATH_TO_IMAGE, 1) // load image as rgb 

img.shape returns for example (28,28,3)

But if i am loading the same image as grayscale:

img = cv2.imread(PATH_TO_IMAGE, 0) // load image as grayscale 

img.shape returns (28,28), but i would expect/need a shape of (28,28,1)

Is this a bug in opencv or am i missing something?

Thanks in advance!

Tim
  • 10,459
  • 4
  • 36
  • 47

1 Answers1

2

OpenCV does behave like that in Python when loading an image in grayscale. If you need to add on a channel in order to make it (28,28,1), you may do

import numpy as np
img = img[:, :, np.newaxis]
cmastudios
  • 146
  • 1
  • 3