What are Laplacian derivatives of an Image in OpenCV

This recipe explains what are Laplacian derivatives of an Image in OpenCV

Recipe Objective: What are Laplacian derivatives of an Image in OpenCV?

Let us take this recipe to understand are Laplacian derivatives of an Image.

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below.

Input image

import numpy as np
import cv2
from matplotlib import pyplot as plt
image = cv2.imread('chess.jpg',0)

Step 2: Understanding image derivatives and Sobel Operator

Before we start extracting Laplacian derivatives, let us first take a moment to understand what image derivatives are and why they are helpful. Image derivatives are highly used in detecting the edges of the image. Image derivatives locate the places in the image where the pixel intensity changes in a drastic manner. This helps us map the edges of any image. The Sobel operator is one such operator which can be used to find the derivative of an image.

Step 3: Calculating the derivative of an image using Laplacian Operator

Sobel Operator usually calculates the first derivative of the image. But estimating the second derivative turns out to be zero in most cases where the edges are detected. This is the principle behind Laplacian derivatives. The Laplacian operator makes use of the Sobel operator internally. It is also important to note that zeros would appear only in the edges. They can occur in other absurd places also. We can overcome this by applying any of the appropriate filters available in OpenCV if needed before extracting its derivative.

Laplacian derivative can be calculated in python using the cv2.Laplacian() function, which takes the following arguments.

  • src: The input image
  • ddepth: The data type of the output image
  • ksize: (Optional) The size of the kernel matrix

We already know that the data type of our input image would be uint8. Generally, the derivates of Black to White transitions are positive. In contrast, the White to Black transitions are negative, and hence it is highly recommended to choose a higher-order output datatype such as cv2.CV_64F and then convert the result to a uint8 type array to avoid missing any edges.

lap_1 = cv2.Laplacian(image, cv2.CV_64F)
lap_1_abs = np.uint(np.absolute(lap_1))

In the above chunk of code, we calculate the Laplacian derivate of the input image and store it in the lap_1 variable. We then take the absolute value of the result and convert it to a uint8 array using the np.absolute() and np.uint8() functions, respectively, which are available in the Numpy package.

Let us also try altering the kernel size and see how the results come out.

lap_2 = cv2.Laplacian(image, cv2.CV_64F, ksize=7)
lap_2_abs = np.uint(np.absolute(lap_2))

Step 4: Displaying the Output

Let us display the results using matplotlib.

titles = ['Original Image',"Laplacian derivative with default ksize", 'Laplacian derivative with kszie=7']
images = [image,lap_1_abs,lap_2_abs]
plt.figure(figsize=(13,5))
for i in range(3):
    plt.subplot(1,3,i+1)
    plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.tight_layout()
plt.show()

Output:

Laplacian derivative

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.