# Image processing stuff

Integral Image

#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;

int main() {
    unsigned int WIDTH = 4;
    unsigned int HEIGHT = 3;
    assert((WIDTH > 0) && (HEIGHT > 0));

    /* create pseudo 2D array / vector for computing a summed-area table */
    vector <vector<int> > xs(WIDTH, vector<int>(HEIGHT, 0));
    assert(xs.size() == WIDTH);
    assert(xs[0].size() == HEIGHT);

    /* fill array / vector with dump values */
    int counter = 1;
    for(unsigned int y = 0; y < HEIGHT; ++y)
        for(unsigned int x = 0; x < WIDTH; ++x)
            xs[x][y] = counter++;


    vector< vector<unsigned int> > summedAreaTable(WIDTH, vector<unsigned int>(HEIGHT, 0));

    /* preprocess filling our summed-area table */
    int value = 0;
    for(unsigned int y = 0; y < HEIGHT; ++y) {
        for(unsigned int x = 0; x < WIDTH; ++x) {
            /* get value from array / vector */
            value = xs[x][y];

            /* compute summed-area table value, avoid out of bounce access */
            if(y > 0)
                value += summedAreaTable[x][y-1];
            if(x > 0)
                value += summedAreaTable[x-1][y];
            if(x > 0 && y > 0)
                value -= summedAreaTable[x-1][y-1];

            /* save computed value in summed-area table */
            summedAreaTable[x][y] = value;
        }
    }

    /* output the tables */
    cout << "\nOriginal Table:\n";
    for(unsigned int y = 0; y < HEIGHT; ++y) {
        for(unsigned int x = 0; x < WIDTH; ++x) {
            cout << xs[x][y] << " ";
        }
        cout << endl;
    }

    cout << "\nSummed Area Table:\n";
    for(unsigned int y = 0; y < HEIGHT; ++y) {
        for(unsigned int x = 0; x < WIDTH; ++x) {
            cout << summedAreaTable[x][y] << " ";
        }
        cout << endl;
    }

    return 0;
}

Last updated