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

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

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