• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/regularization.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008/2009 by Matthias Ihrke   *
00003  *   mihrke@uni-goettingen.de   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "regularization.h"
00022 #include "optarg.h"
00023 #include "linalg.h"
00024 #include "imageproc.h"
00025 
00050 Array* regularization_linear_points( const Array *points, uint dims[2], Array *m ){
00051   if( points->dtype!=INT ){
00052      errprintf("Need INT-array for coordinates\n"); return NULL;
00053   }
00054   if( points->ndim!=2 ){
00055      errprintf("Need 2D-array for coordinates\n"); return NULL;
00056   }
00057   if( points->size[1]<2 ){
00058      errprintf("Need 2 coordinates per point\n"); return NULL;
00059   }
00060   if( m!=NULL ){
00061      bool ismatrix;
00062      matrix_CHECK( ismatrix, m );
00063      if( !ismatrix) return NULL;
00064      if( m->size[0]!=dims[0] || m->size[1]!=dims[1] ){
00065         errprintf( "output matrix must be of same dimension as dims[2]\n");
00066         return NULL;
00067      }
00068   } else {
00069      m=array_new2(DOUBLE, 2, dims[0], dims[1] );
00070   }
00071 
00072   /* bresenham bitmap-image */
00073   Array *mask=array_new2( INT, 2, dims[0], dims[1] );
00074   
00075   /* get line segments */
00076   Array *bres;
00077   if( points==NULL ){
00078      Array *defpoints=array_new2( INT, 2, 2, 2 );
00079      array_INDEX2( points,int, 0, 0)=0;
00080      array_INDEX2( points,int, 1, 0)=0;
00081      array_INDEX2( points,int, 0, 1)=dims[0]-1;
00082      array_INDEX2( points,int, 1, 1)=dims[1]-1;
00083      bres=bresenham_linesegments( defpoints );
00084      array_free( defpoints );
00085   } else {
00086      bres=bresenham_linesegments( points );
00087   }
00088 
00089   /* set to mask */
00090   int i;
00091   for( i=0; i<bres->size[1]; i++ ){
00092      array_INDEX2(mask,int, array_INDEX2(bres,int,0,i),
00093                       array_INDEX2(bres,int,1,i))=1;
00094   }
00095   m=disttransform_deadreckoning( mask, m );
00096 
00097   array_free( mask );
00098   
00099   return m;
00100 }
00101 
00127 Array* regularization_gaussian_corridor( const Array *points, uint dims[2], Array *m, double max_sigma ){
00128   if( points->dtype!=INT ){
00129      errprintf("Need INT-array for coordinates\n"); return NULL;
00130   }
00131   if( points->ndim!=2 ){
00132      errprintf("Need 2D-array for coordinates\n"); return NULL;
00133   }
00134   if( points->size[1]<2 ){
00135      errprintf("Need 2 coordinates per point\n"); return NULL;
00136   }
00137   if( m!=NULL ){
00138      bool ismatrix;
00139      matrix_CHECK( ismatrix, m );
00140      if( !ismatrix) return NULL;
00141      if( m->size[0]!=dims[0] || m->size[1]!=dims[1] ){
00142         errprintf( "output matrix must be of same dimension as dims[2]\n");
00143         return NULL;
00144      }
00145   } else {
00146      m=array_new2(DOUBLE, 2, dims[0], dims[1] );
00147   }
00148   
00149   /* get distance transform of the linear interpolation between markers */
00150   m = regularization_linear_points( points, dims, m );
00151 
00152   int i,j,k;
00153   double sigma;
00154   double maxdist, dist, closest_dist, normgauss;
00155   int flag;
00156   double pointdist=0.1;
00157 
00158   /* apply gaussian with varying sigma */
00159   flag = 0;
00160   maxdist = sqrt(2.0)*(double)(dims[0]-1);
00161 
00162   dprintf("maxdist=%f, max_sigma=%f\n", maxdist, max_sigma);
00163   for( i=0; i<dims[0]; i++ ){
00164      for( j=0; j<dims[1]; j++ ){
00165         closest_dist = DBL_MAX;
00166         for( k=0; k<points->size[1]; k++ ){
00167           dist = ( SQR( (double)(array_INDEX2(points,int,0,k)-i) )
00168                       + SQR( (double)(array_INDEX2(points,int,1,k)-j) ) );
00169           dist = sqrt( dist - SQR( mat_IDX(m,i,j) ) );
00170           dist /= maxdist;
00171           if(dist<closest_dist){
00172              closest_dist=dist;
00173           }
00174           if( dist<(pointdist) )
00175              flag=1;
00176         }
00177 
00178         if( closest_dist==0 ){
00179           closest_dist = 1e-10;
00180         }
00181         sigma = max_sigma*maxdist;
00182         if( flag )
00183           sigma = sigma*closest_dist/pointdist;
00184 
00185         normgauss = gaussian( 0.0, sigma, 0 );
00186         if(normgauss > 10000000) {
00187           mat_IDX(m,i,j)=1;
00188         } else {
00189           mat_IDX(m,i,j)=gaussian( mat_IDX(m,i,j), sigma, 0 )/normgauss;
00190         }
00191         
00192         flag = 0;
00193      }
00194   }
00195 
00196   return m;
00197 }
00198 

Generated on Fri Jun 25 2010 14:10:20 for libeegtools by  doxygen 1.7.0