I use this command to convert an open cv image from RGB to grayscale:
img = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY) # Grayscale image
Printing the shape before this command returns the following:
(64, 128, 3)
And after the grayscale function:
(64, 128)
However it should be like this:
(64, 128, 1)
Can anyone explain what is going on here and how I can get my image to a proper format? Because if I pass this image on to Tensorflow it gives me an error due to wrong input shape. Tensorflow expects an image with the following format (width, height, channels). Channels is either 3 for rgb or 1 for grayscale. In my case however I don't have any channels after the conversion to grayscale. And I don't know why
CodePudding user response:
Input images are 3 channels but gray scales have one channel.
import cv2
img = cv2.imread("HHH.jpg")
print(f"img.shape={img.shape}")
gray = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY) # Grayscale image
print(f"gray.shape={gray.shape}")
output:
img.shape=(960, 1280, 3)
gray.shape=(960, 1280)
If you need to feed images in (H, W, 1) dimension to TensorFlow, so change your dimension by expanding dimension in TF: This code adds a one channel to last dimension of data
tf.expand_dims(image, -1).shape.as_list()