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

Linux Cross Reference
Tina4/src/pgh/pgh_match.c

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

  1 #include <stdio.h>
  2 #include <sys/param.h>
  3 #include <string.h>
  4 #include <tina/sys.h>
  5 #include <tina/sysfuncs.h>
  6 #include <tina/math.h>
  7 #include <tina/mathfuncs.h>
  8 #include <tina/vision.h>
  9 #include <tina/visionfuncs.h>
 10 #include <tina/file.h>
 11 #include <tina/filefuncs.h>
 12 #include <tina/file_gen.h>
 13 #include <tina/draw.h>
 14 #include <tina/drawfuncs.h>
 15 #include <tina/tv.h>
 16 #include <tina/tvfuncs.h>
 17 #include <tina/toolsfuncs.h>
 18 #include <tina/tw_Xfuncs.h>
 19 #include <pgh/pgh_defs.h>
 20 #include <pgh/pgh_types.h>
 21 #include <pgh/pgh_funcs.h>
 22 
 23 /* ---------- */
 24 
 25 void match_scene_and_models(List *mono_geometry)
 26 {
 27     Pairs_hist_def *hist_def;
 28     List           *scene_pairs_list;
 29     List           *model_pairs_list;
 30         Pairs_scale_def *scale_def;
 31    
 32        /* (1) Generate scene histograms.
 33           (2) Match the scene histograms with the model 
 34               histograms. */
 35 
 36     hist_def = pairs_hist_def_get();
 37     scene_pairs_list = pairs_scene_pairs_list_get();
 38     model_pairs_list = pairs_model_pairs_list_get();
 39         scale_def = pairs_scale_def_get();
 40 
 41     init_pairs_entry(hist_def->pairs_type,
 42                      hist_def->dbin_max, 
 43                      hist_def->dbin_size,
 44                      hist_def->num_abin,
 45                      hist_def->angle_sigma,
 46                      hist_def->dist_ramp);
 47 
 48     scene_pairs_list = 
 49             geom_to_pairwise_list(mono_geometry,scene_pairs_list, hist_def->min_length, hist_def->dbin_max);
 50 
 51     match_scene_list_to_model_list(scene_pairs_list, model_pairs_list, scale_def);
 52 
 53     pairs_scene_pairs_list_set(scene_pairs_list);
 54 }
 55 
 56 /* ---------- */
 57 
 58 void match_scene_list_to_model_list(List *scene_pairs_list, List *model_pairs_list, Pairs_scale_def *scale_def)
 59 {
 60     List   *ptr;
 61     Imrect *scene_hist;
 62         int             count=0;
 63         double  pcnt_done=0.0;
 64         double  pcnt_each;
 65 
 66     if ((scene_pairs_list==NULL)||(model_pairs_list==NULL)) return;
 67 
 68     for(ptr=scene_pairs_list;ptr!=NULL;ptr=ptr->next)
 69                 count++;
 70 
 71         pcnt_each = 100.0/(double)count;
 72 
 73         printf("\nMatching: 0.00%%\r");
 74 
 75     for(ptr=scene_pairs_list;ptr!=NULL;ptr=ptr->next)
 76     {
 77                 printf("Matching: %.2f%%\r", pcnt_done);
 78         scene_hist = ptr->to;
 79         match_scene_pairs_to_model_list(scene_hist, model_pairs_list,
 80                                                                                 scale_def);
 81                 pcnt_done+=pcnt_each;
 82     }
 83 
 84         printf("Matching: 100.00%%  \n");
 85 }
 86 
 87 /* ---------- */
 88 
 89 void match_scene_pairs_to_model_list(Imrect *scene_pairs_hist, List *model_list,
 90                                                                         Pairs_scale_def *scale_def)
 91 {
 92     List     *ptr;
 93     double    dp;
 94     Imrect   *model_hist; 
 95     Hist_ref *ref, *model_ref;   
 96     List     *match_list=NULL;
 97     Match_ref *new_match_ref, *match_ref;
 98     int        scene_type,model_type;
 99     double     dp_max = 0.0;
100     double     root_scene_l0;
101     Match_cut_def *match_cut_def;
102     double     sort_func();
103     int        cut_matches_test();
104 
105 /* Access the scene histograms hist ref */
106 
107     ref = hist_ref_get(scene_pairs_hist);
108 
109     if (ref==NULL)
110     {
111         format("Error: Cannot find Hist_ref on props list to add matches.\n");
112         return;
113     }
114 
115 /* Free any previous matches that have been made */
116 
117     if (ref->matches!=NULL)
118     {
119         list_rm(ref->matches, rfree);
120         ref->matches=NULL;
121     }
122 
123 /* For each model histogram calc the dp and add the result to the match list */
124 
125     scene_type = ref->type;
126     root_scene_l0 = sqrt(ref->l0);
127     match_cut_def = pairs_match_cut_def_get();
128 
129     for(ptr=model_list;ptr!=NULL;ptr=ptr->next)
130     {
131         model_hist = ptr->to;
132         if (!(model_ref = hist_ref_get(model_hist)))            
133                         continue;
134         model_type = model_ref->type;
135         if (model_type!=scene_type)
136                          continue;
137         
138         dp = dot_product(scene_pairs_hist, model_hist, scene_type, model_type);
139         if (dp> dp_max) dp_max = dp;
140  
141         if (cut_matches_test(model_ref,dp_max,root_scene_l0,match_cut_def)
142                                                                                                                                         ==TRUE)
143         {
144             new_match_ref = ralloc(sizeof(Match_ref));
145             new_match_ref->hist=model_hist;
146             new_match_ref->dp=dp;
147                 new_match_ref->scale_factor = model_ref->scale_factor;
148             new_match_ref->orientation=1;
149 
150             match_list = 
151                   ref_addtostart(match_list, new_match_ref, MATCH_REF_TYPE);
152         }
153 
154         if (scene_type==DIRECTED)
155         {
156             dp = dot_product2(scene_pairs_hist, model_hist, scene_type,
157                                                                                                                                          model_type);
158             if (dp> dp_max) dp_max = dp;
159 
160             if (cut_matches_test(model_ref,dp_max,root_scene_l0,match_cut_def)
161                                                                                                                                                 ==TRUE)
162             {
163                                 new_match_ref = ralloc(sizeof(Match_ref));
164                                 new_match_ref->hist=model_hist;
165                                 new_match_ref->dp=dp;
166                                 new_match_ref->scale_factor = model_ref->scale_factor;
167                                 new_match_ref->orientation=-1;
168 
169                 match_list =
170                   ref_addtostart(match_list, new_match_ref, MATCH_REF_TYPE);
171             }
172         }
173     }/*endfor(ptr)*/
174 
175 /* Sort the list into descending order */
176 
177   match_list = sort_list(match_list, sort_func, 0);
178   match_list = list_reverse(match_list);
179 
180 /* Add the list to the scene histograms match list */
181 
182   ref->matches = match_list;
183 
184 /* TEMPORARY !!!!!!!!! */
185         match_ref = match_list->to;
186         /*printf("%f\n", match_ref->dp); */  
187 
188 /* Interpolate across the scale space if requested */
189 
190 /* Prune off the unnecessary list elements  NAT 3/3/94 */
191   cut_matches_list(ref, match_cut_def);
192 
193  }
194 
195 /* ---------- */
196 
197 double sort_func(void *ptr, int type, void *data)
198 {
199   Match_ref *match_ref;
200 
201   match_ref = (Match_ref *)ptr;
202 
203   return (match_ref->dp);
204 }
205 
206 /* ---------- */
207 
208 List *copy_and_cut_matches_list(Hist_ref *scene_hist_ref, Match_cut_def *match_cut_def)
209 {
210         /* If 'Best N' has been selected then copy the first
211            N matches providing the other contraints have been 
212            met. Otherwise copy all of the matches again providing 
213            the other contraints have been met.  */
214 
215   List      *match_list;
216   List      *ptr;
217   Match_ref *match_ref;
218   Match_ref *new_match_ref;
219   List      *new_match_list = NULL;
220   int        test;
221   int        count;
222   double     root_scene_l0;
223   Imrect    *model_hist;
224   Hist_ref  *model_hist_ref;
225 
226   if (scene_hist_ref==NULL) return(NULL);
227   match_list = scene_hist_ref->matches;
228   if (match_list==NULL) return(NULL);
229   root_scene_l0 = sqrt(scene_hist_ref->l0);
230 
231   for(ptr=match_list, count = 0;ptr!=NULL;ptr=ptr->next, count++)
232   {
233     if ( (match_ref = ptr->to)==NULL) continue;
234     model_hist = match_ref->hist;
235     model_hist_ref = prop_get(model_hist->props, HIST_REF_TYPE);
236 
237     /* Conditions for match to be excepted */
238     test = cut_matches_test(model_hist_ref,match_ref->dp,root_scene_l0,match_cut_def);
239 
240     if ( ((match_cut_def->match_cut_mode & 1)==1) && 
241                     (count >= match_cut_def->best_N_matches) ) test = FALSE;
242 
243     if (test==TRUE)
244     {
245 
246       new_match_ref = ralloc(sizeof(Match_ref));
247       new_match_ref->hist = match_ref->hist;
248       new_match_ref->dp = match_ref->dp;
249           new_match_ref->scale_factor = match_ref->scale_factor;
250       new_match_ref->orientation = match_ref->orientation;
251 
252       new_match_list =
253                 ref_addtostart(new_match_list, new_match_ref, MATCH_REF_TYPE);
254 
255     }
256                                  
257   }/*endfor(ptr)*/
258 
259   return (new_match_list);
260 }
261 
262 /* ---------- */
263 
264 void cut_matches_list(Hist_ref *scene_hist_ref, Match_cut_def *match_cut_def)
265 {
266         /* If 'Best N' has been selected then copy the first
267            N matches providing the other contraints have been 
268            met. Otherwise copy all of the matches again providing 
269            the other contraints have been met.  */
270 
271   List          *match_list;
272   List      *ptr;
273   List      *ptr_last=NULL;
274   Match_ref *match_ref;
275   int        test;
276   int        count;
277   double     root_scene_l0;
278   Imrect    *model_hist;
279   Hist_ref  *model_hist_ref;
280 
281   if (scene_hist_ref==NULL) return;
282   match_list = scene_hist_ref->matches;
283   if (match_list==NULL) return;
284   root_scene_l0 = sqrt(scene_hist_ref->l0);
285 
286   for(ptr=match_list, count = 0;ptr!=NULL; count++)
287   {
288     if ( (match_ref = ptr->to)==NULL) continue;
289     model_hist = match_ref->hist;
290     model_hist_ref = prop_get(model_hist->props, HIST_REF_TYPE);
291 
292     /* Conditions for match to be excepted */
293     test = cut_matches_test(model_hist_ref,match_ref->dp,root_scene_l0,match_cut_def);
294 
295     if ( ((match_cut_def->match_cut_mode & 1)==1) && 
296                     (count >= match_cut_def->best_N_matches) ) test = FALSE;
297 
298 
299     ptr_last = ptr;
300     ptr = ptr->next;
301     if (test==FALSE) match_list = list_rm_el(match_list,ptr_last,rfree);
302   }/*endfor(ptr)*/
303 
304   scene_hist_ref->matches = match_list;
305 }
306 
307 /* ---------- */
308 
309 
310 int cut_matches_test(Hist_ref *model_hist_ref, double dp, double root_scene_l0, Match_cut_def *match_cut_def)
311 {
312     int        test = TRUE;
313     double     delta_max;
314     double     root_model_l;
315 
316 
317     if ((match_cut_def->match_cut_mode & 2)==2)
318     {
319       root_model_l = sqrt(model_hist_ref->l);
320 
321       if ( (dp/root_scene_l0) >
322                    (root_model_l+root_model_l*
323                        (double)match_cut_def->dp_upper_thres*0.01) )
324         test = FALSE;
325 
326       if ( (dp/root_scene_l0) <
327                    (root_model_l-root_model_l*
328                        (double)match_cut_def->dp_lower_thres*0.01) )
329         test = FALSE;
330     }
331 
332     if ( (match_cut_def->match_cut_mode & 4)==4) 
333     {
334        delta_max = dp-dp*(double) match_cut_def->delta_max_dp*0.01;
335        if (dp<delta_max) test = FALSE;
336     }
337 
338     return (test);
339 }
340 
341 /* ---------- */
342 
343 

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