'opencv: Invalid number of channels in input image

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?



Solution 1:[1]

Seems like this works:

grayscale = open_cv_image[:, :, 0]

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dalireeza
Solution 2 balu