Fast Gaussian blur and Gaussian blur
I have previously written two articles on fast Gaussian filtering, but the code is not complete.
For the algorithm source and introduction, see the blog http://www.cnblogs.com/images#/archive/2013/01/07/2849782.html.
In order to avoid wasting too much time when a friend needs to read my article, delete the first two articles and rewrite this article to provide useful information for those who need it.
The complete c code is provided below.The following code can be further optimized, regardless of the format or speed.
1 /************************************** ** 2 * src: RAW image data * 3 * dst: blurred image data * 4 * width: Image width * 5 * height: Image height * 6 * stride: number of bytes per line of the image * 7 * chan: Number of image channels * 8 * sigma: Gaussian parameter * 9 * chan: number of image channels * 10 *********************************** * *****/11 void IMG_GaussBlur (unsigned char * src, unsigned char * dst, int width, int height, int stride, int chan, float sigma) 12 {13 int I = 0; 14 int h, w; 15 int row = 0; 16 int col = 0; 17 int pos = 0; 18 int channel = 0; 19 int n = 0; 20 int bufsize = 0; 21 int size = 0; 22 int rowstride = 0; 23 int itemp0 = 0; 24 int itemp1 = 0; 25 float temp = 0; 26 float fTab [256] = {0 }; 27 unsigned char * ps; 28 float * pIn; 29 30 int blurH = height + 3; 31 int blurW = width + 3; 32 for (I = 0; I <256; I ++) 33 {34 fTab [I] = I + 1; 35} 36 37 int channelsize = width * height + (width + height) * 6; 38 39 if (width> height) 40 {41 bufsize = width + 6; 42} 43 else 44 {45 bufsize = height + 6; 46} 47 48 float * w1 = (float *) malloc (bufsize * sizeof (float); 49 float * w2 = (float *) malloc (bufsize * sizeof (float); 50 float * in = (float *) malloc (channelsize * sizeof (float); 51 float * out = (float *) malloc (channelsize * sizeof (float); 52 53 // ------------------ calculate Gaussian Kernel --------------------------------------- // 54 float q = 0; 55 float q2, q3; 56 double b0; 57 double b1; 58 double b2; 59 double b3; 60 double B = 0; 61 int N = 3; 62 63 if (sigma >=2.5) 64 {65 q = 0.98711 * sigma-0.96330; 66} 67 else if (sigma> = 0.5) & (sigma <2.5 )) 68 {69 q = 3.97156-4.14554 * (float) sqrt (double) 1-0.26891 * sigma); 70} 71 else 72 {73 q = 0.1147705018520355224609375; 74} 75 76 q2 = q * q; 77 q3 = q * q2; 78 b0 = (1.57825 + (2.44413 * q) + (1.4281 * q2) + (0.422205 * q3); 79 b1 = (2.44413 * q) + (2.85619 * q2) + (1.26661 * q3 )); 80 b2 = (-(1.4281 * q2) + (1.26661 * q3); 81 b3 = (0.422205 * q3 )); 82 B = 1.0-(b1 + b2 + b3)/b0); 83 84 // The acceleration method reduces cycles multiple times/b0 85 b1/= b0; 86 b2/= b0; 87 b3/= b0; 88 89 // ------------------ computing Gaussian Kernel end --------------------------------------- // 90 // multiple channels for processing images 91 for (channel = 0; channel <chan; channel ++) 92 {93 // obtain all pixel values of a channel 94 pIn = in; 95 for (h = 0; h Call reference
1 IplImage * src = cvLoadImage (". /test.jpg ", 1); 2 IplImage * dst = cvLoadImage (". /test.jpg ", 1); 3 4 IMG_GaussBlur (unsigned char *) src-> imageData, 5 (unsigned char *) dst-> imageData, 6 src-> width, src-> height, 7 src-> widthStep, 8 src-> nChannels, 2 );View Code