The opencv cv2.Laplacian() function is supposed to be using the kernel
[[0, 1, 0] ,
[1, -4, 1] ,
[0, 1, 0]]
or ideally (noting that the central point sign might be opposite to the outer points which is acceptable since we are normally interested in the absolute value of the filter mask response)
[[-1, -1, -1] ,
[ -1, 8, -1] ,
[ -1, -1, -1]]
However, it appears to be using the kernel
[[ 2, 0, 2] ,
[ 0, -8, 0] ,
[ 2, 0, 2]]
This can be demonstrated by calling cv2.Laplacian(v, ddepth=3, ksize=3) on
v = np.array([
[0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 1, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, ],
])
v = v.astype(np.uint8)
which gives output as
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 2, 0, 2, 0, 0],
[ 0, 0, 0, -8, 0, 0, 0],
[ 0, 0, 2, 0, 2, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]], dtype=int16)
can anyone advise which mask is in fact being used and why? The results can be quite different when the underlying kernel shape is different to what is stated or expected.
The opencv cv2.Laplacian() function is supposed to be using the kernel
Yes, you are right but when the case of ksize is equal to 1. Which is ksize is 3 in your case. Check the documentation detaily:

If you want to learn what are the coefficeints of your kernel matrix, you can check the getDerivKernels which calculates them according to Sobel and Scharr.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With