【问题标题】:How to write a downsampling function in Java如何在 Java 中编写下采样函数
【发布时间】:2009-10-20 13:17:31
【问题描述】:

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

假设我有以下函数,其中数组内的整数是 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 没有特别有用。任何人都可以提示我或知道任何简单的代码示例吗?

【问题讨论】:

  • 您是否故意不使用 Java API 来调整图像大小?是为了锻炼吗?作业?
  • 我正在使用 Processing.org。我的代码比这复杂一点,输入是一个重复模式(我已经编写了一个函数来从中获取像素)。在创建输出图像然后使用处理 API 调整其大小时,我最终遇到了 OutOfMemoryErrors。因为最后我希望它调整大小,所以我通过自己生成调整大小的版本来避免不必要地浪费 Java 中的堆空间。

标签: java image-processing signal-processing


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多