2

我正在尝试为图像编写过滤函数,但我似乎无法理解(或记住)如何将所有数学理论转换为代码。

假设我有以下函数,其中数组内的整数是介于0和之间的整数255(为了简单起见,使用了相当多的灰度像素)。

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;
}

现在我一直在试图弄清楚如何使用过滤器。我一直在尝试维基百科,但我发现他们那里的文章 并不是特别有用。任何人都可以提示我或知道任何简单的代码示例吗?

4

1 回答 1

2

最简单的方法是最近邻下采样,如下所示:

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

但这并没有给出很好的结果,因此您可能需要使用多个输入像素并对它们进行平均以获得原始区域的更精确颜色的其他方法。

于 2009-10-20T13:25:20.317 回答