PIXELBANKv9.1.0
Menu

Fast Haar-like Feature Computation using Integral Images

Haar-like filters consist of adjacent rectangular regions used to detect visual features like edges or lines. A crucial aspect of their use is the speed with which their response can be calculated, regardless of the filter size. This efficiency is achieved by using an Integral Image representation.

The Integral Image I(x,y)I(x,y) stores the sum of all pixel intensities in the original image that are above and to the left of position (x,y)(x,y). Given the Integral Image, the sum of intensities within any rectangular region defined by top-left corner (x0,y0)(x_0, y_0) and bottom-right corner (x1,y1)(x_1, y_1) can be calculated with just four lookups:

Sum=I(x1,y1)+I(x0−1,y0−1)−I(x1,y0−1)−I(x0−1,y1)\text{Sum} = I(x_1, y_1) + I(x_0-1, y_0-1) - I(x_1, y_0-1) - I(x_0-1, y_1)

Consider a 2-rectangle edge feature, defined by a Positive Region RPR_P and an adjacent Negative Region RNR_N. The filter's response is the difference between the sum of intensities in the positive region and the sum in the negative region:

Response=Sum(RP)−Sum(RN)\text{Response} = \text{Sum}(R_P) - \text{Sum}(R_N)

Your task is to compute the response of a vertical 2-rectangle Haar-like filter, given the Integral Image II and the coordinates defining the two regions.

Example:

Input:
I = [[1, 2, 3, 4],
     [3, 6, 9, 12],
     [4, 8, 12, 16],
     [5, 10, 15, 20]]
rp = [0, 0, 1, 0]  # Positive region: rows 0-1, col 0
rn = [2, 0, 3, 0]  # Negative region: rows 2-3, col 0
Output:
1
Reasoning:
  1. Calculate Sum(RP)\text{Sum}(R_P) for region [0,0][0,0] to [1,0][1,0]:

    • I(1,0)=3I(1,0) = 3, boundary terms are 0
    • Sum(RP)=3\text{Sum}(R_P) = 3
  2. Calculate Sum(RN)\text{Sum}(R_N) for region [2,0][2,0] to [3,0][3,0]:

    • I(3,0)=5I(3,0) = 5, I(1,0)=3I(1,0) = 3
    • Sum(RN)=5−3=2\text{Sum}(R_N) = 5 - 3 = 2
  3. Response = 3−2=13 - 2 = 1

Constraints:

  • The Integral Image II contains non-negative integers
  • Coordinates are given as: [x0,y0,x1,y1][x_0, y_0, x_1, y_1] (top-left to bottom-right)
  • All indices are 0-based
  • For indices <0< 0, assume the integral image value is 0
  • The output must be an integer
solution.py

Test Results

0/0
Run code to see test results.
Fast Haar-like Feature Computation using Integral Images - Medium | PixelBank