Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the kernel used in openCV cv2.Laplacian() function?

Tags:

python

opencv

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.

like image 381
javid Avatar asked Oct 27 '25 09:10

javid


1 Answers

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:

enter image description here

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.

like image 157
Yunus Temurlenk Avatar answered Oct 29 '25 06:10

Yunus Temurlenk