irplib_oddeven.c

00001 /* $Id: irplib_oddeven.c,v 1.8 2008/04/15 08:04:16 lbilbao Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: lbilbao $
00023  * $Date: 2008/04/15 08:04:16 $
00024  * $Revision: 1.8 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_oddeven.h"
00040 
00041 /*-----------------------------------------------------------------------------
00042                             Functions prototypes
00043  -----------------------------------------------------------------------------*/
00044 
00045 static cpl_imagelist * irplib_oddeven_cube_conv_xy_rtheta(cpl_imagelist *) ;
00046 static cpl_imagelist * irplib_oddeven_cube_conv_rtheta_xy(cpl_imagelist *) ;
00047  
00048 /*----------------------------------------------------------------------------*/
00052 /*----------------------------------------------------------------------------*/
00053 
00056 /*----------------------------------------------------------------------------*/
00064 /*----------------------------------------------------------------------------*/
00065 int irplib_oddeven_monitor(
00066         const cpl_image     *   in,
00067         int                     iquad,
00068         double              *   r_even) 
00069 {
00070     cpl_image       *   extracted ;        
00071     cpl_image       *   labels ;        
00072     int             *   plabels ;
00073     int                 llx, lly, urx, ury ;
00074     int                 nx, ny ;
00075     double              f_even, f_tot ;
00076     cpl_apertures   *   aperts ;
00077     int                 i, j ;
00078 
00079     /* Test entries */
00080     if (in == NULL || r_even == NULL) return -1 ;
00081     nx = cpl_image_get_size_x(in) ;
00082     ny = cpl_image_get_size_y(in) ;
00083     
00084     switch (iquad){
00085         case 1:
00086             llx = 1 ; lly = 1 ; urx = nx/2 ; ury = ny/2 ; break ;
00087         case 2:
00088             llx = (nx/2)+1 ; lly = 1 ; urx = nx ; ury = ny/2 ; break ;
00089         case 3:
00090             llx = 1 ; lly = (ny/2)+1 ; urx = nx/2 ; ury = ny ; break ;
00091         case 4:
00092             llx = (nx/2)+1 ; lly = (ny/2)+1 ; urx = nx ; ury = ny ; break ;
00093         case 0:
00094             llx = 1 ; lly = 1 ; urx = nx ; ury = ny ; break ;
00095         default:
00096             cpl_msg_error(cpl_func, "Unsupported mode") ;
00097             *r_even = 0.0 ;
00098             return -1 ;
00099     }
00100    
00101     /* Extract quadrant */
00102     if ((extracted = cpl_image_extract(in, llx, lly, urx, ury)) == NULL) {
00103         cpl_msg_error(cpl_func, "Cannot extract quadrant") ;
00104         *r_even = 0.0 ;
00105         return -1 ;
00106     }
00107     nx = cpl_image_get_size_x(extracted) ;
00108     ny = cpl_image_get_size_y(extracted) ;
00109             
00110     /* Get f_tot */
00111     f_tot = cpl_image_get_median(extracted) ;
00112     if (fabs(f_tot) < 1e-6) {
00113         cpl_msg_warning(cpl_func, "Quadrant median is 0.0") ;
00114         cpl_image_delete(extracted) ;
00115         *r_even = 0.0 ;
00116         return -1 ;
00117     }
00118 
00119     /* Create the label image to define the even columns */
00120     labels = cpl_image_new(nx, ny, CPL_TYPE_INT) ;
00121     plabels = cpl_image_get_data_int(labels) ;
00122     for (i=0 ; i<nx ; i++) {
00123         if (i % 2) for (j=0 ; j<ny ; j++) plabels[i+j*nx] = 0 ;
00124         else for (j=0 ; j<ny ; j++) plabels[i+j*nx] = 1 ;
00125     }
00126     
00127     /* Get the median of even columns */
00128     if ((aperts = cpl_apertures_new_from_image(extracted, labels)) == NULL) {
00129         cpl_msg_error(cpl_func, "Cannot compute the even columns median") ;
00130         cpl_image_delete(extracted) ;
00131         cpl_image_delete(labels) ;
00132         *r_even = 0.0 ;
00133         return -1 ;
00134     }
00135     cpl_image_delete(extracted) ;
00136     cpl_image_delete(labels) ;
00137     f_even = cpl_apertures_get_median(aperts, 1) ;
00138     cpl_apertures_delete(aperts) ;
00139     
00140     /* Compute the even rate and return */
00141     *r_even = f_even / f_tot ;
00142     return 0 ;
00143 }
00144 
00145 /*----------------------------------------------------------------------------*/
00151 /*----------------------------------------------------------------------------*/
00152 cpl_image * irplib_oddeven_correct(const cpl_image * in)
00153 {
00154     cpl_image       *   in_real ;
00155     cpl_image       *   in_imag ;
00156     cpl_imagelist   *   freq_i ;
00157     cpl_imagelist   *   freq_i_amp ;
00158     cpl_image       *   cur_im ;
00159     double          *   pcur_im ;
00160     cpl_image       *   cleaned ;
00161     int                 nx, ny ;
00162     cpl_vector      *   hf_med ;
00163 
00164     /* Test entries */
00165     if (in==NULL) return NULL ;
00166 
00167     nx = cpl_image_get_size_x(in) ;
00168     ny = cpl_image_get_size_y(in) ;
00169 
00170     /* Local copy of the input image in DOUBLE */
00171     in_real = cpl_image_cast(in, CPL_TYPE_DOUBLE) ;
00172     in_imag = cpl_image_duplicate(in_real) ;
00173     cpl_image_multiply_scalar(in_imag, 0.0) ;
00174     
00175     /* Apply FFT to input image */
00176     cpl_image_fft(in_real, in_imag, CPL_FFT_DEFAULT) ;
00177 
00178     /* Put the result in an image list */
00179     freq_i = cpl_imagelist_new() ;
00180     cpl_imagelist_set(freq_i, in_real, 0) ;
00181     cpl_imagelist_set(freq_i, in_imag, 1) ;
00182     
00183     /* Convert to amplitude/phase */
00184     freq_i_amp = irplib_oddeven_cube_conv_xy_rtheta(freq_i);
00185     cpl_imagelist_delete(freq_i) ;
00186 
00187     /* Correct the odd-even frequency */
00188     cur_im = cpl_imagelist_get(freq_i_amp, 0) ;
00189     pcur_im = cpl_image_get_data_double(cur_im) ;
00190     /* Odd-even frequency will be replaced by 
00191        the median of the 5 values around */
00192     hf_med = cpl_vector_new(5); 
00193 
00194     cpl_vector_set(hf_med, 0, pcur_im[nx/2 + 1]); 
00195     cpl_vector_set(hf_med, 1, pcur_im[nx/2 + 2]);
00196     cpl_vector_set(hf_med, 2, pcur_im[nx/2 + 3]);
00197     cpl_vector_set(hf_med, 3, pcur_im[nx/2    ]);
00198     cpl_vector_set(hf_med, 4, pcur_im[nx/2  -1]);
00199 
00200     pcur_im[nx / 2 + 1] = cpl_vector_get_median(hf_med);
00201     cpl_vector_delete(hf_med);
00202 
00203     /* Convert to X/Y */
00204     freq_i = irplib_oddeven_cube_conv_rtheta_xy(freq_i_amp) ;
00205     cpl_imagelist_delete(freq_i_amp) ;
00206     /* FFT back to image space */
00207     cpl_image_fft(cpl_imagelist_get(freq_i, 0), cpl_imagelist_get(freq_i, 1), 
00208             CPL_FFT_INVERSE) ;
00209     cleaned = cpl_image_cast(cpl_imagelist_get(freq_i, 0), CPL_TYPE_FLOAT) ;
00210     cpl_imagelist_delete(freq_i) ;
00211     return cleaned ;
00212 }
00213 
00216 /*----------------------------------------------------------------------------*/
00227 /*----------------------------------------------------------------------------*/
00228 static cpl_imagelist * irplib_oddeven_cube_conv_xy_rtheta(
00229         cpl_imagelist   *   cube_in)
00230 {
00231     cpl_imagelist       *   cube_out ;
00232     double                  re, im ;
00233     double                  mod, phase ;
00234     int                     nx, ny, np ;
00235     cpl_image           *   tmp_im ;
00236     double              *   pim1 ;
00237     double              *   pim2 ;
00238     double              *   pim3 ;
00239     double              *   pim4 ;
00240     int                     i, j ;
00241 
00242     /* Error handling : test entries    */
00243     if (cube_in == NULL) return NULL ;
00244     np = cpl_imagelist_get_size(cube_in) ;
00245     if (np != 2) return NULL ;
00246 
00247     /* Initialise */
00248     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00249     pim1 = cpl_image_get_data_double(tmp_im) ;
00250     nx = cpl_image_get_size_x(tmp_im) ;
00251     ny = cpl_image_get_size_y(tmp_im) ;
00252     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00253     pim2 = cpl_image_get_data_double(tmp_im) ;
00254 
00255     /* Allocate cube_out */
00256     cube_out = cpl_imagelist_duplicate(cube_in) ;
00257 
00258     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00259     pim3 = cpl_image_get_data_double(tmp_im) ;
00260     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00261     pim4 = cpl_image_get_data_double(tmp_im) ;
00262     /* Convert */
00263     for (j=0 ; j<ny ; j++) {
00264         for (i=0 ; i<nx ; i++) {
00265             re = (double)pim1[i+j*nx] ;
00266             im = (double)pim2[i+j*nx] ;
00267             mod = (double)(sqrt(re*re + im*im)) ;
00268             if (re != 0.0)
00269                 phase = (double)atan2(im, re) ;
00270             else 
00271                 phase = 0.0 ;
00272             pim3[i+j*nx] = mod ; 
00273             pim4[i+j*nx] = phase ; 
00274         }
00275     }
00276     return cube_out ;
00277 }
00278 
00279 /*----------------------------------------------------------------------------*/
00292 /*----------------------------------------------------------------------------*/
00293 static cpl_imagelist * irplib_oddeven_cube_conv_rtheta_xy(
00294         cpl_imagelist   *   cube_in)
00295 {
00296     cpl_imagelist       *   cube_out ;
00297     double                  re, im ;
00298     double                  mod, phase ;
00299     int                     nx, ny, np ;
00300     cpl_image           *   tmp_im ;
00301     double              *   pim1 ;
00302     double              *   pim2 ;
00303     double              *   pim3 ;
00304     double              *   pim4 ;
00305     int                     i, j ;
00306 
00307     /* Error handling : test entries    */
00308     if (cube_in == NULL) return NULL ;
00309     np = cpl_imagelist_get_size(cube_in) ;
00310     if (np != 2) return NULL ;
00311 
00312     /* Initialise */
00313     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00314     pim1 = cpl_image_get_data_double(tmp_im) ;
00315     nx = cpl_image_get_size_x(tmp_im) ;
00316     ny = cpl_image_get_size_y(tmp_im) ;
00317     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00318     pim2 = cpl_image_get_data_double(tmp_im) ;
00319 
00320     /* Allocate cube_out */
00321     cube_out = cpl_imagelist_duplicate(cube_in) ;
00322 
00323     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00324     pim3 = cpl_image_get_data_double(tmp_im) ;
00325     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00326     pim4 = cpl_image_get_data_double(tmp_im) ;
00327     /* Convert */
00328     for (j=0 ; j<ny ; j++) {
00329         for (i=0 ; i<nx ; i++) {
00330             mod = (double)pim1[i+j*nx] ;
00331             phase = (double)pim2[i+j*nx] ;
00332             re = (double)(mod * cos(phase));
00333             im = (double)(mod * sin(phase));
00334             pim3[i+j*nx] = re ; 
00335             pim4[i+j*nx] = im ; 
00336         }
00337     }
00338     return cube_out ;
00339 }

Generated on 1 Mar 2011 for DETMON Pipeline Reference Manual by  doxygen 1.6.1