I am doing some image processing and one of the things I need to do is calculate the mean and standard deviations of 5×5 squares of pixels and assign those values to an array with the same index as the central pixel of the 5×5 square (the mean values and standard deviations can go to separate 2D arrays). At the moment the way I’m doing this is:
for i in range(2, num_rows+2, 1):
for j in range(2, num_cols+2, 1):
#enter pixel values of 5x5 cluster into a list
pixel_list = [padded_grey[i-2,j-2],padded_grey[i-2,j-1],padded_grey[i-2,j],padded_grey[i-2,j+1],padded_grey[i-2,j+2],padded_grey[i-1,j-2],padded_grey[i-1,j-1],padded_grey[i-1,j],padded_grey[i-1,j+1],padded_grey[i-1,j+2],padded_grey[i,j-2],padded_grey[i,j-1],padded_grey[i,j+1],padded_grey[i,j+2],padded_grey[i+1,j-2],padded_grey[i+1,j-1],padded_grey[i+1,j],padded_grey[i+1,j+1],padded_grey[i+1,j+2],padded_grey[i+2,j-2],padded_grey[i+2,j-1],padded_grey[i+2,j],padded_grey[i+2,j+1],padded_grey[i+2,j+2],padded_grey[i,j]]
#calculate mean and standard deviation of values in list and enter into index of central pixel
mean_vals[i-2,j-2] = np.mean(pixel_list)
stddev_vals[i-2,j-2] = np.std(pixel_list)
The reason why the for loops are shifted over by 2 indices is that the array padded_grey is zero-padded to prevent "Out of Range" errors when I am near the edge of the array. Now of course this takes forever to finish executing since I have to go through an entire 1024×1280 array pixel by pixel.
Could someone please suggest some ways I could optimise this to run faster? Thank you in advance.
>Solution :
Use sliding_window_view:
window_view = np.lib.stride_tricks.sliding_window_view(pixel_array, (5, 5))
window_view_mean = window_view.mean(axis=(1, 2))
window_view_std = window_view.std(axis=(1, 2))