I am new to image processing, please forgive me if you find the question too simple.
I was trying to replace ndimage.convolve()
with cv2.filter2D()
function for convolution.
Here is the original code
ndimage.convolve(Gxx,f,origin=0)
Here is the replace function
cv2.filter2D(Gxx,-1,cv2.flip(f,-1),borderType=cv2.BORDER_REFLECT,anchor=(-1,-1))
Both of the code return different values, I try to play around with the parameter at ndimage.convolve
and set origin to -1 then it return the exact result as the cv2.filter2D
but this is not what I want, since I want ndimage.convolve to use the default origin which is zero.
After I study the opencv documentation again on filter2D
I realize that
The filtering function actually calculates correlation. If you have a symmetrical convolution kernel, the mathematical expressions for correlation and convolution are the same.
If the kernel is not symmetric, you must flip the kernel and set the anchor point to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1). This will calculate the actual convolution.
So I flip the kernel with cv2.flip(f,-1)
in both direction, but I don't know how to calculate the new anchor point after flip the kernel, according to the documentation, I need to set anchor point to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)
. I guess kernel.col
s and kernel.rows
is the kernel size in both direction but what does anchor.x
and anchor.y
means here? Is it original anchor point of the kernel? Assume the kernel is 42 X 42, what will be the new anchor point? Thanks in advance.