How to use Haar wavelet to detect LINES on an image?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A Haar wavelet is useful for detecting abrupt intensity changes, which makes it a good building block for line and edge analysis. The practical workflow is not "run Haar and magically get lines." Instead, you decompose the image into directional detail coefficients, emphasize the coefficient bands that correspond to the orientation you care about, threshold them, and then optionally post-process the result into actual line segments.
Why Haar Wavelets Help
The Haar wavelet is essentially a simple step detector. In one dimension, it responds strongly where a signal changes abruptly. In two-dimensional images, the discrete wavelet transform separates the image into:
- approximation coefficients
- horizontal detail coefficients
- vertical detail coefficients
- diagonal detail coefficients
That separation is useful because straight lines create strong directional responses in the detail bands.
Start With a Grayscale Image
For line detection, a grayscale image is usually enough.
Normalizing to [0, 1] is not mandatory, but it makes threshold values easier to reason about.
Apply a 2D Haar Transform
With PyWavelets, a single-level 2D Haar transform looks like this:
Interpretation:
- '
cAis the coarse approximation' - '
cHemphasizes horizontal detail' - '
cVemphasizes vertical detail' - '
cDemphasizes diagonal detail'
If you want to highlight horizontal lines, cH is often the first band to inspect. For vertical lines, cV is often more useful.
Threshold the Detail Coefficients
Wavelet detail bands contain both useful structure and noise. Thresholding helps isolate strong line-like responses.
The threshold is problem-dependent. If the image is noisy, you may need a larger threshold or a denoising step before the transform.
Upsample the Response for Visualization
The detail bands are smaller than the original image after one wavelet decomposition. Resize them back if you want to visualize the detected structures over the original image.
This gives you heat-map style line responses rather than final vector line segments.
From Responses to Actual Lines
If your goal is to detect literal line segments, wavelet coefficients are often a feature extraction step, not the final answer. A common next stage is:
- threshold and binarize the selected response band
- clean it with morphology
- extract connected components or apply Hough transform
For example:
This turns the wavelet response into a more line-like mask.
Multi-Scale Analysis
One Haar decomposition level detects features at one scale. If you care about both thin and thick lines, use a multi-level wavelet decomposition and inspect how line responses behave at different resolutions.
That is one of the main advantages of wavelet-based analysis over some purely local edge operators: it naturally supports multi-scale structure.
However, if your problem is simply finding long straight lines in a clean image, a Sobel filter plus Hough transform may be simpler. Haar wavelets become more attractive when scale and directional decomposition are genuinely useful.
Common Pitfalls
The most common mistake is expecting the Haar transform alone to output clean line segments. It produces directional detail coefficients, not a finished line-detection result.
Another mistake is using the wrong detail band for the line orientation of interest. Horizontal and vertical responses are separated for a reason.
Developers also often choose thresholds without normalizing image intensity first. That makes parameter tuning much less predictable.
Finally, wavelet responses are scale-dependent. If the lines are much thicker or thinner than expected, a single decomposition level may miss them or fragment them badly.
Summary
- Haar wavelets detect directional intensity changes that are useful for line analysis.
- Use a 2D Haar transform to obtain approximation and detail coefficient bands.
- Focus on the directional detail band that matches the line orientation you want.
- Threshold and post-process the wavelet response to turn it into a usable line mask.
- For final line segments, combine the wavelet response with morphology or another line-extraction method.
Related reading
- How to use UIVisualEffectView to Blur Image?
- How would I increase my accuracy in the cifar-100 dataset? I have a 10 accuracy at the moment
- How would I tint an image programmatically on iOS?
- I want to know the size of bounding box in object-detection api
- Image augmentation makes performance worse
- Image classification in python
- Image classification with Keras on Tensorflow how to find which images are misclassified during training?
- Image clustering by its similarity in python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.