So I'm doing this course and there's this exercise where I have to blur an image using a 3x3 area of the array and swapping out all values to the average. So I wrote this function which I know is still does not work completely:
def replace_avg(img, block=3):
x_dim, y_dim = img.shape
for row in range(1,x_dim-block+2,3):
for col in range(1,y_dim-block+2,3):
img[row-(block-2):row+(block-1),col-(block-2):col+(block-1)] = np.average(img[row-(block-2):row+(block-1),col-(block-2):col+(block-1)])
return img
My question is is there a more efficient way of looping through this array with a 3x3 filter using numpy?