Haar wavelet
line detection
image processing
computer vision
edge detection

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.

Practice ML system design

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.

python
1import cv2
2import numpy as np
3import pywt
4
5image = cv2.imread("input.png", cv2.IMREAD_GRAYSCALE)
6image = image.astype(np.float32) / 255.0

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:

python
1cA, (cH, cV, cD) = pywt.dwt2(image, "haar")
2
3print(cA.shape)
4print(cH.shape, cV.shape, cD.shape)

Interpretation:

  • 'cA is the coarse approximation'
  • 'cH emphasizes horizontal detail'
  • 'cV emphasizes vertical detail'
  • 'cD emphasizes 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.

python
1def threshold_band(band, threshold):
2    return np.where(np.abs(band) > threshold, np.abs(band), 0)
3
4horizontal_response = threshold_band(cH, 0.1)
5vertical_response = threshold_band(cV, 0.1)

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.

python
1horizontal_vis = cv2.resize(horizontal_response, (image.shape[1], image.shape[0]))
2vertical_vis = cv2.resize(vertical_response, (image.shape[1], image.shape[0]))
3
4cv2.imwrite("horizontal_lines.png", (horizontal_vis * 255).astype(np.uint8))
5cv2.imwrite("vertical_lines.png", (vertical_vis * 255).astype(np.uint8))

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:

python
1binary = (horizontal_vis > 0.2).astype(np.uint8) * 255
2kernel = np.ones((3, 3), np.uint8)
3cleaned = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
4
5cv2.imwrite("horizontal_binary.png", cleaned)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.