Images as 2-D signals: blur and edges
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A grey image is a signal with two indexes, x[m, n]. Every idea from 1-D filtering carries over: slide a small kernel over the image, multiply and add, and you have 2-D convolution. Follow a 6×6 image through a 3×3 box blur (a 2-D moving average, so a low-pass filter) and the Sobel edge kernel (a smoothed difference, so a high-pass filter), see why separable kernels are fast, and do both in Python with numpy and SciPy. Build on the lessons on discrete convolution, the moving average and frequency response first.
An image is a 2-D signal
A grey image is a grid of numbers. Pixel x[m, n] holds the brightness at row m, column n. Our 6×6 image is dark (0) with a bright (9) square at the bottom right. Two indexes instead of one time.
x[m, n] m = row (0..5), n = column (0..5)
n=0 1 2 3 4 5
m=0 0 0 0 0 0 0
m=1 0 0 0 0 0 0
m=2 0 0 9 9 9 9
m=3 0 0 9 9 9 9
m=4 0 0 9 9 9 9
m=5 0 0 9 9 9 9
brightness on a 0..9 scale for easy sums
(8-bit images use 0..255)اقرأ الدرس خطوة بخطوة
An image is a 2-D signal
A grey image is a grid of numbers. Pixel x[m, n] holds the brightness at row m, column n. Our 6×6 image is dark (0) with a bright (9) square at the bottom right. Two indexes instead of one time.
x[m, n] m = row (0..5), n = column (0..5) n=0 1 2 3 4 5 m=0 0 0 0 0 0 0 m=1 0 0 0 0 0 0 m=2 0 0 9 9 9 9 m=3 0 0 9 9 9 9 m=4 0 0 9 9 9 9 m=5 0 0 9 9 9 9 brightness on a 0..9 scale for easy sums (8-bit images use 0..255)Each row is a 1-D signal
Read row 3 from left to right: 0, 0, 9, 9, 9, 9. That is an ordinary discrete signal x[n], here a step. Every row and every column is a signal; the whole grid is a function of two variables.
row 3: x[3, n] = 0, 0, 9, 9, 9, 9 n = 0, 1, 2, 3, 4, 5 a step: dark, then bright from n = 2 1-D signal x[n] one index 2-D signal x[m, n] two indexes (a video adds a third: time)A kernel: a small grid of weights
An image filter is a small grid of weights, the kernel h[k, l]. The 3×3 box kernel has nine weights of 1/9, so each output pixel will be the average of a 3×3 patch of the image.
box kernel h = (1/9) × 1 1 1 1 1 1 1 1 1 nine weights, sum = 1 so a flat patch keeps its brightness: nine 9s × 1/9 = 9Slide, multiply, add
Lay the kernel on a patch, multiply each pixel by its weight and add the nine products: one output pixel. Slide one column and repeat. The kernel fits wholly in 16 places, so the output is 4×4.
y[m, n] = Σk Σl h[k, l] · x[m − k, n − l] at y[3, 2] the patch is 0 9 9 0 9 9 0 9 9 six 9s and three 0s: (6 × 9) × 1/9 = 6 "valid": only places with no padding (6 − 3 + 1) × (6 − 3 + 1) = 4×4Blur: the step becomes a ramp
Row 3 of the output reads 3, 6, 9, 9. The jump from 0 to 9 now climbs over three pixels. Averaging neighbours smooths sharp changes: that is blur, a 2-D moving average.
blur y[m, n], centres m, n = 1..4 m=1 1 2 3 3 m=2 2 4 6 6 m=3 3 6 9 9 m=4 3 6 9 9 row 3: x = 0 9 9 9 (n = 1..4) y = 3 6 9 9 the corner is soft in both directionsBlur is a low-pass filter
Along a row the box averages three neighbours. Its gain |1 + 2cos ω| / 3 is 1 for slow change (ω = 0), 0 at ω = 2π/3, and 1/3 for the fastest pixel-to-pixel flicker (ω = π). Fine detail fades.
along a row: h1 = [1, 1, 1] / 3 H(ω) = (e^jω + 1 + e^−jω) / 3 = (1 + 2 cos ω) / 3 |H(0)| = 1 flat areas kept |H(2π/3)| = 0 period-3 pattern gone |H(π)| = 0.33 0 9 0 9 ... shrinks 3× ω in radians per pixel (a spatial frequency)Separable: rows, then columns
The box kernel is a column [1 1 1]/3 times a row [1 1 1]/3. So blur every row, then every column: 6 multiplies a pixel instead of 9. Each output here is a(m)·a(n), with a = 1, 2, 3, 3.
(1/9) ones(3, 3) = [1 1 1]ᵀ/3 · [1 1 1]/3 row pass, then column pass: 3 + 3 = 6 multiplies per pixel instead of 3 × 3 = 9 (for a k×k kernel: 2k instead of k²) here y[m, n] = a(m)·a(n), a = 1, 2, 3, 3 y[3, 2] = 3 × 2 = 6Edges: the Sobel kernel
Swap in the Sobel kernel. On the image it weighs the right column +1, +2, +1 and the left column −1, −2, −1, so it measures how fast brightness rises from left to right. Slide it again.
Sobel (as written for convolution) +1 0 −1 +2 0 −2 +1 0 −1 convolution flips it 180°, so on the image: −1 0 +1 −2 0 +2 −1 0 +1 = [1 2 1]ᵀ (smooth down) × [1 0 −1] (difference across)Flat areas vanish, edges light up
Where a patch is flat, right minus left is 0. Across the edge the sum is big: 9 + 18 + 9 = 36 in row 3. The output lights up exactly where the image jumps from dark to bright.
edges y[m, n], centres m, n = 1..4 m=1 9 9 0 0 m=2 27 27 0 0 m=3 36 36 0 0 m=4 36 36 0 0 y[3, 2]: patch columns 0 | 9 | 9 (9 − 0)·1 + (9 − 0)·2 + (9 − 0)·1 = 36 row 1 sees only one bright row: 9 flat right half: 0Edges are a high-pass filter
Along a row Sobel takes x[n+1] − x[n−1]. Its gain 2|sin ω| is 0 for flat areas (ω = 0) and peaks at 2 for ω = π/2: it keeps change and drops the constant, the opposite of blur.
along a row: h2 = [−1, 0, 1] H(ω) = −e^jω + e^−jω = −2j sin ω (centred) |H(ω)| = 2 |sin ω| |H(0)| = 0 flat areas -> 0 |H(π/2)| = 2 fastest useful change |H(π)| = 0 (the 0 9 0 9 flicker is blocked too) down a column [1 2 1] is a gentle blur: |H| = 2 + 2 cos ωBlur and edges in Python
SciPy's convolve2d does the slide, multiply and add; "valid" keeps the 16 full-fit places. Convolution flips the kernel, so Sobel is written np.outer([1, 2, 1], [1, 0, −1]) and lands as −1 0 +1.
import numpy as np from scipy.signal import convolve2d img = np.zeros((6, 6)); img[2:, 2:] = 9 box = np.ones((3, 3)) / 9 blur = convolve2d(img, box, "valid") sx = np.outer([1, 2, 1], [1, 0, -1]) edge = convolve2d(img, sx, "valid") >>> blur array([[1., 2., 3., 3.], [2., 4., 6., 6.], [3., 6., 9., 9.], [3., 6., 9., 9.]]) >>> edge array([[ 9., 9., 0., 0.], [27., 27., 0., 0.], [36., 36., 0., 0.], [36., 36., 0., 0.]]) (Python 3 with numpy and SciPy)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A 3×3 box blur is applied to a patch whose nine pixels are 0, 0, 9 / 0, 9, 9 / 9, 9, 9. What is the output pixel?
The box kernel weighs every pixel by 1/9. Six of the nine pixels are 9 (one, two and three per row), so the sum is 54 and 54 × 1/9 = 6.
02A 100×100 image is convolved with a 5×5 kernel in 'valid' mode. How big is the output?
The kernel must fit wholly inside the image: 100 − 5 + 1 = 96 places in each direction. 'Full' mode would give 104×104 and 'same' mode pads to keep 100×100.
03Why does a box blur remove fine detail?
Along a row the 3-pixel average has gain |1 + 2cos ω| / 3: 1 at ω = 0, 0 at ω = 2π/3 and 1/3 at ω = π. Fast changes, which are fine detail and sharp edges, are cut down.
04Where is the Sobel output (left-to-right kernel) zero?
Sobel adds the right column and subtracts the left column of the patch. In any flat patch they cancel, so its gain at ω = 0 is 0: a high-pass filter. In the example the bright right half gives 0 just like the dark left part.
05A 7×7 blur kernel is separable. About how many multiplies per pixel does a row pass followed by a column pass need?
A separable k×k kernel is a column times a row, so two 1-D passes of k multiplies each: 2 × 7 = 14 instead of 7 × 7 = 49.
المسرد
- Pixel
- One sample of an image: the brightness x[m, n] at row m and column n.
- 2-D signal
- A signal with two independent variables, such as the row and column of an image.
- Kernel
- The small grid of weights h[k, l] a 2-D filter slides over the image; it is the filter's impulse response (point-spread function).
- 2-D convolution
- y[m, n] = Σ Σ h[k, l] · x[m − k, n − l]: flip the kernel, slide it, multiply each pixel by its weight and add.
- Box blur
- A kernel of equal weights (1/9 for 3×3) that replaces each pixel by the average of its neighbours: a low-pass filter.
- Sobel operator
- A 3×3 edge kernel, a [1 2 1] smoothing in one direction times a [1 0 −1] difference in the other.
- Separable kernel
- A kernel equal to a column times a row, so it can run as two cheap 1-D passes.
- Spatial frequency
- How fast brightness changes across the image, in radians per pixel; fine detail and sharp edges are high spatial frequencies.
الملاحظات والمصادر
- Brightness uses a 0 to 9 scale so the sums stay small; 8-bit images store 0 to 255 per pixel and the arithmetic is the same. Every number and curve in the figure is computed from this model and matches numpy 2.5.4 with SciPy 1.18.1.
- "valid" convolution keeps only outputs that need no padding, so a 6×6 image gives 4×4. Other modes pad the border (with zeros, or a copy of the edge) to keep the size; the padded border then shows fake edges.
- Sources write the Sobel kernel with either sign, and some libraries slide the kernel without flipping it (correlation). The flip only changes the sign of the edge value here, never where the edge is.
- In the row plot the two sets of stems are nudged sideways a little so both stay visible; each belongs to the whole-number column n under it.
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson, 1997 (convolution and frequency response)
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed. (course text listed on MIT OCW 6.341 readings)
- D. Rowell, Introduction to Two-Dimensional/Image Processing, MIT OCW 2.161 Signal Processing: Continuous and Discrete, Fall 2008
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011)
- Box blur (Wikipedia): average of the neighbours, a low-pass filter, separable
- Sobel operator (Wikipedia): the kernels, 2-D convolution and separability
- SciPy documentation: scipy.signal.convolve2d (modes full, valid, same)
- NumPy documentation: numpy.outer
اختبار يتاح بعد الخطوة الأخيرة.