def sat(x): """Summed area table / integral image. The integral image contains the sum of all elements above and to the left of it, i.e.: .. math:: S[m, n] = \sum_{i \leq m} \sum_{j \leq n} X[i, j] Parameters ---------- X : ndarray Input image. Returns ------- S : ndarray Summed area table. References ---------- .. [1] F.C. Crow, "Summed-area tables for texture mapping," ACM SIGGRAPH Computer Graphics, vol. 18, 1984, pp. 207-212. """ return x.cumsum(1).cumsum(0) def sat_sum(sat, r0, c0, r1, c1): """Use a summed area table / integral image to sum over a given window. Parameters ---------- sat : ndarray Summed area table / integral image. r0, c0 : int Top-left corner of block to be summed. r1, c1 : int Bottom-right corner of block to be summed. Returns ------- S : int Sum over the given window. """ S = 0 S += sat[r1, c1] if (r0 - 1 >= 0) and (c0 - 1 >= 0): S += sat[r0 - 1, c0 - 1] if (r0 - 1 >= 0): S -= sat[r0 - 1, c1] if (c0 - 1 >= 0): S -= sat[r1, c0 - 1] return S