2010-01-11 41 views
5

我希望对图像产生影响,其中由此产生的图像看起来好像我们通过有纹理的玻璃(非平滑/光滑)看待它...请帮我写一个算法以产生这样的效果。玻璃效果 - 艺术效果

这里的效果我正在寻找

第一个图像是原始图像和第二图像是输出即时寻找的类型an example

回答

4

首先创建一个尺寸为(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); 
    } 
} 
+0

感谢安德烈亚斯。这正是我所期待的。再次感谢 – megha 2010-01-12 05:51:43

1

我不能给你一个具体的例子,但gamedev论坛&文章部分有很多图像处理,3d渲染等黄金。 例如,这里是an article谈论使用卷积矩阵对图像应用类似的效果,这可能是一个好的起点。