【快速高斯模糊的实现】

转自:http://www.cnblogs.com/tntmonks/p/4899649.html

刚才发现一份快速高斯模糊的实现。

源地址为:http://incubator.quasimondo.com/processing/gaussian_blur_1.php

作者信息为:
Fast Gaussian Blur v1.3 by Mario Klingemann <http://incubator.quasimondo.com>
processing源码: http://incubator.quasimondo.com/processing/fastblur.pde
效果图:



转为C语言实现版本。

代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Fast Gaussian Blur v1.3
// by Mario Klingemann <http://incubator.quasimondo.com>
// C version updated and performance optimization by tntmonks(http://tntmonks.cnblogs.com)
// One of my first steps with Processing. I am a fan
// of blurring. Especially as you can use blurred images
// as a base for other effects. So this is something I
// might get back to in later experiments.
//
// What you see is an attempt to implement a Gaussian Blur algorithm
// which is exact but fast. I think that this one should be
// relatively fast because it uses a special trick by first
// making a horizontal blur on the original image and afterwards
// making a vertical blur on the pre-processed image. This
// is a mathematical correct thing to do and reduces the
// calculation a lot.
//
// In order to avoid the overhead of function calls I unrolled
// the whole convolution routine in one method. This may not
// look nice, but brings a huge performance boost.
//
//
// v1.1: I replaced some multiplications by additions
//       and added aome minor pre-caclulations.
//       Also add correct rounding for float->int conversion
//
// v1.2: I completely got rid of all floating point calculations
//       and speeded up the whole process by using a
//       precalculated multiplication table. Unfortunately
//       a precalculated division table was becoming too
//       huge. But maybe there is some way to even speed
//       up the divisions.
//
// v1.3: Fixed a bug that caused blurs that start at y>0
//   to go wrong. Thanks to Jeroen Schellekens for
//       finding it!
 
void  GaussianBlur(unsigned  char * img, unsigned   int  x, unsigned  int  y, unsigned  int  w, unsigned  int  h, unsigned  int  comp, unsigned  int  radius)
{
     unsigned  int  i, j ;
     radius = min(max(1, radius), 248);
     unsigned  int  kernelSize = 1 + radius * 2;
     unsigned  int * kernel = (unsigned  int *) malloc (kernelSize*  sizeof (unsigned  int ));
     memset (kernel, 0, kernelSize*  sizeof (unsigned  int ));
     unsigned  int (*mult)[256] = (unsigned  int (*)[256]) malloc (kernelSize * 256 *  sizeof (unsigned  int ));
     memset (mult, 0, kernelSize * 256 *  sizeof (unsigned  int ));
     unsigned     int  sum = 0;
     for  (i = 1; i < radius; i++){
         unsigned  int  szi = radius - i;
         kernel[radius + i] = kernel[szi] = szi*szi;
         sum += kernel[szi] + kernel[szi];
         for  (j = 0; j < 256; j++){
             mult[radius + i][j] = mult[szi][j] = kernel[szi] * j;
         }
     }
     kernel[radius] = radius*radius;
     sum += kernel[radius];
     for  (j = 0; j < 256; j++){
          
         mult[radius][j] = kernel[radius] * j;
     }
 
     unsigned  int    cr, cg, cb;
     unsigned  int    xl, yl, yi, ym, riw;
     unsigned  int    read, ri, p,   n;
     unsigned     int  imgWidth = w;
     unsigned     int  imgHeight = h;
     unsigned     int  imageSize = imgWidth*imgHeight;
     unsigned  char  * rgb = (unsigned  char  *) malloc ( sizeof (unsigned  char ) * imageSize * 3);
     unsigned  char  * r = rgb;
     unsigned  char  * g = rgb + imageSize;
     unsigned  char  * b = rgb + imageSize * 2;
     unsigned  char  * rgb2 = (unsigned  char  *) malloc ( sizeof (unsigned  char ) * imageSize * 3);
     unsigned  char  * r2 = rgb2;
     unsigned  char  * g2 = rgb2 + imageSize;
     unsigned  char  * b2 = rgb2 + imageSize * 2;
  
     for  ( size_t  yh = 0; yh < imgHeight; ++yh) {
  
         for  ( size_t  xw = 0; xw < imgWidth; ++xw) {
             n = xw + yh* imgWidth;
             p = n*comp;
             r[n] = img[p];
             g[n] = img[p + 1];
             b[n] = img[p + 2];
         }
     }
     
     x = max(0, x);
     y = max(0, y);
     w = x + w - max(0, (x + w) - imgWidth);
     h = y + h - max(0, (y + h) - imgHeight);
     yi = y*imgWidth;
  
     for  (yl = y; yl < h; yl++){
  
         for  (xl = x; xl < w; xl++){
             cb = cg = cr = sum = 0;
             ri = xl - radius;
             for  (i = 0; i < kernelSize; i++){
                 read = ri + i;
                 if  (read >= x && read < w)
                 {
                     read += yi;
                     cr += mult[i][r[read]];
                     cg += mult[i][g[read]];
                     cb += mult[i][b[read]];
                     sum += kernel[i];
                 }
             }
             ri = yi + xl;
             r2[ri] = cr / sum;
             g2[ri] = cg / sum;
             b2[ri] = cb / sum;
         }
         yi += imgWidth;
     }
     yi = y*imgWidth;
  
     for  (yl = y; yl < h; yl++){
         ym = yl - radius;
         riw = ym*imgWidth;
         for  (xl = x; xl < w; xl++){
             cb = cg = cr = sum = 0;
             ri = ym;
             read = xl + riw;
             for  (i = 0; i < kernelSize; i++){
                 if  (ri < h && ri >= y)
                 {
                     cr += mult[i][r2[read]];
                     cg += mult[i][g2[read]];
                     cb += mult[i][b2[read]];
                     sum += kernel[i];
                 }
                 ri++;
                 read += imgWidth;
             }
             p = (xl + yi)*comp;
             img[p] = (unsigned  char )(cr / sum);
             img[p + 1] = (unsigned  char )(cg / sum);
             img[p + 2] = (unsigned  char )(cb / sum);
         }
         yi += imgWidth;
     }
 
     free (rgb);
     free (rgb2);
     free (kernel);
     free (mult);
}

  

  

  该代码,将二维数组进一步优化后可提升一定的速度。

在博主机子上测试一张5000x3000的图像,模糊半径为10的情况下,耗时4s.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 材质球高斯模糊shader是一种用于渲染图像的特殊技术。高斯模糊Gaussian blur)是一种常用于图像处理中的滤镜效果,能够使图像变得更加模糊和柔和。 对于材质球来说,高斯模糊shader是一种应用于表面的特殊效果,能够使得材质的外观变得模糊。这种效果常用于创建一些特殊的视觉效果,比如表示景深或者模拟柔和的光线。 高斯模糊shader的基本原理是通过对图像进行多次模糊处理,使用一组叫做高斯核(Gaussian kernel)的卷积矩阵来实现。卷积运算会通过对图像中的每个像素点与该核进行相加的方式创建新的模糊像素。多次进行卷积运算,可以得到更加模糊的效果。 在材质球的实现中,高斯模糊shader通常需要设置一些参数,比如模糊的程度和半径。通过调整这些参数,可以达到不同程度的模糊效果。此外,高斯模糊shader还可以与其他shader效果配合使用,从而获得更加复杂的渲染效果。 总的来说,材质球高斯模糊shader是一种非常常用的图像处理技术,可以应用于各种需要模糊效果的场景。它不仅能够增强图像的柔和感,还可以用于创造出更加艺术化的视觉效果。 ### 回答2: 高斯模糊shader是一种常用的图像处理技术,用于在计算机图形中创建模糊效果。它使用高斯函数来对每个像素周围的像素进行加权平均,以模拟出焦点外的图像模糊。 材质球高斯模糊shader是将高斯模糊应用于物体的表面材质,以产生模糊的外观效果。通过将高斯模糊shader应用于物体的材质,可以在渲染过程中实时模糊物体的外观,从而实现一些特殊的视觉效果。 使用材质球高斯模糊shader可以带来一些实际的应用。例如,在游戏中,可以用它来实现物体的动态模糊效果,比如快速移动的车辆或人物,可以通过高斯模糊使其看起来更加流畅,减少运动模糊。 另外,高斯模糊shader还可以在电影特效中使用,例如用于在影片中创建屏幕上的文字模糊效果,使其在画面中更加和谐统一。 通过调整高斯模糊shader的参数,可以实现不同的效果。可以通过改变模糊半径来调整模糊的程度,通过改变方向和强度来调整模糊的方向和强度。 总之,材质球高斯模糊shader在计算机图形和视觉效果中扮演着重要的角色。它通过模拟模糊效果,可以使物体看起来更加真实和流畅,从而提升渲染的质量和观感。 ### 回答3: 材质球高斯模糊shader是一种可用于渲染引擎中的特殊shader程序,用于实现高斯模糊效果。高斯模糊是一种常用的图像处理技术,其原理是对图像中的像素进行加权平均,以降低图像细节并模糊图像。 在材质球高斯模糊shader中,首先需要确定模糊的程度。通过调整高斯函数中的标准差来实现模糊的强度,标准差越大,模糊效果越明显。 然后,shader程序会根据标准差计算出一组权重值,这些权重值代表了每个像素所需的模糊程度。通常使用二维高斯函数来计算权重值,较远的像素权重较小,较近的像素权重较大。 接下来,shader会遍历每个像素,并根据权重值对周围像素进行加权平均。这样,每个像素所得的模糊值就是周围像素的加权平均值。通过循环迭代多次,可以增加模糊效果的强度。 最后,将计算得到的模糊像素值应用到渲染的场景中,以实现高斯模糊效果。 总之,材质球高斯模糊shader是一种能够实现图像模糊效果的特殊shader程序。通过计算周围像素的加权平均值,可以实现不同程度的模糊效果,使渲染的图像看起来更加柔和和模糊。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值