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

Linux Cross Reference
Tina5/tina-libs/tina/vision/visPghTest_loc.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/visPghTest_loc.c,v $
 23  * Date    :  $Date: 2005/01/23 19:10:21 $
 24  * Version :  $Revision: 1.5 $
 25  * CVS Id  :  $Id: visPghTest_loc.c,v 1.5 2005/01/23 19:10:21 paul Exp $
 26  *
 27  */
 28 /**
 29  * @file Routines associated with testing hough based object location routines.
 30  * @brief Residual distributions are plotted for distances from the centriod of
 31  *        the located object. 
 32  */
 33 
 34 #include "visPghTest_loc.h"
 35 
 36 #include <math.h>
 37 #include <string.h>
 38 #include <tina/sys/sysDef.h>
 39 #include <tina/sys/sysPro.h>
 40 #include <tina/math/mathDef.h>
 41 #include <tina/math/mathPro.h>
 42 #include <tina/image/imgDef.h>
 43 #include <tina/image/imgPro.h>
 44 #include <tina/geometry/geomDef.h>
 45 #include <tina/geometry/geomPro.h>
 46 #include <tina/vision/vis_ShapePro.h>
 47 #include <tina/vision/visPgh_defs.h>
 48 #include <tina/vision/visPgh_types.h>
 49 #include <tina/vision/visPgh_model.h>
 50 #include <tina/vision/visPgh_match.h>
 51 #include <tina/vision/visPgh_locate.h>
 52 #include <tina/vision/visPgh_hist.h>
 53 #include <tina/vision/visPgh_HT.h>
 54 #include <tina/vision/visPgh_var.h>
 55 #include <tina/vision/visPgh_analysis2.h>
 56 #include <tina/vision/visPgh_HTcovar.h>
 57 #include <tina/vision/visPgh_misc.h>
 58 
 59 
 60 static double chi_squared_stat(Vec2 *int_pt, double weight, Line2 *line1,
 61                           Line2 *line2, Point2* location,double error_scale,
 62                           double eig_lim)
 63 {
 64      Point2          *int_point;
 65      Vec2             chi_location;
 66      double           temp, a, b, c, d, det;
 67      void             get_covariance();
 68      double           chi_squared_test();
 69      double           inva, invb, invc, invd;
 70      double           eig1, eig2;
 71      double theta;
 72 
 73 /* This function will blur using the 2D chi squared from the error propagation */
 74 
 75 /* Remove lines that are to short to merit consideration */
 76 
 77     if(line1->length < 4.0 || line2->length < 4.0)
 78          return(0.0);
 79 
 80     int_point = point2_make(*int_pt, double_v);
 81 
 82 /* get inverse!!! covariance NAT */
 83     get_covariance(line1, line2, &a, &b, &c, &d);
 84     det = a*d - c*b;
 85     inva = d/det;
 86     invb = -c/det;
 87     invc = -b/det;
 88     invd = a/det;
 89 
 90     conic_eigen(inva, invb, invd, &theta, &eig1, &eig2);
 91     if(eig1 > eig_lim*eig_lim || eig2 > eig_lim*eig_lim)
 92           return(0.0);
 93 
 94     chi_location = location->p;
 95     temp = chi_squared_test(&chi_location, int_point, &a, &b, &c, &d)/error_scale;
 96     return(temp);
 97 }
 98 
 99 double *test_locate_models(char *model_name,Point2 *location, double range)
100 {
101     List              *model_ptr;
102     Model_poly_header *polyh;
103     double            *chi_sq_test=NULL;
104     double            *locate_test_points();
105     Imrect            *location_hough_space;
106     List              *scene_pairs_list;
107     List              *matched_models_list;
108     Line2             *matched_line;
109     List              *model_poly_list;
110     Pairs_hough_def   *hough_def;
111     Match_cut_def     *match_cut_def;
112 
113 
114         /* (1) For the chosen model in the database, construct a Hough space,
115                and vote for the position using the matched scene histograms.
116            (2) Search for significant peaks which
117                hypothesize the presence of a model in the scene.ref
118            (3) For the chosen peak peak determine the orientation of the model
119                at that location by voting for the angle between each
120                scene line and the model line it has matched to.
121         */
122 
123     scene_pairs_list = pairs_scene_pairs_list_get();
124     matched_models_list = pairs_matched_models_list_get();
125     matched_line = pairs_matched_line_get();
126     model_poly_list = pairs_model_poly_list_get();
127     hough_def = pairs_hough_def_get();
128     match_cut_def = pairs_match_cut_def_get();
129 
130     if (scene_pairs_list==NULL) return(NULL);
131 
132 /* Free the matched_models list */
133 
134     if(matched_models_list!=NULL) list_rm(matched_models_list, geom_free);
135     matched_models_list=NULL;
136     pairs_matched_models_list_set(matched_models_list);
137 
138     for(model_ptr=model_poly_list;model_ptr!=NULL;model_ptr=model_ptr->next)
139     {
140         polyh = model_ptr->to;
141         if (strcmp(model_name, polyh->name)==0) break;
142     }
143     if (model_ptr==NULL) return(NULL);
144 
145     location_hough_space = hough2_alloc(hough_def->loc_hough2_min_x,
146                                         hough_def->loc_hough2_min_y,
147                                         hough_def->loc_hough2_max_x,
148                                         hough_def->loc_hough2_max_y,
149                                         hough_def->loc_hough2_binsize_x,
150                                         hough_def->loc_hough2_binsize_y,
151                                         double_v);
152 
153     chi_sq_test = locate_test_points(polyh,location_hough_space,
154                   scene_pairs_list, match_cut_def,location,range);
155     return(chi_sq_test);
156 }
157 
158 
159 double *locate_test_points(Model_poly_header *current_model, 
160                            Imrect *location_hough_space,
161                            List *scene_pairs_list,
162                            Match_cut_def *match_cut_def,
163                            Point2 *location,
164                            double range)
165 {
166         /* (1) For each pair of scene histograms which have been
167                matched to lines from this model place votes in
168                the hough space at the respective location - ie,  4
169                points which are the intersection of parallel lines
170                to the scene lines. */
171 
172     Line2      *ref;
173     List       *scene_ptr;
174     List       *match_ptr;
175     Vec2        model_perp_vec;
176     Vec2        scene_perp_vec;
177     Mat2        rotation_mat;
178     Line2      *scene_line;
179     Line2      *match_line;
180     Line2       line;
181     Line2      *temp_line;
182     Line2      *new_line;
183     Imrect     *scene_hist;
184     Imrect     *match_hist;
185     double      rotation;
186     Hist_ref   *scene_hist_ref;
187     Hist_ref   *match_hist_ref;
188     Transform2  trans;
189     Match_ref  *match_ref;
190     List       *cut_match_list;
191     List       *copy_and_cut_matches_list();
192     List       *list_of_parallel_line2s = NULL;
193     int         orientation;
194     double     *chi_sq_vec=NULL;
195     double     *hough2_test_points();
196 
197 /* Get the data from the scene and the match list */
198 
199   for(scene_ptr=scene_pairs_list;scene_ptr!=NULL;scene_ptr=scene_ptr->next)
200     {
201         scene_hist = scene_ptr->to;
202         scene_hist_ref = prop_get(scene_hist->props, HIST_REF_TYPE);
203         scene_line = scene_hist_ref->line_seg;
204 
205         cut_match_list = copy_and_cut_matches_list(scene_hist_ref, match_cut_def);
206 
207         for(match_ptr=cut_match_list;match_ptr!=NULL;
208                                       match_ptr=match_ptr->next)
209         {
210             match_ref = match_ptr->to;
211             match_hist = match_ref->hist;
212             match_hist_ref = prop_get(match_hist->props, HIST_REF_TYPE);
213             if (match_hist_ref->type == DIRECTED)
214                orientation = match_ref->orientation;
215             else
216                orientation = 0;
217 
218             match_line = match_hist_ref->line_seg;
219 
220             if (match_hist_ref->model==current_model)
221             {
222 
223 /* Compute the position of the parallel lines then store them in a linked list */
224 
225                 model_perp_vec = vec2_projperp(match_line->p, match_line->v);
226                 rotation = vec2_angle(scene_line->v, match_line->v);
227                 rotation_mat = rot2(rotation);
228                 scene_perp_vec = mat2_vprod(rotation_mat, model_perp_vec);
229 
230                 if (orientation == 0 || orientation == -1 )
231                 {
232                     line = *scene_line;
233                     rotation_mat=rot2(0.0);
234                     trans.R = rotation_mat;
235                     trans.t = scene_perp_vec;
236                     line2_transform(&line,trans);
237                     ref = line2_alloc(LINE2);    /* Add original line data to props list */
238                    *ref = line;
239 
240                     if(hough2_extend_line(&line,location_hough_space)==TRUE)
241                     {
242                             new_line = line2_alloc(LINE2);
243                             temp_line = &line;
244                            *new_line = *temp_line;
245                             new_line->props = proplist_add(new_line->props, ref, LINE2,
246                                                                               line2_free);
247                             list_of_parallel_line2s =
248                                     ref_addtostart(list_of_parallel_line2s,new_line,LINE2);
249                     }
250                 }
251                 if (orientation == 0 || orientation == 1 )
252                 {
253                     line = *scene_line;
254                     rotation_mat=rot2(0.0);
255                     trans.R = rotation_mat;
256                     trans.t = vec2_minus(scene_perp_vec);
257                     line2_transform(&line,trans);
258                     ref = line2_alloc(LINE2);
259                    *ref = line;
260 
261                     if(hough2_extend_line(&line,location_hough_space)==TRUE)
262                     {
263                             new_line = line2_alloc(LINE2);
264                             temp_line = &line;
265                            *new_line = *temp_line;
266                             new_line->props = proplist_add(new_line->props, ref, LINE2,
267                                                                                line2_free);
268                             list_of_parallel_line2s =
269                                     ref_addtostart(list_of_parallel_line2s,new_line,LINE2);
270 
271                     }
272                 }
273             } /*end if match_hist_ref*/
274         } /*end for match_ptr*/
275 
276         list_rm(cut_match_list, rfree);
277 
278      } /*end for scene_ptr*/
279 
280 
281     /* Call the plotting procedure then on return delete the parallel lines list */
282 
283 
284     chi_sq_vec = (double*)hough2_test_points(list_of_parallel_line2s,
285                          location_hough_space, location,range);
286 
287     list_rm(list_of_parallel_line2s, rfree);
288     return(chi_sq_vec);
289 }
290 
291 double *hough2_test_points(List *list_of_parallel_line2s,
292                            Imrect *hough2, 
293                            Point2 *location, 
294                            double range)
295 {
296     double          min_angle;
297     Line2          *lin1, *lin2;
298     List           *ptr1;
299     List           *ptr2;
300     Vec2            intersect;
301     Vec2            get_intersection();
302     double          weight, angle;
303     Bool            lin1_intsct, lin2_intsct;
304     Line2          *len1, *len2;
305     Pairs_hough_def *hough_def;
306     double          *chi_sq_test;
307     double           error_scale;
308     double           eig_lim;
309     int              i=1;
310 
311 /* Get the values which define the extended parallel lines from the list then find the */
312 /* intersection between a pair.  Plot a point in the hough space to correspond to the */
313                     /* intersection after smoothing using a gaussian */
314 
315     hough_def = pairs_hough_def_get();
316 
317     min_angle = hough_def->min_angle;
318     error_scale = hough_def->gauss_sigma*hough_def->gauss_sigma;
319     eig_lim = hough_def->eig_lim;
320     ptr1 = list_of_parallel_line2s;
321     chi_sq_test = (double *)ralloc(1000*sizeof(double));
322 
323     while (ptr1 != NULL )
324     {
325           ptr2 = ptr1->next;
326           lin1 = ptr1->to;
327           while ( ptr2 != NULL && i<999 )
328           {
329                 lin2 = ptr2->to;
330                 intersect = get_intersection(lin1, lin2, &lin1_intsct, &lin2_intsct);
331                 angle = fabs(vec2_angle(lin1->v, lin2->v ));
332                 if( (angle > min_angle) && lin1_intsct && lin2_intsct)
333                 {
334                       len1 = prop_get(lin1->props, LINE2);
335                       len2 = prop_get(lin2->props, LINE2);
336                       weight = len1->length * len2->length;
337                       chi_sq_test[i] = chi_squared_stat(&intersect, weight, len1, len2,location,
338                                                      error_scale,eig_lim);
339                       if (chi_sq_test[i]!=0.0&&chi_sq_test[i]<range*range) i++;
340                 } /* endif angle */
341                 ptr2 = ptr2->next;
342           }  /* endwhile ptr2 */
343 
344           ptr1 = ptr1->next;
345           if( ptr1 != NULL )
346           {
347               ptr2 = list_of_parallel_line2s;
348           }
349      }  /* endwhile ptr1 */
350      chi_sq_test[0] = (double)i;
351      return(chi_sq_test);
352 }
353 
354 
355 

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