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

Linux Cross Reference
Tina5/tina-libs/tina/vision/visCorr_ctf.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/visCorr_ctf.c,v $
 23  * Date    :  $Date: 2004/08/04 15:07:47 $
 24  * Version :  $Revision: 1.1 $
 25  * CVS Id  :  $Id: visCorr_ctf.c,v 1.1 2004/08/04 15:07:47 paul Exp $
 26  *
 27  * Author : Simon Crossley (see thesis Univ. Sheffield U.K.), NAT.
 28  */
 29 /**
 30  * @file stereo correlation algorithms
 31  * @brief Routines to control coarse to fine matching. 
 32  *
 33  *********
 34 */
 35 
 36 #include "visCorr_ctf.h"
 37 
 38 #include <math.h>
 39 #include <float.h>
 40 #include <limits.h>
 41 #include <tina/sys/sysDef.h>
 42 #include <tina/sys/sysPro.h>
 43 #include <tina/math/mathDef.h>
 44 #include <tina/math/mathPro.h>
 45 #include <tina/geometry/geomDef.h>
 46 #include <tina/geometry/geomPro.h>
 47 #include <tina/image/imgDef.h>
 48 #include <tina/image/imgPro.h>
 49 #include <tina/vision/visDef.h>
 50 #include <tina/vision/visPro.h>
 51 
 52 #include "vis_CorrDef.h"
 53 #include "vis_CorrPro.h"
 54 
 55 static Ctf_info *ctf_alloc(Imregion *blk_roi, Imregion *pix_roi,
 56                            int ctf_level)
 57 {
 58   Ctf_info *info_ptr;
 59 
 60   info_ptr = (Ctf_info *)ralloc(sizeof(Ctf_info));
 61   
 62   info_ptr->blk_roi = *blk_roi;
 63   info_ptr->pix_roi = *pix_roi;
 64   info_ptr->ctf_level = ctf_level;
 65   info_ptr->ctf_scale = 1 << ctf_level;
 66   info_ptr->edge_count = -1;
 67   info_ptr->max_disp = -FLT_MAX;
 68   info_ptr->min_disp = FLT_MAX;
 69   info_ptr->new_blk = true;
 70   info_ptr->keep = true;
 71   info_ptr->valid_disp = false;
 72   return(info_ptr);
 73 }
 74 
 75 static void update_disp_info(Imrect *ctf_im, Imrect *disp_im,
 76                              int pickup_range, Bool refine)
 77 {
 78   int x, y, m, n, srch_range;
 79   Ctf_info *info_ptr;
 80 
 81   /* find the min/max disparities for each block */
 82   FOR_IM (ctf_im->region, n, m)
 83     {
 84       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, n, m)) == NULL)
 85         continue;
 86 
 87       if (info_ptr->valid_disp == true)
 88         continue;
 89 
 90       x = (info_ptr->pix_roi.ux + info_ptr->pix_roi.lx) / 2;
 91       y = (info_ptr->pix_roi.uy + info_ptr->pix_roi.ly) / 2;
 92       if (refine)
 93         {
 94           if (info_ptr->ctf_level == 0)
 95             srch_range = pickup_range;
 96           else
 97             srch_range = info_ptr->ctf_scale * pickup_range / 2;
 98         }
 99       else
100         {
101           srch_range = info_ptr->ctf_scale * pickup_range;
102         }
103       imf_nz_maxmin(disp_im, &(info_ptr->max_disp), &(info_ptr->min_disp),
104                     y, x, srch_range);
105 
106       if (info_ptr->max_disp == -FLT_MAX)
107         continue;
108 
109       info_ptr->min_disp /= (float)info_ptr->ctf_scale;
110       info_ptr->max_disp /= (float)info_ptr->ctf_scale;
111     }
112 }
113 
114 void init_ctf_im(Imrect *ctf_im, Imrect *grey_im, int max_ctf_level,
115                  int blk_width, int blk_height)
116 {
117   int x, y, n, m, i, j, ctf_scale, pix_xinc, pix_yinc;
118   Imregion blk_roi, pix_roi, im_roi;
119   Ctf_info *info_ptr;
120 
121   im_roi = *(grey_im->region);
122   im_roi.lx += PAD_DEPTH;
123   im_roi.ly += PAD_DEPTH;
124   im_roi.ux -= PAD_DEPTH;
125   im_roi.uy -= PAD_DEPTH;
126 
127   /* fill the whole image with fine scale blocks */
128   for (n=0, x=im_roi.lx; n<ctf_im->region->ux; x+=blk_width, n++)
129     {
130       blk_roi.lx = n;
131       blk_roi.ux = n + 1;
132       pix_roi.lx = x;
133       pix_roi.ux = x + blk_width;
134       
135       for (m=0, y=im_roi.ly; m<ctf_im->region->uy; y+=blk_height, m++)
136         {
137           blk_roi.ly = m;
138           blk_roi.uy = m + 1;
139           pix_roi.ly = y;
140           pix_roi.uy = y + blk_height;
141           
142           info_ptr = ctf_alloc(&blk_roi, &pix_roi, 0);
143           IM_PTR(ctf_im, m, n) = (void *)info_ptr;
144         }
145     }
146 
147   if (max_ctf_level == 0)
148     return;
149 
150   /* set all possible blocks to coarsest scale */
151 
152   ctf_scale = 1 << max_ctf_level;
153   pix_xinc = ctf_scale * blk_width;
154   pix_yinc = ctf_scale * blk_height;
155 
156   /* allocate the maximum scale ctf blocks 1 per ctf region */
157   for (n=0, x=im_roi.lx; n+ctf_scale<=ctf_im->region->ux;
158        x+=pix_xinc, n+=ctf_scale)
159     {
160       blk_roi.lx = n;
161       blk_roi.ux = n + ctf_scale;
162       pix_roi.lx = x;
163       pix_roi.ux = x + pix_xinc;
164       
165       for (m=0, y=im_roi.ly; m+ctf_scale<=ctf_im->region->uy;
166            y+=pix_yinc, m+=ctf_scale)
167         {
168           blk_roi.ly = m;
169           blk_roi.uy = m + ctf_scale;
170           pix_roi.ly = y;
171           pix_roi.uy = y + pix_yinc;
172           
173           /* replace the fine blocks with a single coarse one */
174           FOR_IM (&blk_roi, j, i)
175             {
176               if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, j, i)) == NULL)
177                 continue;
178               rfree((void *)info_ptr);
179               IM_PTR(ctf_im, j, i) = NULL;
180             }
181           
182           info_ptr = ctf_alloc(&blk_roi, &pix_roi, max_ctf_level);
183           IM_PTR(ctf_im, m, n) = (void *)info_ptr;
184         }
185     }
186 }
187 
188 static void refine_ctf_block(Imrect *ctf_im, Ctf_info *info_ptr)
189 {
190   Imregion blk_roi, pix_roi;
191   Ctf_info *new_ptr;
192 
193   /* create a new top left block */
194   blk_roi.lx = info_ptr->blk_roi.lx;
195   blk_roi.ly = info_ptr->blk_roi.ly;
196   blk_roi.ux = (info_ptr->blk_roi.lx + info_ptr->blk_roi.ux) / 2;
197   blk_roi.uy = (info_ptr->blk_roi.ly + info_ptr->blk_roi.uy) / 2;
198   
199   pix_roi.lx = info_ptr->pix_roi.lx;
200   pix_roi.ly = info_ptr->pix_roi.ly;
201   pix_roi.ux = (info_ptr->pix_roi.lx + info_ptr->pix_roi.ux) / 2;
202   pix_roi.uy = (info_ptr->pix_roi.ly + info_ptr->pix_roi.uy) / 2;
203   
204   new_ptr = ctf_alloc(&blk_roi, &pix_roi, info_ptr->ctf_level - 1);
205   if (info_ptr->max_disp == -FLT_MAX)
206     {
207       new_ptr->max_disp = -FLT_MAX;
208       new_ptr->min_disp = FLT_MAX;
209     }
210   else
211     {
212       new_ptr->max_disp = info_ptr->max_disp * 2;
213       new_ptr->min_disp = info_ptr->min_disp * 2;
214     }
215   new_ptr->valid_disp = true;
216   IM_PTR(ctf_im, blk_roi.ly, blk_roi.lx) = (void *)new_ptr;
217   
218   /* create a new bottom left block */
219   blk_roi.ly = (info_ptr->blk_roi.ly + info_ptr->blk_roi.uy) / 2;
220   blk_roi.uy = info_ptr->blk_roi.uy;
221   
222   pix_roi.ly = (info_ptr->pix_roi.ly + info_ptr->pix_roi.uy) / 2;
223   pix_roi.uy = info_ptr->pix_roi.uy;
224   
225   new_ptr = ctf_alloc(&blk_roi, &pix_roi, info_ptr->ctf_level - 1);
226   if (info_ptr->max_disp == -FLT_MAX)
227     {
228       new_ptr->max_disp = -FLT_MAX;
229       new_ptr->min_disp = FLT_MAX;
230     }
231   else
232     {
233       new_ptr->max_disp = info_ptr->max_disp * 2;
234       new_ptr->min_disp = info_ptr->min_disp * 2;
235     }
236   new_ptr->valid_disp = true;
237   IM_PTR(ctf_im, blk_roi.ly, blk_roi.lx) = (void *)new_ptr;
238   
239   /* create a new bottom right block */
240   blk_roi.lx = (info_ptr->blk_roi.lx + info_ptr->blk_roi.ux) / 2;
241   blk_roi.ux = info_ptr->blk_roi.ux;
242   
243   pix_roi.lx = (info_ptr->pix_roi.lx + info_ptr->pix_roi.ux) / 2;
244   pix_roi.ux = info_ptr->pix_roi.ux;
245   
246   new_ptr = ctf_alloc(&blk_roi, &pix_roi, info_ptr->ctf_level - 1);
247   if (info_ptr->max_disp == -FLT_MAX)
248     {
249       new_ptr->max_disp = -FLT_MAX;
250       new_ptr->min_disp = FLT_MAX;
251     }
252   else
253     {
254       new_ptr->max_disp = info_ptr->max_disp * 2;
255       new_ptr->min_disp = info_ptr->min_disp * 2;
256     }
257   new_ptr->valid_disp = true;
258   IM_PTR(ctf_im, blk_roi.ly, blk_roi.lx) = (void *)new_ptr;
259   
260   /* create a new top right block */
261   blk_roi.ly = info_ptr->blk_roi.ly;
262   blk_roi.uy = (info_ptr->blk_roi.ly + info_ptr->blk_roi.uy) / 2;
263   
264   pix_roi.ly = info_ptr->pix_roi.ly;
265   pix_roi.uy = (info_ptr->pix_roi.ly + info_ptr->pix_roi.uy) / 2;
266   
267   new_ptr = ctf_alloc(&blk_roi, &pix_roi, info_ptr->ctf_level - 1);
268   if (info_ptr->max_disp == -FLT_MAX)
269     {
270       new_ptr->max_disp = -FLT_MAX;
271       new_ptr->min_disp = FLT_MAX;
272     }
273   else
274     {
275       new_ptr->max_disp = info_ptr->max_disp * 2;
276       new_ptr->min_disp = info_ptr->min_disp * 2;
277     }
278   new_ptr->valid_disp = true;
279   IM_PTR(ctf_im, blk_roi.ly, blk_roi.lx) = (void *)new_ptr;
280   
281   /* free the coarse block */
282   rfree((void *)info_ptr);
283 }
284 
285 static void coarsen_ctf_block(Imrect *ctf_im, Ctf_info *info_ptr,
286                               int blk_width, int blk_height)
287 {
288   Imregion blk_roi, pix_roi;
289   Ctf_info *new_ptr, *tmp_ptr;
290   int x, y;
291   int new_level = info_ptr->ctf_level + 1;
292   int ctf_scale;
293 
294   ctf_scale = 1 << new_level;
295 
296   if ((ctf_im->region->lx - info_ptr->blk_roi.lx) % ctf_scale == 0)
297     {
298       /* block is leftmost in coarse block */
299       blk_roi.lx = info_ptr->blk_roi.lx;
300       blk_roi.ux = blk_roi.lx + ctf_scale;
301       pix_roi.lx = info_ptr->pix_roi.lx;
302       pix_roi.ux = pix_roi.lx + (ctf_scale * blk_width);
303     }
304   else
305     {
306       /* block is rightmost in coarse block */
307       blk_roi.lx = info_ptr->blk_roi.lx - (ctf_scale / 2);
308       blk_roi.ux = info_ptr->blk_roi.ux;
309       pix_roi.lx = info_ptr->pix_roi.lx - ((ctf_scale*blk_width) / 2);
310       pix_roi.ux = info_ptr->pix_roi.ux;
311     }
312   
313   /* dont coarsen if block steps outside right of image */
314   if (blk_roi.ux > ctf_im->region->ux)
315     return;
316   
317   if ((ctf_im->region->ly - info_ptr->blk_roi.ly) % ctf_scale == 0)
318     {
319       /* block is topmost in coarse block */
320       blk_roi.ly = info_ptr->blk_roi.ly;
321       blk_roi.uy = blk_roi.ly + ctf_scale;
322       pix_roi.ly = info_ptr->pix_roi.ly;
323       pix_roi.uy = pix_roi.ly + (ctf_scale * blk_height);
324     }
325   else
326     {
327       /* block is bottommost in coarse block */
328       blk_roi.ly = info_ptr->blk_roi.ly - (ctf_scale / 2);
329       blk_roi.uy = info_ptr->blk_roi.uy;
330       pix_roi.ly = info_ptr->pix_roi.ly - ((ctf_scale*blk_height) / 2);
331       pix_roi.uy = info_ptr->pix_roi.uy;
332     }
333   
334   /* dont coarsen if block steps below bottom of image */
335   if (blk_roi.uy > ctf_im->region->uy)
336     return;
337   
338   /* dont coarsen if active fine blocks would be replaced */
339   FOR_IM (&blk_roi, y, x)
340     if ((tmp_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)))
341       {
342         if (tmp_ptr->keep || tmp_ptr->new_blk)
343           return;
344       }
345   
346   FOR_IM (&blk_roi, y, x)
347     if ((tmp_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)))
348       {
349         rfree((void *)tmp_ptr);
350         IM_PTR(ctf_im, y, x) = NULL;
351       }
352   
353   new_ptr = ctf_alloc(&blk_roi, &pix_roi, new_level);
354   IM_PTR(ctf_im, blk_roi.ly, blk_roi.lx) = (void *)new_ptr;
355 }
356 
357 void update_ctf_im(Imrect *ctf_im, Imrect *grey_im, Imrect *disp_im,
358                    int pickup_range, int max_ctf_level,
359                    int blk_width, int blk_height)
360 {
361   Ctf_info *info_ptr;
362   int x, y;
363 
364   /* mark all blocks as old */
365   FOR_IM (ctf_im->region, y, x)
366     {
367       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)) == NULL)
368         continue;
369       
370       info_ptr->new_blk = false;
371       info_ptr->keep = false;
372       info_ptr->valid_disp = false;
373     }
374 
375   /* check for local activity around ctf blocks */
376   update_disp_info(ctf_im, disp_im, pickup_range, true);
377 
378   /* check for blocks that need to be refined */
379   FOR_IM (ctf_im->region, y, x)
380     {
381       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)) == NULL)
382         continue;
383 
384       if (info_ptr->new_blk)
385         /* dont refine newly refined blocks again */
386         continue;
387       
388       if (info_ptr->ctf_level != 0 &&
389           info_ptr->ctf_level > max_ctf_level)
390         {
391           /* enforced refinement when only boostrapping using ctf */
392           /* replace a coarse block with 4 finer smaller ones */
393           refine_ctf_block(ctf_im, info_ptr);
394           continue;
395         }
396 
397       if (info_ptr->max_disp != -FLT_MAX &&
398           info_ptr->ctf_level == 0)
399         {
400           /* with local activity around a fine block */
401           /* mark it to keep */
402           info_ptr->keep = true;
403 /* statement with no effect NAT 27/4/04 
404           info_ptr->valid_disp;
405 */
406           continue;
407         }
408    
409       if (info_ptr->max_disp != -FLT_MAX &&
410           info_ptr->ctf_level != 0)
411         {
412           /* with local activity around a coarse block */
413           /* replace a coarse block with 4 finer smaller ones */
414           refine_ctf_block(ctf_im, info_ptr);
415           continue;
416         }
417     }
418 
419   /* check for blocks that need to be coarsened */
420   FOR_IM (ctf_im->region, y, x)
421     {
422       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)) == NULL)
423         continue;
424       
425       if (info_ptr->new_blk || info_ptr->keep)
426         /* dont coarsen newly refined or finest scale active blocks */
427         continue;
428 
429       if (info_ptr->max_disp == -FLT_MAX &&
430           info_ptr->ctf_level < max_ctf_level)
431         {
432           /* with no local activity around a fine block */
433           /* replace (conditionally) 4 fine small blocks with a coarse one */
434           coarsen_ctf_block(ctf_im, info_ptr, blk_width, blk_height);
435         }
436     }
437   
438   update_disp_info(ctf_im, disp_im, pickup_range, false);
439 }
440 
441 void update_edge_info(Imrect *ctf_im, Imrect *edge_im)
442 {
443   Ctf_info *info_ptr;
444   int x, y;
445 
446   /* count the number of edgels contained in each block */
447   FOR_IM (ctf_im->region, y, x)
448     {
449       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)))
450         info_ptr->edge_count = im_count_edges(edge_im, &(info_ptr->pix_roi));
451     }
452 }
453 
454 void ctf_im_free(Imrect *ctf_im)
455 {
456   Ctf_info *info_ptr;
457   int x, y;
458   
459   if (ctf_im == NULL)
460     return;
461 
462   FOR_IM (ctf_im->region, y, x)
463     {
464       if ((info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x)))
465         rfree((void *)info_ptr);
466     }
467 
468   im_free(ctf_im);
469 }
470 
471 Imrect *ctf_im_alloc(int brows, int bcols)
472 {
473   return(im_alloc(brows, bcols, NULL, ptr_v));
474 }
475 

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