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

Linux Cross Reference
Tina4/src/tools/smartROI/sroi_tool.c

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

  1 /*
  2  * sroi_tool.c
  3  * 
  4  * top level tool for smart region of interest tool
  5  *
  6  */
  7 
  8 #include <stdio.h>
  9 #include <values.h>
 10 #include <math.h>
 11 #include <tina/sys.h>
 12 #include <tina/sysfuncs.h>
 13 #include <tina/math.h>
 14 #include <tina/mathfuncs.h>
 15 #include <tina/vision.h>
 16 #include <tina/visionfuncs.h>
 17 #include <tina/tv.h>
 18 #include <tina/tvfuncs.h>
 19 #include <tina/draw.h>
 20 #include <tina/drawfuncs.h>
 21 #include <tina/infrtoolfuncs.h>
 22 #include <tina/toolsfuncs.h>
 23 #include <tina/sroi_tool.h>
 24 
 25 /*
 26  * Prototypes
 27  *
 28  */
 29 
 30 extern void       *tw_fglobal();
 31 extern void       *tw_iglobal();
 32 
 33 /*
 34  * Parameters
 35  *
 36  */
 37 
 38 static Sroi_dparams sroi_dparams;
 39 
 40 /*
 41  * Param pointers
 42  *
 43  */
 44 
 45 static void *currenttv;
 46 static void *ptrb;
 47 static void *ptre;
 48 
 49 static void *ptx, *pty, *psc, *pth;
 50 static void *pw0, *pw1, *pw2, *pw3, *pw4;
 51 static void *pw5, *pw6, *pw7, *pw8, *pw9;
 52 static void *pdtx, *pdty, *pdsc, *pdth;
 53 static void *pdwx;
 54 static void *ppt, *pot, *prw, *pvs, *ppv;
 55 static void *pmm, *ppm, *ppl, *psx;
 56 static void *psm, *ptm, *pmx, *ppx, *pgx;
 57 
 58 /*
 59  * Local variables
 60  *
 61  */
 62 
 63 static char  pcafname[512];
 64 static char  meanfname[512];
 65 static char  modelfname[512];
 66 static char  fname[512];
 67 static char  pname[512];
 68 static char  tv_namestr[256] = "";
 69 
 70 
 71 Sroi_dparams  *sroi_get_dparams()
 72 {
 73     return(&sroi_dparams);
 74 }
 75 
 76 static void reset_weights()
 77 {
 78   int  i;
 79 
 80   for (i = 4; i < PLEN; i++)
 81     sroi_dparams.sparams[i] = 0.0;
 82 }
 83 
 84 static void sroitool_tv_update(void)
 85 {
 86    Tv     *tv = tv_get_next ();
 87 
 88    if (tv!=NULL) strcpy(tv_namestr, tv->label);
 89      (void)tw_sglobal_reset(currenttv);
 90 }
 91 
 92 static void prof_check_proc(int val)
 93 {
 94    prof_profile_on(val & PROFILE_MASK);
 95    prof_normalise_on(val & NORMALISE_MASK);
 96    prof_mse_on(val & MSE_MASK);
 97    prof_global_on(val & GLOBAL_MASK);
 98 }
 99 
100 static void input_model_proc()
101 {
102   input_model(fname);
103   position();
104   angle_points();
105   update_dparams();
106   reset_weights();
107   flush_dialog();
108 }
109 
110 static void output_model_proc()
111 {
112   if (get_model() == NULL)
113     return;
114 
115   update_model();
116   i_position();
117   angle_points();
118   output_model(fname);
119 }
120 
121 static void sroi_sample_proc()
122 {
123   Imrect *im;
124   
125   if ((im = tv_get_imrect(tv_get_next())) == NULL)
126     return;
127 
128   sroi_sample_profile(im);
129 }
130 
131 
132 static void show_model_proc()
133 {
134   Tv  *tv;
135 
136   if ((tv = tv_get_next()) == NULL)
137     return;
138 
139   update_model();
140   position();
141   angle_points();
142   tv_repaint(tv);
143   draw_sample_outer(tv);
144   draw_sample_inner(tv);
145 }
146 
147 static  void sroi_mouse_proc(Tv_mouse(*func) ( /*???*/ ))
148 {
149   Tv  *tv;
150 
151   if ((tv = tv_get_next()) == NULL)
152        return;
153   
154   tv_set_mouse(tv, (*func) ());
155   (void) tv_set_activity(tv, MOUSE);
156 }
157 
158 
159 /*
160 static void sample_outer_proc()
161 {
162   List *poly = (List *)tv_poly_get();
163 
164   sample_outer(poly);
165   i_position();
166   angle_points();
167   draw_sample_outer(tv_get_next());
168   update_dparams();
169   reset_weights();
170   flush_dialog();
171 }
172 */
173 
174 static void export_outer_proc()
175 {
176   Tv *tv;
177 
178   if ((tv = tv_get_next()) == NULL)
179     return;
180 
181   export_outer();
182   tv_reset_draw(tv);
183   tv_repaint(tv);
184   tv_set_color(tv, red);
185   tv_poly_draw(tv);
186 }
187 
188 /*
189 static void sample_inner_proc()
190 {
191   List *poly = (List *)tv_poly_get();
192 
193   sample_inner(poly);
194   i_position();
195   angle_points();
196   draw_sample_inner(tv_get_next());
197   update_dparams();
198   reset_weights();
199   flush_dialog();
200 }
201 */
202 
203 static void export_inner_proc()
204 {
205   Tv *tv;
206 
207   if ((tv = tv_get_next()) == NULL)
208     return;
209 
210   export_inner();
211   tv_reset_draw(tv);
212   tv_repaint(tv);
213   tv_set_color(tv, red);
214   tv_poly_draw(tv);
215 }
216 
217 
218 static void make_model_proc()
219 {
220   List  *poly = (List *)sroi_get_markup();
221   
222   sample_markup(poly);
223   i_position();
224   angle_points();
225   draw_sample_outer(tv_get_next());
226   draw_sample_inner(tv_get_next());
227   update_dparams();
228   reset_weights();
229   flush_dialog();
230 }
231 
232 
233 static void build_pca_proc()
234 {
235    sprintf(modelfname, "%s", fname);
236    sprintf(pcafname, "%s%s", fname, ".pca");
237    sprintf(meanfname, "%s%s", fname, ".mml");
238    pca_model(modelfname, pcafname, meanfname);
239 }
240 
241 
242 static void input_pca_proc()
243 {
244    sprintf(pcafname, "%s%s", fname, ".pca");
245    input_pca(pcafname);
246 }
247 
248 
249 static void search_proc()
250 {
251   Imrect *im;
252 
253   if ((im = tv_get_imrect(tv_get_next())) == NULL)
254     return;
255 
256   find_simplex(im);
257   position();
258   angle_points();
259   update_dparams();
260   flush_dialog();
261   draw_sample_outer(tv_get_next());
262   draw_sample_inner(tv_get_next());
263 }
264 
265 
266 static void input_poly_proc()
267 {
268   Model   *mdl;
269   Tv      *tv = tv_get_next();
270   double **pos;
271 
272   if (((mdl = get_model()) == NULL) || (tv == NULL)) 
273     return;
274 
275   if ((mdl->m == NULL) || ((pos = mdl->m->el.double_v) == NULL))
276     return;
277 
278   sroi_polygon(pos, mdl->m->m, mdl->m->n, pname);
279   tv_reset_draw(tv);
280   tv_repaint(tv);
281   tv_set_color(tv, red);
282   tv_poly_draw(tv);
283 }
284 
285 
286 void init_dparams()
287 {
288   int  i;
289 
290   sroi_dparams.lambda[0] = 10.0;
291   sroi_dparams.lambda[1] = 10.0;
292   sroi_dparams.lambda[2] = 5.0;
293   sroi_dparams.lambda[3] = 0.05;
294 
295   for (i = 4; i < PLEN; i++)
296     sroi_dparams.lambda[i] = 0.5;
297   
298   sroi_dparams.sparams[0] = 0.0;
299   sroi_dparams.sparams[1] = 0.0;
300   sroi_dparams.sparams[2] = 0.0;
301   sroi_dparams.sparams[3] = 0.0;
302 
303   reset_weights();
304    
305   sroi_dparams.s_min = 0.0;
306   sroi_dparams.s_max = 70;
307   sroi_dparams.theta = 0.2;
308   sroi_dparams.m_max = 3;
309   sroi_dparams.p_max = 3;
310   sroi_dparams.g_max = 3;
311   sroi_dparams.ftol = 1e-06;
312 
313   sroi_dparams.points = 0;
314   sroi_dparams.outer = 0;
315   sroi_dparams.rows = 8;
316   sroi_dparams.vsize = 10;
317   sroi_dparams.pcavsize = 10;
318   sroi_dparams.m_modes = 8;
319   sroi_dparams.p_modes = 0;
320   sroi_dparams.p_length = 10;
321 }
322 
323 
324 static void update_model()
325 {
326   Model  *mdl;
327   double *mw, *M_Eval;
328   int     i;
329 
330   if ((mdl = get_model()) == NULL)
331     return;
332   
333   mw = (double *) mdl->m_weight->data;
334   M_Eval = (double *) mdl->M_Eval->data;
335   mdl->tx = sroi_dparams.sparams[0]; 
336   mdl->ty = sroi_dparams.sparams[1]; 
337   mdl->s = sroi_dparams.sparams[2]; 
338   mdl->theta = sroi_dparams.sparams[3]; 
339   mdl->p_modes = sroi_dparams.p_modes;
340   mdl->m_modes = sroi_dparams.m_modes;
341   
342   for (i = 4; i < PLEN; i++)
343     sroi_dparams.lambda[i] = sroi_dparams.lambda[4];
344 
345   for (i = 0; (i < (PLEN - 4) && i < mdl->m_modes); i++)
346     mw[i] = sroi_dparams.sparams[i+4]*M_Eval[i];
347  
348   mdl->c = sroi_dparams.points; 
349   mdl->o = sroi_dparams.outer; 
350   mdl->r = sroi_dparams.rows; 
351   mdl->vsize = sroi_dparams.vsize; 
352   mdl->pcavsize = sroi_dparams.pcavsize; 
353   
354   mdl->s_min = sroi_dparams.s_min;
355   mdl->s_max = sroi_dparams.s_max;
356   mdl->theta_max = sroi_dparams.theta;
357   mdl->m_max = sroi_dparams.m_max;
358   mdl->p_max = sroi_dparams.p_max;
359   mdl->g_max = sroi_dparams.g_max;
360 } 
361 
362 
363 static void update_dparams()
364 {
365   Model  *mdl;
366   double *mw, *M_Eval;
367   int     i;
368 
369   if ((mdl = get_model()) == NULL)
370     return;
371   
372   mw = (double *) mdl->m_weight->data;
373   M_Eval = (double *) mdl->M_Eval->data;
374   sroi_dparams.sparams[0] = mdl->tx;
375   sroi_dparams.sparams[1] = mdl->ty;
376   sroi_dparams.sparams[2] = mdl->s;
377   sroi_dparams.sparams[3] = mdl->theta;
378   
379   for (i = 0; (i < PLEN && i < mdl->m_modes); i++)
380   {
381     if (M_Eval[i] != 0.0)
382       sroi_dparams.sparams[i+4] = mw[i]/M_Eval[i];
383     else 
384       sroi_dparams.sparams[i+4] = 0.0;
385   }
386   for (i = mdl->m_modes; i < PLEN - 4; i++)
387     sroi_dparams.sparams[i+4] = 0.0;
388  
389   sroi_dparams.s_min = mdl->s_min;
390   sroi_dparams.s_max = mdl->s_max; 
391   sroi_dparams.theta = mdl->theta_max;
392   sroi_dparams.m_max = mdl->m_max;
393   sroi_dparams.p_max = mdl->p_max;
394   sroi_dparams.g_max = mdl->g_max;
395 
396   sroi_dparams.points = mdl->c;
397   sroi_dparams.outer = mdl->o;
398   sroi_dparams.rows = mdl->r;
399   sroi_dparams.vsize = mdl->vsize;
400   sroi_dparams.pcavsize = mdl->pcavsize;
401   sroi_dparams.m_modes = mdl->m_modes;
402   sroi_dparams.p_modes = mdl->p_modes;
403   sroi_dparams.p_length = mdl->p_length;
404 } 
405 
406 
407 static void flush_dialog()
408 {
409   (void) tw_fglobal_reset(ptx);
410   (void) tw_fglobal_reset(pty);
411   (void) tw_fglobal_reset(psc);
412   (void) tw_fglobal_reset(pth);
413   (void) tw_fglobal_reset(pw0);
414   (void) tw_fglobal_reset(pw1);
415   (void) tw_fglobal_reset(pw2);
416   (void) tw_fglobal_reset(pw3);
417   (void) tw_fglobal_reset(pw4);
418   (void) tw_fglobal_reset(pw5);
419   (void) tw_fglobal_reset(pw6);
420   (void) tw_fglobal_reset(pw7);
421   (void) tw_fglobal_reset(pw8);
422   (void) tw_fglobal_reset(pw9);
423   
424   (void) tw_fglobal_reset(pdtx);
425   (void) tw_fglobal_reset(pdty);
426   (void) tw_fglobal_reset(pdsc);
427   (void) tw_fglobal_reset(pdth);
428   (void) tw_fglobal_reset(pdwx);
429   
430   (void) tw_iglobal_reset(ppt);
431   (void) tw_iglobal_reset(pot);
432   (void) tw_iglobal_reset(prw);
433   (void) tw_iglobal_reset(pvs);
434   (void) tw_iglobal_reset(ppv);
435   (void) tw_iglobal_reset(pmm);
436   (void) tw_iglobal_reset(ppm);
437   (void) tw_iglobal_reset(ppl);
438   
439   (void) tw_fglobal_reset(psx);
440   (void) tw_fglobal_reset(psm);
441   (void) tw_fglobal_reset(ptm);
442   (void) tw_fglobal_reset(pmx);
443   (void) tw_fglobal_reset(ppx);
444   (void) tw_fglobal_reset(pgx);
445 }
446 
447 
448 static void param_dialog()
449 {
450   static int  dialog = 0;
451 
452   if (dialog)
453   {
454     tw_show_dialog(dialog);
455     return;
456   }
457 
458   dialog = tw_dialog("smartROI Params");
459   
460   tw_label("Model  ");
461   tw_button("show", show_model_proc, NULL);
462 
463   tw_newrow();
464   ppt = (void *) tw_iglobal(" points : ", &(sroi_dparams.points), 7);
465   pot = (void *) tw_iglobal(" outer :", &(sroi_dparams.outer), 7);
466   prw = (void *) tw_iglobal(" rows :", &(sroi_dparams.rows), 7);
467   tw_newrow();
468   pvs = (void *) tw_iglobal(" vsize :", &(sroi_dparams.vsize), 7);
469   ppv = (void *) tw_iglobal(" pca vsize :", &(sroi_dparams.pcavsize), 7);
470   pmm = (void *) tw_iglobal(" mdl modes :", &(sroi_dparams.m_modes), 7);
471   tw_newrow();
472   ppm = (void *) tw_iglobal(" prf modes :", &(sroi_dparams.p_modes), 7);
473   ppl = (void *) tw_iglobal(" prf length :", &(sroi_dparams.p_length), 7);
474   tw_newrow();
475   ptx = (void *) tw_fglobal(" tx :", &(sroi_dparams.sparams[0]), 10);
476   pty = (void *) tw_fglobal(" ty :", &(sroi_dparams.sparams[1]), 10);
477   psc = (void *) tw_fglobal(" scale :", &(sroi_dparams.sparams[2]), 10);
478   tw_newrow();
479   pth = (void *) tw_fglobal(" theta :", &(sroi_dparams.sparams[3]), 10);
480   pw0 = (void *) tw_fglobal(" w0:", &(sroi_dparams.sparams[4]), 10);
481   pw1 = (void *) tw_fglobal(" w1:", &(sroi_dparams.sparams[5]), 10);
482   tw_newrow();
483   pw2 = (void *) tw_fglobal(" w2:", &(sroi_dparams.sparams[6]), 10);
484   pw3 = (void *) tw_fglobal(" w3:", &(sroi_dparams.sparams[7]), 10);
485   pw4 = (void *) tw_fglobal(" w4:", &(sroi_dparams.sparams[8]), 10);
486   tw_newrow();
487   pw5 = (void *) tw_fglobal(" w5:", &(sroi_dparams.sparams[9]), 10);
488   pw6 = (void *) tw_fglobal(" w6:", &(sroi_dparams.sparams[10]), 10);
489   pw7 = (void *) tw_fglobal(" w7:", &(sroi_dparams.sparams[11]), 10);
490   tw_newrow();
491   pw8 = (void *) tw_fglobal(" w8:", &(sroi_dparams.sparams[12]), 10);
492   pw9 = (void *) tw_fglobal(" w9:", &(sroi_dparams.sparams[13]), 10);
493   tw_newrow();
494   
495   tw_label("Initial search");
496   
497   tw_newrow();
498   pdtx = (void *) tw_fglobal(" dtx :", &(sroi_dparams.lambda[0]), 10);
499   pdty = (void *) tw_fglobal(" dty :", &(sroi_dparams.lambda[1]), 10);
500   pdsc = (void *) tw_fglobal(" dscale :", &(sroi_dparams.lambda[2]), 10);
501   tw_newrow();
502   pdth = (void *) tw_fglobal(" dtheta :", &(sroi_dparams.lambda[3]), 10);
503   pdwx = (void *) tw_fglobal(" dwx:", &(sroi_dparams.lambda[4]), 10);
504   tw_newrow();
505 
506   tw_label("Search limits");
507 
508   tw_newrow();
509   psm = (void *) tw_fglobal(" scale min :", &(sroi_dparams.s_min), 10);
510   psx = (void *) tw_fglobal(" scale max :", &(sroi_dparams.s_max), 10); 
511   tw_newrow();
512   ptm = (void *) tw_fglobal(" theta max :", &(sroi_dparams.theta), 10);
513   pmx = (void *) tw_fglobal(" model max :", &(sroi_dparams.m_max), 10);
514   tw_newrow();
515   ppx = (void *) tw_fglobal(" profile max :", &(sroi_dparams.p_max), 10);
516   pgx = (void *) tw_fglobal(" global max :", &(sroi_dparams.g_max), 10);
517   
518   tw_end_dialog();
519 }
520 
521 
522 void sroi_tool(int x, int y)
523 {
524   static int  tool = 0;
525 
526   if (tool)
527   {
528     tw_show_tool(tool);
529     return;
530   }
531   tool = tw_tool("smartROI Tool", x, y);
532 
533   init_dparams();
534   
535   tw_check("cost:", prof_check_proc, 1, "prof", "norm", "mse", "glob", NULL);
536   tw_button("smartROI params", param_dialog, NULL);
537   tw_newrow();
538   currenttv = (void *)tw_sglobal("current tv: ", tv_namestr, 20);
539   set_sroi_tool_func((void (*) ())sroitool_tv_update);
540   tw_newrow();
541   tw_sglobal("filename :", fname, 40);
542   tw_newrow();
543   tw_sglobal("polyname :", pname, 40);
544   tw_newrow();
545   tw_button("input", input_model_proc, NULL);
546   tw_button("output", output_model_proc, NULL);
547   tw_button("sample", sroi_sample_proc, NULL);
548   tw_newrow();
549   tw_menubar("mouse: ",
550              "markup",
551              "p axis", sroi_mouse_proc, sroi_parall_axis,
552              "r axis", sroi_mouse_proc, sroi_radial_axis,
553              "markup", sroi_mouse_proc, sroi_markup_mouse,
554              NULL, NULL);
555   tw_button("make", make_model_proc, NULL);
556   tw_button("outer2roi", export_outer_proc, NULL);
557   tw_button("inner2roi", export_inner_proc, NULL);
558   tw_newrow();
559   tw_button("build pca", build_pca_proc, NULL);
560   tw_button("input pca", input_pca_proc, NULL);
561   tw_button("search", search_proc, NULL);
562   tw_newrow();
563   tw_button("poly2roi", input_poly_proc, NULL);
564   tw_end_tool();
565 }
566 

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