Image Gradient, Edge Detection, and Gaussian Filtering
Exploring image gradient computation, edge detection with thresholding, Gaussian smoothing, and efficiency of separable filtering via 1D convolutions.
Image Gradient, Edge Detection, and Gaussian Filtering
Overview
B1. Finding the Magnitude of the Gradient Using Convolution
- Used
conv2()
with kernelsdx = [1 -1]
anddy = [1; -1]
to compute x and y gradients. - Constructed gradient magnitude image from these components.
- Brightness scaled to 255 using the max value of the gradient matrix.
B2. Thresholding the Gradient Magnitude
- Applied threshold based on statistical metrics of the gradient matrix (mean, median, etc.).
- Chose threshold of 18.5 based on mean value.
- Pixels above threshold set to 255 (white), below set to 0 (black).
B3. Smoothing the Image with Gaussian Convolution
- Created a 13×13 Gaussian kernel using σ = 2.
- Kernel scaled to sum to 1 for displayability.
- Smoothed the grayscale image using
conv2()
with the Gaussian kernel andsame
mode.
B4. Edge Detection on Blurred Image
- Re-applied gradient magnitude and thresholding to the blurred image.
- Resulting edges appeared thicker than those from the original, potentially over-enhanced.
B5. Combined Blurring and Edge Detection Filter
- Created combined kernel by convolving Gaussian kernel (σ = 4) with finite difference kernels.
- Generated new x and y kernels, used to compute edge magnitude as before.
- Compared favorably to sequential blurring + edge detection, showing slightly improved results.
B6. Re-implementing Gaussian Smoothing Using 1D Convolution
- Proved that 2D Gaussian is separable:
f(x, y) = g(x) * h(y)
whereg(x) = exp(-x^2 / 2σ^2)
,h(y) = exp(-y^2 / 2σ^2)
- Replaced 2D convolution with two 1D convolutions for efficiency.
- Used
conv2(X, Y, image, 'same')
with 1D vectorsX
,Y
. - Benchmarked:
- 2D kernel (13x13): 7.04 seconds
- Two 1D filters (13-length): 1.82 seconds
- Confirms reduced complexity: From
O(i * j * k^2)
toO(i * j * k)
wherei, j
are image dimensions,k
is kernel size.
Conclusion
This assignment implemented gradient magnitude detection, thresholding, Gaussian blurring, and separable convolution filters. Combining smoothing and edge detection into a single kernel was shown to be effective, and using separable 1D convolutions provided significant speedups for large kernel sizes.
This post is licensed under CC BY 4.0 by the author.