1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*
* This file is part of the HDRL
* Copyright (C) 2013 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "hdrl_bpm_utils.h"
#include "hdrl_image.h"
#include <cpl.h>
#include <math.h>
/*----------------------------------------------------------------------------*/
/**
@defgroup hdrl_bpm_utils_test Testing of the HDRL bpm_utils
*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/**
@brief Check hdrl_bpm_to_mask() in various conditions
@return cpl_error_code
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code hdrl_bpm_test_bpm_to_mask(void)
{
cpl_size nx = 20;
cpl_size ny = 20;
{
cpl_mask * mask = hdrl_bpm_to_mask(NULL, 0);
cpl_test_error(CPL_ERROR_NULL_INPUT);
cpl_test_null(mask);
}
/* non int input */
{
cpl_image * bpm = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
cpl_mask * mask = hdrl_bpm_to_mask(bpm, 0);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
cpl_test_null(mask);
cpl_image_delete(bpm);
/* too big mask */
bpm = cpl_image_new(nx, ny, CPL_TYPE_INT);
mask = hdrl_bpm_to_mask(bpm, ~0LLU);
cpl_test_null(mask);
cpl_test_error(CPL_ERROR_UNSUPPORTED_MODE);
cpl_image_delete(bpm);
}
/* empty bpm */
{
cpl_image * bpm = cpl_image_new(nx, ny, CPL_TYPE_INT);
cpl_mask * mask = hdrl_bpm_to_mask(bpm, 0);
cpl_test_nonnull(mask);
cpl_test_eq(cpl_mask_count(mask), 0);
cpl_image_delete(bpm);
cpl_mask_delete(mask);
}
/* range of codes */
{
cpl_image * bpm = cpl_image_new(nx, ny, CPL_TYPE_INT);
cpl_image_set(bpm, 1, 1, 1);
cpl_image_set(bpm, 1, 2, 2);
cpl_image_set(bpm, 1, 3, 3);
cpl_image_set(bpm, 1, 4, 4);
cpl_mask * mask;
mask = hdrl_bpm_to_mask(bpm, 1);
cpl_test_nonnull(mask);
cpl_test_eq(cpl_mask_count(mask), 2);
cpl_mask_delete(mask);
mask = hdrl_bpm_to_mask(bpm, ~0u);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_nonnull(mask);
cpl_test_eq(cpl_mask_count(mask), 4);
cpl_mask_delete(mask);
cpl_image_delete(bpm);
}
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Check hdrl_mask_to_bpm() in various conditions
@return cpl_error_code
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code hdrl_bpm_test_mask_to_bpm(void)
{
cpl_size nx = 20;
cpl_size ny = 20;
{
cpl_image * bpm = hdrl_mask_to_bpm(NULL, 0);
cpl_test_error(CPL_ERROR_NULL_INPUT);
cpl_test_null(bpm);
}
/* empty mask */
{
cpl_image * bpm;
cpl_mask * mask = cpl_mask_new(nx, ny);
bpm = hdrl_mask_to_bpm(mask, 0);
cpl_test_nonnull(bpm);
cpl_test_eq(cpl_image_get_flux(bpm), 0);
cpl_image_delete(bpm);
cpl_mask_delete(mask);
}
/* non-empty mask */
{
cpl_mask * mask = cpl_mask_new(nx, ny);
cpl_mask_set(mask, 1, 1, CPL_BINARY_1);
cpl_mask_set(mask, 1, 2, CPL_BINARY_1);
cpl_mask_set(mask, 1, 3, CPL_BINARY_1);
cpl_mask_set(mask, 1, 4, CPL_BINARY_1);
cpl_image * bpm;
bpm = hdrl_mask_to_bpm(mask, 1);
cpl_test_nonnull(bpm);
cpl_test_eq(cpl_image_get_flux(bpm), 4);
cpl_image_delete(bpm);
bpm = hdrl_mask_to_bpm(mask, 5);
cpl_test_nonnull(bpm);
cpl_test_eq(cpl_image_get_flux(bpm), 5 * 4);
cpl_image_delete(bpm);
cpl_mask_delete(mask);
}
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Check hdrl_bpm_filter() in various conditions
@return cpl_error_code
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code hdrl_bpm_test_hdrl_bpm_filter(void)
{
cpl_mask *img_mask = cpl_mask_new(200, 300);
cpl_mask_set(img_mask, 50, 50, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 100, CPL_BINARY_1);
cpl_mask_set(img_mask, 150, 150, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 250, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 300, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 299, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 298, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 300, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 299, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 298, CPL_BINARY_1);
/*Test the border behaviour*/
cpl_mask_set(img_mask, 199, 200, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 198, CPL_BINARY_1);
/*cpl_mask_save(img_mask, "img_mask.fits", NULL, CPL_IO_CREATE);*/
{
cpl_mask *filtered_mask = hdrl_bpm_filter(img_mask, 3, 3, CPL_FILTER_CLOSING);
cpl_test_eq(cpl_mask_get(filtered_mask, 100, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 101, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 102, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 103, 255), CPL_BINARY_0);
cpl_test_eq(cpl_mask_get(filtered_mask, 100, 251), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 198, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 199, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 200, 255), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 198, 254), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 199, 254), CPL_BINARY_1);
cpl_test_eq(cpl_mask_get(filtered_mask, 200, 254), CPL_BINARY_1);
/*Test the border behaviour*/
cpl_test_eq(cpl_mask_get(filtered_mask, 200, 199), CPL_BINARY_0);
/*cpl_mask_save(filtered_mask, "filtered_closing_mask.fits", NULL,
CPL_IO_CREATE);*/
cpl_mask_delete(filtered_mask);
}
/* free the memory */
cpl_mask_delete(img_mask);
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Can be used to test additional bad pixel growing algoritms
@return cpl_error_code
NOT TRIGGERED
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code hdrl_bpm_test_bpmgrow(void)
{
const char* img_mask_name = "img_mask.fits";
const char* filtered_morpho_mask_name = "filtered_morpho_mask.fits";
const char* filtered_average_mask_name = "filtered_average_mask.fits";
const char* file_gauss_name = "gauss.fits";
const char* filtered_gauss_data_name = "filtered_gauss_data.fits";
const char* filtered_gauss_mask_name = "filtered_gauss_mask.fits";
cpl_mask *img_mask = cpl_mask_new(200, 300);
cpl_mask_set(img_mask, 50, 50, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 100, CPL_BINARY_1);
cpl_mask_set(img_mask, 150, 150, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 250, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 100, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 102, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 198, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 252, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 254, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 256, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 300, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 299, CPL_BINARY_1);
cpl_mask_set(img_mask, 199, 298, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 300, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 299, CPL_BINARY_1);
cpl_mask_set(img_mask, 200, 298, CPL_BINARY_1);
cpl_mask_save(img_mask, "img_mask.fits", NULL, CPL_IO_CREATE);
if(1){
/* Set all pixels to bad, if there are a predefined number of bad
* pixels in the neighborhood - it uses the morpho filter which
* is much slower than e.g. the CPL_FILTER_AVERAGE_FAST filter */
cpl_matrix * kernel = cpl_matrix_new(3, 3);
cpl_matrix_fill(kernel, 1.0);
cpl_image * result_data = cpl_image_new_from_mask(img_mask);
cpl_image * filtered_data = cpl_image_new(
cpl_image_get_size_x(result_data),
cpl_image_get_size_y(result_data),
CPL_TYPE_FLOAT);
cpl_image_filter(filtered_data, result_data, kernel,
CPL_FILTER_MORPHO_SCALE, CPL_BORDER_FILTER);
cpl_mask * filtered_mask = cpl_mask_threshold_image_create(
filtered_data, 3.-0.5, DBL_MAX);
cpl_mask_save(filtered_mask, filtered_morpho_mask_name, NULL,
CPL_IO_CREATE);
cpl_mask_delete(filtered_mask);
cpl_image_delete(result_data);
cpl_image_delete(filtered_data);
cpl_matrix_delete(kernel);
}
if(1){
/* Set all pixels to bad, if there are a predefined number of bad
* pixels in the neighborhood - it uses the CPL_FILTER_AVERAGE_FAST
* filter. This filter is fast but shrinks the window at the border.
* Therefore a simple scaling to the number of bad pixels in the
* neighborhood (nx * ny * average) can not be done at the image-border.
* Nevertheless one can never detect less, but only more neighboring
* bad pixels near the border (the bad pixel density increases at the
* border as the windows shrinks) -
* so it would be a conservative approach.
*
* */
cpl_mask * kernel = cpl_mask_new(3, 3);
cpl_mask_not(kernel); /* All values set to unity*/
cpl_image * result_data = cpl_image_new_from_mask(img_mask);
cpl_image * filtered_data = cpl_image_new(
cpl_image_get_size_x(result_data),
cpl_image_get_size_y(result_data),
CPL_TYPE_FLOAT);
cpl_image_filter_mask(filtered_data, result_data, kernel,
CPL_FILTER_AVERAGE_FAST, CPL_BORDER_FILTER);
cpl_mask * filtered_mask = cpl_mask_threshold_image_create(
filtered_data, (3.-0.5)/(3.*3.), DBL_MAX);
cpl_mask_save(filtered_mask, filtered_average_mask_name, NULL, CPL_IO_CREATE);
cpl_mask_delete(filtered_mask);
cpl_image_delete(result_data);
cpl_image_delete(filtered_data);
cpl_mask_delete(kernel);
}
if(1){
/* This algo first smoothed the bad pixels by a gaussian kernel and
* then one can use a threshold to detect new bad pixels on the
* smoothed image. Here it is difficult to find good parameters for the
* gaussian and the subsequent thresholding.
* */
double sig_x = 3.; /*Sigma in x for the gaussian distribution.*/
double sig_y = 3.; /*Sigma in y for the gaussian distribution.*/
/* creating the Gaussian kernel */
cpl_size n = 5;
cpl_image * gauss = cpl_image_new(2 * n + 1, 2 * n + 1, CPL_TYPE_DOUBLE);
cpl_image_fill_gaussian(gauss, n + 1, n + 1, (double)121.0, sig_x, sig_y);
/* filtering the image */
cpl_matrix * kernel = cpl_matrix_wrap(2 * n + 1, 2 * n + 1,
cpl_image_get_data_double(gauss));
/*cpl_image_filter(im1, im2, gauss_data, CPL_FILTER_LINEAR,
CPL_BORDER_FILTER);*/
/*cpl_matrix * kernel = cpl_matrix_new(3, 3);
cpl_matrix_fill(kernel, 1.0);*/
cpl_image * result_data = cpl_image_new_from_mask(img_mask);
cpl_image * filtered_data = cpl_image_new(
cpl_image_get_size_x(result_data),
cpl_image_get_size_y(result_data),
CPL_TYPE_DOUBLE);
cpl_image_filter(filtered_data, result_data, kernel,
CPL_FILTER_LINEAR, CPL_BORDER_FILTER);
cpl_mask * filtered_mask = cpl_mask_threshold_image_create(
filtered_data, 3.-0.5, DBL_MAX);
cpl_image_save(filtered_data, filtered_gauss_data_name,
CPL_TYPE_DOUBLE, NULL, CPL_IO_CREATE);
cpl_image_save(gauss, file_gauss_name,
CPL_TYPE_DOUBLE, NULL, CPL_IO_CREATE);
cpl_mask_save(filtered_mask, filtered_gauss_mask_name, NULL, CPL_IO_CREATE);
cpl_matrix_unwrap(kernel);
cpl_image_delete(gauss);
cpl_mask_delete(filtered_mask);
cpl_image_delete(result_data);
cpl_image_delete(filtered_data);
}
/* free the memory */
cpl_mask_delete(img_mask);
/* Remove to disk */
remove(img_mask_name);
remove(filtered_morpho_mask_name);
remove(filtered_average_mask_name);
remove(file_gauss_name);
remove(filtered_gauss_data_name);
remove(filtered_gauss_mask_name);
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Can be used to test how to apply masks to imagelist
@return cpl_error_code
NOT TRIGGERED
*/
/*----------------------------------------------------------------------------*/
static cpl_error_code hdrl_bpm_test_apply_masks_to_imagelist(void)
{
#define NUM_IMAGES 2
int nx = 64;
int ny = 64;
/* Create a imagelist */
cpl_image *img = cpl_image_fill_test_create(nx, ny);
cpl_imagelist *list = cpl_imagelist_new();
for (cpl_size i=0; i < NUM_IMAGES; i++) {
cpl_imagelist_set(list, cpl_image_duplicate(img), i);
}
cpl_image_delete(img);
/* join masks -> fill with a new allocated mask the vector*/
cpl_mask *new_mask = cpl_mask_new(nx, ny);
cpl_mask **orig_masks;
hdrl_join_mask_on_imagelist(list, new_mask, &orig_masks);
cpl_mask_delete(new_mask);
/* restore original mask */
hdrl_set_masks_on_imagelist(list, orig_masks);
/* free memory */
cpl_imagelist_delete(list);
for (cpl_size i = 0; i < NUM_IMAGES; i++) {
cpl_mask_delete(orig_masks[i]);
}
cpl_free(orig_masks);
return CPL_ERROR_NONE;
}
/*----------------------------------------------------------------------------*/
/**
@brief Unit tests of BPM module
**/
/*----------------------------------------------------------------------------*/
int main(void)
{
cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
hdrl_bpm_test_bpm_to_mask();
hdrl_bpm_test_mask_to_bpm();
hdrl_bpm_test_hdrl_bpm_filter();
hdrl_bpm_test_bpmgrow() ;
hdrl_bpm_test_apply_masks_to_imagelist();
cpl_test_error(CPL_ERROR_NONE);
return cpl_test_end(0);
}
|