【发布时间】:2010-01-11 06:47:09
【问题描述】:
我希望给图像一个效果,生成的图像看起来好像我们正在通过有纹理的玻璃(不是普通/光滑)看它...请帮助我编写一个算法来生成这样的效果.
这是an example 我正在寻找的效果类型
第一个图像是原始图像,第二个图像是我正在寻找的输出。
【问题讨论】:
我希望给图像一个效果,生成的图像看起来好像我们正在通过有纹理的玻璃(不是普通/光滑)看它...请帮助我编写一个算法来生成这样的效果.
这是an example 我正在寻找的效果类型
第一个图像是原始图像,第二个图像是我正在寻找的输出。
【问题讨论】:
首先创建一个尺寸为(width + 1) x (height + 1)的噪声图,用于替换原始图像。我建议使用某种perlin noise 以便位移不是随机的。这是一个很好的link,关于如何生成柏林噪声。
一旦我们有了噪音,我们就可以这样做:
Image noisemap; //size is (width + 1) x (height + 1) gray scale values in [0 255] range
Image source; //source image
Image destination; //destination image
float displacementRadius = 10.0f; //Displacemnet amount in pixels
for (int y = 0; y < source.height(); ++y) {
for (int x = 0; x < source.width(); ++x) {
const float n0 = float(noise.getValue(x, y)) / 255.0f;
const float n1 = float(noise.getValue(x + 1, y)) / 255.0f;
const float n2 = float(noise.getValue(x, y + 1)) / 255.0f;
const int dx = int(floorf((n1 - n0) * displacementRadius + 0.5f));
const int dy = int(floorf((n2 - n0) * displacementRadius + 0.5f));
const int sx = std::min(std::max(x + dx, 0), source.width() - 1); //Clamp
const int sy = std::min(std::max(y + dy, 0), source.height() - 1); //Clamp
const Pixel& value = source.getValue(sx, sy);
destination.setValue(x, y, value);
}
}
【讨论】:
我无法为您提供具体示例,但gamedev 论坛和文章部分在图像处理、3D 渲染等方面有很多黄金。 例如,an article 谈论使用卷积矩阵将类似效果应用于图像,这可能是一个很好的起点。
【讨论】: