views:

268

answers:

1

I'm trying to write a filtering function for an image, but I can't seem to wrap my head around (or remember) how to transfer all that math theory into code.

Lets say I have the following function, where the ints inside the arrays are integers between 0 and 255 (pretty much grayscale pixels to keep it simple).

private int[][] resample(int[][] input, int oldWidth, int oldHeight,
        width, int height) 
{
    int[][] output = createArray(width, height);
        // Assume createArray creates an array with the given dimension

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            output[x][y] = input[x][y];
            // right now the output will be "cropped"
            // instead of resampled
        }
    }

    return output;
}

Right now I'm stuck trying to figure out how to use filters. I've been trying wikipedia but I find the articles they have there not particularly helpful. Can anyone clue me in on this or knows of any simple code sample?

+2  A: 

The easiest approach would be nearest-neighbor downsampling, like this:

for (int x = 0; x < width; ++x) {
    for (int y = 0; y < height; ++y) {
        output[x][y] = input[x*width/oldWidth][y*height/oldHeight];
    }
}

But this doesn't give nice results, so you might need other approaches that use several input pixels and average them to get a more exact color for the original region.

schnaader
thanks, this clears up some things for me
Spoike