Sunday, 17 December 2023

Detect edges using Laplacian operator

Laplacian edge detection uses the Laplacian operator to identify regions of rapid intensity change. 'Laplacian operator' is used to measure the change in the intensity of an image at a pixel.

 

Using cv2.Laplacian method, we can calculate the Laplacian image.

 

Signature

dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])

Following table summarizes the parameters of Laplacian method.

 

Parameter

Description

src

Source image in which you want to apply Laplacian filter.

ddepth

Specifies the depth of destination image. It specifies the desired data type for the output image, which can be one of the following values:

 

cv2.CV_8U: 8-bit unsigned integer.

cv2.CV_16U: 16-bit unsigned integer.

cv2.CV_16S: 16-bit signed integer.

cv2.CV_32F: 32-bit floating-point.

cv2.CV_64F: 64-bit floating-point.

ksize

It is optional parameter, specifies the size of the kernel (filter) that defines the extent of the Laplacian operation

scale

It is optional parameter, specifies the scale factor for the computed Laplacian values.

delta

It is optional parameter, added to the result.

borderType

It is optional parameter, specifies how border of the image is handled. Following values are supported.

 

cv2.BORDER_DEFAULT: Default border behavior.

cv2.BORDER_CONSTANT: Adds a constant border (specified by borderValue).

cv2.BORDER_REFLECT: Reflects the image around the border.

cv2.BORDER_REFLECT_101: Reflects the image around the border with a slight change.

cv2.BORDER_REPLICATE: Replicates the last pixel of the border.

 

Example

laplacian = cv.Laplacian(gray_scale_image, -1)

The result of Laplacian method might have both positive and negative values, so you convert it to an appropriate data type using cv2.convertScaleAbs to ensure it's in the correct format for display.

 

laplacian = cv.convertScaleAbs(laplacian)

 

Find the below working application.

 

laplacian_edge_detection.py

import cv2 as cv
import numpy as np

image = cv.imread('gateway_of_india.png')
gray_scale_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

laplacian = cv.Laplacian(gray_scale_image, -1)
#laplacian = np.uint8(np.absolute(laplacian))
# Convert the result to the appropriate data type and scale it
laplacian = cv.convertScaleAbs(laplacian)

cv.imshow('Original image', image)
cv.imshow('gray_scale_image', gray_scale_image)
cv.imshow('Laplacian image', laplacian)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

Original image


 

Gray scale image



Laplacian image

  



Previous                                                    Next                                                    Home

No comments:

Post a Comment