Home > Mobile >  opencv: Invalid number of channels in input image
opencv: Invalid number of channels in input image

Time:03-09

I have an image with this shape:

(300,512,2)

I want to convert it to grayscale, I'm using this code:

grayscale = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)

but getting an error:


> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 2

I understand that cvtColor requires 3 channels. But I have only 2. What can I do now?

CodePudding user response:

Seems like this works:

grayscale = open_cv_image[:, :, 0]

CodePudding user response:

BGR to GRAY expects 3 channels . But your image has 2 channels and it will not work.

When you do

grayscale = open_cv_image[:, :, 0]

you are considering first channel as gray.

  • Related