Ratus
Ratus

Reputation: 107

Convert a list of images to grayscale using OpenCV

I have a pickled training dataset containing 32x32 RGB images. As a preprocessing step, I want to convert them to grayscale. I read in the training dataset as -

import pickle
import cv2

training_file = "../data/train.p"

with open(training_file, mode='rb') as f:
    train = pickle.load(f)

X_train, y_train = train['features'], train['labels']

Then, I try using a for loop to convert each of the images to grayscale using the following code -

for i in range(0,len(X_train)-1): 
    X_train[i] = cv2.cvtColor(X_train[i], cv2.COLOR_BGR2GRAY) 

But this throws the following error -

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-a920a94faefc> in <module>()
      1 for i in range(0,len(X_train)-1):
----> 2     X_train[i] = cv2.cvtColor(X_train[i], cv2.COLOR_BGR2GRAY)

ValueError: could not broadcast input array from shape (32,32) into shape (32,32,3)

However, if I repeat this procedure using just a single image, I don't get any error. I did the following - convert single image to grayscale

Can someone explain the reason for the error and how I could convert the entire list to grayscale?

Upvotes: 4

Views: 5458

Answers (1)

Sunreef
Sunreef

Reputation: 4542

Your problem comes from the fact that you're trying to store grayscale images with shape (32, 32) in an array that stores RGB images with shapes (32, 32, 3).

X_train_grayscale = np.zeros(X_train.shape[:-1])
for i in range(X_train.shape[0]): 
    X_train_grayscale[i] = cv2.cvtColor(X_train[i], cv2.COLOR_BGR2GRAY) 

This creates a new array called X_train_grayscale that will contain your grayscale images after converting.

(Of course you need the usual import numpy as np at the top of your Python script to make it work.)

Upvotes: 3

Related Questions