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

Linux Cross Reference
Tina6/tina-libs/tina/vision/visCorr_global.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_global.c,v $
 27  * Date    :  $Date: 2005/01/09 17:49:25 $
 28  * Version :  $Revision: 1.2 $
 29  * CVS Id  :  $Id: visCorr_global.c,v 1.2 2005/01/09 17:49:25 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 compute block level disparity support for ctf stereo matcher.
 36  *
 37  *********
 38 */
 39 
 40 #include "visCorr_global.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/file/fileDef.h>
 50 #include <tina/file/filePro.h>
 51 #include <tina/geometry/geomDef.h>
 52 #include <tina/geometry/geomPro.h>
 53 #include <tina/image/imgDef.h>
 54 #include <tina/image/imgPro.h>
 55 
 56 #include "vis_CorrDef.h"
 57 
 58 #define ST_MAT 1010
 59 
 60 /* made global for speed */
 61 
 62 static Vec2 pix_cen, blk_cen;
 63 static int k, l;
 64 static float disp1;
 65 static int im_lx, im_ux, im_ly, im_uy;
 66 static float tot_found, tot_accepted;
 67 
 68 static void DG_check(Imrect *mlist_info_im, float dg_limit)
 69 {
 70   Stereo_match *m;
 71   Mlist_info *mlist_info;
 72   List *ptr;
 73   float delta_i, delta_j, dist, dg;
 74   float disp2;
 75 
 76   if (k>=im_ly && k<im_uy && l>=im_lx && l<im_ux)
 77     {
 78       mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, k, l);
 79       if ((mlist_info==NULL) || mlist_info->used)
 80         return;
 81 
 82       tot_found++;
 83       ptr = mlist_info->list_ptr;
 84       m = (Stereo_match *)ptr->to;
 85       disp2 = (float)((m->pos + m->disparity) * mlist_info->ctf_scale);
 86       mlist_info->used = true;
 87       
 88       delta_i = vec2_y(mlist_info->pix_cen) - vec2_y(pix_cen);
 89       delta_j = vec2_x(mlist_info->pix_cen) - vec2_x(pix_cen);
 90             
 91       dist = sqrt(sqr(delta_i) + sqr(delta_j));
 92       dg = fabs(disp1 - disp2) / dist;
 93       if (dg <= dg_limit)
 94         tot_accepted++;
 95     }
 96 }
 97 
 98 static int match_support(Imrect *mlist_info_im, int y, int x, double dg_limit,
 99                          double dg_pass, int dg_range)
100 {
101   List *ptr;
102   int i, j, radius;
103   Stereo_match *m;
104   Mlist_info *mlist_info;
105   
106   tot_found = 0;
107   tot_accepted = 0;
108 
109   /* mark all the match lists as unused */
110   FOR_IM (mlist_info_im->region, i, j)
111     if ((mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, i, j)))
112       mlist_info->used = false;
113 
114   mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, y, x);
115   ptr = mlist_info->list_ptr;
116   m = (Stereo_match *)ptr->to;
117   disp1 = (float)((m->pos + m->disparity) * mlist_info->ctf_scale);
118   mlist_info->used = true;
119   pix_cen = mlist_info->pix_cen;
120   i = tina_int(vec2_y(mlist_info->blk_cen));
121   j = tina_int(vec2_x(mlist_info->blk_cen));
122   dg_range *= mlist_info->ctf_scale;
123 
124   /* checks for neighbourhood disparity gradient support 
125      and returns an integer indicating this support:
126      0 for no support
127      1 for DG support */
128 
129   for (radius = 1; radius <= dg_range; radius++)
130     {
131       for (k=i-radius, l=j-radius; k<=i+radius; k++)
132         DG_check(mlist_info_im, dg_limit);
133       for (k=i-radius, l=j+radius; k<=i+radius; k++)
134         DG_check(mlist_info_im, dg_limit);
135       for (k=i-radius, l=j-radius+1; l<j+radius; l++)
136         DG_check(mlist_info_im, dg_limit);
137       for (k=i+radius, l=j-radius+1; l<j+radius; l++)
138         DG_check(mlist_info_im, dg_limit);
139     }
140 
141   if (tot_found==0.0) return(0);
142   if ((tot_accepted/tot_found)*100.0>=dg_pass) return(1);
143   return(0);
144 }
145 
146 void apply_global(Imrect *ctf_im, List ***mgrid, double dg_limit,
147                   double dg_pass, int dg_range)
148 {
149   Imrect *mlist_info_im;
150   Imregion roi;
151   int x, y, m, n, rejected = 1;
152   Ctf_info *info_ptr;
153   Mlist_info *mlist_info;
154   List *lptr;
155 
156   /* build the match list accessor image
157      used to ensure each block can only provide support once */
158 
159   mlist_info_im = im_alloc(ctf_im->width, ctf_im->height, ctf_im->region, ptr_v);
160   im_lx = mlist_info_im->region->lx;
161   im_ux = mlist_info_im->region->ux;
162   im_ly = mlist_info_im->region->ly;
163   im_uy = mlist_info_im->region->uy;
164 
165   FOR_IM (mlist_info_im->region, y, x)
166     {
167       if ((lptr = mgrid[y][x]) == NULL)
168         continue;
169 
170       info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x);
171       
172       /* roi is the extent in pixels of this ctf block */
173       roi = info_ptr->pix_roi;
174       /* find the pixel centre of the block from where the disparity applies */
175       vec2_y(pix_cen) = 0.5 * (float)(roi.uy + roi.ly);
176       vec2_x(pix_cen) = 0.5 * (float)(roi.ux + roi.lx);
177 
178       /* roi is the extent in fine blocks of this ctf block */
179       roi = info_ptr->blk_roi;
180       /* find the block centre of the block */
181       vec2_y(blk_cen) = 0.5 * (float)(roi.uy + roi.ly);
182       vec2_x(blk_cen) = 0.5 * (float)(roi.ux + roi.lx);
183 
184       mlist_info = (Mlist_info *)ralloc(sizeof(Mlist_info));
185           
186       mlist_info->list_ptr = lptr;
187       mlist_info->ctf_scale = info_ptr->ctf_scale;
188       mlist_info->pix_cen = pix_cen;
189       mlist_info->blk_cen = blk_cen;
190 
191       FOR_IM (&roi, m, n)
192         IM_PTR(mlist_info_im, m, n) = (void *)mlist_info;
193     }
194   
195   /* keep removing bad blocks until only blocks with neighbourhood support
196      are left */
197 
198   while (rejected!=0)
199     {
200       rejected = 0;
201       
202       /* build the neightbourhood support image */
203 
204       FOR_IM (mlist_info_im->region, y, x)
205         {
206           if ((lptr = mgrid[y][x]) == NULL)
207             continue;
208           
209           mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, y, x);
210           mlist_info->support = match_support(mlist_info_im, y, x, dg_limit,
211                                               dg_pass, dg_range);
212         }
213 
214       /* remove possible matches not satisfying the DG constraint */
215       
216       FOR_IM (mlist_info_im->region, y, x)
217         {
218           if ((lptr = mgrid[y][x]) == NULL)
219             continue;
220 
221           mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, y, x);
222           
223           if (mlist_info->support)
224             continue;
225 
226           rejected++;
227 
228           /* find the next possible match in the list */
229           lptr = link_rm(lptr, rfree);
230           mlist_info->list_ptr = mgrid[y][x] = lptr;
231 
232           if (lptr)
233             continue;
234 
235           /* if there are no more possibles then free the
236              mlist_info belonging to this block */
237 
238           info_ptr = (Ctf_info *)IM_PTR(ctf_im, y, x);
239           roi = info_ptr->blk_roi;
240           rfree((void *)mlist_info);
241           FOR_IM (&roi, m, n)
242             IM_PTR(mlist_info_im, m, n) = NULL;
243         }       
244     }           
245 
246   /* clean up */
247 
248   FOR_IM (mlist_info_im->region, y, x)
249     {
250       if (mgrid[y][x] == NULL)
251         continue;
252 
253       mlist_info = (Mlist_info *)IM_PTR(mlist_info_im, y, x);
254       rfree((void *)mlist_info);
255     }
256 
257   im_free(mlist_info_im);
258 }
259   
260 static int possible_match(float **score, int disp, int warp, float mthresh,
261                           int disp_min, int disp_max, int warp_min,
262                           int warp_max)
263 {
264   int           lft, rgt, top, bot;
265   float         cor;
266 
267   lft = warp == -warp_min;
268   rgt = warp == warp_max;
269   top = disp == disp_min;
270   bot = disp == disp_max;
271 
272   /* accepts a possible match ONLY if it is locally unique
273      and a maxima in the local correlation space */
274 
275   return(((cor = score[disp][warp]) > mthresh) &&
276          (lft || cor > score[disp][warp-1]) &&
277          (rgt || cor > score[disp][warp+1]) &&
278          (top || cor > score[disp-1][warp]) &&
279          (bot || cor > score[disp+1][warp]) &&
280          (lft || top || cor > score[disp-1][warp-1]) &&
281          (rgt || top || cor > score[disp-1][warp+1]) &&
282          (lft || bot || cor > score[disp+1][warp-1]) &&
283          (rgt || bot || cor > score[disp+1][warp+1]));
284 }           
285 
286 static double sort_match(Stereo_match *match, int type)
287 {
288   if (type == ST_MAT)
289     return(-match->score);
290   else
291     return(0.0);
292 }
293 
294 static List *add_match(List *list, int pos, int disparity, float corr_max,
295                        int warp_fac)
296 {
297   Stereo_match  *match;
298 
299   match = ralloc(sizeof(Stereo_match));
300 
301   match->pos = pos;
302   match->disparity = disparity;
303   match->score = corr_max;
304   match->warp = warp_fac;
305 
306   return(list_add_sorted(list, link_alloc(match, ST_MAT), sort_match));
307 }
308 
309 List *add_match_branch(float **score, float mthresh, int disp_min,
310                        int disp_max, int warp_min, int warp_max,
311                        int pos)
312 {
313   int            k, l;
314   float          corr_max, cor;
315   int            warp_fac=0;
316   List          *bptr = NULL;
317         
318   for (l = disp_min; l <= disp_max; l++)
319     {
320       corr_max = -FLT_MAX;
321       for (k = -warp_min; k <= warp_max; k++)
322         {
323           if ((cor = score[l][k]) > corr_max)
324             {
325               corr_max = cor;
326               warp_fac = k;
327             }
328         }
329       if (corr_max != -FLT_MAX &&
330           possible_match(score, l, warp_fac, mthresh,
331                          disp_min, disp_max, warp_min, warp_max))
332         bptr = add_match(bptr, pos, l, corr_max, warp_fac);
333     }
334   return(bptr);
335 }
336 

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