快速堆栈模糊算法

上一篇快速高斯模糊的原作者也有另一个比较快速的模糊算法Stack Blur,字面意思为堆栈模糊。

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

这个算法在多个平台上都有实现,安卓以及IOS,JS等。

processing源码:http://incubator.quasimondo.com/processing/stackblur.pde 效果图:

转为C语言实现版本。

代码如下:
// Stack Blur v1.0
//
// Author: Mario Klingemann <[email protected]>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// C version updated and performance optimization by tntmonks(http://tntmonks.cnblogs.com)

// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
// Stack Blur Algorithm by Mario Klingemann <[email protected]>

#define MAX(x,y) (x>y?x:y)
#define MIN(x,y) (x>y?y:x)
void fastStackBlur(unsigned char* pix, unsigned int w, unsigned int h, unsigned int comp, int radius) {
	unsigned int wm = w - 1;
	unsigned int hm = h - 1;
	unsigned int imageSize = w * h;
	unsigned int div = radius + radius + 1;

	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;
	int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;

	unsigned int *vmin = (unsigned int *)malloc(MAX(w, h) * sizeof(unsigned int));

	int divsum = (div + 1) >> 1;
	divsum *= divsum;
	int *dv = (int *)malloc(256 * divsum * sizeof(int));
	for (i = 0; i < 256 * divsum; i++) {
		dv[i] = (i / divsum);
	}

	yw = yi = 0;

	int(*stack)[3] = (int(*)[3])malloc(div * 3 * sizeof(int));
	unsigned int stackpointer;
	unsigned int stackstart;
	int *sir;
	int rbs;
	int r1 = radius + 1;
	int routsum, goutsum, boutsum;
	int rinsum, ginsum, binsum;

	for (y = 0; y < h; y++) {
		rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
		for (i = -radius; i <= radius; i++) {
			p = yi + (MIN(wm, MAX(i, 0)));
			sir = stack[i + radius];
			sir[0] = pix[(p*comp)];
			sir[1] = pix[(p*comp) + 1];
			sir[2] = pix[(p*comp) + 2];

			rbs = r1 - abs(i);
			rsum += sir[0] * rbs;
			gsum += sir[1] * rbs;
			bsum += sir[2] * rbs;
			if (i > 0) {
				rinsum += sir[0];
				ginsum += sir[1];
				binsum += sir[2];
			}
			else {
				routsum += sir[0];
				goutsum += sir[1];
				boutsum += sir[2];
			}
		}
		stackpointer = radius;

		for (x = 0; x < w; x++) {

			r[yi] = dv[rsum];
			g[yi] = dv[gsum];
			b[yi] = dv[bsum];

			rsum -= routsum;
			gsum -= goutsum;
			bsum -= boutsum;

			stackstart = stackpointer - radius + div;
			sir = stack[stackstart % div];

			routsum -= sir[0];
			goutsum -= sir[1];
			boutsum -= sir[2];

			if (y == 0) {
				vmin[x] = MIN(x + radius + 1, wm);
			}
			p = yw + vmin[x];

			sir[0] = pix[(p*comp)];
			sir[1] = pix[(p*comp) + 1];
			sir[2] = pix[(p*comp) + 2];
			rinsum += sir[0];
			ginsum += sir[1];
			binsum += sir[2];

			rsum += rinsum;
			gsum += ginsum;
			bsum += binsum;

			stackpointer = (stackpointer + 1) % div;
			sir = stack[(stackpointer) % div];

			routsum += sir[0];
			goutsum += sir[1];
			boutsum += sir[2];

			rinsum -= sir[0];
			ginsum -= sir[1];
			binsum -= sir[2];

			yi++;
		}
		yw += w;
	}
	for (x = 0; x < w; x++) {
		rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
		yp = -radius * w;
		for (i = -radius; i <= radius; i++) {
			yi = MAX(0, yp) + x;

			sir = stack[i + radius];

			sir[0] = r[yi];
			sir[1] = g[yi];
			sir[2] = b[yi];

			rbs = r1 - abs(i);

			rsum += r[yi] * rbs;
			gsum += g[yi] * rbs;
			bsum += b[yi] * rbs;

			if (i > 0) {
				rinsum += sir[0];
				ginsum += sir[1];
				binsum += sir[2];
			}
			else {
				routsum += sir[0];
				goutsum += sir[1];
				boutsum += sir[2];
			}

			if (i < hm) {
				yp += w;
			}
		}
		yi = x;
		stackpointer = radius;
		for (y = 0; y < h; y++) {

			pix[(yi*comp)] = dv[rsum];
			pix[(yi*comp) + 1] = dv[gsum];
			pix[(yi*comp) + 2] = dv[bsum];
			rsum -= routsum;
			gsum -= goutsum;
			bsum -= boutsum;

			stackstart = stackpointer - radius + div;
			sir = stack[stackstart % div];

			routsum -= sir[0];
			goutsum -= sir[1];
			boutsum -= sir[2];

			if (x == 0) {
				vmin[y] = MIN(y + r1, hm) * w;
			}
			p = x + vmin[y];

			sir[0] = r[p];
			sir[1] = g[p];
			sir[2] = b[p];

			rinsum += sir[0];
			ginsum += sir[1];
			binsum += sir[2];

			rsum += rinsum;
			gsum += ginsum;
			bsum += binsum;

			stackpointer = (stackpointer + 1) % div;
			sir = stack[stackpointer];

			routsum += sir[0];
			goutsum += sir[1];
			boutsum += sir[2];

			rinsum -= sir[0];
			ginsum -= sir[1];
			binsum -= sir[2];

			yi += w;
		}
	}

	free(rgb);
	free(vmin);
	free(dv);
	free(stack);
}

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

时间: 2024-10-11 13:13:05

快速堆栈模糊算法的相关文章

快速高速模糊算法

刚才发现一份快速高速模糊的实现. 源地址为: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语言

快速模糊算法

前段时间在网上看到一个快速模糊算法,性能很不错. 源博客: http://www.lellansin.com/super-fast-blur-%E6%A8%A1%E7%B3%8A%E7%AE%97%E6%B3%95.html 博主对其进行了简单的bug修正以及性能优化. 在博主机子上使用该算法对一张5000x3000的图片进行模糊处理,仅需500-600毫秒,速度非常快. 代码如下: /* * Super Fast Blur v1.1+ * Original author: Mario Klin

最快速的“高斯”模糊算法(附Android源码)

? 这是一个外国人的算法,本人是搬运工.参考:http://blog.ivank.net/fastest-gaussian-blur.html ? 1:高斯模糊算法(参考:http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html) 所谓的模糊算法就是当前像素点和周围像素点进行加权均值之后的结果替换当前像素值.因此均值模糊是最简单的,只要将周围的像素点相加取平均值即可. ????而高斯模糊则是将周围的像素点的权值按照高斯分布进行取值,即根据

每日一小练——快速Fibonacci数算法

上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:快速Fibonacci数算法 内容:先说说Fibonacci数列,它的定义是数列:f1,f2....fn有如下规律: 尝试寻找快速的求出fn的方法 我的解法:上来没多想,打开vs2013就敲了起来,问题果然很简单,分分钟就超神..奥,不对就解决了! 其实题目中就给出了这个算法的递归形式,所以首先我想到的是递归解法,不过因为求解快速方法在递归之前,我编写了一个非递归的算法 #include <iostream> usi

位运算之——按位与(&amp;)操作——(快速取模算法)

由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. 按位与(Bitwise AND),运算符号为& a&b 的操作的结果:a.b中对应位同时为1,则对应结果位也为1. 例如: 10010001101000101011001111000 & 111111100000000 --------------------------------------------- 10101100000000 对10101100000000进行右移8位得到的是101011,这就得

移植StackBlur模糊算法至SDL

StackBlur是Android标配的模糊算法,这也在当时引起了一股毛玻璃热潮.IOS7就采用了此算法(这有抄袭Android之嫌,因为Android1.5就在标库中加入了此函数).算法效率很高,这也是能流畅运行在移动设备上的原因.但其只在Andorid上是标准库函数,移植版也只出现在Java上.本文带来一个在SDL上移植的StackBlur算法.运行效果基本如下图. 一眼就可以看出,毛玻璃效果与Radius有直接的关系.Radius在5时,效果与动态模糊相似,在10时,会有点马赛克的感觉,在

算法快速回顾——排序算法

常用排序算法有以下几种:冒泡排序.插入排序.快速排序.归并排序.堆排序. 本文将对五种常用算法分析并实现. //交换两个元素的值 这里列出几种不同写法 void swap(int *a, int *b) { int c = *a; *a = *b; *b = c; } void swap(int *a,int *b) { *a = (*a)^(*b); *b = (*b)^(*a); *a = (*a)^(*b); } void swap(int *a,int *b) { *a = *a + *

快速傅立叶变换算法FFT——图像处理中的数学原理详解22

欢迎关注我的博客专栏"图像处理中的数学原理详解" 全文目录请见 图像处理中的数学原理详解(总纲) http://blog.csdn.net/baimafujinji/article/details/48467225 图像处理中的数学原理详解(已发布的部分链接整理) http://blog.csdn.net/baimafujinji/article/details/48751037 交流学习可加图像处理研究学习QQ群(529549320) 傅立叶变换以高等数学(微积分)中的傅立叶级数为基

干货分享,图片模糊算法,解析xml绘制图片

        项目需求:有一个xml文件,记录了一张图片每个元素的位置,大小,样式信息. 通过XmlResourceParser解析xml文件,得到每个元素的属性. 然后使用Paint绘制元素到Canvas上,得到一张Bitmap位图 将位图模糊处理,处理算法的原理(取图片上每个像素点周围的8个点平均值) 模糊算法: package com.metek.blur; import android.content.Context; import android.graphics.Bitmap; p