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

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

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