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

Linux Cross Reference
Tina4/src/snake/region.c

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

  1 /**@(#)Snake region growing
  2   **/
  3 
  4 #include <stdio.h>
  5 #include <math.h>
  6 #include <values.h>
  7 #include <tina/sys.h>
  8 #include <tina/sysfuncs.h>
  9 #include <tina/math.h>
 10 #include <tina/mathfuncs.h>
 11 #include <tina/vision.h>
 12 #include <tina/visionfuncs.h>
 13 #include <tina/cvr.h>
 14 
 15 #define CVR_INSIDE   0
 16 #define CVR_BOUNDARY 1
 17 #define CVR_OUTSIDE  2
 18 
 19 void    er_seed_call(Imrect * im, int i, int j, int val, void (*func) ( /* ??? */ ), void *data)
 20 {
 21     if (j < im->region->lx || j >= im->region->ux)
 22         return;
 23     if (i < im->region->ly || i >= im->region->uy)
 24         return;
 25     if (IM_UCHAR(im, i, j) == (int)NULL)
 26     {
 27         (*func) (ipos(j, i), data);
 28         IM_UCHAR(im, i, j) = val;
 29         er_seed_call(im, i - 1, j, val, func, data);
 30         er_seed_call(im, i + 1, j, val, func, data);
 31         er_seed_call(im, i, j - 1, val, func, data);
 32         er_seed_call(im, i, j + 1, val, func, data);
 33     }
 34 }
 35 
 36 void    er_seed_call2(Imrect * im, int i, int j, int val, void (*func) ( /* ??? */ ), void *data)
 37 {
 38     if (j < im->region->lx || j >= im->region->ux)
 39         return;
 40     if (i < im->region->ly || i >= im->region->uy)
 41         return;
 42     if (IM_INT(im, i, j) == 0)
 43     {
 44         IM_INT(im, i, j) = val;
 45         (*func) (vec2((double) j, (double) i), data);
 46         er_seed_call2(im, i - 1, j, val, func, data);
 47         er_seed_call2(im, i + 1, j, val, func, data);
 48         er_seed_call2(im, i, j - 1, val, func, data);
 49         er_seed_call2(im, i, j + 1, val, func, data);
 50     }
 51 }
 52 
 53 void    er_seed_uchar(Imrect * im, int i, int j, unsigned char val)
 54 {
 55     if (j < im->region->lx || j >= im->region->ux)
 56         return;
 57     if (i < im->region->ly || i >= im->region->uy)
 58         return;
 59     if (IM_UCHAR(im, i, j) == 0)
 60     {
 61         IM_UCHAR(im, i, j) = val;
 62         er_seed_uchar(im, i - 1, j, val);
 63         er_seed_uchar(im, i + 1, j, val);
 64         er_seed_uchar(im, i, j - 1, val);
 65         er_seed_uchar(im, i, j + 1, val);
 66     }
 67 }
 68 
 69 Imregion *es_roi(Tstring * es)
 70 {
 71     List *ptr;
 72     int     xmin = MAXINT, xmax = -MAXINT, x;
 73     int     ymin = MAXINT, ymax = -MAXINT, y;
 74 
 75     for (ptr = es->start;; ptr = ptr->next)
 76     {
 77         Vec2    v = {Vec2_id};
 78 
 79         DD_GET_POS2(ptr, v);
 80         x = floor(vec2_x(v));
 81         y = floor(vec2_y(v));
 82         xmin = MIN(x, xmin);
 83         xmax = MAX(x, xmax);
 84         ymin = MIN(y, ymin);
 85         ymax = MAX(y, ymax);
 86         if (ptr == es->end)
 87             break;
 88     }
 89 
 90     /** one pixel boundary round curve **/
 91     return (roi_alloc(xmin - 1, ymin - 1, xmax + 2, ymax + 2));
 92 }
 93 
 94 Imrect *es_region(Tstring * es)
 95 {
 96     List *ptr;
 97     Imrect *rr;
 98     int     lx, ly, ux, uy;
 99     Imregion *roi = es_roi(es);
100 
101     lx = roi->lx;
102     ux = roi->ux;
103     ly = roi->ly;
104     uy = roi->uy;
105 
106     /** initialise region to CVR_INSIDE ( = 0) **/
107     rr = im_alloc(uy, ux, roi, uchar_v);
108 
109     /** mark all string points as CVR_BOUNDARY **/
110     for (ptr = es->start;; ptr = ptr->next)
111     {
112         Vec2    v = {Vec2_id};
113         int     x, y;
114 
115         DD_GET_POS2(ptr, v);
116         x = floor(vec2_x(v));
117         y = floor(vec2_y(v));
118         IM_UCHAR(rr, y, x) = CVR_BOUNDARY;
119         if (ptr == es->end)
120             break;
121     }
122 
123     /** Mark exterior points as CVR_OUTSIDE **/
124     er_seed_uchar(rr, ly, lx, CVR_OUTSIDE);
125 
126     return (rr);
127 }
128 
129 
130 /****
131 static int num;
132 static double   sum, sum2;
133   static void     stats_add(xnew)
134 double          xnew;
135 {
136     num++;
137     sum += xnew;
138     sum2 += xnew * xnew;
139 }
140 
141 static void     (State *)ts_get(n, mean, sigma)
142 int            *n;
143 double         *mean, *sigma;
144 {
145     *n = num;
146     *mean = sum / num;
147     *sigma = sqrt(sum2 / num - (*mean) * (*mean));
148 }
149 ****/
150 
151 #define IN_REGION(y, x) ((y)>=ly && (x)>=lx && (y)<uy && (x)<ux)
152 
153 static Epos *epos_make(int i, int j, double g)
154 {
155     Epos   *e = ts_ralloc(Epos);
156 
157     e->i = i;
158     e->j = j;
159     e->g = g;
160     return (e);
161 }
162 
163 void    region_seed(Imrect * rr, Imrect * im, int i, int j, double rate, double error, int setval, void (*setfunc) ( /* ??? */ ), void *data)
164 {
165     double  g;
166     List   *start;
167     List   *end;
168     static unsigned int block_label = 0;
169     int     lx, ly, ux, uy;
170 
171     if (im == NULL || rr == NULL)
172         return;
173 
174     lx = im->region->lx;
175     ly = im->region->ly;
176     ux = im->region->ux;
177     uy = im->region->uy;
178 
179     if (!IN_REGION(i, j))
180         return;
181 
182     ralloc_free_blocked(block_label);
183     block_label = ralloc_new_blocked(1024);
184 
185     /** start iteration at seed point **/
186     IM_PIX_GET(im, i, j, g);
187     start = end = ref_addtostart((List *) NULL, (void *) epos_make(i, j, g), IPOS);
188 
189     while (start != NULL)
190     {
191         int     status;
192         Epos   *pos = (Epos *) start->to;
193 
194         i = pos->i;
195         j = pos->j;
196         IM_PIX_GET(rr, i, j, status);
197 
198         if (status == 0)
199         {
200             /** if pixel empty check whether to fill it **/
201             IM_PIX_GET(im, i, j, g);
202             if (fabs(g - pos->g) > error)
203                 goto remove;
204 
205             g = (1.0 - rate) * pos->g + rate * g;
206 
207             IM_PIX_SET(rr, i, j, setval);
208             setfunc(pos, (Ipos *) data);
209 
210             if (IN_REGION(i + 1, j))
211             {
212                 IM_PIX_GET(rr, i + 1, j, status);
213                 if (status == 0)
214                     end = ref_addtoend(end, (void *) epos_make(i + 1, j, g), IPOS);
215             }
216             if (IN_REGION(i, j - 1))
217             {
218                 IM_PIX_GET(rr, i, j - 1, status);
219                 if (status == 0)
220                     end = ref_addtoend(end, (void *) epos_make(i, j - 1, g), IPOS);
221             }
222             if (IN_REGION(i - 1, j))
223             {
224                 IM_PIX_GET(rr, i - 1, j, status);
225                 if (status == 0)
226                     end = ref_addtoend(end, (void *) epos_make(i - 1, j, g), IPOS);
227             }
228             if (IN_REGION(i, j + 1))
229             {
230                 IM_PIX_GET(rr, i, j + 1, status);
231                 if (status == 0)
232                     end = ref_addtoend(end, (void *) epos_make(i, j + 1, g), IPOS);
233             }
234         }
235 remove:
236         start = link_rm(start, rfree);
237     }
238 
239     (void) ralloc_end_blocked();
240 }
241 
242 #undef IN_REGION
243 

~ [ 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.