In digital image processing, we use a Laplacian filter to compute the second-order derivative of an image to detect edges. We need a Laplacian filter so that we can extract the features of the image in a better way. The better we can extract the features of the image, the better we will make the model to train.
The question arises of why we need to compute the second-order derivative when the first-order derivative is doing the work. We detect the horizontal and vertical edges in first-order derivatives and combine them. On the other hand, the second-order derivative allows us to detect all the edges of an image at once. Have a look at the Laplacian filters given below.
As we have discussed earlier, in the Laplacian filter, we are interested in finding the second-order derivatives of the image vertically and horizontally. So the equation of this scenario is given below.
In this equation,
This equation represents how we take second-order derivation of an image.
The code for applying convolution with Laplacian is given below. As you know, Image is represented in the form of an array.
import numpy as npdef WindowMultiply(LaplacianFilter,Image,row,col):Result=0for i in range(3):for c in range(3):Result=Result+LaplacianFilter[i][c]*Image[i+row][col+c]return Resultdef ConvolutionWithLaplacian(fitler,Image):Result = np.random.rand(3,4)for i in range(3):for c in range(4):Result[i][c]=WindowMultiply(LaplacianFilter,Image,i,c)return Resultif __name__=="__main__":LaplacianFilter=np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])Image=np.array([[4,7,10,1,14,5],[8,13,8,10,18,20],[7,8,54,10,3,10],[9,6,9,7,9,12],[54,9,1,9,5,9]])Result=ConvolutionWithLaplacian(LaplacianFilter, Image)print("Result after Convolution with Laplacian Filter : ")print(Result)
The explanation of the code is given below.
Line 3–8: The function WindowMultiply()
multiplies the LaplacianFilter
and window of the Image
.
Line 10–15: ConvolutionWithLaplacian()
performs the convolution of the image with a filter. The nested for loops in this function indicates the sliding window, and for each sliding window, we call the function WindowMultiply()
.
Line 21–23: Initializing the Laplacian filter.
Line 24: Represents the Image in the form of an array.
Line 25: Storing the convolution result in the variable Result
.
Note: If you are curious to learn about how the convolution is done then please refer to this link.
Look at the picture to which we have applied the Laplacian filter. Observe how the Laplacian filter helps us detect all the edges of an image.
We use the Laplacian filter in various tasks. Besides detecting edges, many other wide-range applications of Laplacian filters, some of which are illustrated below.
Laplacian filters help in convolutional neural networks. These networks are designed to deal with images. So with the help of convolution, we extract the features of the image. Feature extraction is defined by the type of filter we are using. Edge-detecting filters detect the edges present in the images.
Similarly, the Laplacian filter is also used to find the overall edges. It computes the rate of change of the first derivative. So it is very sensitive in detecting the edges of the image.
What is the primary purpose of the Laplacian filter in image processing?
Color correction
Noise addition
Edge detection
Blur reduction
Free Resources