Reputation: 99
My program takes in an image and splits it into a grid (of smaller images), then the mean RGB value is worked out at each window of the grid.
I am trying to work out the difference between the mean_val
(mean RGB) of a window and the previous window in the sequence, to find the change in colour between neighbouring windows ((x+1) - x) but I am unsure how to implement this.
Here is my code:
#import packages
import numpy as np
import cv2
#Read in image
img = cv2.imread('images/0021.jpg')
#print img.shape
scale = 9
#Get x and y components of image
y_len,x_len,_ = img.shape
#cv2.imshow('Original',img)
#cv2.waitKey(5000)
mean_values = []
for y in range(scale):
for x in range(scale):
#Crop image scale*scale windows
cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale,(x*x_len)/scale:((x+1)*x_len)/scale]
mean_val=cv2.mean(cropped_img)
mean_val=mean_val[:3]
#set pixel values of each window to the meanBGR
cropped_img[:,:,:] = mean_val
#Print mean_values array
mean_values.append([mean_val])
mean_values=np.asarray(mean_values)
print mean_values.reshape(3,scale,scale)
cv2.imshow('output',img)
cv2.waitKey(0)
#cv2.imwrite('images/BGR_90.jpg',img,[int(cv2.IMWRITE_JPEG_QUALITY), 90])
cv2.destroyAllWindows()
Here is the output image of my program:
Thank you for reading and any help is greatly appreciated :)
Upvotes: 1
Views: 586
Reputation: 136
Use this list comprehension before you change diff_mean
to an array.
diff_mean=[[mean_values[i][j]-mean_values[i-1][j] for j in range(3)] for i in range(1,len(mean_values))]
Also if mean_val
is a tuple, you should change mean_values.append([mean_val])
to mean_values.append(mean_val)
Edit: Or if you want to have the differences between each window without the difference between the rightmost window and the leftmost window in the row below, you could do this (after transforming to array):
diff_mean=mean_values[:,:,:-1]-mean_values[:,:,1:]
Edit 2: updated code to work for a list of 3-element tuples / and also for a 3D array.
Upvotes: 1