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