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

Linux Cross Reference
Tina5/tina-libs/tina/vision/visPgh_model.c

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

  1 /**********
  2  *
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc.,
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  **********
 20  *
 21  * Program :    TINA
 22  * File    :  $Source: /home/tina/cvs/tina-libs/tina/vision/visPgh_model.c,v $
 23  * Date    :  $Date: 2006/04/12 21:42:35 $
 24  * Version :  $Revision: 1.6 $
 25  * CVS Id  :  $Id: visPgh_model.c,v 1.6 2006/04/12 21:42:35 paul Exp $
 26  *
 27  * Author  : A.Ashbrook, NAT.
 28  *
 29  */
 30 /**
 31  * @file Routines for constructing the structures used for object matching using PGH.
 32  * @brief Data can be read from file or provided as a list of geometry.
 33  *
 34  *
 35 */
 36 
 37 #define MAXPATHLEN 256
 38 
 39 #include "visPgh_model.h"
 40 
 41 #include <string.h>
 42 #include <math.h>
 43 #include <stdio.h>
 44 
 45 #include <tina/math/mathGeom_geom2.h>
 46 #include <tina/file/fileUtil_name.h>
 47 #include <tina/file/file_UtilPro.h>
 48 #include <tina/file/file_GeomPro.h>
 49 #include <tina/image/img_GenPro.h>
 50 #include <tina/geometry/geomGen_free.h>
 51 #include <tina/geometry/geomDef.h>
 52 #include <tina/geometry/geom_LinePro.h>
 53 #include <tina/geometry/geomLine_poly_trans.h>
 54 #include <tina/vision/visPgh_var.h>
 55 #include <tina/vision/visPgh_hist.h>
 56 
 57 
 58 /* ---------- Functions ---------- */
 59 
 60 void add_model_poly_set(char *base_name, char *directory_name)
 61 {
 62         char model_set_path[MAXPATHLEN];
 63         char model_dir[MAXPATHLEN];
 64         FILE *ifp;
 65         char buffer[MAXPATHLEN];
 66 
 67         string_append(model_set_path, directory_name, "/", base_name, ".msd", NULL);
 68 
 69         ifp = fopen(model_set_path, "r");
 70 
 71         if (ifp==NULL)
 72         {
 73                 printf("Error: Cannot find '%s'.\n", model_set_path);
 74                 return;
 75         }
 76 
 77 
 78         while(fscanf(ifp, "%s", buffer) != EOF)
 79     {
 80                 if (strcmp(buffer, "SETDIR")==0)
 81                         fscanf(ifp, "%s", model_dir);
 82                 else
 83                 {
 84                         printf("Adding Model: %s\n", buffer);
 85                         add_model_poly(buffer, model_dir);
 86                 }
 87         }
 88 
 89         fclose(ifp);
 90 }
 91 
 92 /* ---------- */
 93 
 94 void add_model_poly(char *base_name, char *dir_name)
 95 {
 96 
 97         Pairs_scale_def   *scale_def;
 98         Model_poly_header *polyh;
 99 
100 
101         scale_def = pairs_scale_def_get();
102 
103         polyh = pairs_load_model_poly(base_name, dir_name);
104 
105         if ( (scale_def->scale_min != 1.0) || 
106                  (scale_def->scale_max != 1.0 ))
107                 add_model_scaled(polyh);
108         else
109                 add_model_unscaled(polyh);
110 }
111 
112 /* ---------- */
113 void add_model_unscaled(Model_poly_header *polyh)
114 {
115     Pairs_hist_def    *hist_def;
116     List              *geom, *ptr;
117     Line2             *lptr;
118     Imrect            *im;
119     List              *model_poly_list;
120     List              *model_pairs_list;
121     int                pairs_model_present();
122 
123     if (polyh==NULL) return;
124 
125     hist_def = pairs_hist_def_get();
126     init_pairs_entry(hist_def->pairs_type,
127                      hist_def->dbin_max,
128                      hist_def->dbin_size,
129                      hist_def->num_abin,
130                      hist_def->angle_sigma,
131                      hist_def->dist_ramp);
132 
133     model_poly_list = pairs_model_poly_list_get();
134     model_pairs_list = pairs_model_pairs_list_get();
135 
136     geom = polyh->model_poly_data;
137 
138 /* Generate a pairwise histogram for each of the new model lines and then
139    add these to the model_pairwise_list after normalizing. */
140 
141     for(ptr=geom;ptr!=NULL;ptr=ptr->next)
142     {
143         lptr = ptr->to;
144         if (lptr->length>hist_def->min_length)
145         {
146            im = build_normalized_pairwise(lptr, polyh, geom, hist_def->dbin_max);
147 
148            model_pairs_list =
149              list_addtoend(model_pairs_list, link_alloc(im, IMRECT));
150         }
151     }
152 
153     pairs_model_pairs_list_set(model_pairs_list);
154 }
155 
156 
157 /* ---------- */
158 
159 /* Adds sufficient numbers of model histograms to represent the
160    specified range of scale and precision */
161 
162 void add_model_scaled(Model_poly_header *polyh)
163 {
164     Pairs_scale_def *scale_def;
165     Pairs_hist_def    *hist_def;
166     double   scale_min, scale_max;
167     List     *geom, *ptr;
168     double   marker, next_guess, upper_bound, lower_bound, last_cross;
169     double   dist_to_thres;
170     int      count;
171     int      mode;
172     Line2    *lptr;
173     Imrect   *marker_hist, *guess_hist;
174     List     *model_pairs_list;
175     Pgh_serr *serr;
176 
177    if (polyh==NULL) return;
178 
179    hist_def = pairs_hist_def_get();
180    init_pairs_entry(hist_def->pairs_type,
181                     hist_def->dbin_max,
182                     hist_def->dbin_size,
183                     hist_def->num_abin,
184                     hist_def->angle_sigma,
185                     hist_def->dist_ramp);
186 
187    scale_def = pairs_scale_def_get();
188    scale_min = scale_def->scale_min;
189    scale_max = scale_def->scale_max;
190 
191    model_pairs_list = pairs_model_pairs_list_get();
192 
193    geom = polyh->model_poly_data;
194 
195    for(ptr=geom;ptr!=NULL;ptr=ptr->next)
196    {
197       lptr = ptr->to;
198 
199       if (lptr->length>hist_def->min_length)
200       {
201 
202          mode = STORE;
203          marker = scale_min;
204          marker_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, marker);
205          upper_bound = scale_max;
206          lower_bound = scale_min;
207          count = 0;
208          last_cross = scale_min;
209 
210          while(last_cross<scale_max)
211          {
212 
213 /* Find the value of scale above marker which lies on the theshold */
214             next_guess = upper_bound;
215             guess_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, next_guess);
216 
217             dist_to_thres = dot_product(marker_hist, guess_hist,
218             hist_ref_get(marker_hist)->type, hist_ref_get(guess_hist)->type)
219                           - scale_def->match_score_thres;
220 
221             while(fabs(dist_to_thres) > scale_def->precision)
222             {
223                if (dist_to_thres>0.0) /* Above threshold */
224                {
225                   if (next_guess==upper_bound)
226                   {
227                      upper_bound*=2.0;
228                      next_guess = upper_bound;
229                   }
230                   else
231                   {
232                      lower_bound = next_guess;
233                      next_guess = next_guess+(upper_bound-next_guess)/2.0;
234                   }
235                }
236                else
237                {
238                   upper_bound = next_guess;
239                   next_guess = lower_bound+(next_guess-lower_bound)/2.0;
240                }
241 
242                im_free(guess_hist);
243                guess_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, next_guess);
244 
245                dist_to_thres = dot_product(marker_hist, guess_hist,
246                hist_ref_get(marker_hist)->type, hist_ref_get(guess_hist)->type)
247                                 - scale_def->match_score_thres;
248             }
249 
250 
251             if (mode==STORE)
252             {
253    /* Add scale error data to the stored hist */
254                serr = ralloc(sizeof(Pgh_serr));
255                serr->smin = marker;
256                serr->smax = next_guess; /* For now at least */
257                guess_hist->props = proplist_add(guess_hist->props, serr,
258                                        PGH_SERR_TYPE, rfree);
259                im_free(marker_hist);
260                marker = next_guess;
261                marker_hist = guess_hist;
262                model_pairs_list = ref_addtostart(model_pairs_list, marker_hist, IMRECT);
263                count+=1;
264                mode = DONT_STORE;
265             }
266             else
267             {
268                serr = prop_get(marker_hist->props, PGH_SERR_TYPE);
269                serr->smax = next_guess;
270                marker = next_guess;
271                marker_hist = guess_hist;
272                mode = STORE;
273                last_cross = marker;
274             }
275          }
276 /*printf("Stored hists = %d\n", count);*/
277       }
278    }
279    pairs_model_pairs_list_set(model_pairs_list);
280 }
281 
282 
283 /* ---------- */
284 
285 void free_model_list(void)
286 {
287     List *model_poly_list;
288     List *model_pairs_list;
289 
290     model_poly_list = pairs_model_poly_list_get();
291     model_pairs_list = pairs_model_pairs_list_get();
292 
293     list_rm(model_poly_list, polyh_free);
294     model_poly_list = NULL;
295 
296     list_rm(model_pairs_list, im_free);
297     model_pairs_list = NULL;
298 
299     pairs_model_poly_list_set(model_poly_list);
300     pairs_model_pairs_list_set(model_pairs_list);
301 }
302 
303 /* ---------- */
304 
305 Model_poly_header *polyh_alloc(List *geom, char *basename)
306 {
307      Model_poly_header *polyh;
308 
309     polyh = ralloc(sizeof(Model_poly_header));
310     strcpy(polyh->name, basename);
311     polyh->ref = 0; /* this may be used later */
312     polyh->model_poly_data = geom;
313     return(polyh);
314 }
315 
316 /* ---------- */
317 
318 void polyh_free(Model_poly_header *polyh)
319 {
320     if (polyh==NULL) 
321                 return;
322 
323     if (polyh->model_poly_data!=NULL);
324         reclist_list_free(polyh->model_poly_data, geom_free, LINE2, NULL);
325 
326     rfree(polyh);
327 }
328 
329 /* ---------- */
330 
331 /* descends the model_poly_list to see if the requested model has already 
332    been loaded. */
333 
334 int pairs_model_present(char *name)
335 {
336     Model_poly_header   *polyh;
337         List                            *ptr;
338     List                        *model_poly_list;
339 
340 
341     model_poly_list = pairs_model_poly_list_get();
342 
343 
344     strip_spaces(name);
345 
346     for(ptr=model_poly_list;ptr!=NULL;ptr=ptr->next)
347     {
348         polyh = ptr->to;
349         if (strcmp(name,polyh->name) == 0) 
350           return TRUE;
351     }
352         return FALSE;
353 }
354 
355 /*------------------------------------------------*/
356 /* ---------- */
357 
358 
359 /* place the specified geometry on the Model_poly_header */
360 
361 Model_poly_header *pairs_store_model_poly(char *base_name, List *geom)
362 {
363     List                *ptr, *model_poly_list;
364     Line2               *lptr;
365     Model_poly_header   *polyh;
366     Model_poly_header   *polyh_alloc();
367 
368     model_poly_list = pairs_model_poly_list_get();
369 
370     if (geom==NULL)
371         return NULL;
372 
373         /* set the p-field of each line to the lines mid-point */
374     for(ptr=geom; ptr!=NULL;ptr=ptr->next)
375     {
376         lptr = ptr->to;
377         lptr->p = vec2_midpoint(lptr->p1, lptr->p2);
378     }
379 
380     polyh = polyh_alloc(geom,base_name);
381     model_poly_list = 
382        list_addtoend(model_poly_list, link_alloc(polyh, MODEL_POLY_TYPE));
383 
384     pairs_model_poly_list_set(model_poly_list);
385 
386     for(ptr=geom; ptr!=NULL;ptr=ptr->next)
387     {
388          lptr = ptr->to;
389          lptr->p = vec2_midpoint(lptr->p1, lptr->p2);
390     }
391         /* Move the model data axis to the model's centre */
392     polyh->centroid = centre_poly_axis(geom);
393 
394     return polyh;
395 }
396 
397 
398 /*------------------------------------------------------*/
399 
400 /* ---------- */
401 
402 
403 /* load the poly data and add this to a Model_poly_header which is added
404    to the model_poly_list */
405 
406 
407 Model_poly_header *pairs_load_model_poly(char *base_name, char *dir_name)
408 {
409     char                                pathname[MAXPATHLEN];
410     List                                *geom, *model_poly_list;
411     Model_poly_header   *polyh;
412 
413 
414     model_poly_list = pairs_model_poly_list_get();
415 
416     printf("%s\n",dir_name);
417     printf("%s\n",base_name);
418 
419     strip_spaces(base_name);
420     strip_spaces(dir_name);
421     string_append(pathname, dir_name, "/", base_name, NULL);
422     strcat(pathname, ".poly");
423 
424         printf("model path: %s\n",pathname);
425 
426     geom = geom2_read(pathname);
427 
428         /*save the model for display*/
429     pairs_model_geom_set(geom2_read(pathname));
430 
431     polyh = pairs_store_model_poly(base_name, geom);
432 
433     return polyh;
434 }
435 
436 /* ---------- */
437 
438 
439 
440 

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