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.
Sobel and Laplace edge detection in 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.
sobel_horizontal = ndimage.sobel(image, axis=0) print(sobel_horizontal.shape)
sobel_vertical = ndimage.sobel(image, axis=1) print(sobel_vertical.shape)
laplace_edges = ndimage.laplace(image)
print(laplace_edges.shape)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.
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()
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.
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.