代码之家  ›  专栏  ›  技术社区  ›  Spoike Otávio Décio

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

  •  2
  • Spoike Otávio Décio  · 技术社区  · 16 年前

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

    假设我有以下函数,其中数组内的整数是 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;
    }
    

    现在,我一直在努力弄清楚如何使用过滤器。我一直在尝试维基百科,但我发现 articles they have 没有什么特别有用的。有人能告诉我这件事,或者知道任何简单的代码示例吗?

    1 回复  |  直到 16 年前
        1
  •  2
  •   schnaader    16 年前

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

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