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

# Linux Cross ReferenceTina4/src/pgh/pgh_analysis.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

```  1 /* APA 3/2/94 */
2 /* Modified by APA and CIB 3/2/94 */
3
4 #include <stdio.h>
5 #include <sys/param.h>
6 #include <string.h>
7 #include <tina/sys.h>
8 #include <tina/sysfuncs.h>
9 #include <tina/math.h>
10 #include <tina/mathfuncs.h>
11 #include <tina/vision.h>
12 #include <tina/visionfuncs.h>
13 #include <tina/file.h>
14 #include <tina/filefuncs.h>
15 #include <tina/file_gen.h>
16 #include <tina/draw.h>
17 #include <tina/drawfuncs.h>
18 #include <tina/tv.h>
19 #include <tina/tvfuncs.h>
20 #include <tina/toolsfuncs.h>
21 #include <tina/tw_Xfuncs.h>
22 #include <pgh/pgh_defs.h>
23 #include <pgh/pgh_types.h>
24 #include <pgh/pgh_funcs.h>
25
26 /* ---------- */
27
28 /* Plots distributions of intraclass distances.
29
30    1) Count the number of loaded models N
31    2) Find pghs which give the best N matches between the selected
32       scene line and the stored models
33    3) Find the model pgh which is closest to the mean
34    4) Plot the match scores between this hist and the others
35 */
36
37 void pairs_plot_intraclass(void)
38 {
39     List *ptr, *ptr2;
40     int N=0;
41     List *picked_geom;
42     Line2 *picked_line;
43     Pairs_hist_def    *hist_def;
44     Imrect *pgh, *pgh2;
45     List *model_pairs_list;
46     Hist_ref *hist_ref;
47     Match_ref *match_ref;
48     List *pgh_list = NULL;
49     double dp, dp_min, dp_min_best = 0.0;
50     Imrect *mean_pgh;
51
52     picked_geom = pairs_picked_geom_get();
53     hist_def = pairs_hist_def_get();
54     model_pairs_list = pairs_model_pairs_list_get();
55
56     if (picked_geom==NULL) return;
57
58     picked_line = (Line2*)picked_geom->to;
59
60     init_pairs_entry(hist_def->pairs_type,
61                      hist_def->dbin_max,
62                      hist_def->dbin_size,
63                      hist_def->num_abin,
64                      hist_def->angle_sigma,
65                      hist_def->dist_ramp);
66
67     pgh = build_normalized_pairwise(picked_line, NULL, mono_geom(), hist_def->dbin_max);
68
69     match_scene_pairs_to_model_list(pgh, model_pairs_list, NULL);
70
71     /* Construct a list of histograms from the match list */
72
73     hist_ref = hist_ref_get(pgh);
74
75     for(ptr = hist_ref->matches;ptr!=NULL;ptr = ptr->next)
76     {
77         match_ref = ptr->to;
79     }
80
81     /* Find the hist which is closest to the mean */
82
83     for(ptr = pgh_list;ptr!=NULL;ptr = ptr->next)
84     {
85         pgh = ptr->to;
86         dp_min = 1.0;
87
88         for(ptr2 = pgh_list;ptr2!=NULL;ptr2 = ptr2->next)
89         {
90             pgh2 = ptr2->to;
91
92             dp = dot_product(pgh, pgh2, 1, 1); /* TEMP */
93
94             if (dp<=dp_min) dp_min = dp;
95         }
96
97         if (dp_min>=dp_min_best)
98         {
99             dp_min_best = dp_min;
100             mean_pgh = pgh;
101         }
102     }
103
104     for(ptr = pgh_list;ptr!=NULL;ptr = ptr->next)
105     {
106         pgh = ptr->to;
107
108         dp = dot_product(mean_pgh, pgh, 1, 1); /* TEMP */
109
110         printf("%f\n", dp);
111     }
112
113 }
114
115 /* ---------- */
116
117 /* Cross matches the histograms in the supplied list,
118    puts the results into a normalised frequency histogram
119    and stores this in the supplied file */
120
121 void pairs_cross_match(List *pairs_list, char *filename)
122 {
123         List *ptr1;
124         List *ptr2;
125         Imrect *im1;
126         Imrect *im2;
127         double dp;
128         FILE *ofp;
129         char log_path[MAXPATHLEN];
130         int bins = 100;
131         double frequency_hist[100]; /* Max number of bins */
132         int bin;
133         int i;
134         double total = 0.0;
135
136         for(i=0;i<bins;i++) frequency_hist[i]=0.0;
137
138         string_append(log_path, filename, ".cross", NULL);
139
140         ofp = fopen(log_path, "w");
141
142         if (ofp==NULL)
143         {
144                 printf("Error: Cannot create '%s'.\n", log_path);
145                 return;
146         }
147
148         fprintf(ofp, "Cross match score frequency histogram\n");
149         fprintf(ofp, "-------------------------------------\n\n");
150         fprintf(ofp, "Number of bins: %d\n\n", bins);
151
152         for(ptr1=pairs_list;ptr1!=NULL;ptr1=ptr1->next)
153         {
154                 im1 = ptr1->to;
155                 for(ptr2=ptr1->next;ptr2!=NULL;ptr2=ptr2->next)
156                 {
157                         im2 = ptr2->to;
158
159                         dp = dot_product(im1, im2, im1->vtype, im2->vtype);
160
161                         bin = tina_int((dp*bins));
162                         if (bin==bins) bin--; /* When dp=1.0 */
163                         frequency_hist[bin]+=1.0;
164                 }
165         }
166
167         for(i=0;i<bins;i++)
168         {
169                 total+=frequency_hist[i];
170         }
171
172         for(i=0;i<bins;i++)
173         {
174                 fprintf(ofp, "%f, %f,\n", (i+0.5)*(1.0/bins), (frequency_hist[i]/total));
175         }
176
177         fprintf(ofp, "\n      ------------\n");
178         fprintf(ofp, "Total: %d\n", total);
179
180         fclose(ofp);
181 }
182
183 /* ---------- */
184
185 /* For T trials:
186
187        Select at random a pth of the histograms from the pairs list.
188        Construct match score pdf for those pghs and store in file.
189        Calc mean and variance for the pdf.
190
191    Store T means and T variance in file.
192 */
193
194 void pairs_match_score_pdf_analysis(List *pairs_list, char *filename, int T, double p)
195 {
196         List *ptr1;
197         Imrect *im1;
198         Imrect *im2;
199         double dp;
200         FILE *ofp, *ofp2;
201         char log_path[MAXPATHLEN];
202         int bins = 100;
203         double frequency_hist[100]; /* Max number of bins */
204         int bin;
205         int i, j, k;
206         double total = 0.0;
207     int count, t, num_samples;
208     Imrect **array;
209     double *mean_array;
210     double *std_array;
211     int x;
212     double mean, std;
213
214     /* Count the number of model histograms */
215
216     for(ptr1 = pairs_list, count=0;ptr1!=NULL;ptr1 = ptr1->next)
217     {
218         count++;
219     }
220
221     num_samples = (int)(p*count);
222
223     printf("Models pghs:%d    Samples:%d\n", count, num_samples);
224
225     /* Create an array of NULL pointers, one for each model pgh */
226
227     array = ralloc(count*sizeof(Imrect*));
228
229     /* Create arrays for the means and the standard deviations */
230
231     mean_array = ralloc(T*sizeof(double));
232     std_array = ralloc(T*sizeof(double));
233
234     for(t=0;t<T;t++)
235     {
236         printf("Trial:%d\n", t);
237
238         /* Randomly pick 'num_samples' pghs */
239
240         for(i=0;i<count;i++) array[i] = NULL;
241
242         for(i=0;i<num_samples;i++)
243         {
244             /* Pick a number between 0 and count-1 which has not been
246
247             for(x = (int)((double)count * (rand()/(pow(2.0,31.0)-1.0) ) );
248                 array[x]!=NULL;
249                 x = (int)((double)count * (rand()/(pow(2.0,31.0)-1.0) ) ) );
250
251             /* Set up a pointer to this pgh */
252
253             for(j=0, ptr1 = pairs_list;j<x;j++, ptr1 = ptr1->next);
254             array[x] = ptr1->to;
255         }
256
257         /* Clear the frequency histogram */
258             for(j=0;j<bins;j++) frequency_hist[j]=0.0;
259
260         for(j=0;j<count;j++)
261         {
262             if (array[j]!=NULL)
263             {
264                 im1 = array[j];
265
266                 for(k=j+1;k<count;k++)
267                 {
268                     if (array[k]!=NULL)
269                     {
270                         im2 = array[k];
271
272                         dp = dot_product(im1, im2, im1->vtype, im2->vtype);
273                         bin = tina_int((dp*bins));
274                         if (bin==bins) bin--; /* When dp=1.0 */
275                         frequency_hist[bin]+=1.0;
276                     }
277                 }
278             }
279         }
280
281
282         sprintf(log_path, "%s%d.data", filename, t);
283         ofp = fopen(log_path, "w");
284
285         total = 0.0;
286         for(i=0;i<bins;i++) total+=frequency_hist[i];
287
288         for(i=0;i<bins;i++)
289             {
290                     frequency_hist[i] /= total;
291             }
292
293         for(i=0;i<bins;i++)
294             {
295                     fprintf(ofp, "%f %f\n", (i+0.5)*(1.0/bins), frequency_hist[i]);
296             }
297
298             fclose(ofp);
299
300         /* Calc the mean */
301
302         mean = 0.0;
303
304         for(i=0;i<bins;i++)
305             {
306             mean += (i+0.5)*(1.0/bins)*frequency_hist[i];
307         }
308
309         mean_array[t] = mean;
310
311         /* Calc the standard deviation */
312
313         std = 0.0;
314
315         for(i=0;i<bins;i++)
316             {
317             std += (i+0.5)*(1.0/bins)*(i+0.5)*(1.0/bins)*frequency_hist[i];
318         }
319
320         std -= mean*mean;
321
322         std_array[t] = std;
323
324     }/*endfort*/
325
326     /* Store the mean and standard deviations */
327
328     sprintf(log_path, "%s_mean.data", filename);
329     ofp2 = fopen(log_path, "w");
330
331     for(i=0;i<T;i++)
332     {
333         fprintf(ofp2, "%f\n", mean_array[i]);
334     }
335
336     fclose(ofp2);
337
338
339     sprintf(log_path, "%s_std.data", filename);
340     ofp2 = fopen(log_path, "w");
341
342     for(i=0;i<T;i++)
343     {
344         fprintf(ofp2, "%f\n", std_array[i]);
345     }
346
347     fclose(ofp2);
348
349     rfree(array);
350     rfree(mean_array);
351     rfree(std_array);
352 }
353
354 /* ---------- */
355 ```

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