visir_utils-test.c

00001 /*                                                                              *
00002  *   This file is part of the ESO VISIR package                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019  
00020 #ifdef HAVE_CONFIG_H
00021 #  include <config.h>
00022 #endif
00023 
00024 /*-----------------------------------------------------------------------------
00025                                 Includes
00026  -----------------------------------------------------------------------------*/
00027 
00028 #include <cpl.h>
00029 
00030 #include <visir_utils.h>
00031 
00032 /*----------------------------------------------------------------------------*/
00036 /*----------------------------------------------------------------------------*/
00037 
00038 #define CONCAT(a,b) a ## _ ## b
00039 #define CONCAT2X(a,b) CONCAT(a,b)
00040 
00041 #define PIXEL_TYPE double
00042 #define STDEV_TYPE CPL_TYPE_DOUBLE
00043 #define PIXEL_TYPE_CPL CPL_TYPE_DOUBLE
00044 #include "../recipes/visir_util_clip_body.c"
00045 #undef PIXEL_TYPE
00046 #undef STDEV_TYPE
00047 #undef PIXEL_TYPE_CPL
00048 
00049 /* column major */
00050 #define IND(x,y,nx) ((x) + (y) * (nx))
00051 
00052 static void visir_get_kth_test(void);
00053 static void visir_clip_kappa_sigma_test(void);
00054 static void visir_fftxcorrelate_test(void);
00055 
00056 /*----------------------------------------------------------------------------*/
00060 /*----------------------------------------------------------------------------*/
00061 
00062 #define LEN(a) sizeof((a))/sizeof((a)[0])
00063 
00064 int main(void)
00065 {
00066 
00067     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00068 
00069     visir_get_kth_test();
00070     visir_clip_kappa_sigma_test();
00071     visir_fftxcorrelate_test();
00072 
00073     return cpl_test_end(0);
00074 }
00075 
00076 static void visir_get_kth_test(void)
00077 {
00078     FILE          * stream;
00079     double res = -1;
00080 
00081     stream = cpl_msg_get_level() > CPL_MSG_INFO
00082         ? fopen("/dev/null", "a") : stdout;
00083 
00084     {
00085         double a[10];
00086 
00087         for (int i = 0; i < LEN(a); i++)
00088             a[i] = 0;
00089         for (int i = 0; i < LEN(a); i++) {
00090             res = cpl_tools_get_kth_double(a, LEN(a), i);
00091             cpl_test_eq(0, res);
00092         }
00093 
00094         for (int i = 0; i < LEN(a); i++)
00095             a[i] = i;
00096         for (int i = 0; i < LEN(a); i++) {
00097             res = cpl_tools_get_kth_double(a, LEN(a), i);
00098             cpl_test_eq(i, res);
00099         }
00100 
00101         for (int i = 0; i < LEN(a); i++)
00102             a[i] = -i;
00103         for (int i = 0; i < LEN(a); i++) {
00104             res = cpl_tools_get_kth_double(a, LEN(a), i);
00105             cpl_test_eq((double)i - LEN(a) + 1., res);
00106         }
00107     }
00108 
00109     {
00110         double a[] = {3,1,4,2,0,9,7,8,6,5};
00111         res = cpl_tools_get_kth_double(a, LEN(a), 4);
00112         cpl_test_eq(4, res);
00113         for (int i = 0; i < LEN(a) / 2; i++)
00114             cpl_test_leq(a[i], 4);
00115         for (int i = LEN(a)/2; i < LEN(a); i++)
00116             cpl_test_lt(4, a[i]);
00117     }
00118 
00119     {
00120         double a[] = {8,5,9,2,0,4,6,3,7,1};
00121         res = cpl_tools_get_kth_double(a, LEN(a), 2);
00122         cpl_test_eq(2, res);
00123         res = cpl_tools_get_kth_double(a, LEN(a), 7);
00124         cpl_test_eq(7, res);
00125 
00126         cpl_test_eq(9, a[LEN(a) - 1]);
00127         cpl_test_eq(8, a[LEN(a) - 2]);
00128         cpl_test_eq(0, a[0]);
00129         cpl_test_eq(1, a[1]);
00130     }
00131 
00132     {
00133         double a[] = {8,5,9,2,0,4,6,3,7,1};
00134         res = cpl_tools_get_kth_double(a, LEN(a), 7);
00135         cpl_test_eq(7, res);
00136         res = cpl_tools_get_kth_double(a, LEN(a), 2);
00137         cpl_test_eq(2, res);
00138 
00139         cpl_test_eq(9, a[LEN(a) - 1]);
00140         cpl_test_eq(8, a[LEN(a) - 2]);
00141         cpl_test_eq(0, a[0]);
00142         cpl_test_eq(1, a[1]);
00143     }
00144 
00145     cpl_test_nonnull( stream );
00146 
00147     if (stream != stdout) cpl_test_zero( fclose(stream) );
00148 
00149 }
00150 
00151 static void visir_clip_kappa_sigma_test(void)
00152 {
00153     FILE          * stream;
00154     int dump;
00155     double res = -1;
00156 
00157     stream = cpl_msg_get_level() > CPL_MSG_INFO
00158         ? fopen("/dev/null", "a") : stdout;
00159 
00160     cpl_test_nonnull( stream );
00161 
00162     {
00163         double * pixels = cpl_calloc(9, sizeof(double));
00164         int shifts[3 * 2] = {0};
00165         pixels[1 + 3 * 1] = 1;
00166 
00167         cpl_imagelist * list = cpl_imagelist_new();
00168         cpl_imagelist * dev = cpl_imagelist_new();
00169 
00170         cpl_image* img = cpl_image_wrap_double(3, 3, pixels);
00171         cpl_imagelist_set(list, img, 0);
00172         img = cpl_image_duplicate(img);
00173         cpl_imagelist_set(list, img, 1);
00174         img = cpl_image_duplicate(img);
00175         cpl_imagelist_set(list, img, 2);
00176 
00177         visir_util_clip_kappa_sigma_double(list, dev, 1.0, 3, 3, shifts);
00178 
00179         cpl_image * map = cpl_image_new_from_accepted(list);
00180         cpl_test_eq(3, cpl_image_get(map, 2, 2, &dump));
00181 
00182         cpl_image_delete(map);
00183         cpl_imagelist_delete(list);
00184         cpl_imagelist_delete(dev);
00185     }
00186 
00187 
00188     {
00189         double * pixels = cpl_calloc(9, sizeof(double));
00190         /* gaus mean 100 sigma 5, 2 sigma range */
00191         int values[] = {92, 93, 94, 94, 95, 95, 96, 96, 96, 97,
00192                         97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
00193                         99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
00194                         102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
00195                         104, 105, 105, 106, 106, 107, 108 };
00196         int shifts[LEN(values) * 2] = {0};
00197 
00198         cpl_imagelist * list = cpl_imagelist_new();
00199         cpl_imagelist * dev = cpl_imagelist_new();
00200 
00201         cpl_image* img = cpl_image_wrap_double(3, 3, pixels);
00202         for (int i = 0; i < LEN(values); i++) {
00203             cpl_image_set(img, 2, 2, values[i]);
00204             cpl_imagelist_set(list, img, i);
00205             img = cpl_image_duplicate(img);
00206         }
00207         cpl_image_delete(img);
00208 
00209         visir_util_clip_kappa_sigma_double(list, dev, 1.0, 3, 3, shifts);
00210 
00211         cpl_image * map = cpl_image_new_from_accepted(list);
00212         cpl_test_eq(LEN(values), cpl_image_get(map, 2, 2, &dump));
00213 
00214         cpl_image_delete(map);
00215         cpl_imagelist_delete(list);
00216         cpl_imagelist_delete(dev);
00217     }
00218 
00219     {
00220         double * pixels = cpl_calloc(9, sizeof(double));
00221         /* gaus mean 100 sigma 5, 2 sigma range, 2 outliers */
00222         int values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
00223                         97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
00224                         99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
00225                         102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
00226                         104, 105, 105, 106, 106, 107, 108 };
00227         int shifts[LEN(values) * 2] = {0};
00228 
00229         cpl_imagelist * list = cpl_imagelist_new();
00230         cpl_imagelist * dev = cpl_imagelist_new();
00231 
00232         cpl_image* img = cpl_image_wrap_double(3, 3, pixels);
00233         for (int i = 0; i < LEN(values); i++) {
00234             cpl_image_set(img, 2, 2, values[i]);
00235             cpl_imagelist_set(list, img, i);
00236             img = cpl_image_duplicate(img);
00237         }
00238         cpl_image_delete(img);
00239 
00240         visir_util_clip_kappa_sigma_double(list, dev, 1.0, 3, 3, shifts);
00241 
00242         cpl_image * map = cpl_image_new_from_accepted(list);
00243         //cpl_image_dump_structure(map, stdout);
00244         //cpl_image_dump_window(map, 1, 1, 3, 3, stdout);
00245         //cpl_image_dump_window(img, 1, 1, 3, 3, stdout);
00246         cpl_test_eq(LEN(values) - 2, cpl_image_get(map, 2, 2, &dump));
00247 
00248         cpl_image_delete(map);
00249         cpl_imagelist_delete(list);
00250         cpl_imagelist_delete(dev);
00251     }
00252 
00253     {
00254         double * pixels = cpl_calloc(9, sizeof(double));
00255         cpl_msg_info(cpl_func, "-----------------");
00256         int values[] = { -3, 10, 10, 10, 10, 10, 10, 10 };
00257         int shifts[LEN(values) * 2] = {0};
00258 
00259         cpl_imagelist * list = cpl_imagelist_new();
00260         cpl_imagelist * dev = cpl_imagelist_new();
00261 
00262         cpl_image* origimg = cpl_image_wrap_double(3, 3, pixels);
00263         for (int i = 0; i < LEN(values); i++) {
00264             cpl_image * img = cpl_image_duplicate(origimg);
00265             if (i == 1) {
00266                 cpl_image_set(img, 1, 2, values[i]);
00267                 shifts[i * 2] = -1;
00268             }
00269             else if (i == 2) {
00270                 cpl_image_set(img, 2, 3, values[i]);
00271                 shifts[i * 2 + 1] = 1;
00272             }
00273             else if (i == 3) {
00274                 cpl_image_set(img, 3, 1, values[i]);
00275                 shifts[i * 2] = 1;
00276                 shifts[i * 2 + 1] = -1;
00277             }
00278             else
00279                 cpl_image_set(img, 2, 2, values[i]);
00280             cpl_imagelist_set(list, img, i);
00281         }
00282         cpl_image_delete(origimg);
00283 
00284         visir_util_clip_kappa_sigma_double(list, dev, 1.0, 2, 2, shifts);
00285 
00286         cpl_image * map = cpl_image_new_from_accepted(list);
00287         //cpl_image_dump_window(map, 1, 1, 3, 3, stdout);
00288         /* to test boundaries change recipes/visir_util_clip_body.c to mark
00289          * pixels shifted out of bounds as bad */
00290         if (0) {
00291             cpl_test_eq(LEN(values) - 2, cpl_image_get(map, 1, 1, &dump));
00292             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 1, 2, &dump));
00293             cpl_test_eq(LEN(values) - 2, cpl_image_get(map, 1, 3, &dump));
00294             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 2, 1, &dump));
00295             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 2, 2, &dump)); // one rejected
00296             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 2, 3, &dump));
00297             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 3, 1, &dump));
00298             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 3, 2, &dump));
00299             cpl_test_eq(LEN(values) - 2, cpl_image_get(map, 3, 3, &dump));
00300         }
00301         else
00302             cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 2, 2, &dump));
00303 
00304         cpl_image_delete(map);
00305         cpl_imagelist_delete(list);
00306         cpl_imagelist_delete(dev);
00307     }
00308 
00309     /* test shifted rejects */
00310     {
00311         double * pixels = cpl_calloc(9, sizeof(double));
00312         /* gaus mean 100 sigma 5, 2 sigma range, 3 outliers */
00313         int values[] = {1, 150, -94, 94, 95, 95, 96, 96, 96, 97,
00314                         97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
00315                         99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
00316                         102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
00317                         104, 105, 105, 106, 106, 107, 108 };
00318         int shifts[LEN(values) * 2] = {0};
00319         shifts[2] = 1;
00320         shifts[4] = 1;
00321         cpl_image * out1, * out2;
00322 
00323         cpl_imagelist * list = cpl_imagelist_new();
00324         cpl_imagelist * dev = cpl_imagelist_new();
00325 
00326         cpl_image* img = cpl_image_wrap_double(3, 3, pixels);
00327         for (int i = 0; i < LEN(values); i++) {
00328             cpl_image_set(img, 2, 2, values[i]);
00329             cpl_imagelist_set(list, img, i);
00330             img = cpl_image_duplicate(img);
00331         }
00332         cpl_image_delete(img);
00333         out1 = cpl_imagelist_get(list, 1);
00334         out2 = cpl_imagelist_get(list, 2);
00335         cpl_image_set(out1, 2, 2, 0);
00336         cpl_image_set(out1, 3, 2, -94);
00337         cpl_image_set(out2, 2, 2, 0);
00338         cpl_image_set(out2, 3, 2, 150);
00339 
00340         visir_util_clip_kappa_sigma_double(list, dev, 1.0, 3, 3, shifts);
00341 
00342         cpl_image * map = cpl_image_new_from_accepted(list);
00343         //cpl_image_dump_structure(map, stdout);
00344         //cpl_image_dump_window(map, 2, 2, 3, 3, stdout);
00345         //cpl_image_dump_window(img, 1, 1, 3, 3, stdout);
00346         cpl_test_eq(LEN(values) - 1, cpl_image_get(map, 2, 2, &dump));
00347         cpl_test_eq(LEN(values) - 2, cpl_image_get(map, 3, 2, &dump));
00348         dump = 0;
00349         cpl_image_get(out1, 3, 2, &dump);
00350         cpl_test_eq(dump, 1);
00351         dump = 0;
00352         cpl_image_get(out2, 3, 2, &dump);
00353         cpl_test_eq(dump, 1);
00354 
00355         cpl_image_delete(map);
00356         cpl_imagelist_delete(list);
00357         cpl_imagelist_delete(dev);
00358     }
00359 
00360     if (stream != stdout) cpl_test_zero( fclose(stream) );
00361 
00362 }
00363 
00364 static void visir_fftxcorrelate_test(void)
00365 {
00366     FILE          * stream;
00367     int dump;
00368     double res = -1;
00369 
00370     stream = cpl_msg_get_level() > CPL_MSG_INFO
00371         ? fopen("/dev/null", "a") : stdout;
00372 
00373     cpl_test_nonnull( stream );
00374     {
00375         double xshift = 0, yshift = 0;
00376         double * values = cpl_calloc(16, sizeof(double));
00377         cpl_error_code err;
00378 
00379         values[IND(2,2,4)] = 1;
00380         cpl_image * tmp = cpl_image_wrap_double(4, 4, values);
00381         values = cpl_calloc(16, sizeof(double));
00382         values[IND(3,3,4)] = 1;
00383         cpl_image * img2 = cpl_image_wrap_double(4, 4, values);
00384 
00385         err = visir_fftxcorrelate(tmp, img2, CPL_TRUE, &xshift, &yshift);
00386         cpl_test_eq(CPL_ERROR_NONE, err);
00387 
00388         cpl_test_rel(1, xshift, 0.05);
00389         cpl_test_rel(1, yshift, 0.05);
00390 
00391         cpl_image_delete(tmp);
00392         cpl_image_delete(img2);
00393     }
00394     /* subpixel shift test */
00395     {
00396         cpl_size N = 5;
00397         double xshift = 0, yshift = 0;
00398         double * values = cpl_calloc(N*N, sizeof(double));
00399         cpl_error_code err;
00400 
00401         values[IND(2,1,N)] = 1;
00402         values[IND(2,2,N)] = 3;
00403         values[IND(2,3,N)] = 2;
00404         cpl_image * tmp = cpl_image_wrap_double(N, N, values);
00405         values = cpl_calloc(N*N, sizeof(double));
00406         values[IND(2,1,N)] = 0.5;
00407         values[IND(2,2,N)] = 2;
00408         values[IND(2,3,N)] = 2.5;
00409         values[IND(2,4,N)] = 1;
00410         cpl_image * img2 = cpl_image_wrap_double(N, N, values);
00411 
00412         err = visir_fftxcorrelate(tmp, img2, CPL_TRUE, &xshift, &yshift);
00413         cpl_test_eq(CPL_ERROR_NONE, err);
00414 
00415         cpl_test_rel(1, xshift+1, 0.05);
00416         /* subpixel estimate has low precision */
00417         cpl_test_rel(0.5, yshift, 0.15);
00418 
00419         cpl_image_delete(tmp);
00420         cpl_image_delete(img2);
00421     }
00422     /* subpixel shift test 2*/
00423     {
00424         cpl_size N = 5;
00425         double xshift = 0, yshift = 0;
00426         double * values = cpl_calloc(N*N, sizeof(double));
00427         cpl_error_code err;
00428 
00429         values[IND(2,1,N)] = 1;
00430         values[IND(2,2,N)] = 3;
00431         values[IND(2,3,N)] = 2;
00432         cpl_image * tmp = cpl_image_wrap_double(N, N, values);
00433         values = cpl_calloc(N*N, sizeof(double));
00434         values[IND(3,0,N)] = 2;
00435         values[IND(3,1,N)] = 2.5;
00436         values[IND(3,2,N)] = 1;
00437         cpl_image * img2 = cpl_image_wrap_double(N, N, values);
00438 
00439         err = visir_fftxcorrelate(tmp, img2, CPL_TRUE, &xshift, &yshift);
00440         cpl_test_eq(CPL_ERROR_NONE, err);
00441 
00442         cpl_test_rel(1, xshift, 0.05);
00443         /* subpixel estimate has low precision */
00444         cpl_test_rel(-1.5, yshift, 0.1);
00445 
00446         cpl_image_delete(tmp);
00447         cpl_image_delete(img2);
00448     }
00449     /* test bad pixel */
00450     {
00451         double xshift = 0, yshift = 0;
00452         double * values = cpl_calloc(16, sizeof(double));
00453         cpl_error_code err;
00454 
00455         values[IND(2,2,4)] = 1;
00456         cpl_image * tmp = cpl_image_wrap_double(4, 4, values);
00457         values = cpl_calloc(16, sizeof(double));
00458         values[IND(3,1,4)] = 2;
00459         values[IND(1,1,4)] = 200;
00460         cpl_image * img2 = cpl_image_wrap_double(4, 4, values);
00461 
00462         cpl_mask * m = cpl_mask_threshold_image_create(img2, 10, FLT_MAX);
00463         cpl_image_reject_from_mask(img2, m);
00464         cpl_mask_delete(m);
00465 
00466         err = visir_fftxcorrelate(tmp, img2, CPL_TRUE, &xshift, &yshift);
00467         cpl_test_eq(CPL_ERROR_NONE, err);
00468 
00469         cpl_test_rel(1, xshift, 0.05);
00470         cpl_test_rel(-1, yshift, 0.05);
00471 
00472         cpl_image_delete(tmp);
00473         cpl_image_delete(img2);
00474     }
00475     /* test non-square */
00476     {
00477         cpl_size Nx = 8, Ny = 4;
00478         double xshift = 0, yshift = 0;
00479         double * values = cpl_calloc(Nx*Ny, sizeof(double));
00480         cpl_error_code err;
00481 
00482         values[IND(2,1,Nx)] = 1;
00483         values[IND(2,2,Nx)] = 3;
00484         values[IND(2,3,Nx)] = 2;
00485         cpl_image * tmp = cpl_image_wrap_double(Nx, Ny, values);
00486         values = cpl_calloc(Nx*Ny, sizeof(double));
00487         values[IND(3,0,Nx)] = 2;
00488         values[IND(3,1,Nx)] = 2.5;
00489         values[IND(3,2,Nx)] = 1;
00490         cpl_image * img2 = cpl_image_wrap_double(Nx, Ny, values);
00491 
00492         err = visir_fftxcorrelate(tmp, img2, CPL_TRUE, &xshift, &yshift);
00493         cpl_test_eq(CPL_ERROR_NONE, err);
00494 
00495         cpl_test_rel(1, xshift, 0.05);
00496         /* subpixel estimate has low precision */
00497         cpl_test_rel(-1.5, yshift, 0.1);
00498 
00499         cpl_image_delete(tmp);
00500         cpl_image_delete(img2);
00501     }
00502 
00503     if (stream != stdout) cpl_test_zero( fclose(stream) );
00504 }

Generated on Mon Feb 6 15:23:49 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8