gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:57:40 25 4
gpt4 key购买 nike

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

假设我有以下函数,其中数组中的整数是 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没有特别有帮助。谁能告诉我这件事或知道任何简单的代码示例?

最佳答案

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

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

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

关于java - 如何在 Java 中编写下采样函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1594543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com