irplib_slitpos.c

00001 /* $Id: irplib_slitpos.c,v 1.15 2006/11/29 13:22:59 yjung 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: yjung $
00023  * $Date: 2006/11/29 13:22:59 $
00024  * $Revision: 1.15 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /* The IRPLIB-based application may have checked for the availability of
00033    memrchr() in which case the macro HAVE_DECL_MEMRCHR is defined as either
00034    0 or 1. Without checks it is assumed that the function is not available.
00035    With a suitable version of autoconf the macro can be defined with this
00036    entry in configure.ac:
00037    AC_CHECK_DECLS([memrchr])
00038 */
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Includes
00042  -----------------------------------------------------------------------------*/
00043 
00044 #include <math.h>
00045 #include <assert.h>
00046 #include <cpl.h>
00047 
00048 #include "irplib_slitpos.h"
00049 #include "irplib_flat.h"
00050 
00051 /*-----------------------------------------------------------------------------
00052                                 Defines
00053  -----------------------------------------------------------------------------*/
00054 
00055 #ifndef IRPLIB_SLITPOS_KERNEL_SIZE_Y
00056 #define IRPLIB_SLITPOS_KERNEL_SIZE_Y      5
00057 #endif
00058 
00059 #ifndef IRPLIB_SLITPOS_MAX_EROSION
00060 #define IRPLIB_SLITPOS_MAX_EROSION     1024
00061 #endif
00062 
00063 /*-----------------------------------------------------------------------------
00064                             Functions prototypes
00065  -----------------------------------------------------------------------------*/
00066 
00067 static cpl_error_code irplib_slitpos_find_edges_one_line(const cpl_image *,
00068                                                          int, int *, int *);
00069 static cpl_error_code irplib_slitpos_find_vert_slit_ends(const cpl_image *,
00070                                                          int, int *, int *);
00071 static cpl_error_code irplib_slitpos_find_vert_pos(const cpl_image *, int,
00072                                                    int *);
00073 
00074 /*----------------------------------------------------------------------------*/
00078 /*----------------------------------------------------------------------------*/
00079 
00081 /*----------------------------------------------------------------------------*/
00107 /*----------------------------------------------------------------------------*/
00108 cpl_table * irplib_slitpos_analysis(const cpl_image * imslit,
00109                                     int               slit_max_width,
00110                                     double          * slit_flux)
00111 {
00112     const int       slit_size = cpl_image_get_size_y(imslit);
00113     int             slit_length;
00114     int             slit_pos;
00115     cpl_image   *   filtered;
00116     cpl_matrix  *   kernel;
00117     cpl_image   *   thin_im;
00118     int             slit_top_y = 0; /* Avoid (false) uninit warning */
00119     int             slit_bot_y = 0; /* Avoid (false) uninit warning */
00120     cpl_table   *   self;
00121     double      *   slit_y,
00122                 *   slit_x_l,
00123                 *   slit_x_r;
00124     double      *   coeff_r;
00125     double      *   coeff_l;
00126     int             i;
00127     cpl_error_code error = CPL_ERROR_NONE;
00128 
00129     /* Initialize */
00130     if (slit_flux != NULL) *slit_flux = 0.0 ;
00131     
00132     /* Filter the input image to 'erase' the bad pixels */
00133     kernel = cpl_matrix_new(3, 3);
00134     cpl_ensure(!cpl_matrix_fill(kernel, 1.0), cpl_error_get_code(), NULL);
00135 
00136     filtered = cpl_image_filter_median(imslit, kernel);
00137     cpl_matrix_delete(kernel);
00138 
00139     if (filtered == NULL) {
00140         cpl_msg_error(cpl_func, "Could not filter the image");
00141         cpl_ensure(0, cpl_error_get_code(), NULL);
00142     }
00143 
00144     /* Find the position of the slit */
00145     if (irplib_slitpos_find_vert_pos(filtered, slit_max_width/2, &slit_pos)) {
00146         cpl_msg_error(cpl_func, "Could not find the slit position");
00147         cpl_ensure(0, cpl_error_get_code(), NULL);
00148     }
00149 
00150     /* Extract a thin image containing the slit */
00151     if ((thin_im = cpl_image_extract(filtered,
00152                                      slit_pos-slit_max_width/2,
00153                                      1,
00154                                      slit_pos+slit_max_width/2,
00155                                      slit_size)) == NULL) {
00156         cpl_msg_error(cpl_func, "Could not extract the %d pixel thin image "
00157                       "around position %d", slit_max_width, slit_pos);
00158         cpl_image_delete(filtered);
00159         cpl_ensure(0, cpl_error_get_code(), NULL);
00160     }
00161 
00162     /* Find the ends of the slit */
00163     if ((irplib_slitpos_find_vert_slit_ends(thin_im,
00164                                             IRPLIB_SLITPOS_KERNEL_SIZE_Y,
00165                                             &slit_bot_y,
00166                                             &slit_top_y))) {
00167         cpl_msg_error(cpl_func, "Could not find the ends of the slit at %s",
00168                       cpl_error_get_where());
00169         cpl_image_delete(filtered);
00170         cpl_image_delete(thin_im);
00171         cpl_ensure(0, cpl_error_get_code(), NULL);
00172     }
00173     cpl_image_delete(thin_im);
00174 
00175     /* Extract an image with exactly the slit */
00176     thin_im = cpl_image_extract(filtered,
00177                                 slit_pos-slit_max_width/2,
00178                                 slit_bot_y,
00179                                 slit_pos+slit_max_width/2,
00180                                 slit_top_y);
00181     cpl_image_delete(filtered);
00182 
00183     if (thin_im == NULL) {
00184         cpl_msg_error(cpl_func, "Could not extract the thin image from %d to %d",
00185                       slit_bot_y, slit_top_y);
00186         cpl_ensure(0, cpl_error_get_code(), NULL);
00187     }
00188 
00189     slit_length = 1 + slit_top_y - slit_bot_y;
00190 
00191     /* Allocate some arrays */
00192     slit_y = cpl_malloc(slit_length * sizeof(double));
00193     slit_x_l = cpl_malloc(slit_length * sizeof(double));
00194     slit_x_r = cpl_malloc(slit_length * sizeof(double));
00195     
00196     /* Find the edges of the slit */
00197     for (i=0 ; i<slit_length ; i++) {
00198         int right_pos = 0; /* Avoid (false) uninit warning */
00199         int left_pos  = 0; /* Avoid (false) uninit warning */
00200 
00201         if (irplib_slitpos_find_edges_one_line(thin_im,
00202                                                 i,
00203                                                 &left_pos,
00204                                                 &right_pos)) {
00205             cpl_msg_error(cpl_func, "cannot find the edges of the [%d]th line", 
00206                     i+1);
00207             cpl_image_delete(thin_im);
00208             return NULL;
00209         }
00210 
00211         /* Update the slit_flux */
00212         if (slit_flux != NULL) {
00213             *slit_flux += cpl_image_get_flux_window(thin_im, left_pos+1,
00214                     i+1, right_pos+1, i+1) ;
00215         }
00216         
00217         /* Store the edges for the fit */
00218         slit_x_l[i] = (double)left_pos;
00219         slit_x_r[i] = (double)right_pos;
00220         slit_y[i]   = (double)(i+slit_bot_y-1);
00221     }
00222     cpl_image_delete(thin_im);
00223 
00224     /* Linear regression to find the edges */
00225     coeff_l = irplib_flat_fit_slope_robust(slit_y, slit_x_l, slit_length);
00226     coeff_r = irplib_flat_fit_slope_robust(slit_y, slit_x_r, slit_length);
00227     cpl_free(slit_y);
00228     cpl_free(slit_x_l);
00229     cpl_free(slit_x_r);
00230 
00231     /* Allocate the table containing the results */
00232     self = cpl_table_new(slit_length);
00233     error |= cpl_table_new_column(self, "SLIT_Y",      CPL_TYPE_INT);
00234     error |= cpl_table_new_column(self, "SLIT_LEFT",   CPL_TYPE_DOUBLE);
00235     error |= cpl_table_new_column(self, "SLIT_CENTER", CPL_TYPE_DOUBLE);
00236     error |= cpl_table_new_column(self, "SLIT_RIGHT",  CPL_TYPE_DOUBLE);
00237 
00238     error |= cpl_table_set_column_unit(self, "SLIT_Y", "pixel");
00239     error |= cpl_table_set_column_unit(self, "SLIT_LEFT", "pixel");
00240     error |= cpl_table_set_column_unit(self, "SLIT_CENTER", "pixel");
00241     error |= cpl_table_set_column_unit(self, "SLIT_RIGHT", "pixel");
00242 
00243     cpl_ensure(!error, cpl_error_get_code(), NULL);
00244 
00245     /* Rewrite the edges in the out table, and write the center */
00246     for (i=0 ; i < slit_length ; i++) {
00247         const int    islity = i + slit_bot_y;
00248         const double dslit  = slit_pos - slit_max_width / 2.0;
00249         const double dleft  = coeff_l[0] + coeff_l[1] * (double)islity + dslit;
00250         const double dright = coeff_r[0] + coeff_r[1] * (double)islity + dslit;
00251         const double dcent  = 0.5 * (dleft + dright);
00252 
00253         if (cpl_table_set_int(self,    "SLIT_Y",      i, islity)) break;
00254         if (cpl_table_set_double(self, "SLIT_LEFT",   i, dleft))  break;
00255         if (cpl_table_set_double(self, "SLIT_RIGHT",  i, dright)) break;
00256         if (cpl_table_set_double(self, "SLIT_CENTER", i, dcent))  break;
00257     }
00258 
00259     cpl_free(coeff_r);
00260     cpl_free(coeff_l);
00261 
00262     if (i != slit_length) {
00263         cpl_table_delete(self);
00264         cpl_ensure(0, cpl_error_get_code(), NULL);
00265     }
00266 
00267     return self;
00268 }
00269 
00272 /*----------------------------------------------------------------------------*/
00284 /*----------------------------------------------------------------------------*/
00285 static cpl_error_code irplib_slitpos_find_edges_one_line(const cpl_image * self,
00286                                                          int          line_pos,
00287                                                          int        * left_pos,
00288                                                          int        * right_pos)
00289 {
00290     const int     size_x = cpl_image_get_size_x(self);
00291     const float * pself;
00292     double        threshold;
00293     int           i;
00294 
00295     cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00296     cpl_ensure_code(cpl_image_get_type(self) == CPL_TYPE_FLOAT,
00297                     CPL_ERROR_INVALID_TYPE);
00298 
00299     pself = cpl_image_get_data_float(self);
00300 
00301     /* Find the threshold */
00302     threshold = cpl_image_get_mean_window(self, 1, line_pos+1, size_x,
00303                                           line_pos+1);
00304 
00305     /* Detect the left edge */
00306     i = 0;
00307     while (i < size_x && pself[line_pos*size_x+i] < threshold) i++;
00308     *left_pos = i;
00309 
00310     /* Detect the right edge */
00311     i = size_x - 1;
00312     while (i >= 0 && pself[line_pos*size_x+i] < threshold) i--;
00313     *right_pos = i;
00314 
00315     return CPL_ERROR_NONE;
00316 }
00317 
00318 /*----------------------------------------------------------------------------*/
00329 /*----------------------------------------------------------------------------*/
00330 static cpl_error_code irplib_slitpos_find_vert_slit_ends(const cpl_image * self,
00331                                                          int        kernel_size,
00332                                                          int      * bot_slit_y,
00333                                                          int      * top_slit_y)
00334 {
00335     cpl_mask         * binary;
00336     cpl_matrix       * kernel;
00337     cpl_image        * label_image;
00338     int                nobj, erosions_nb;
00339     const int          size_x = cpl_image_get_size_x(self);
00340     const int          npix = size_x * cpl_image_get_size_y(self);
00341     const cpl_binary * pbinary;
00342     const cpl_binary * pfind;
00343     int                i, itop, ibot;
00344 
00345 
00346     cpl_ensure_code(size_x > 0, cpl_error_get_code());
00347 
00348     /* Threshold to have a binary image */
00349     binary = cpl_mask_threshold_image_create(self, cpl_image_get_mean(self),
00350                                              cpl_image_get_max(self));
00351     cpl_ensure_code(binary != NULL, cpl_error_get_code());
00352 
00353     /* Define the kernel for morpho operations */
00354     kernel = cpl_matrix_new(kernel_size, 1);
00355 
00356     /* Erode until there is 1 object left in the image */
00357     label_image = cpl_image_labelise_mask_create(binary, &nobj);
00358     cpl_image_delete(label_image);
00359 
00360     if (label_image == NULL || kernel == NULL || cpl_matrix_fill(kernel, 1.0)) {
00361         cpl_mask_delete(binary);
00362         cpl_matrix_delete(kernel);
00363         cpl_ensure_code(0, cpl_error_get_code());
00364     }
00365 
00366     for (erosions_nb = 0; erosions_nb < IRPLIB_SLITPOS_MAX_EROSION && nobj > 1;
00367          erosions_nb++) {
00368 
00369         /* Should not be possible to break from this loop */
00370         if (cpl_mask_erosion(binary, kernel)) break;
00371 
00372         label_image = cpl_image_labelise_mask_create(binary, &nobj);
00373         if (label_image == NULL) break; /* Assuming nobj was not set to 1 */
00374         cpl_image_delete(label_image);
00375     }
00376 
00377     if (nobj > 1) {
00378         cpl_mask_delete(binary);
00379         cpl_matrix_delete(kernel);
00380         if (erosions_nb >= IRPLIB_SLITPOS_MAX_EROSION) {
00381             cpl_msg_error(cpl_func, "Number of erosions reached a limit of %d "
00382                           "with %d possible slits left",
00383                           IRPLIB_SLITPOS_MAX_EROSION, nobj);
00384             cpl_ensure_code(0, CPL_ERROR_CONTINUE);
00385         }
00386         cpl_ensure_code(0, cpl_error_get_code());
00387     } else if (nobj < 1) {
00388         cpl_mask_delete(binary);
00389         cpl_matrix_delete(kernel);
00390         if (erosions_nb == 0)
00391             cpl_msg_error(cpl_func, "No slit could be detected across %d "
00392                           "pixels", size_x);
00393         else 
00394             cpl_msg_error(cpl_func, "The last of %d erosions removed all the "
00395                           "possible slits", erosions_nb);
00396         cpl_ensure_code(0, CPL_ERROR_DATA_NOT_FOUND);
00397     }
00398 
00399     /* Reconstruct the slit with dilations */
00400     for (i=0 ; i < erosions_nb ; i++) {
00401         if (cpl_mask_dilation(binary, kernel)) break;
00402     }
00403     cpl_matrix_delete(kernel);
00404 
00405     if (i != erosions_nb) {
00406         cpl_msg_error(cpl_func, "Dilation number %d out of %d failed",
00407                       i, erosions_nb);
00408         cpl_mask_delete(binary);
00409         cpl_ensure_code(0, cpl_error_get_code());
00410     }
00411 
00412     /* Find the ends of the slit */
00413     pbinary = cpl_mask_get_data(binary);
00414     assert( pbinary != NULL );
00415 
00416     pfind = memchr(pbinary, CPL_BINARY_1, (size_t)npix);
00417     assert( pfind != NULL );
00418 
00419     ibot = (int)(pfind - pbinary);
00420 
00421 #if defined HAVE_DECL_MEMRCHR && HAVE_DECL_MEMRCHR == 1
00422     /* FIXME: Not tested */
00423     pfind = memrchr(pfind, CPL_BINARY_1, (size_t)(npix - ibot));
00424     assert( pfind != NULL );
00425 
00426     itop = (int)(pfind - pbinary);
00427 #else
00428 
00429     itop = npix - 1;
00430     while (itop > ibot && pbinary[itop] != CPL_BINARY_1) itop--;
00431 
00432 #endif
00433 
00434     *bot_slit_y = 1 + ibot / size_x;
00435     *top_slit_y = 1 + itop / size_x;
00436 
00437     cpl_msg_info(cpl_func, "Detected %d-pixel slit from pixel %d to %d "
00438                  "using %d erosions/dilations", cpl_mask_count(binary),
00439                  *bot_slit_y, *top_slit_y, erosions_nb);
00440 
00441     cpl_mask_delete(binary);
00442 
00443     /* Should really be an assert() */
00444     cpl_ensure_code(ibot <= itop, CPL_ERROR_DATA_NOT_FOUND);
00445 
00446     return CPL_ERROR_NONE;
00447 }
00448 
00449 /*----------------------------------------------------------------------------*/
00459 /*----------------------------------------------------------------------------*/
00460 static cpl_error_code irplib_slitpos_find_vert_pos(const cpl_image * self,
00461                                                    int               xwidth,
00462                                                    int             * slit_pos)
00463 {
00464     const int      size_x = cpl_image_get_size_x(self);
00465     cpl_image    * image1D;
00466     int            yone;
00467     cpl_error_code error;
00468 
00469 
00470     /* Collapse the image to a horizontal 1D image */
00471     image1D = cpl_image_collapse_create(self, 0);
00472 
00473     cpl_ensure_code(image1D != NULL, cpl_error_get_code());
00474 
00475     /* Search the max of the 1D image to identify the slit position */
00476     error = cpl_image_get_maxpos_window(image1D, 1+xwidth, 1, size_x-xwidth,
00477                                         1, slit_pos, &yone);
00478 
00479     cpl_image_delete(image1D);
00480 
00481     cpl_ensure_code(!error, error);
00482 
00483     return CPL_ERROR_NONE;
00484 }

Generated on Wed Jan 17 08:33:41 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4