0
0
SciPydata~10 mins

Sobel and Laplace edge detection in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sobel and Laplace edge detection
Input Image
Apply Sobel Filter
Calculate Gradient Magnitude
Edges Detected by Sobel
Apply Laplace Filter
Edges Detected by Laplace
Output Edge Images
Start with an image, apply Sobel filter to find edges by gradients, then apply Laplace filter to find edges by second derivatives, output both edge images.
Execution Sample
SciPy
from scipy import ndimage
import numpy as np

image = np.array([[10,10,10],[10,50,10],[10,10,10]])
sobel_x = ndimage.sobel(image, axis=0)
laplace = ndimage.laplace(image)
This code applies Sobel filter along vertical axis and Laplace filter on a small 3x3 image.
Execution Table
StepOperationInputOutputExplanation
1Input Image[[10,10,10],[10,50,10],[10,10,10]]SameOriginal 3x3 image with a bright center pixel
2Apply Sobel (axis=0)Image[[ 0 40 0],[ 0 0 0],[ 0 -40 0]]Detects vertical edges by gradient along rows
3Apply LaplaceImage[[ 30 20 30],[ 20 -160 20],[ 30 20 30]]Detects edges by second derivative highlighting center pixel
4Output Sobel and LaplaceSobel and Laplace arraysEdge imagesFinal edge detection results from both filters
💡 All steps complete, edge images produced
Variable Tracker
VariableStartAfter SobelAfter LaplaceFinal
image[[10 10 10] [10 50 10] [10 10 10]][[10 10 10] [10 50 10] [10 10 10]][[10 10 10] [10 50 10] [10 10 10]]Unchanged original image
sobel_xNone[[ 0 40 0] [ 0 0 0] [ 0 -40 0]][[ 0 40 0] [ 0 0 0] [ 0 -40 0]]Vertical gradient edges
laplaceNoneNone[[ 30 20 30] [ 20 -160 20] [ 30 20 30]]Second derivative edges
Key Moments - 2 Insights
Why does the Sobel filter output have zeros in some places?
Because Sobel calculates gradient along one axis, edges only appear where pixel values change vertically; flat areas produce zero gradient as shown in execution_table step 2.
Why is the Laplace filter output negative in the center pixel?
Laplace highlights regions where intensity changes rapidly in all directions; the bright center pixel surrounded by darker pixels creates a strong negative value as seen in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table step 2, what is the Sobel filter output at position (0,1)?
A40
B0
C-40
D10
💡 Hint
Check the 'Output' column in step 2 for Sobel filter values.
At which step does the Laplace filter produce a strong negative value at the center pixel?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in step 3 for Laplace filter results.
If the image had uniform pixel values, how would the Sobel output change?
ASame as input
BAll zeros
CRandom values
DNegative values
💡 Hint
Refer to how Sobel detects edges by gradients; no change means zero gradient.
Concept Snapshot
Sobel and Laplace edge detection:
- Sobel finds edges by gradient (first derivative) along an axis.
- Laplace finds edges by second derivative (changes in gradient).
- Use scipy.ndimage.sobel(image, axis) and laplace(image).
- Sobel highlights edges in one direction; Laplace highlights all edges.
- Outputs are arrays showing edge strength.
Full Transcript
We start with an input image, a small 3x3 grid with a bright center pixel. First, we apply the Sobel filter along the vertical axis to detect vertical edges. The output shows strong positive and negative values where pixel intensity changes vertically, and zeros where it does not. Next, we apply the Laplace filter, which calculates the second derivative, highlighting areas where intensity changes rapidly in all directions. This produces a strong negative value at the center pixel surrounded by positive values. The Sobel output shows edges as gradients, while Laplace highlights edges by curvature. Both outputs are arrays representing edge strength. This step-by-step process helps visualize how these filters detect edges differently.