如何在Java中编写下采样函数

时间:2009-10-20 13:17:32

标签: java image-processing signal-processing

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

假设我有以下函数,其中数组中的整数是0255之间的整数(几乎是灰度像素,以保持简单)。

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

现在我一直试图弄清楚如何使用过滤器。我一直在尝试维基百科,但我发现articles they have没有特别的帮助。谁能让我知道这个或知道任何简单的代码示例?

1 个答案:

答案 0 :(得分: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];
    }
}

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