19,471
社区成员




/*
* Calculate the coefficients for the recursive filter algorithm
* Fast Computation of gaussian blurring.
*/
static void
compute_coefs3 (gauss3_coefs *c, gfloat sigma)
{
/*
* Papers: "Recursive Implementation of the gaussian filter.",
* Ian T. Young , Lucas J. Van Vliet, Signal Processing 44, Elsevier 1995.
* formula: 11b computation of q
* 8c computation of b0..b1
* 10 alpha is normalization constant B
*/
gfloat q, q2, q3;
q = 0;
if (sigma >= 2.5)
{
q = 0.98711 * sigma - 0.96330;
}
else if ((sigma >= 0.5) && (sigma < 2.5))
{
q = 3.97156 - 4.14554 * (gfloat) sqrt ((double) 1 - 0.26891 * sigma);
}
else
{
q = 0.1147705018520355224609375;
}
q2 = q * q;
q3 = q * q2;
c->b[0] = (1.57825+(2.44413*q)+(1.4281 *q2)+(0.422205*q3));
c->b[1] = ( (2.44413*q)+(2.85619*q2)+(1.26661 *q3));
c->b[2] = ( -((1.4281*q2)+(1.26661 *q3)));
c->b[3] = ( (0.422205*q3));
c->B = 1.0-((c->b[1]+c->b[2]+c->b[3])/c->b[0]);
c->sigma = sigma;
c->N = 3;
}
View Code
static void
gausssmooth (gfloat *in, gfloat *out, gint size, gint rowstride, gauss3_coefs *c)
{
/*
* Papers: "Recursive Implementation of the gaussian filter.",
* Ian T. Young , Lucas J. Van Vliet, Signal Processing 44, Elsevier 1995.
* formula: 9a forward filter
* 9b backward filter
* fig7 algorithm
*/
gint i,n, bufsize;
gfloat *w1,*w2;
/* forward pass */
bufsize = size+3;
size -= 1;
w1 = (gfloat *) g_try_malloc (bufsize * sizeof (gfloat));
w2 = (gfloat *) g_try_malloc (bufsize * sizeof (gfloat));
w1[0] = in[0];
w1[1] = in[0];
w1[2] = in[0];
for ( i = 0 , n=3; i <= size ; i++, n++)
{
w1[n] = (gfloat)(c->B*in[i*rowstride] +
((c->b[1]*w1[n-1] +
c->b[2]*w1[n-2] +
c->b[3]*w1[n-3] ) / c->b[0]));
}
/* backward pass */
w2[size+1]= w1[size+3];
w2[size+2]= w1[size+3];
w2[size+3]= w1[size+3];
for (i = size, n = i; i >= 0; i--, n--)
{
w2[n]= out[i * rowstride] = (gfloat)(c->B*w1[n] +
((c->b[1]*w2[n+1] +
c->b[2]*w2[n+2] +
c->b[3]*w2[n+3] ) / c->b[0]));
}
g_free (w1);
g_free (w2);
}
delta=54;
N=256;
in=zeros(1,N);
in(N/2)=1;
if delta>=2.5
q=0.98711*delta-0.96330;
else
if (delta>=0.5)&&(delta<=2.5)
q=3.97156-4.14554*sqrt(1-0.26891*delta);
else
q = 0.1147705018520355224609375;
end
end
b0=1.57825+2.44413*q+1.4281*q*q+0.422205*q*q*q;
b1=2.44413*q+2.85619*q*q+1.26661*q*q*q;
b2=-1.4281*q*q-1.26661*q*q*q;
b3=0.422205*q*q*q;
B=1-(b1+b2+b3)/b0;
w1=zeros(1,N+3);
w1(1)=in(1);
w1(2)=in(1);
w1(3)=in(1);
for i=4:N+3
w1(i)=B*in(i-3)+(b1*w1(i-1)+b2*w1(i-2)+b3*w1(i-3))/b0;
end
figure,plot(w1),title('w1');
w2=zeros(1,N+3);
w2(N+1)=w1(N+3);
w2(N+2)=w1(N+3);
w2(N+3)=w1(N+3);
for i=N:-1:1
w2(i)=B*w1(i)+(b1*w2(i+1)+b2*w2(i+2)+b3*w2(i+3))/b0;
end
out=w2(1:N);
figure,plot(out),title('out');