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

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

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

作者信息为: Fast Gaussian Blur v1.3by 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.

【快速高斯模糊的实现】相关推荐

  1. 高斯模糊的Java实现及优化(含源文件)

    高斯模糊是被广泛使用的图形算法之一,在实现高斯模糊之前,先要了解正态分布 正态分布 一维的正态分布为 直接让f(x)和f(y)相乘,就得到了二维的正态分布 此处直接令μ=0,将会在下面解释. 权值矩阵 ...

  2. 图形算法 - 模糊函数比较,Blur Function Compare

    from: http://www.cppblog.com/foxriver/archive/2011/01/11/138316.html 加入比较的4种方法有: 1. 快速高斯模糊. 2. 二次Sum ...

  3. 我爱机器学习网机器学习类别文章汇总

    机器学习领域的几种主要学习方式 From Stumps to Trees to Forests KDD-2014 – The Biggest, Best, and Booming Data Scien ...

  4. 我爱机器学习--机器学习方向资料汇总

    转载:http://blog.csdn.net/shuimanting520/article/details/45748505 机器学习爱好者资料 机器学习领域的几种主要学习方式 From Stump ...

  5. python图片读取优化_Python下图片的高斯模糊化的优化

    资源下载 #本文PDF版下载 Python下图片的高斯模糊化的优化(或者单击我博客园右上角的github小标,找到lab102的W6目录下即可) #本文代码下载 高斯模糊(一维)优化代码(和本文方法集 ...

  6. html快速把网站整体去色/高斯模糊

    一.页面整体调成灰黑色/去色 html { filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); -webkit-fil ...

  7. [转]后期-快速消除痘痘,完美修复MM肌肤

    是面对美景,即使皮肤不好也得露个脸啊!那MM的面子问题怎么办呢?简单,咱就通过Photoshop后期处理来<?xml:namespace prefix = o /> 给MM打造完美水嫩的肌 ...

  8. 【如何快速的开发一个完整的iOS直播app】(美颜篇)

    前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,美颜功能是很重要的,如果没有美颜功能,可能分分钟钟掉粉千万,本篇主要讲 ...

  9. akaze特征匹配怎么去掉不合适的点_一种无人机滑坡遥感影像的快速匹配算法

    作 者 信 息 郝豪杰1,2,3,刘贤赵3,李朝奎1,2,方 军1,2 (1. 湖南科技大学 地理空间信息技术国家地方联合工程实验室,湖南 湘潭 411201:2. 湖南科技大学 测绘遥感信息工程湖南 ...

  10. 炫酷背光文字html,详细PS教程大放送:如何快速做出高级创意的文字背光效果?...

    原标题:详细PS教程大放送:如何快速做出高级创意的文字背光效果? 现在做海报,做广告等,都需要有文案,高级炫酷的文字,会为你的作品加分,让整张海报瞬间高逼格,那如何快速用ps做错高级创意的文字背光效果 ...

最新文章

  1. 分析460万份数据发现,女警比男警检查汽车几率少2倍,但发现违禁品还多10%
  2. SVN、Git设置提交时忽略的文件
  3. PyTorch模型量化工具学习
  4. 设计模式学习(六):重构与模式,推荐书籍(完)
  5. 2021-04-12 电机滑模控制 LuGre摩擦模型
  6. C#与C++ DLL的交互
  7. 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
  8. matlab的max与min函数
  9. 在window是下安装hadoop过程
  10. 汉诺塔移动次数递归算法c语言,谁知道C语言汉诺塔递归算法及其详细注释?
  11. 大学生心理健康调研报告
  12. 如何用linux系统进行远程控制windows服务器
  13. 实名寻人搜索引擎app_谷歌搜索引擎寻人
  14. android 自定义吐司,[Android开发]Android 自定义Toast
  15. CVE-2017-12635+12636 复现+反弹shell
  16. 第十四周 项目2 - 用哈希法组织关键字
  17. Trunk支持vlan的范围
  18. vhs预设_如何在Linux中数字化VHS磁带
  19. 5.2 BGP水平分割
  20. Linux篇之解决root密码修改失败报错之Authentication token manipulation error

热门文章

  1. java二维数组冷知识
  2. 数据结构———>队列
  3. python如何获取权限_python 权限系统设计
  4. (day 37 - 动态规划)剑指 Offer 46. 把数字翻译成字符串
  5. android中修饰void的类型,方法添加Android中
  6. Java进阶:Docker
  7. ElementUI:input表单验证
  8. Java编程:排序算法——希尔排序
  9. linux远程控制本地用户登录,linux 本地无法登录 远程可以登陆的解决办法
  10. 线性表_循环链表(增减删查 + 约瑟夫环问题 代码实现 )