38#define MODULE_ID "XSH_FIT"
41#define XSH_TEST_MARGIN 2.0
43#define xsh_test(expr) \
45 if (!(expr)) assure(0, CPL_ERROR_ILLEGAL_OUTPUT, "Test failed"); \
48#define xsh_test_tol(first, second, tolerance) \
50 const double xsh_test_first = (double)(first); \
51 const double xsh_test_second = (double)(second); \
52 const double xsh_test_tolerance = (double)(tolerance); \
54 const double xsh_test_margin = (double)(XSH_TEST_MARGIN); \
56 if (!(fabs(xsh_test_first - xsh_test_second) <= \
57 xsh_test_tolerance * xsh_test_margin)) \
67#define xsh_fit_imagelist_is_zero(A, B) \
68 xsh_fit_imagelist_is_zero_macro(A, B)
69#define xsh_fit_image_is_zero(A, B) \
70 xsh_fit_image_is_zero_macro(A, B)
108 if (cpl_error_get_code() != CPL_ERROR_NONE) {
120 const double ditval[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
122 cpl_imagelist * input;
123 cpl_image * dfiterror
125 cpl_image * ffiterror
127 cpl_image * ifiterror
129 const int ndits = (int)(
sizeof(ditval)/
sizeof(double));
130 cpl_vector * vdit = cpl_vector_wrap(ndits, (
double*)ditval);
131 const double sqsum = 204.0;
132 const double mytol = 2.76 * FLT_EPSILON;
134 const cpl_type pixel_type[] = {CPL_TYPE_DOUBLE, CPL_TYPE_FLOAT, CPL_TYPE_INT};
138 cpl_msg_info(cpl_func,
"Testing with %d %d X %d images",
142 xsh_test( cpl_error_get_code() != CPL_ERROR_NONE );
143 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
146 cpl_imagelist_delete(fit);
148 input = cpl_imagelist_new();
150 xsh_test( cpl_error_get_code() != CPL_ERROR_NONE );
151 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
154 cpl_imagelist_delete(fit);
157 xsh_test( cpl_error_get_code() != CPL_ERROR_NONE );
158 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
161 cpl_imagelist_delete(fit);
164 for (ntest = 0; ntest < 3; ntest++) {
166 const cpl_type test_type = pixel_type[ntest];
171 cpl_msg_info(cpl_func,
"Fitting with pixel type %u",
172 (
unsigned)test_type);
174 xsh_test(!cpl_imagelist_set(input, image, 0));
176 image = cpl_image_duplicate(image);
180 xsh_test(!cpl_image_multiply_scalar(image, ditval[1]));
182 xsh_test(!cpl_imagelist_set(input, image, 1));
185 for (i=2; i < ndits; i++) {
188 = cpl_image_multiply_scalar_create(cpl_imagelist_get(input, 1),
191 xsh_test(!cpl_imagelist_set(input, image, i));
197 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
198 xsh_test( cpl_imagelist_get_size(fit) == ndits - 1 );
202 xsh_test(!cpl_image_divide(cpl_imagelist_get(fit, 0),
203 cpl_imagelist_get(input, 1)));
206 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 0), 1.0));
210 cpl_imagelist_delete(fit);
211 cpl_imagelist_delete(input);
212 input = cpl_imagelist_new();
216 for (i=0; i < ndits; i++) {
220 xsh_test(!cpl_image_add_scalar(image, ditval[i]*ditval[i]));
222 xsh_test(!cpl_imagelist_set(input, image, i));
224 cpl_msg_debug(cpl_func,
"Dit and mean of input image no. %d: %g %g",
225 i, ditval[i], cpl_image_get_mean(image));
230 if (cpl_error_get_code() != CPL_ERROR_NONE ) {
232 xsh_test( cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX );
236 cpl_imagelist_delete(fit);
240 xsh_test( cpl_error_get_code() != CPL_ERROR_NONE );
241 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
244 cpl_imagelist_delete(fit);
248 xsh_test( cpl_error_get_code() != CPL_ERROR_NONE );
249 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
252 cpl_imagelist_delete(fit);
260 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
261 xsh_test( cpl_imagelist_get_size(fit) == 3 );
264 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 2), 1.0));
268 cpl_imagelist_delete(fit);
277 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
278 xsh_test( cpl_imagelist_get_size(fit) == ndits );
281 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 2), 1.0));
285 cpl_imagelist_delete(fit);
291 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
292 xsh_test( cpl_imagelist_get_size(fit) == ndits-1 );
294 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 1), 1.0));
299 cpl_imagelist_delete(fit);
305 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
306 xsh_test( cpl_imagelist_get_size(fit) == ndits-1 );
308 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 0), 1.0));
313 cpl_imagelist_delete(fit);
319 xsh_test( cpl_error_get_code() == CPL_ERROR_NONE );
320 xsh_test( cpl_imagelist_get_size(fit) == 1 );
322 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 0),
323 sqsum/(
double)ndits));
327 cpl_imagelist_delete(fit);
329 cpl_imagelist_delete(input);
331 (void)cpl_vector_unwrap(vdit);
336 input = cpl_imagelist_new();
338 for (ntest = 1; ntest <= ndits; ntest++) {
339 const double gain = 4.0;
341 cpl_msg_info(cpl_func,
"Fitting %d coefficients to as many points",
344 vdit = cpl_vector_wrap(ntest, (
double*)ditval);
347 for (i = ntest - 1; i < ntest; i++) {
351 xsh_test(!cpl_image_add_scalar(image, gain * ditval[i]*ditval[i]));
353 xsh_test(!cpl_imagelist_set(input, image, i));
355 cpl_msg_debug(cpl_func,
"Dit and mean of input image no. %d: %g %g",
356 i, ditval[i], cpl_image_get_mean(image));
365 (void)cpl_vector_unwrap(vdit);
367 if (cpl_error_get_code() != CPL_ERROR_NONE) {
370 cpl_msg_info(cpl_func,
"Could not fit %d coefficients to as many "
373 xsh_test( cpl_error_get_code() != CPL_ERROR_UNSPECIFIED );
379 xsh_test( cpl_imagelist_get_size(fit) == ntest );
382 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 1),
384 }
else if (ntest > 2) {
385 xsh_test(!cpl_image_subtract_scalar(cpl_imagelist_get(fit, 2),
393 cpl_imagelist_delete(fit);
396 cpl_imagelist_delete(input);
399 cpl_image_delete(dfiterror);
400 cpl_image_delete(ffiterror);
401 cpl_image_delete(ifiterror);
422 const int n = cpl_imagelist_get_size(self);
425 for (i = 0; i <
n; i++) {
450 cpl_stats * stats = cpl_stats_new_from_image(self,
451 CPL_STATS_MIN | CPL_STATS_MAX
454 const double mymin = cpl_stats_get_min(stats);
455 const double mymax = cpl_stats_get_max(stats);
461 cpl_stats_delete(stats);
486 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
488 if (cpl_image_get_type(self) == CPL_TYPE_INT) {
490 int * pi = cpl_image_get_data_int(self);
491 const int nx = cpl_image_get_size_x(self);
492 const int ny = cpl_image_get_size_y(self);
496 for (j=0 ; j <
ny ; j++) {
497 for (i=0 ; i <
nx ; i++) {
499 = min_pix + (max_pix * (double)rand())/(
double)RAND_MAX;
501 pi[i + j *
nx] = (int) value;
506 const cpl_error_code error
507 = cpl_image_fill_noise_uniform(self, min_pix, max_pix);
508 cpl_ensure_code(!error, error);
511 return CPL_ERROR_NONE;
#define xsh_error_dump(level)
cpl_imagelist * xsh_fit_imagelist_polynomial(const cpl_vector *x_pos, const cpl_imagelist *values, int mindeg, int maxdeg, cpl_boolean is_eqdist, cpl_image *fiterror)
Fit a polynomial to each pixel in a list of images.
static void xsh_fit_imagelist_is_zero_macro(const cpl_imagelist *, double)
Verify that all elements in an imagelist are zero (within a tolerance)
static void xsh_fit_image_is_zero_macro(const cpl_image *, double)
Verify that all elements in an image are zero (within a tolerance)
static void xsh_fit_imagelist_polynomial_tests(void)
int main(void)
Unit tests of fit module.
static cpl_error_code xsh_image_fill_noise_uniform(cpl_image *, double, double)
Fill an image with uniform random noise distribution.
#define xsh_fit_imagelist_is_zero(A, B)
#define xsh_fit_image_is_zero(A, B)
#define xsh_test_tol(first, second, tolerance)
#define TESTS_INIT(DRL_ID)