Reputation: 2163
I need to perform a 2D convolution. I have a similarity matrix of shape 100, 100
and a filter of shape 5,5
.
If I do using scipy:
scipy.signal.convolve2d(similarity_matrix, np.diag(filter))
I get 104,104
matrix in response.
But if I do using OpenCV's filter2D method:
cv2.filter2D(similarity_matrix, -1, np.diag(filter))
I get a 100,100
matrix in response.
Upvotes: 0
Views: 5468
Reputation: 321
Amanda, a full convolution includes the zero-padding. In other words, you must remove the out-of-bounds pixels:
res=scipy.signal.convolve2d(similarity_matrix, np.diag(filter))
w,h=res.shape()
res=res[w-2][w-2]
Upvotes: 1
Reputation: 3775
The default output mode of scipy.signal.convolve2d is a full convolution. If you want your output size to be the same as your input size, set mode parameter as "same".
scipy.signal.convolve2d(similarity_matrix, np.diag(filter), mode="same")
Update: When you convolve an image with a kernel, you can't directly calculate convolution results at the edges of the image because there are some neighbours missing. There are different ways to solve this issue
1- Ignore values of the edges, eg. if your kernel is 3x3, ignore elements at the edges, if your kernel is 5x5, ignore last 2 elements at the edges and so on..
2- Apply some kind of padding to image, meaning that enlarge your image in a specific way temporarily so that you can apply apply convolution kernel to values at the edge. Again, there are different ways of doing this (and this time more than 2), but most basic way is to zero-padding. For example if your image size is 100x100, and your kernel is 5x5, at each side of the image (right, up, left, down) we can not calculate convolution of two outer values due to lack of neighbours. With zero padding method you first enlarge your image to 104x104. This enlarged image is consist of your original 100x100 image at the center, and some zero values added to edges, now the main thing is now you can apply your filter to the 100x100 region because you created artificial neighbours for the edge values.
About the mode names:
1- If you choose "padding way" and keep your output size same as your input size, its called same convolution. With padding method above this is done by dropping outer values at your enlarged image.
2- If you choose "ignore edge values way" of doing convolution, your output will be smaller. This is called valid convolution. As the name implies, you only performed convolution operation on "valid" region.
3- If you choose "padding way" and keep added values also, its called full convolution. Notice that by cropping output of full convolution, you can obtain same and valid convolution too.
Upvotes: 2