0
0
SciPydata~5 mins

Sobel and Laplace edge detection in SciPy

Choose your learning style9 modes available
Introduction

Edge detection helps find the outlines of objects in images. Sobel and Laplace methods highlight these edges by showing where colors or brightness change sharply.

To find the borders of objects in a photo for counting or measuring.
To prepare images for further analysis like recognizing shapes or letters.
To detect changes in textures or patterns in medical images.
To improve the clarity of features in satellite images.
To help robots or self-driving cars understand their surroundings.
Syntax
SciPy
import numpy as np
from scipy import ndimage

# Sobel edge detection
sobel_horizontal = ndimage.sobel(image, axis=0)
sobel_vertical = ndimage.sobel(image, axis=1)
sobel_edges = np.hypot(sobel_horizontal, sobel_vertical)

# Laplace edge detection
laplace_edges = ndimage.laplace(image)

image should be a 2D array representing a grayscale image.

Sobel detects edges by looking at horizontal and vertical changes separately, then combines them.

Examples
Detects edges by looking at changes along rows (horizontal edges).
SciPy
sobel_horizontal = ndimage.sobel(image, axis=0)
print(sobel_horizontal.shape)
Detects edges by looking at changes along columns (vertical edges).
SciPy
sobel_vertical = ndimage.sobel(image, axis=1)
print(sobel_vertical.shape)
Detects edges by looking at the second derivative, highlighting areas where brightness changes quickly.
SciPy
laplace_edges = ndimage.laplace(image)
print(laplace_edges.shape)
Sample Program

This code creates a simple image with a white square on black background. It then finds edges using Sobel and Laplace methods. The printed arrays show the edge strength values. The plots help you see where edges are detected.

SciPy
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

# Create a simple 5x5 image with a white square in the middle
image = np.zeros((5, 5))
image[1:4, 1:4] = 1

# Apply Sobel edge detection
sobel_horizontal = ndimage.sobel(image, axis=0)
sobel_vertical = ndimage.sobel(image, axis=1)
sobel_edges = np.hypot(sobel_horizontal, sobel_vertical)

# Apply Laplace edge detection
laplace_edges = ndimage.laplace(image)

# Print arrays to see edge values
print('Original Image:\n', image)
print('\nSobel Edges:\n', sobel_edges)
print('\nLaplace Edges:\n', laplace_edges)

# Plot images for visual understanding
plt.figure(figsize=(10,3))
plt.subplot(1,4,1)
plt.title('Original')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1,4,2)
plt.title('Sobel Horizontal')
plt.imshow(sobel_horizontal, cmap='gray')
plt.axis('off')

plt.subplot(1,4,3)
plt.title('Sobel Vertical')
plt.imshow(sobel_vertical, cmap='gray')
plt.axis('off')

plt.subplot(1,4,4)
plt.title('Laplace')
plt.imshow(laplace_edges, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Sobel edges combine horizontal and vertical changes to find strong edges.

Laplace uses second derivatives, so it can detect edges differently, sometimes highlighting corners.

Input images should be grayscale (2D arrays) for these filters to work correctly.

Summary

Sobel and Laplace are simple ways to find edges in images.

Sobel looks at horizontal and vertical changes separately and then combines them.

Laplace looks at how brightness changes twice to find edges.