visir_destripe.c

00001 /* $Id: visir_destripe.c,v 1.14 2010/07/26 13:55:51 llundin Exp $
00002  *
00003  * This file is part of the VISIR Pipeline
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: 2010/07/26 13:55:51 $
00024  * $Revision: 1.14 $
00025  * $Name: visir-3_5_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "visir_destripe.h"
00037 #include "visir_utils.h"
00038 
00039 /*-----------------------------------------------------------------------------
00040                           Private function prototypes
00041  -----------------------------------------------------------------------------*/
00042 
00043 static cpl_error_code visir_destripe_mask(cpl_mask *);
00044 
00045 static int visir_destripe_image_one_float(cpl_image *, double, double,
00046                                           cpl_boolean, cpl_boolean);
00047 static cpl_error_code visir_destripe_find_max_index_float(const cpl_image *,
00048                                                           int, int,
00049                                                           int, int, double *,
00050                                                           int *, int *, int *);
00051 
00052 static int visir_destripe_image_one_double(cpl_image *, double, double,
00053                                            cpl_boolean, cpl_boolean);
00054 static cpl_error_code visir_destripe_find_max_index_double(const cpl_image *,
00055                                                            int, int,
00056                                                            int, int, double *,
00057                                                            int *, int *, int *);
00058 
00059 /*----------------------------------------------------------------------------*/
00063 /*----------------------------------------------------------------------------*/
00064 
00067 /*----------------------------------------------------------------------------*/
00081 /*----------------------------------------------------------------------------*/
00082 static
00083 cpl_error_code visir_destripe_image(cpl_image * self, int niter,
00084                                     double threshold, double thres_detect,
00085                                     cpl_boolean morpho)
00086 {
00087 
00088     cpl_boolean do_horizontal = CPL_TRUE;
00089     cpl_boolean did_find = CPL_FALSE;
00090 
00091     bug_if(self == NULL);
00092     bug_if(niter < 1);
00093 
00094     do {
00095 
00096         const char * sdir = do_horizontal ? "horizontal" : "vertical";
00097         int j = 0; /* Avoid (false) uninit warning */
00098 
00099         switch (cpl_image_get_type(self)) {
00100         case CPL_TYPE_DOUBLE:
00101             for (j = 0; j < niter; j++) {
00102                 if (visir_destripe_image_one_double(self, threshold,
00103                                                     thres_detect, morpho,
00104                                                     do_horizontal)) break;
00105             }
00106             break;
00107         case CPL_TYPE_FLOAT:
00108             for (j = 0; j < niter; j++) {
00109                 if (visir_destripe_image_one_float(self, threshold,
00110                                                    thres_detect, morpho,
00111                                                    do_horizontal)) break;
00112             }
00113             break;
00114         default:
00115             bug_if( 1 );
00116         }
00117 
00118         if (j == 0) {
00119             cpl_msg_info(cpl_func, "No %s stripes found", sdir);
00120         } else if (j < niter) {
00121             did_find = CPL_TRUE;
00122             cpl_msg_info(cpl_func, "No more %s stripes found in iteration %d",
00123                          sdir, j+1);
00124         } else {
00125             did_find = CPL_TRUE;
00126             cpl_msg_info(cpl_func, "Stopped %s destriping after %d iterations",
00127                          sdir, niter);
00128         }
00129 
00130         skip_if(0);
00131 
00132         do_horizontal = !do_horizontal;
00133     } while (!did_find && !do_horizontal);
00134 
00135     end_skip;
00136 
00137     return cpl_error_get_code();
00138 
00139 }
00140 
00144 /*----------------------------------------------------------------------------*/
00152 /*----------------------------------------------------------------------------*/
00153 static cpl_error_code visir_destripe_mask(cpl_mask * self)
00154 {
00155 
00156     const int  niter  = 3;
00157     cpl_mask * kernel = cpl_mask_new(3, 5);
00158     cpl_mask * copy   = cpl_mask_new(cpl_mask_get_size_x(self),
00159                                      cpl_mask_get_size_y(self));
00160     int        i;
00161 
00162     bug_if(0);
00163 
00164     /* self = 1-(closing(1-self)) */
00165     cpl_mask_not(self);
00166 
00167     /* Fill the kernel */
00168     cpl_mask_not(kernel);
00169 
00170     bug_if(cpl_mask_filter(self, self, kernel,
00171                            CPL_FILTER_CLOSING, CPL_BORDER_ZERO));
00172 
00173     cpl_mask_delete(kernel);
00174 
00175     /* Create the new kernel */
00176     kernel = cpl_mask_new(5, 3);
00177     cpl_mask_not(kernel);
00178     cpl_mask_set(kernel, 1, 1, CPL_BINARY_0);
00179     cpl_mask_set(kernel, 5, 1, CPL_BINARY_0);
00180     cpl_mask_set(kernel, 1, 3, CPL_BINARY_0);
00181     cpl_mask_set(kernel, 5, 3, CPL_BINARY_0);
00182     /* self = 1-(dilation(1-self)) */
00183     for (i = 0; i < 2*(niter/2); i += 2) {
00184         bug_if(cpl_mask_filter(copy, self, kernel,
00185                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00186         bug_if(cpl_mask_filter(self, copy, kernel,
00187                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00188     }
00189     for (; i < niter; i++) { /* Last iteration w. copy, when niter is odd */
00190         bug_if(cpl_mask_filter(copy, self, kernel,
00191                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00192         bug_if(cpl_mask_copy(self, copy, 1, 1));
00193     }
00194 
00195     cpl_mask_delete(kernel);
00196 
00197     /* Create the new kernel */
00198     kernel = cpl_mask_new(5, 5); /* Initialized to zero */
00199     cpl_mask_set(kernel, 3, 1, CPL_BINARY_1);
00200     cpl_mask_set(kernel, 2, 2, CPL_BINARY_1);
00201     cpl_mask_set(kernel, 3, 2, CPL_BINARY_1);
00202     cpl_mask_set(kernel, 4, 2, CPL_BINARY_1);
00203     cpl_mask_set(kernel, 1, 3, CPL_BINARY_1);
00204     cpl_mask_set(kernel, 2, 3, CPL_BINARY_1);
00205     cpl_mask_set(kernel, 3, 3, CPL_BINARY_1);
00206     cpl_mask_set(kernel, 4, 3, CPL_BINARY_1);
00207     cpl_mask_set(kernel, 5, 3, CPL_BINARY_1);
00208     cpl_mask_set(kernel, 2, 4, CPL_BINARY_1);
00209     cpl_mask_set(kernel, 3, 4, CPL_BINARY_1);
00210     cpl_mask_set(kernel, 4, 4, CPL_BINARY_1);
00211     cpl_mask_set(kernel, 3, 5, CPL_BINARY_1);
00212     /* self = 1-(dilation(1-self)) */
00213     for (i = 0; i < 2*(niter/2); i += 2) {
00214         bug_if(cpl_mask_filter(copy, self, kernel,
00215                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00216         bug_if(cpl_mask_filter(self, copy, kernel,
00217                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00218     }
00219     for (; i < niter; i++) { /* Last iteration w. copy, when niter is odd */
00220         bug_if(cpl_mask_filter(copy, self, kernel,
00221                                CPL_FILTER_DILATION, CPL_BORDER_ZERO));
00222         bug_if(cpl_mask_copy(self, copy, 1, 1));
00223     }
00224 
00225     bug_if(cpl_mask_not(self));
00226 
00227     end_skip;
00228 
00229     cpl_mask_delete(copy);
00230     cpl_mask_delete(kernel);
00231 
00232     return cpl_error_get_code();
00233 }
00234 
00235 
00236 
00237 /* These macros are needed for support of the different pixel types */
00238 
00239 #define CONCAT(a,b) a ## _ ## b
00240 #define CONCAT2X(a,b) CONCAT(a,b)
00241 
00242 #define PIXEL_TYPE double
00243 #define PIXEL_TYPE_CPL CPL_TYPE_DOUBLE
00244 #include "visir_destripe_body.c"
00245 #undef PIXEL_TYPE
00246 #undef PIXEL_TYPE_CPL
00247 
00248 #define PIXEL_TYPE float
00249 #define PIXEL_TYPE_CPL CPL_TYPE_FLOAT
00250 #include "visir_destripe_body.c"
00251 #undef PIXEL_TYPE
00252 #undef PIXEL_TYPE_CPL

Generated on Mon Feb 6 15:23:49 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8