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