how to calculate mean of images in dataset

Question:

I Have data set contain 2060 image i want to calculate the mean and then (each image – mean) as normalization

num_classes = 10
lable = [0,1,2,3,4,5,6,7,8,9]
X_data = []
Y_data = []
size = 100
#mean_sum = np.zeros((100,100))
#std_sum = np.zeros((100,100))
#print(mean_sum.shape)
for file in data:
   folder = os.listdir(dir +'\' + file)
   for f in folder:
       x=cv2.imread(dir +'\' + file+'\'+f) #read each image in each folder
       z=cv2.resize(x,(size,size))#resize image to 100*100 
       gray = cv2.cvtColor(z,cv2.COLOR_BGR2GRAY) #convert to gray
       gray = gray.astype(float)/255 #divide by 255
       y=to_categorical(lable[int(file)],num_classes)
       #mean = cv2.mean(z)
       #print(mean.shape)
       #mean_sum += np.squeeze(mean)    
       X_data.append(np.array(gray)) #append x in X_data list
       Y_data.append(y) #append y in Y_data list

this is my code i tried to calc the mean but when i use cv2.mean the shape of mean (3,1) and my mean_sum array shape (100,100) and image shape is (100,100) so i got error different shapes
how can i solve it

Asked By: Ahmed Abd El-Samie

||

Answers:

Are you trying to calculate the mean for each pixel across the 2060 images? Or are you trying to calculate the mean of all pixels in each image?

Your current code seems to be doing the latter, that is, calculating the mean of all the pixel values in a single image.

But the way you framed your question, I am believing you want the former?

If that is the case, one of the easiest way to get it done from current code, will be to use the mean_sum to just add the actual pixel values, i.e., z and then later divide by number of images (2056):

for file in data:
   folder = os.listdir(dir +'\' + file)
   for f in folder:
       x=cv2.imread(dir +'\' + file+'\'+f) #read each image in each folder
       z=cv2.resize(x,(size,size))#resize image to 100*100 
       gray = cv2.cvtColor(z,cv2.COLOR_BGR2GRAY) #convert to gray
       gray = gray.astype(float)/255 #divide by 255
       y=to_categorical(lable[int(file)],num_classes)
       #mean = cv2.mean(z)
       #print(mean.shape)
       mean_sum += z
       X_data.append(np.array(gray)) #append x in X_data list
       Y_data.append(y) #append y in Y_data list
    mean_sum /= len(folder)
Answered By: Bipin Lekhak
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.