~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/vision/improc/im_lsf.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)
  2 **/
  3 /* im_lsf.c
  4  * 
  5  * linear sequential filter functions
  6  * 
  7  */
  8 
  9 #include <math.h>
 10 #include <tina/sys.h>
 11 #include <tina/sysfuncs.h>
 12 #include <tina/math.h>
 13 #include <tina/mathfuncs.h>
 14 #include <tina/vision.h>
 15 
 16 static void lsf_smooth(int n1, int n2, float *u1, float *u2, double sigma, float *weight)
 17 {
 18     int     i;
 19     float  *v1 = fvector_alloc(n1, n2);
 20     float  *v2 = fvector_alloc(n1, n2);
 21     double  a, b = exp(-1.0 / sigma), ab;
 22 
 23     a = (1 - b) / (1 + b);      /** makes filter sum to 1 **/
 24     ab = a * b;
 25 
 26     v1[n1] = (float)(a * u1[n1]);
 27     for (i = n1 + 1; i < n2; i++)
 28         v1[i] = (float)(a * u1[i] + b * v1[i - 1]);
 29 
 30     v2[n2 - 1] = (float) 0.0;
 31     for (i = n2 - 2; i >= n1; i--)
 32         v2[i] = (float) (ab * u1[i + 1] + b * v2[i + 1]);
 33 
 34     for (i = n1; i < n2; i++)
 35         u2[i] = v1[i] + v2[i];
 36 
 37     if (weight != NULL)
 38         for (i = n1; i < n2; i++)
 39             u2[i] /= weight[i];
 40 
 41     fvector_free((void *) v1, n1);
 42     fvector_free((void *) v2, n1);
 43 }
 44 
 45 static void lsf_smooth_side(int n1, int n2, float *u1, float *u2, double sigma, float *weight, int side)
 46 {
 47     int     i;
 48     double  a, b = exp(-1.0 / sigma);
 49 
 50     a = 1 - b;                  /** makes filter sum to 1 **/
 51 
 52     if (side == 1)
 53     {
 54         u2[n1] = (float)(a * u1[n1]);
 55         for (i = n1 + 1; i < n2; i++)
 56             u2[i] = (float)(a * u1[i] + b * u2[i - 1]);
 57     } 
 58     else if (side == -1)
 59     {
 60         u2[n2 - 1] = (float)(a * u1[n2 - 1]);
 61         for (i = n2 - 2; i >= n1; i--)
 62             u2[i] = (float)(a * u1[i] + b * u2[i + 1]);
 63     }
 64     else
 65     {
 66         for (i = n1; i < n2; i++)
 67            u2[i] = (float)u1[i];
 68     }
 69 
 70     if (weight != NULL)
 71         for (i = n1; i < n2; i++)
 72             u2[i] /= weight[i];
 73 }
 74 
 75 Imrect *imf_lsf_smooth(Imrect * im1, double sigma)
 76 {
 77     Imrect *im2;
 78     Imrect *im3;
 79     Imregion *roi;
 80     float  *row1, *row2, *col1, *col2, *ones, *weight;
 81     int     lx, ux, ly, uy;
 82     int     i;
 83 
 84     if (im1 == NULL)
 85         return (NULL);
 86 
 87     roi = im1->region;
 88     if (roi == NULL)
 89         return (NULL);
 90     lx = roi->lx;
 91     ux = roi->ux;
 92     ly = roi->ly;
 93     uy = roi->uy;
 94 
 95 
 96     ones = fvector_alloc(lx, ux);
 97     weight = fvector_alloc(lx, ux);
 98     for (i = lx; i < ux; ++i)
 99         ones[i] = (float)1.0;
100     lsf_smooth(lx, ux, ones, weight, sigma, (float *) NULL);
101     fvector_free((void *) ones, lx);
102 
103     im2 = im_alloc(im1->height, im1->width, roi, float_v);
104     row1 = fvector_alloc(lx, ux);
105     row2 = fvector_alloc(lx, ux);
106     for (i = ly; i < uy; ++i)
107     {
108         im_get_rowf(row1, im1, i, lx, ux);
109         lsf_smooth(lx, ux, row1, row2, sigma, weight);
110         im_put_rowf(row2, im2, i, lx, ux);
111     }
112     fvector_free((void *) row1, lx);
113     fvector_free((void *) row2, lx);
114     fvector_free((void *) weight, lx);
115 
116     ones = fvector_alloc(ly, uy);
117     weight = fvector_alloc(ly, uy);
118     for (i = ly; i < uy; ++i)
119         ones[i] = (float)1.0;
120     lsf_smooth(ly, uy, ones, weight, sigma, (float *) NULL);
121     fvector_free((void *) ones, ly);
122 
123     im3 = im_alloc(im1->height, im1->width, roi, float_v);
124     col1 = fvector_alloc(ly, uy);
125     col2 = fvector_alloc(ly, uy);
126     for (i = lx; i < ux; ++i)
127     {
128         im_get_colf(col1, im2, i, ly, uy);
129         lsf_smooth(ly, uy, col1, col2, sigma, weight);
130         im_put_colf(col2, im3, i, ly, uy);
131     }
132     fvector_free((void *) col1, ly);
133     fvector_free((void *) col2, ly);
134     fvector_free((void *) weight, ly);
135 
136     im_free(im2);
137 
138     return (im3);
139 }
140 
141 Imrect *imf_lsf_smooth_quad(Imrect * im1, double sigma, int sidex, int sidey)
142 {
143     Imrect *im2;
144     Imrect *im3;
145     Imregion *roi;
146     float  *row1, *row2, *col1, *col2, *ones, *weight;
147     int     lx, ux, ly, uy;
148     int     i;
149 
150     if (im1 == NULL)
151         return (NULL);
152 
153     roi = im1->region;
154     if (roi == NULL)
155         return (NULL);
156     lx = roi->lx;
157     ux = roi->ux;
158     ly = roi->ly;
159     uy = roi->uy;
160 
161 
162     ones = fvector_alloc(lx, ux);
163     weight = fvector_alloc(lx, ux);
164     for (i = lx; i < ux; ++i)
165         ones[i] = (float)1.0;
166     lsf_smooth_side(lx, ux, ones, weight, sigma, (float *) NULL, sidex);
167     fvector_free((void *) ones, lx);
168 
169     im2 = im_alloc(im1->height, im1->width, roi, float_v);
170     row1 = fvector_alloc(lx, ux);
171     row2 = fvector_alloc(lx, ux);
172     for (i = ly; i < uy; ++i)
173     {
174         im_get_rowf(row1, im1, i, lx, ux);
175         lsf_smooth_side(lx, ux, row1, row2, sigma, weight, sidex);
176         im_put_rowf(row2, im2, i, lx, ux);
177     }
178     fvector_free((void *) row1, lx);
179     fvector_free((void *) row2, lx);
180     fvector_free((void *) weight, lx);
181 
182     ones = fvector_alloc(ly, uy);
183     weight = fvector_alloc(ly, uy);
184     for (i = ly; i < uy; ++i)
185         ones[i] = (float)1.0;
186     lsf_smooth_side(ly, uy, ones, weight, sigma, (float *) NULL, sidey);
187     fvector_free((void *) ones, ly);
188 
189     im3 = im_alloc(im1->height, im1->width, roi, float_v);
190     col1 = fvector_alloc(ly, uy);
191     col2 = fvector_alloc(ly, uy);
192     for (i = lx; i < ux; ++i)
193     {
194         im_get_colf(col1, im2, i, ly, uy);
195         lsf_smooth_side(ly, uy, col1, col2, sigma, weight, sidey);
196         im_put_colf(col2, im3, i, ly, uy);
197     }
198     fvector_free((void *) col1, ly);
199     fvector_free((void *) col2, ly);
200     fvector_free((void *) weight, ly);
201 
202     im_free(im2);
203 
204     return (im3);
205 }
206 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.