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

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

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