I am trying to implement my own algorithm for convoluting an image with a certain filter and getting a bit of help from this post. What I have so far:
My image in numpy with shape (510,510)
imageInArray = np.asarray(gray_image)
print(imageInArray.shape)
## it prints this
(512, 512)
My convolution filter:
conv_filter = np.array([[0,1,0],[1,-4,1],[0,1,0]])
print(conv_filter.shape)
##prints this:
(3, 3)
Then I am spliting the image in submatrices depending on the filter kernel size like this:
### split image in submatrices
def getsubmatrices(imgArray, partitioning_shape):
view_shape = tuple(np.subtract(imgArray.shape, partitioning_shape) + 1) + partitioning_shape
strides = imgArray.strides + imgArray.strides
sub_matrices = np.lib.stride_tricks.as_strided(imgArray,view_shape,strides)
return sub_matrices
imgSubMatrices = getsubmatrices(imageInArray, conv_filter.shape)
print(imgSubMatrices.shape)
##prints an array of size (510, 510, 3, 3)
But then when I am using the einsum
method of numpy to multiply the submatrices with the filters like this:
multiplied_subs = np.einsum('ij,ijkl>ijkl',conv_filter,imgSubMatrices)
I am getting the following error:
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,3)->(3,3,newaxis,newaxis) (510,510,3,3)->(510,510,3,3)
Could someone help me understand why this doesnt work? since I have spliting my matrix is submatrices of 3x3 it shouldnt be a problem to use the einsum
method, right? I really apprecite any help you can provide as I have been trying to understand whats wrong since a few hours