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

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

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