visir_destripe_body.c

00001 /* $Id: visir_destripe_body.c,v 1.5 2009/03/05 13:21:38 llundin 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: llundin $
00023  * $Date: 2009/03/05 13:21:38 $
00024  * $Revision: 1.5 $
00025  * $Name: visir-3_2_2 $
00026  */
00027 
00028 #define TYPE_ADD(a) CONCAT2X(a, PIXEL_TYPE)
00029 #define TYPE_ADD_CONST(a) CONCAT2X(a, CONCAT2X(PIXEL_TYPE, const))
00030 
00031 /*----------------------------------------------------------------------------*/
00048 /*----------------------------------------------------------------------------*/
00049 static cpl_error_code
00050 TYPE_ADD(visir_destripe_find_max_index)(const cpl_image * self,
00051                                         int per_stripe, int n_per,
00052                                         int dimy, int off_min,
00053                                         double * pmaximum,
00054                                         int * pindex,
00055                                         int * pmax_x, int * pmax_y)
00056 {
00057     const PIXEL_TYPE * pimf = TYPE_ADD_CONST(cpl_image_get_data)(self);
00058     double maximum = 0.0;
00059     int iindex = -1; /* Avoid (false) uninit warning */
00060     int max_x  = -1; /* Avoid (false) uninit warning */
00061     int max_y  = -1; /* Avoid (false) uninit warning */
00062     int i;
00063 
00064 
00065     bug_if(self == NULL);
00066     bug_if(cpl_image_get_type(self) != PIXEL_TYPE_CPL);
00067     bug_if(pmaximum == NULL);
00068     bug_if(pindex == NULL);
00069     bug_if(pmax_x == NULL);
00070     bug_if(pmax_y == NULL);
00071     bug_if(off_min <= 0);
00072     bug_if(pimf == NULL);
00073     bug_if(dimy < 2 * off_min);
00074     bug_if(n_per < 1);
00075 
00076 
00077     /*find the maximum of the 'maxi' vector and the indice of the maximum*/
00078     for (i = 0; i < n_per; i++) {
00079         int istart;
00080         for (istart = 0; istart < dimy - 2 * off_min; istart++) {
00081             int istop = istart + off_min;
00082             int j;
00083             double ilen = (double)off_min;
00084             double sum;
00085             double sum_pix = 0.0;
00086 
00087             for (j = istart; j <= istop; j++)
00088                 sum_pix += pimf[i + j * per_stripe];
00089 
00090             sum = sum_pix * sum_pix;
00091             if (sum > maximum * ilen) {
00092                 maximum = sum / ilen;
00093                 max_x = istart;
00094                 max_y = istop;
00095                 iindex = i;
00096             }
00097 
00098             for (istop++; istop < dimy - off_min; istop++) {
00099                 sum_pix += pimf[i + istop * per_stripe];
00100                 sum = sum_pix * sum_pix;
00101                 ilen += 1.0;
00102                 if (sum > maximum * ilen) {
00103                     maximum = sum / ilen;
00104                     max_x = istart;
00105                     max_y = istop;
00106                     iindex = i;
00107                 }
00108             }
00109         }
00110     }
00111 
00112     *pindex = iindex;
00113     *pmax_x = 1 + max_x;
00114     *pmax_y = 1 + max_y;
00115     *pmaximum = maximum;
00116 
00117     cpl_msg_debug(cpl_func, "Maximum image is image nb %d", 1 + *pindex);
00118     cpl_msg_debug(cpl_func, "Max x : %d   Max y : %d", *pmax_x, *pmax_y);
00119 
00120     end_skip;
00121 
00122     return cpl_error_get_code();
00123 }
00124 
00125 /*----------------------------------------------------------------------------*/
00138 /*----------------------------------------------------------------------------*/
00139 static int TYPE_ADD(visir_destripe_image_one)(cpl_image * self,
00140                                               double      threshold,
00141                                               double      thres_detect,
00142                                               cpl_boolean morpho,
00143                                               cpl_boolean do_horizontal)
00144 {
00145 
00146     /*period of stripe*/
00147     const int           per_stripe=16;
00148     const int           img_border = 2 + per_stripe;
00149     const int           dimx = cpl_image_get_size_y(self); /* Rotated */
00150     const int           dimy = cpl_image_get_size_x(self);
00151     cpl_image       *   bkgd = NULL;
00152     cpl_image       *   imabc = NULL;
00153     cpl_image       *   image_extract = NULL;
00154     double              noise, bgd;
00155     cpl_image       *   ima_med = NULL;
00156     cpl_image       *   supp_total = NULL;
00157     cpl_mask        *   supp_total_mask = NULL;
00158     cpl_image       *   imf = NULL;
00159     cpl_image       *   suppf = NULL;
00160     int                 n_per;
00161     /*minimum size of stripe*/
00162     int                 off_min=20;
00163     cpl_image       *   stripes = NULL;
00164     double              maximum = 0.0; /* Avoid (false) uninit warning */
00165     int                 max_x   = 0;   /* Avoid (false) uninit warning */
00166     int                 max_y   = 0;   /* Avoid (false) uninit warning */
00167     int                 indice  = 0;   /* Avoid (false) uninit warning */
00168     int                 ind;
00169     int                 i,j;
00170     cpl_vector      *   val_stripe = NULL;
00171     int                 isdone = 0;
00172 
00173 
00174     bug_if(0);
00175     bug_if(cpl_image_get_type(self) != PIXEL_TYPE_CPL);
00176 
00177     /* Low-pass filter 29x29 */
00178     bkgd = TYPE_ADD(cpl_image_wrap)(dimy, dimx,
00179                                     cpl_malloc(dimx*dimy*sizeof(PIXEL_TYPE)));
00180 
00181     bug_if (irplib_image_filter(bkgd, self,  14, 14, IRPLIB_FILTER_AVERAGE
00182                                 | IRPLIB_FILTER_BORDER_FILTER));
00183 
00184     imabc = cpl_image_subtract_create(self, bkgd);
00185     cpl_image_delete(bkgd);
00186     bkgd = NULL;
00187     bug_if(0);
00188 
00189     if (do_horizontal) {
00190         /* Stripes have to be vertical
00191            - turn the image to destripe horizontally */
00192         bug_if (cpl_image_turn(imabc, -1));
00193     }
00194 
00195     /* Extract an image to calculate the stdev and background  */
00196     /* because of the low quality borders  */
00197     image_extract = cpl_image_extract(imabc, img_border, img_border,
00198                                       dimx-img_border, dimy-img_border);
00199 
00200     bug_if (image_extract == NULL);
00201 
00202     noise = visir_image_sigma_clip(image_extract, &bgd);
00203     skip_if (0);
00204 
00205     cpl_image_delete(image_extract);
00206     image_extract = NULL;
00207 
00208     cpl_msg_debug(cpl_func, "imabc noise = %g", noise);
00209     cpl_msg_debug(cpl_func, "imabc bgd   = %g", bgd);
00210     bug_if(cpl_image_subtract_scalar(imabc, bgd));
00211 
00212     /* Create supp_total */
00213     ima_med = TYPE_ADD(cpl_image_wrap)(dimy, dimx,
00214                                    cpl_malloc(dimx*dimy*sizeof(PIXEL_TYPE)));
00215     bug_if(irplib_image_filter(ima_med, imabc, 1, 1, IRPLIB_FILTER_MEDIAN
00216                                | IRPLIB_FILTER_BORDER_FILTER));
00217 
00218     /*CAREFUL:ima_med changed in abs(ima_med)*/
00219     bug_if (cpl_image_abs(ima_med));
00220     supp_total_mask = cpl_mask_threshold_image_create(ima_med,
00221                                                       -0.5 * DBL_MAX,
00222                                                       thres_detect * noise);
00223 
00224     bug_if (supp_total_mask == NULL);
00225     cpl_image_delete(ima_med);
00226     ima_med = NULL;
00227 
00228     supp_total = cpl_image_new_from_mask(supp_total_mask);
00229 
00230     bug_if(cpl_image_save(supp_total, "supp_total_1.fits", CPL_BPP_8_UNSIGNED,
00231                           NULL, CPL_IO_DEFAULT));
00232 
00233     if (morpho) {
00234 
00235         bug_if(visir_destripe_mask(supp_total_mask));
00236 
00237         cpl_image_delete(supp_total); /* :-( */
00238         supp_total = cpl_image_new_from_mask(supp_total_mask);
00239 
00240         bug_if(cpl_image_save(supp_total, "supp_total_2.fits",
00241                               CPL_BPP_8_UNSIGNED, NULL, CPL_IO_DEFAULT));
00242     }
00243 
00244     cpl_mask_delete(supp_total_mask);
00245     supp_total_mask = NULL;
00246 
00247     /* imabc *= supp_total / noise */
00248     bug_if(cpl_image_multiply(imabc, supp_total));
00249     bug_if(cpl_image_divide_scalar(imabc, noise));
00250 
00251     /* Extract the 16 first columns */
00252     /*sum of 16 columns by 16 columns*/
00253     imf   = cpl_image_extract(imabc,      1, 1, per_stripe, dimy);
00254     bug_if( imf == NULL);
00255 
00256     suppf = cpl_image_extract(supp_total, 1, 1, per_stripe, dimy);
00257     bug_if( suppf == NULL);
00258 
00259     n_per=dimx/per_stripe;
00260     for (i=1; i <n_per; i ++)
00261     {
00262         cpl_image * suppf_tmp;
00263         cpl_image * imf_tmp =
00264             cpl_image_extract(imabc, (per_stripe*i)+1, 1,
00265                               per_stripe*(i+1), dimy);
00266         cpl_error_code error = cpl_image_add(imf, imf_tmp);
00267 
00268         cpl_image_delete(imf_tmp);
00269 
00270         bug_if(error);
00271 
00272         suppf_tmp = cpl_image_extract(supp_total, (per_stripe*i)+1, 1,
00273                                      per_stripe*(i+1), dimy);
00274         error = cpl_image_add(suppf, suppf_tmp);
00275         cpl_image_delete(suppf_tmp);
00276         bug_if(error);
00277     }
00278     bug_if(cpl_image_divide_scalar(imf, n_per));
00279 
00280     /* Set to 0 in imf the pixels that are equal to 0 in suppf */
00281     bug_if(cpl_image_threshold(suppf, -0.5*DBL_MAX, 1, 0, 1));
00282     bug_if(cpl_image_multiply(imf, suppf));
00283     cpl_image_delete(suppf);
00284     suppf = NULL;
00285 
00286     bug_if (TYPE_ADD(visir_destripe_find_max_index)(imf, per_stripe, n_per, dimy,
00287                                                     off_min, &maximum, &indice,
00288                                                     &max_x, &max_y));
00289     cpl_image_delete(imf);
00290     imf = NULL;
00291 
00292     /* If maximum too low, no stripes found */
00293     if (maximum < threshold) {
00294         cpl_msg_info(cpl_func,"No stripes found: maximum=%g < treshold=%g",
00295                      maximum, threshold);
00296         isdone = 1;
00297     } else {
00298 
00299         PIXEL_TYPE       * pima;
00300         const int   * psupp_total;
00301         const PIXEL_TYPE * pimabc;
00302         double        stripe_median;
00303 
00304         cpl_msg_info(cpl_func,"Destriping: maximum=%g < treshold=%g",
00305                      maximum, threshold);
00306 
00307         stripes = cpl_image_new(dimx, dimy, PIXEL_TYPE_CPL);
00308 
00309         pima = TYPE_ADD(cpl_image_get_data)(stripes);
00310         pimabc = TYPE_ADD_CONST(cpl_image_get_data)(imabc);
00311         psupp_total = cpl_image_get_data_int_const(supp_total);
00312 
00313 
00314         /*not avergae stripe: default program
00315           for (i=0; i<n_per; i++)
00316           {
00317           value=0.;
00318           N=0;
00319           for (j=max_x; j<max_y+1; j++)
00320           {
00321           ind = i*per_stripe+indice + j * dimx;
00322           if (psupp_total[ind] == 1)
00323           {
00324           value += pimabc[ind];
00325           N++;
00326           }
00327           }
00328           if (N==0)
00329           {
00330           printf("i --- %d\n", i);
00331           value = 0;
00332           } else value /= N;
00333 
00334           for (j=max_x; j<max_y+1; j++)
00335           {
00336           pima[i*per_stripe+indice+j*dimx] = value;
00337           }
00338           }
00339 
00340         */
00341 
00342 
00343         /*average stripes: take the median*/
00344         val_stripe = cpl_vector_new(n_per);
00345 
00346         for (i=0; i < n_per; i++) {
00347             double value = 0.0;
00348             int    N = 0;
00349             for (j=max_x; j<max_y+1; j++)
00350                 {
00351                     ind = i*per_stripe+indice + j * dimx;
00352                     if (psupp_total[ind] == 1)
00353                         {
00354                             value += pimabc[ind];
00355                             N++;
00356                         }
00357                 }
00358             if (N > 0) value /= N;
00359             cpl_vector_set(val_stripe, i, value);
00360         }
00361         stripe_median = cpl_vector_get_median(val_stripe);
00362         cpl_vector_delete(val_stripe);
00363         val_stripe = NULL;
00364 
00365         bug_if(0);
00366 
00367         for (i=0; i<n_per; i++)
00368             {
00369                 for (j=max_x; j<max_y+1; j++)
00370                     {
00371                         pima[i*per_stripe+indice+j*dimx] = stripe_median;
00372                     }
00373             }
00374 
00375 
00376         if (do_horizontal) {
00377             bug_if(cpl_image_turn(stripes, 1));
00378         }
00379 
00380         /*to recompensate for the normalisation*/
00381         bug_if(cpl_image_multiply_scalar(stripes, noise));
00382 
00383         bug_if(cpl_image_subtract(self, stripes));
00384     }
00385 
00386     bug_if(0);
00387 
00388     end_skip;
00389 
00390     cpl_image_delete(imabc);
00391     cpl_vector_delete(val_stripe);
00392     cpl_image_delete(imf);
00393     cpl_image_delete(suppf);
00394     cpl_mask_delete(supp_total_mask);
00395     cpl_image_delete(ima_med);
00396     cpl_image_delete(image_extract);
00397     cpl_image_delete(stripes);
00398     cpl_image_delete(bkgd);
00399     cpl_image_delete(supp_total);
00400 
00401     return cpl_error_get_code() ? -1 : isdone;
00402 }
00403 

Generated on Fri Jul 3 11:15:23 2009 for VISIR Pipeline Reference Manual by  doxygen 1.5.8