玻璃效应 - 艺术效果

meg*_*gha 5 c c++ algorithm graphics

我希望对图像产生影响,其中所得到的图像看起来好像我们通过纹理玻璃(不是普通/平滑)来看它...请帮我写一个算法来产生这样的效果.

这是我正在寻找的效果类型的一个例子

第一个图像是原始图像,第二个图像是我要查找的输出.

And*_*nck 4

首先创建一个噪声图,其尺寸(width + 1) x (height + 1)将用于替换原始图像。我建议使用某种柏林噪声,这样位移就不会随机。这是关于如何生成柏林噪声的一个很好的链接。

一旦我们有了噪音,我们就可以做这样的事情:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)