So I'm trying to create a function that will apply a 5-by-5 smoothing kernel to a 2D image data that will return a 2D Numpy array with the same shape as data. Here's what I have so far.
def applyFilter( data, kernel ):
ret = np.zeros_like(data)
KI = kernel.shape[0]//2
KJ = kernel.shape[1]//2
d=np.zeros_like(kernel)
for i in range(KI, data.shape[0]-KI):
for j in range(KJ, data.shape[1]-KJ):
d=np.array(data[i-KI:i+KI+1,j-KJ:j+KJ+1])
d.shape=(1,25)
k=np.array(kernel)
k.shape=(1,25)
ret[i,j]=np.dot(d,k)
return ret
What I'm trying to do is for all i and j's in their respective ranges, find the weighted averages of its 5-by-5 neighbors by finding the dot product of the 5-by-5 neighbors for every i and j (I changed the shape of data to (1,25) to allow the calculations to be easier) in the range and the kernel (did the same to the shape of the kernel for the previous reason). However, I'm receiving an error that says:
Traceback (most recent call last):
File "/Users/jbunker7/Documents/Comp 116/M2_COMP116-003/P5.py", line 40, in <module>
result = applyFilter(data, kernel)
File "/Users/jbunker7/Documents/Comp 116/M2_COMP116-003/P5.py", line 30, in applyFilter
ret[i,j]=np.dot(d,k)
ValueError: matrices are not aligned
Can someone help me identify the error and guide me on the right path? If possible, I'd like to keep this structure because I'm in an introductory computing class and I'm not allowed to use upper-level computing techniques.