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

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

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