Convert 3D array image into 2D array

sayres kabir

I am trying to import an image by from tensorflow.keras.preprocessing import image.My image is hand written digit that I wrote on a paper and then I took a picture from it by mobile and then changed its size to 28*28:

Hand written digit 7

I used the following code :

img_width, img_height = 28, 28
img = image.load_img('rgb_seven.jpeg', target_size=(img_width, img_height))
img_tensor = image.img_to_array(img)
img_tensor.shape

The shape result is:

(28, 28, 3)

Seems the image is loaded as a 3D array. I need a 2D array, so I do :

x_image = img_tensor.reshape(len(img_tensor),-1)
x_image.shape

result is:

(28, 84)

Why 84? I need 28 because I want to flat this to insert as input layer.

What is problem?

Mr. For Example

You should load image using color_mode='grayscale'

img = image.load_img('rgb_seven.jpeg', color_mode='grayscale', target_size=(img_width, img_height))

It will give you shape (28, 28, 1)

Then using x_image = img_tensor.reshape(28, 28) will give you shape (28, 28)

The reason you get shape (28, 84) is because the reshape() will not drop the dimension, so if you want using img_tensor.reshape(28,-1) to reshape array with the (28, 28, 3) it will return an array as combine the last two dimensions thus you get shape (28, 28 * 3)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related