0
$\begingroup$

I want to transfer X-ray data images to grayscale in this code

from sklearn.preprocessing import StandardScaler
import numpy as np

# Reshape the input data from (batch_size, height, width, channels) to (batch_size, height * width * channels)
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

X_train = np.reshape(X_train, (X_train.shape[0], 224, 224, 1))
X_test = np.reshape(X_test, (X_test.shape[0], 224, 224, 1))

but I'm getting this error

ValueError                                Traceback (most recent call last)
<ipython-input-22-3c5fff7f26c8> in <cell line: 12>()
     10 X_test = scaler.transform(X_test)
     11 
---> 12 X_train = np.reshape(X_train, (X_train.shape[0], 224, 224, 1))
     13 X_test = np.reshape(X_test, (X_test.shape[0], 224, 224, 1))

2 frames
/usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     55 
     56     try:
---> 57         return bound(*args, **kwds)
     58     except TypeError:
     59         # A TypeError occurs if the object does have such a method in its

ValueError: cannot reshape array of size 47717376 into shape (317,224,224,1) 
$\endgroup$

2 Answers 2

0
$\begingroup$

The error you're seeing is because the sizes of the thing you're trying to reshape don't match the sizes you're trying to reshape into. I'm guessing that your input has 3 channels here? Instead of

X_train = np.reshape(X_train, (X_train.shape[0], 224, 224, 1))
X_test = np.reshape(X_test, (X_test.shape[0], 224, 224, 1))

what happens if you

X_train = np.reshape(X_train, (X_train.shape[0], 224, 224, 3))
X_test = np.reshape(X_test, (X_test.shape[0], 224, 224, 3))

Also, if you want to convert color images to grayscale, please check the other threads on this: https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python. Personal preference is to do this on input as mentioned in one of the lower answers:

from skimage import color
from skimage import io

img = color.rgb2gray(io.imread('image.png'))
$\endgroup$
0
$\begingroup$

Converting an image from RGB to grayscale means combining the data from the three channels into a single channel. You are currently trying to do the conversion by simply reshaping the array, for this to be possible the number of output values needs to equal to the number of input values. This is not the case when converting an image to grayscale as you are combining three channels into one, leaving you with one-third of the number of values and losing information in the process. The number of input values is exactly three times the amount of values in the target output (47717376 vs. 317 x 224 x 224 = 15905792). To do the conversion you can for example use the cvtColor function from the opencv-python library:

import cv2

cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.