CR2RE Pipeline Reference Manual 1.6.10
hdrl_lacosmics-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2013 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include "hdrl_lacosmics.h"
29#include "hdrl_bpm_2d.h"
30
31#include <cpl.h>
32
33#include <math.h>
34
35/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
40
41cpl_error_code test_lacosmic_inputs(void)
42{
43 /* Create parameters */
44 hdrl_parameter *pFake = hdrl_bpm_2d_parameter_create_legendresmooth(
45 4., 5., 6, 20, 21, 11, 12, 2, 10);
46 hdrl_parameter *pErr1 = hdrl_lacosmic_parameter_create( 0, 0, 0);
47 hdrl_parameter *pErr2 = hdrl_lacosmic_parameter_create( 0, -1., 1);
48 hdrl_parameter *pErr3 = hdrl_lacosmic_parameter_create(-1., 0, 1);
49 hdrl_parameter *params = hdrl_lacosmic_parameter_create(5, 2., 5);
50 cpl_test_error(CPL_ERROR_NONE);
51
52
53 /* Check parameter */
54 cpl_test(!hdrl_lacosmic_parameter_check(pFake ));
55 cpl_test( hdrl_lacosmic_parameter_check(params));
56
57
58 /* Verify parameter */
60 cpl_test_error(CPL_ERROR_NULL_INPUT);
61
63 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
64
66 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
67
69 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
70
72 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
73
75 cpl_test_error(CPL_ERROR_NONE);
76
77
78 /* Gets */
79
80 cpl_test_eq(hdrl_lacosmic_parameter_get_sigma_lim(NULL), -1.);
81 cpl_test_error(CPL_ERROR_NULL_INPUT);
82 cpl_test_eq(hdrl_lacosmic_parameter_get_sigma_lim(params), 5);
83 cpl_test_error(CPL_ERROR_NONE);
84
85 cpl_test_eq(hdrl_lacosmic_parameter_get_f_lim(NULL), -1.);
86 cpl_test_error(CPL_ERROR_NULL_INPUT);
87 cpl_test_eq(hdrl_lacosmic_parameter_get_f_lim(params), 2.);
88 cpl_test_error(CPL_ERROR_NONE);
89
90 cpl_test_eq(hdrl_lacosmic_parameter_get_max_iter(NULL), -1);
91 cpl_test_error(CPL_ERROR_NULL_INPUT);
92 cpl_test_eq(hdrl_lacosmic_parameter_get_max_iter(params), 5);
93 cpl_test_error(CPL_ERROR_NONE);
94
95
96 /* Create ParameterList */
97 cpl_parameterlist *pl;
98
99 pl = hdrl_lacosmic_parameter_create_parlist(NULL, "lacosmic", params);
100 cpl_test_error(CPL_ERROR_NULL_INPUT);
101 cpl_test_null(pl);
102
103 pl = hdrl_lacosmic_parameter_create_parlist("test", NULL, params);
104 cpl_test_error(CPL_ERROR_NULL_INPUT);
105 cpl_test_null(pl);
106
107 pl = hdrl_lacosmic_parameter_create_parlist("test", "lacosmic", NULL);
108 cpl_test_error(CPL_ERROR_NULL_INPUT);
109 cpl_test_null(pl);
110
111 cpl_parameterlist *plFake;
112 plFake = hdrl_lacosmic_parameter_create_parlist("test", "lacosmic", pFake);
113 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
114 cpl_test_null(plFake);
115
116 pl = hdrl_lacosmic_parameter_create_parlist("test", "lacosmic", params);
117 cpl_test_error(CPL_ERROR_NONE);
118 cpl_test_nonnull(pl);
119
120
121 /* Parse ParameterList */
122 hdrl_parameter *check;
123
124 check = hdrl_lacosmic_parameter_parse_parlist(NULL, "test.lacosmic");
125 cpl_test_error(CPL_ERROR_NULL_INPUT);
126 cpl_test_null(check);
127
129 cpl_test_error(CPL_ERROR_NULL_INPUT);
130 cpl_test_null(check);
131
132 check = hdrl_lacosmic_parameter_parse_parlist(pl, "test.lacosmic");
133 cpl_test_error(CPL_ERROR_NONE);
134 cpl_test_nonnull(check);
135
136
137 /* image smaller than 7x7 kernel */
138 hdrl_image *img1 = hdrl_image_new(6, 1000);
139 cpl_mask *res1 = hdrl_lacosmic_edgedetect(img1, params);
140 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
141 cpl_test_null(res1);
142 hdrl_image_delete(img1);
143
144
145 /* image larger */
146 hdrl_image *img2 = hdrl_image_new(1200, 4);
147 cpl_mask *res2 = hdrl_lacosmic_edgedetect(img2, params);
148 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
149 cpl_test_null(res2);
150 hdrl_image_delete(img2);
151
152
153 /* Clean up */
154 cpl_parameterlist_delete(pl);
159 hdrl_parameter_delete(params);
161
162 return cpl_error_get_code();
163}
164
165cpl_error_code test_lacosmic_edgedetect(void)
166{
167 cpl_image * img_data = NULL;
168 cpl_mask * img_mask = NULL;
169 cpl_image * img_error = NULL;
170 cpl_mask * result_mask = NULL;
171 /* detect single pixel cosmics */
172 {
173 img_data = cpl_image_new(200, 300, CPL_TYPE_DOUBLE);
174 img_mask = cpl_mask_new(200, 300);
175 cpl_image_fill_noise_uniform(img_data, 90, 110);
176 double error = (110 - 90) / sqrt(12);
177 cpl_image_set(img_data, 50, 50, 300.);
178 cpl_image_set(img_data, 100, 100, 300.);
179 cpl_image_set(img_data, 150, 150, 300.);
180 cpl_image_set(img_data, 100, 250, 300.);
181
182 img_error = cpl_image_new(cpl_image_get_size_x(img_data),
183 cpl_image_get_size_y(img_data),
184 CPL_TYPE_DOUBLE);
185 cpl_image_add_scalar(img_error, error);
186
187 cpl_mask_set(img_mask, 120,120, CPL_BINARY_1);
188 cpl_mask_set(img_mask, 120,121, CPL_BINARY_1);
189 cpl_mask_set(img_mask, 120,122, CPL_BINARY_1);
190 cpl_mask_set(img_mask, 121,120, CPL_BINARY_1);
191 cpl_mask_set(img_mask, 121,121, CPL_BINARY_1);
192 cpl_mask_set(img_mask, 121,122, CPL_BINARY_1);
193 cpl_mask_set(img_mask, 122,120, CPL_BINARY_1);
194 cpl_mask_set(img_mask, 122,121, CPL_BINARY_1);
195 cpl_mask_set(img_mask, 122,122, CPL_BINARY_1);
196 /*set one outlier on a bad pixel*/
197 cpl_image_set(img_data, 122, 122, 300.);
198
199 cpl_image_reject_from_mask(img_data, img_mask);
200 hdrl_image * image = hdrl_image_create(img_data, img_error);
201 hdrl_parameter * params =
202 hdrl_lacosmic_parameter_create(error * 2, 2.0, 5);
203 result_mask = hdrl_lacosmic_edgedetect(image, params);
204 hdrl_parameter_delete(params) ;
205
206 /*
207 cpl_image_save(data, "test_image.fits", CPL_TYPE_DOUBLE, NULL,
208 CPL_IO_CREATE);
209 cpl_image_save(data_error, "test_error.fits", CPL_TYPE_DOUBLE, NULL,
210 CPL_IO_CREATE);
211 cpl_mask_save(data_result, "test_cr.fits", NULL, CPL_IO_CREATE);
212 */
213
214 cpl_test_eq(cpl_mask_get(result_mask, 50, 50), CPL_BINARY_1);
215 cpl_test_eq(cpl_mask_get(result_mask, 100, 100), CPL_BINARY_1);
216 cpl_test_eq(cpl_mask_get(result_mask, 150, 150), CPL_BINARY_1);
217 cpl_test_eq(cpl_mask_get(result_mask, 100, 250), CPL_BINARY_1);
218 cpl_test_eq(cpl_mask_get(result_mask, 122, 122), CPL_BINARY_0);
219
220 cpl_test_eq(cpl_mask_get(result_mask, 110, 260), CPL_BINARY_0);
221
222 /* free the memory */
223 cpl_image_delete(img_data);
224 cpl_mask_delete(img_mask);
225 cpl_image_delete(img_error);
226 cpl_mask_delete(result_mask);
227 hdrl_image_delete(image);
228 }
229
230 /* detect a very big rectangular cosmic */
231 {
232 img_data = cpl_image_new(150, 200, CPL_TYPE_DOUBLE);
233 img_mask = cpl_mask_new(150, 200);
234 cpl_image_fill_noise_uniform(img_data, 90, 110);
235 double error = (110 - 90) / sqrt(12);
236
237 for (int varx = 50; varx < 75; ++varx) {
238 for (int vary = 60; vary < 130; ++vary) {
239 cpl_image_set(img_data, varx, vary, 5000);
240 }
241 }
242 for (int varx = 20; varx < 120; ++varx) {
243 for (int vary = 20; vary < 40; ++vary) {
244 cpl_image_set(img_data, varx, vary, 5000);
245 }
246 }
247 img_error = cpl_image_new(cpl_image_get_size_x(img_data),
248 cpl_image_get_size_y(img_data),
249 CPL_TYPE_DOUBLE);
250 cpl_image_add_scalar(img_error, error);
251
252 hdrl_image * image = hdrl_image_create(img_data, img_error);
253 /* In order to detect the full rectangle, f_lim has to be set to a low
254 * value - if not, the central part is not detected */
255 hdrl_parameter * params =
256 hdrl_lacosmic_parameter_create(error * 2, 0.5, 65);
257 result_mask = hdrl_lacosmic_edgedetect(image, params);
258 hdrl_parameter_delete(params) ;
259
260 /*
261 cpl_image_save(data, "input_data.fits", CPL_TYPE_DOUBLE, NULL,
262 CPL_IO_CREATE);
263 cpl_image_save(data_error, "input_error.fits", CPL_TYPE_DOUBLE, NULL,
264 CPL_IO_CREATE);
265 cpl_mask_save(data_result, "output_mask.fits", NULL, CPL_IO_CREATE);
266 */
267
268 cpl_test_eq(cpl_mask_count(result_mask), 100*20+25*70);
269 /* free the memory */
270 cpl_image_delete(img_data);
271 cpl_mask_delete(img_mask);
272 cpl_image_delete(img_error);
273 cpl_mask_delete(result_mask);
274 hdrl_image_delete(image);
275 }
276 /* detect a very big rectangular cosmic with bad pixels */
277 {
278 img_data = cpl_image_new(150, 200, CPL_TYPE_DOUBLE);
279 img_mask = cpl_mask_new(150, 200);
280 cpl_image_fill_noise_uniform(img_data, 90, 110);
281 double error = (110 - 90) / sqrt(12);
282
283 for (int varx = 50; varx < 75; ++varx) {
284 for (int vary = 60; vary < 130; ++vary) {
285 cpl_image_set(img_data, varx, vary, 5000);
286 }
287 }
288 for (int varx = 20; varx < 120; ++varx) {
289 for (int vary = 20; vary < 40; ++vary) {
290 cpl_image_set(img_data, varx, vary, 5000);
291 }
292 }
293
294 /*mark bad pixels*/
295 for (int varx = 65; varx < 68; ++varx) {
296 for (int vary = 1; vary < 150; ++vary) {
297 cpl_mask_set(img_mask, varx, vary, CPL_BINARY_1);
298 }
299 }
300 img_error = cpl_image_new(cpl_image_get_size_x(img_data),
301 cpl_image_get_size_y(img_data),
302 CPL_TYPE_DOUBLE);
303 cpl_image_add_scalar(img_error, error);
304 cpl_image_reject_from_mask(img_data, img_mask);
305
306 hdrl_image * image = hdrl_image_create(img_data, img_error);
307
308 /* In order to detect the full rectangle, f_lim has to be set to a low
309 * value - if not, the central part is not detected */
310 hdrl_parameter * params =
311 hdrl_lacosmic_parameter_create(error * 2, 0.5, 80);
312 result_mask = hdrl_lacosmic_edgedetect(image, params);
313 hdrl_parameter_delete(params) ;
314 /*
315 cpl_image_save(data, "input_data.fits", CPL_TYPE_DOUBLE, NULL,
316 CPL_IO_CREATE);
317 cpl_image_save(data_error, "input_error.fits", CPL_TYPE_DOUBLE, NULL,
318 CPL_IO_CREATE);
319 cpl_mask_save(data_bpm, "input_mask.fits", NULL, CPL_IO_CREATE);
320
321 cpl_mask_save(data_result, "output_mask.fits", NULL, CPL_IO_CREATE);
322 */
323
324 cpl_test_eq(cpl_mask_count(result_mask), 100*20 + 25*70 - 3*70 - 3*20);
325 /* free the memory */
326 cpl_image_delete(img_data);
327 cpl_mask_delete(img_mask);
328 cpl_image_delete(img_error);
329 cpl_mask_delete(result_mask);
330 hdrl_image_delete(image);
331 }
332 return cpl_error_get_code();
333}
334
335/*----------------------------------------------------------------------------*/
339/*----------------------------------------------------------------------------*/
340int main(void)
341{
342 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
343
344 test_lacosmic_inputs();
345
346 test_lacosmic_edgedetect();
347
348 return cpl_test_end(0);
349}
hdrl_parameter * hdrl_bpm_2d_parameter_create_legendresmooth(double kappa_low, double kappa_high, int maxiter, int steps_x, int steps_y, int filter_size_x, int filter_size_y, int order_x, int order_y)
Creates BPM_2D Parameters object for HDRL_BPM_2D_LEGENDRESMOOTH.
Definition: hdrl_bpm_2d.c:191
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition: hdrl_image.c:311
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
cpl_parameterlist * hdrl_lacosmic_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create parameter list for the LaCosmic computation.
double hdrl_lacosmic_parameter_get_sigma_lim(const hdrl_parameter *p)
Access the sigma_lim in the LaCosmic parameter.
cpl_mask * hdrl_lacosmic_edgedetect(const hdrl_image *ima_in, const hdrl_parameter *params)
Detect bad-pixels / cosmic-rays on a single image.
int hdrl_lacosmic_parameter_get_max_iter(const hdrl_parameter *p)
Access the max_iter in the LaCosmic parameter.
cpl_error_code hdrl_lacosmic_parameter_verify(const hdrl_parameter *param)
Verify basic correctness of the LaCosmic parameters.
hdrl_parameter * hdrl_lacosmic_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse parameterlist to create input parameters for the LaCosmic.
double hdrl_lacosmic_parameter_get_f_lim(const hdrl_parameter *p)
Access the f_lim in the LaCosmic parameter.
cpl_boolean hdrl_lacosmic_parameter_check(const hdrl_parameter *self)
Check that the parameter is an LaCosmic parameter.
hdrl_parameter * hdrl_lacosmic_parameter_create(double sigma_lim, double f_lim, int max_iter)
Creates LaCosmic parameters object.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter