Avatar of kalyangkm
kalyangkmFlag for United States of America

asked on 

Applying grayscale conversion to list of numpy array of images using Python

Hi,

I have .npy file which is numpy version of the images.
The shape of numpy image data set is
images_all.shape
(4750, 128, 128, 3)

I need help to convert the images_all numpy array which is having colored images into grayscale images using Python?



Artificial IntelligencePython

Avatar of undefined
Last Comment
kalyangkm
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

import scipy.misc
import scipy.ndimage

# Load an example image
# Use scipy.ndimage.imread(file_name, mode='L') if you have your own
img = scipy.misc.face()

# Convert the image
R = img[:, :, 0]
G = img[:, :, 1]
B = img[:, :, 2]
img_gray = R * 299. / 1000 + G * 587. / 1000 + B * 114. / 1000

# Show the image
scipy.misc.imshow(img_gray)

Open in new window

https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python
Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

Hi David, thanks for the response, but I would need help in converting not just one image but multiple images which are in the form of numpy array in a .not file and thr dimensions as mentioned. I am having trouble creating an empty numpy array of shape of  the original with depth 1 as it is grayscale and also in appending the converted grayscale array into the overall numpy dataset
Avatar of ste5an
ste5an
Flag of Germany image

Well, what about posting a concise and complete example? That would help us much more than a description.
Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

Hi Ste5an,

Yes, I was waiting for someone to ask for that. Thank You. PFA is the complete notebook (CNN_Plan_Seedings2.ipynb)

CNN_Plant_Seedings2.ipynb

The data set used in this is the Kaggle data set from "https://www.kaggle.com/c/plant-seedlings-classification"
but instead of taking raw images it is converted into numpy array which is in the form of images.npy and along with that have another .csv which is having just the labels (Lables.csv).

The CNN model works if I just use the color images but I am interested in converting all the images in the images.npy file into gray scale and then apply the model. I am not good at using numpy arrays may be that is the reason I am struggling.

The following section is where I am struggling to convert the X_train images into gray scale images

from skimage.color import rgb2gray
X_gray = np.zeros(X_train.shape[128:128:1])
for i in range(X_train.shape[0]):
  X_gray[i] = rgb2gray(i)
  #X_gray[i] = cv2.cvtColor(X_train[i], cv2.COLOR_BGR2GRAY)


Open in new window


Please also find the google drive link for the data sets images.npy and Lables.csv.

https://drive.google.com/drive/folders/1JNLrmmbrjIErdnhJLXoJEDjfbZqvx-LI?usp=sharing

Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

Please note though I have not including the following in the notebook, I was able to achieve the color to gray scale of one image using the following code. But as I mentioned i would need it for the entire list of numpy images in the images.npy

i = 10
image = images_all[i]
label = label_data[i]
print(f'Label \n Label name: {label}')
plt.imshow(image);

Open in new window

from skimage.color import rgb2gray
# calculating the illuminance apparantly gives us the gray scale of the image
im_gray = rgb2gray(image)

# the module to visualize the image
# we  decide the display size of the visualisation
plt.figure(figsize = (10,4))

# In the current figure, create and return an Axes, at position index of a (virtual) grid of nrows by ncols axes. Indexes go from 1 to nrows * ncols, incrementing in row-major order.
# If nrows, ncols and index are all less than 10, they can also be given as a single, concatenated, three-digit number.
plt.subplot(1,2,1)

#displays the image on the axis
plt.imshow(image)

#turns off the axis and the labels
plt.axis('off')

plt.title("the original image")

#if the value of parameters in the arguements are less than 10 then they can be written in the sequence
plt.subplot(122)

#for gray scale image a corresponding parameter for Cmap is passed
plt.imshow(im_gray,cmap = 'gray')

plt.axis('off')

# the display of the grayScale image
plt.title("grayScaleImage")

# tight_layout automatically adjusts subplot params so that the subplot(s) fits in to the figure area.
plt.tight_layout()

plt.show()



Open in new window

then you need to loop through each item in the array, convert to grayscale, write to a new array
Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

hi David, that's what I need help on as I am getting confused with multi dimensional arrays like these 3 D images.
Avatar of ste5an
ste5an
Flag of Germany image

We need to see the structure of your code for concrete advice.

Thus where you load the data and how you process it.
Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

hi Ste5an,

I have given the entire notebook. please check my reply to you earlier.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kalyangkm
kalyangkm
Flag of United States of America image

ASKER

Hi Ste5an,

I thought it would be annoying with dataset, I should have reduced the images. sorry for the trouble. I implemented your code, but looks like the though the dimensions have changed to (128, 128, 1), the image doesn't look Grayscale, its still sort of colored but with different colors. PFA is the updated notebook,(regarding the Jupiter notebook, I have limitation in the laptop I am working on that is the reason I am using it, I am actually using google collab.)

CNN_Plant_Seedings_Gray_Scale.ipynb

original