Scikit Image − Lapalace Operator



Laplacian operator, often referred to as the Laplacian filter, is an image processing operator used for edge detection and enhancement. It is a second-order derivative filter, which identifies areas in the image where the intensity changes rapidly, which typically corresponds to edges or boundaries between objects.

The Laplacian operator is particularly effective in highlighting fine details and edges in an image, making it a valuable tool in computer vision, image processing, and feature extraction tasks.

The scikit-image (skimage) library provides a function called laplace() within its filter module for finding the edges of an image using the Laplacian operator.

Using the skimage.filters.laplace() function

The filters.laplace() function is used to find the edges of an image using the Laplace operator.

Syntax

Following is the syntax of this function −

skimage.filters.laplace(image, ksize=3, mask=None)

Parameters

The function accepts the following parameters −

  • image (ndarray): This parameter represents the input image on which the te Laplace operator will be applied to find the edges.
  • ksize (int, optional): Define the size of the discrete Laplacian operator. The operator size will be (ksize,) * image.ndim, where image.ndim represents the number of dimensions of the input image. The larger ksize values will lead to more extensive edge detection.
  • mask (ndarray, optional): An optional mask can be provided to limit the application of the Laplace operator to a specific area within the image. Pixels surrounding masked regions are also masked to ensure that masked regions do not affect the result.

Return value

The function returns an ndarray representing the Laplace edge map.

Note: It is important to note that the Laplace operator is generated using the function skimage.restoration.uft.laplacian() and only the real part of the filter is kept.

Example

The following example demonstrates how to apply the Laplace filter to an image for detecting edges using the skimage.filters.laplace() function −

import matplotlib.pyplot as plt
from skimage import io, color, filters

# Load the input image and convert it to grayscale
image = color.rgb2gray(io.imread('Images/lines_3.jpg'))

# Apply the Laplace filter for edge detection
edge_map = filters.laplace(image, ksize= 4)

# Plot the original image and the Laplace edge map
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

# Display the Original Image
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')
ax[0].axis('off')

# Display the Laplace Edge Map
ax[1].imshow(edge_map, cmap='gray')
ax[1].set_title('Laplace Edge Map')
ax[1].axis('off')

plt.tight_layout()
plt.show()

Output

Lapalace Filter

Example

The following example demonstrates edge detection using the Laplace filter with and without a mask on a color image using the skimage.filters.laplace() function −

import numpy as np
from skimage import io, color, filters
import matplotlib.pyplot as plt

# Load the input image 
image = io.imread('Images/group chat.jpg')

# Define the coordinates and dimensions of the square region
x_0 = 200
y_0 = 20
width = 250
height = 250

# Create a mask with the same dimensions as the image
mask = np.zeros_like(image, dtype=np.bool)

# Create the square mask
mask[y_0:(y_0 + height), x_0:(x_0 + width)] = True

# Apply the Laplace filter without a mask on a color image
edge_map_no_mask = filters.laplace(image)

# Apply the Laplace filter with the mask
edge_map_with_mask = filters.laplace(image, ksize=3, mask=mask)

# Plot the original image and the results with and without the mask
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
ax = axes.ravel()

# Display the Original Image
ax[0].imshow(image)
ax[0].set_title('Original Image')
ax[0].axis('off')

# Display the Laplace Edge Map without the Mask (Color Image)
ax[1].imshow(edge_map_no_mask)
ax[1].set_title('Laplace filter result without Mask ')
ax[1].axis('off')

# Display the Laplace Edge Map with the Mask
ax[2].imshow(edge_map_with_mask)
ax[2].set_title('Laplace filter result with Mask')
ax[2].axis('off')

plt.tight_layout()
plt.show()

Output

Lapalace Filter
Advertisements