Reputation: 143
I used python code to find the laplacian of an image with just a general kernel= 1 (using cv2). Now I want to use a new kernel array([0,-1,0][-1,5,-1][0,-1,0])
but not sure how I can implement that using the code I have, or how to write new code calling the kernel.
ksize = 1
scale = 1
delta = 0
ddepth = cv2.CV_64F
img = cv2.imread('/Users//Desktop/Programming/image.tif')
img = cv2.GaussianBlur(img,(3,3),0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray_lap = cv2.Laplacian(gray,ddepth,ksize = ksize,scale = scale,delta = delta)
dst = cv2.convertScaleAbs(gray_lap)
plt.imshow(dst, cmap = 'gray')
Upvotes: 1
Views: 6230
Reputation: 1175
https://dsp.stackexchange.com/questions/44928/what-does-derivative-means-in-image-processing/44930
Provides some background on what the Laplacian does in terms of zero'ing out the second derivative. Commonly used Laplace kernels are also listed there but I think it has to sum to zero where as the kernel you mentioned of array([0,-1,0][-1,5,-1][0,-1,0])
does not but rather sums to 1. The most similar kernel for Laplacian to yours would be [[0,1,0],[1,-4,1],[0,1,0]]
for a 2D image. Also, the second derivative for an image is very sensitive to noise so a Gaussian blur can be applied first in which case the resulting filter can be thought of as an LoG (Laplacian of Gaussian). I am starting to learn this myself but here is a good resource: https://docs.opencv.org/3.4/d5/db5/tutorial_laplace_operator.html
A code example would be:
gblur = cv2.GaussianBlur(image, (3,3), 0)
l_kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
temp = cv2.filter2D(gblur, -1, laplacian_kernel)
cv2.imshow('temp', temp)
Upvotes: 1
Reputation: 11251
I'm not familiar with Python's OpenCV bindings, but for a custom convolution kernel you'll want filter2D
. Build up the kernel yourself as a 3x3 array. This tutorial is in C++ but it looks helpful.
Upvotes: 5