玻璃效果 - 艺术效果

5
我希望给图片添加一种效果,使得结果图片看起来像是透过有纹理的玻璃观察(而不是平滑的)...请帮我编写一个算法来生成这样的效果。
以下是我要寻找的效果类型的示例
第一张图片是原始图片,第二张图片是我想要的输出结果。
2个回答

4

首先创建一个噪声图,其尺寸为(width + 1) x (height + 1),用于扭曲原始图像。建议使用某种Perlin噪声,以便扭曲效果不太随机。这里有一个很好的链接,介绍如何生成Perlin噪声。

一旦我们有了噪声,就可以进行以下操作:

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

1
我无法为您提供具体的例子,但是gamedev论坛和文章部分有大量关于图像处理、3D渲染等方面的优质内容。例如,这里有一篇文章介绍使用卷积矩阵来应用类似效果于图像,可能是一个不错的起点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接