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

Linux Cross Reference
Tina5/tina-libs/tina/vision/visPgh_locate.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_locate.c,v $
 23  * Date    :  $Date: 2008/12/09 00:15:22 $
 24  * Version :  $Revision: 1.7 $
 25  * CVS Id  :  $Id: visPgh_locate.c,v 1.7 2008/12/09 00:15:22 paul Exp $
 26  *
 27  * Author  : NAT
 28  */
 29 /**
 30  * @file Routines for analysis of scene contents using PGH.
 31  * @brief Functions for locating individual parts of an object or multiple instances 
 32  *        of specified object in a line based approximation to an edge map. 
 33  *
 34 */
 35 
 36 
 37 #include "visPgh_locate.h"
 38 #include <stdio.h>
 39 #include <string.h>
 40 #include <math.h>
 41 
 42 #include <tina/image/img_GenDef.h>
 43 #include <tina/image/img_GenPro.h>
 44 #include <tina/image/img_PrcPro.h>
 45 #include <tina/math/math_GeomDef.h>
 46 #include <tina/math/mathGeom_geom2.h>
 47 #include <tina/math/mathTran_rot2.h>
 48 #include <tina/math/mathGeom_vec2.h>
 49 #include <tina/math/math_TranDef.h>
 50 #include <tina/math/mathGeom_mat2.h>
 51 #include <tina/file/file_UtilPro.h>
 52 #include <tina/geometry/geomGen_free.h>
 53 #include <tina/geometry/geomDef.h>
 54 #include <tina/geometry/geom_LinePro.h>
 55 #include <tina/geometry/geom_PointPro.h>
 56 #include <tina/geometry/geom_CurvePro.h>
 57 #include <tina/vision/vis_ShapeDef.h>
 58 #include <tina/vision/visShape_hough2.h>
 59 #include <tina/vision/visShape_hough1.h>
 60 #include <tina/vision/visPgh_var.h>
 61 #include <tina/vision/visPgh_HT.h>
 62 #include <tina/vision/visPgh_hist.h>
 63 #include <tina/vision/visPgh_misc.h>
 64 #include <tina/vision/visPgh_match.h>
 65 #include <tina/vision/visPgh_histfuncs.h>
 66 
 67 
 68 
 69 double pgh_scale_estimate;
 70 
 71 /* ---------- */
 72 
 73 Point2 *line_to_model(List *geom)
 74 {
 75     Vec2             translation; 
 76     Mat2             rotation_matrix;
 77     List            *ptr;
 78     /*List            *geom=NULL;*/
 79     List            *match_list;
 80     Line2           *lptr;
 81     Imrect          *im, *match_hist;
 82     double           rotation;
 83     Hist_ref        *scene_hist_ref, *match_hist_ref; 
 84     Transform2       model_to_scene_trans;
 85     Match_ref       *match_ref;
 86     List            *model_pairs_list;
 87     List            *matched_models_list;
 88     Line2           *matched_line;
 89     List            *picked_geom;
 90     Pairs_hist_def  *hist_def;
 91     Pairs_scale_def *scale_def;
 92 
 93 
 94        /* (1) Generate a pairwise histogram for the currently 
 95               selected line. (Or the first line if a number 
 96               of lines have been selected).
 97           (2) Find the best match between this histogram and
 98               all of the model histograms.
 99           (3) Report this match to the main Tina window.
100           (4) Overlay the model corresponding to the best match
101               onto the scene. Allow the user to transform 
102               between the 4 possible locations. */
103 
104     model_pairs_list = pairs_model_pairs_list_get();
105     matched_models_list = pairs_matched_models_list_get();
106     matched_line = pairs_matched_line_get();
107     picked_geom = pairs_picked_geom_get();
108     hist_def = pairs_hist_def_get();
109     scale_def = pairs_scale_def_get();
110 
111     init_pairs_entry(hist_def->pairs_type,
112                      hist_def->dbin_max,
113                      hist_def->dbin_size,
114                      hist_def->num_abin,
115                      hist_def->angle_sigma,
116                      hist_def->dist_ramp);
117 
118     /*geom = mono_geom_get();*/
119     if (geom==NULL||picked_geom==NULL||model_pairs_list==NULL) return(NULL); 
120 
121     lptr = picked_geom->to;
122    
123 /* Build the pairwise histogram */
124 
125     im = build_normalized_pairwise(lptr, NULL, geom, hist_def->dbin_max);
126 
127 /* Now search the model database for the best match */
128 
129     match_scene_pairs_to_model_list(im, model_pairs_list, scale_def);
130 
131 /* Fetch the best (first) match from the histogram matches list */
132 
133     scene_hist_ref = hist_ref_get(im);
134     match_list = scene_hist_ref->matches;
135     if (match_list==NULL) return(NULL);
136         match_ref = match_list->to;
137     match_hist = match_ref->hist;
138     match_hist_ref = hist_ref_get(match_hist);
139 
140 /* Report the best match */
141 
142         printf("Scale:%f Score:%f\n", match_ref->scale_factor, match_ref->dp);
143 
144 /* If requested output the scores for matches to this line at different scales */
145 
146 /* Now overlay the model over the scene */
147 /* set the p-field of the scene line to its mid-point */
148 
149     lptr->p = vec2_midpoint(lptr->p1, lptr->p2);
150 
151 /* First transform the model line to fit the scene line */
152 
153 /* Initially determine the rotation required to map the model line to the 
154    same orientation as the scene line */
155 
156     rotation = vec2_angle( lptr->v, match_hist_ref->line_seg->v);
157 
158 /* Construct the rotation matrix with the appropiate scale factor */
159 
160     rotation_matrix = rot2_with_scale(rotation, match_ref->scale_factor);
161 
162 /* Now determine the translation required to map the midpoint of the model 
163    line onto the midpoint of the scene line. This mapping has 2 components:
164 
165    (1) Mapping the centre point of the model line back to its position before
166        the rotation.
167    (2) Mapping the centre point of the models line from its original position
168        to the midpoint of the scene line. */
169 
170     translation = vec2_diff(match_hist_ref->line_seg->p,
171                    mat2_vprod(rotation_matrix, match_hist_ref->line_seg->p));
172 
173     translation = vec2_sum(translation, 
174                     vec2_diff(lptr->p, match_hist_ref->line_seg->p));
175 
176 
177     model_to_scene_trans.R = rotation_matrix;
178     model_to_scene_trans.t = translation;
179 
180 /* Now copy the model to the matched model data */
181 
182     list_rm(matched_models_list, geom_free);
183     matched_models_list=NULL;
184     
185     for(ptr=match_hist_ref->model->model_poly_data;ptr!=NULL;ptr=ptr->next)
186     {
187         lptr = line2_copy(ptr->to);
188         matched_models_list = 
189            list_addtoend(matched_models_list, link_alloc(lptr, 0));
190         if (ptr->to==match_hist_ref->line_seg)
191             matched_line = lptr;
192     }   
193 
194     pairs_matched_models_list_set(matched_models_list); 
195     pairs_matched_line_set(matched_line);
196 
197 /* Now transform the copied model data */
198 
199     for(ptr=matched_models_list;ptr!=NULL;ptr=ptr->next)
200         line2_transform(ptr->to, model_to_scene_trans);      
201 
202     if(match_ref->orientation == -1)
203                 rotate_matched_model();
204         else
205         {
206                 /*draw_matched_models();*/
207         }
208 
209     return(point2_make(translation, double_v));
210 }
211 
212 /* ---------- */
213 
214 List * segment_model(char *model_name)
215 {
216     List          *ptr, *match_list, *cut_match_list;
217     List          *segmented_lines_list = NULL;
218     Imrect        *scene_hist, *match_hist;
219     Hist_ref      *scene_hist_ref, *match_hist_ref; 
220     int            found_match;   
221     Match_ref     *match_ref;
222     List          *scene_pairs_list;
223     Match_cut_def *match_cut_def;
224 
225         /* (1) Descend the scene pairwise list adding each
226                scene line to the segmented_lines_list if it
227                has a match corresponding to the specified model.
228            (2) Display the segmented_lines_list. */
229 
230     scene_pairs_list = pairs_scene_pairs_list_get();
231     match_cut_def = pairs_match_cut_def_get();
232 
233     if (scene_pairs_list==NULL) 
234         return(NULL);
235 
236     strip_spaces(model_name); /* prepare the model name for comparison later */
237 
238     for(ptr=scene_pairs_list;ptr!=NULL;ptr=ptr->next)
239     {
240         scene_hist = ptr->to;
241         scene_hist_ref = prop_get(scene_hist->props, HIST_REF_TYPE);
242         found_match=FALSE; 
243 
244         if (scene_hist_ref!=NULL)
245         {
246           /* Make a cut copy of the matches list */
247   
248           cut_match_list = copy_and_cut_matches_list(scene_hist_ref,  
249                                                               match_cut_def); 
250 
251           for(match_list=cut_match_list;match_list!=NULL;
252                                                 match_list=match_list->next)
253             {
254                 match_ref = match_list->to;
255                 match_hist = match_ref->hist;
256                 if (match_hist!=NULL)
257                 {
258                     match_hist_ref = prop_get(match_hist->props,
259                                          HIST_REF_TYPE);
260                     if (match_hist_ref!=NULL &&
261                         strcmp(model_name, match_hist_ref->model->name)==0)
262                         found_match=TRUE;
263                 }
264             }
265 
266         if (found_match)
267             segmented_lines_list = ref_addtostart(segmented_lines_list, 
268                                            scene_hist_ref->line_seg, LINE2);
269 
270         /* Free the generated match list */
271 
272         list_rm(cut_match_list, rfree);
273         }
274 
275     }/*endfor(ptr)*/
276     return(segmented_lines_list);
277 }
278 
279 /* ---------- */
280 /* This function identifies any known models in the mono scene - the hough 
281    transforms constructed for 'model_name' are placed onto the image stack */
282 
283 Point2 *locate_models(char *model_name, List **new_peak_list, Vec2 *shift)
284 {
285     Imrect *im_trans(Imrect *im, double angle, Vec2 *im_center, Vec2 *offset);
286     List              *model_ptr;
287     List              *peak_list, *peak_ptr;
288     double             thres;
289     Imrect            *location_hough_space;
290     Imrect            *orient_hough;
291 #ifdef _PGHDEBUG
292     Imrect            *trans_mono_im;
293 #endif
294     Model_poly_header *polyh;
295     int                peak_count;
296     List              *ptr;
297     List              *scene_pairs_list;
298     List              *matched_models_list;
299     List              *model_poly_list;
300     Pairs_hough_def   *hough_def;
301     Match_cut_def     *match_cut_def;
302     int                max_x,max_y;
303     Hough1_peak        orient_peak;
304     List              *geom;
305     Vec2               origin;
306 #ifdef _PGHDEBUG
307     Vec2               offset;
308 #endif
309     Point2            *peak_pos;
310     char               mess[64];
311     Transform2         poly_trans;
312     double scale_factor;
313     Mat2 *C, rotation_matrix;
314     double theta, lam1, lam2, major, minor;
315 
316 
317     vec2_x(origin)=(float)0.0;
318     vec2_y(origin)=(float)0.0;
319 
320     
321         /* (1) For the chosen model in the database, construct a Hough space, 
322                and vote for the position using the matched scene histograms.
323            (2) Search for significant peaks which
324                hypothesize the presence of a model in the scene.ref
325            (3) For the chosen peak peak determine the orientation of the model 
326                at that location by voting for the angle between each
327                scene line and the model line it has matched to.
328         */
329 
330     scene_pairs_list = pairs_scene_pairs_list_get();
331     matched_models_list = pairs_matched_models_list_get();
332     model_poly_list = pairs_model_poly_list_get();
333     hough_def = pairs_hough_def_get();
334     match_cut_def = pairs_match_cut_def_get();
335 
336     if (scene_pairs_list==NULL) return(NULL);
337 
338 /* Free the matched_models list */
339 
340     if(matched_models_list!=NULL) list_rm(matched_models_list, geom_free);
341     matched_models_list=NULL;
342     pairs_matched_models_list_set(matched_models_list);
343 
344     for(model_ptr=model_poly_list;model_ptr!=NULL;model_ptr=model_ptr->next)
345     {
346         polyh = model_ptr->to;
347         if (strcmp(model_name, polyh->name)==0) break;
348     }
349     if (model_ptr==NULL) return(NULL);
350 
351 
352     location_hough_space = hough2_alloc(hough_def->loc_hough2_min_x, 
353                                         hough_def->loc_hough2_min_y,
354                                         hough_def->loc_hough2_max_x, 
355                                         hough_def->loc_hough2_max_y,
356                                         hough_def->loc_hough2_binsize_x, 
357                                         hough_def->loc_hough2_binsize_y, 
358                                         double_v);
359 
360         /*TEMP*/
361         hough2_fill(location_hough_space, 0.0);
362 
363     if (shift !=NULL)
364          polyh->landmark = *shift; 
365     else
366          polyh->landmark = polyh->centroid;
367 
368     if (hough_def->plot_type==LONG_LINES) 
369     {
370         locate_using_long_lines(polyh, location_hough_space,
371                                scene_pairs_list, match_cut_def);
372     }
373 
374     if (hough_def->plot_type==SHORT_LINES) 
375     {
376         locate_using_short_lines(polyh, location_hough_space,
377                                  scene_pairs_list, match_cut_def);
378     }
379          
380     if (hough_def->plot_type==POINTS)
381     {
382         locate_using_points(polyh,
383                      location_hough_space, scene_pairs_list, match_cut_def);
384     }
385 
386 /* Sqrt the hough transform to approximate a chi_2 */
387 
388     sqrt_hough2(location_hough_space);
389 
390 /* Search the loc hough2 for peaks */
391 
392     thres = imf_max(location_hough_space, &max_y, &max_x)*
393                      (hough_def->peak_percent/100.0);
394     peak_list = hough2_locate_peaks(location_hough_space, thres);
395 
396 /* file must be compiled as a project with tina-tools library in order to debug */
397 #ifdef _PGHDEBUG
398     stack_push(location_hough_space, IMRECT, im_free);
399 #else
400     im_free(location_hough_space);
401 #endif
402 
403 /* Determine model orientation at hough2 peak_no */
404 
405     peak_list = sort_list(peak_list, peak_func, NULL);
406 
407     for(peak_ptr=peak_list,peak_count=0;peak_ptr!=NULL;peak_count++,peak_ptr=peak_ptr->next)
408     {
409         if(peak_count==hough_def->peak_no) break;
410     }
411 
412     if (peak_ptr==NULL) return(NULL);
413 
414     sprintf(mess,"hough peak = %4.2f / %4.2f \n",-peak_func(peak_ptr->to),thres);
415     format(mess);
416 
417 
418     /* Display the error of this peak */
419     C = prop_get(((Point2*)peak_ptr->to)->props, COVAR2);
420     conic_eigen(mat2_xx(*C), mat2_xy(*C), mat2_yy(*C),
421                 &theta, &lam1, &lam2);
422     theta = 180.0*theta/PI;
423     /* Assumes binsizex = binsizey */
424     major = sqrt(lam1)*hough_def->loc_hough2_binsize_x;
425     minor = sqrt(lam2)*hough_def->loc_hough2_binsize_x;
426     format("major: %f\nminor: = %f\nangle = %f\n\n", major, minor, theta);
427 
428     /* Determine the scale of the model responsible for this peak */
429 
430         scale_factor = scale_from_loc(peak_ptr->to, model_ptr->to);
431         printf("Scale:%f\n", scale_factor);
432 
433     pgh_scale_estimate = scale_factor; /* Stored for global access */
434 
435     orient_hough = hough1_alloc(-PI-(PI/hough_def->orient_binsize),
436                                  PI+(PI/hough_def->orient_binsize),
437                                 (PI/hough_def->orient_binsize), float_v);
438 
439     angle_from_loc(orient_hough, peak_ptr->to, model_ptr->to);
440     thres = imf_max(orient_hough, &max_y, &max_x)*
441                      (hough_def->peak_percent/100.0);
442     orient_peak = hough1_locate_max_peak(orient_hough, thres ,float_v);
443                
444 /* Make a copy of the model data */
445 
446     polyh=model_ptr->to;
447     geom = poly_copy(polyh->model_poly_data);
448 
449 /* Transform to landmark or centroid */
450     rotation_matrix = rot2(0.0);
451     poly_trans.R = rotation_matrix;
452     poly_trans.t = polyh->landmark;
453 
454     for(ptr=geom;ptr!=NULL;ptr=ptr->next)
455         line2_transform(ptr->to, poly_trans);
456 
457 
458 /* Transform the copied data to the determined angle, position and scale*/
459 
460     poly_rotate_and_scale(geom, orient_peak.x, scale_factor, origin);
461     peak_pos = peak_ptr->to;
462     poly_translate(geom, peak_pos->p );
463 
464 /* Add the transformed copy to the matched_models list */
465 
466     matched_models_list = list_append(matched_models_list, geom);
467 
468 /* file must be compiled as a project with tina-tools library in order to debug */
469 #ifdef _PGHDEBUG
470     stack_push(orient_hough, IMRECT, im_free);
471     origin = peak_pos->p;
472     offset = vec2_sum(poly_trans.t,origin);
473     trans_mono_im = im_trans(mono_image_get(), orient_peak.x, &origin, &offset);
474     stack_push(trans_mono_im, IMRECT, im_free);
475 #else
476     im_free(orient_hough);
477 #endif
478 
479     pairs_matched_models_list_set(matched_models_list);
480 
481     if(new_peak_list != NULL) *new_peak_list = peak_list;
482     return(peak_pos);
483 }
484 
485 /* ---------- */
486 
487 

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