/* $Id: cpl_mask-test.c,v 1.23 2007/11/22 08:48:48 yjung Exp $ * * This file is part of the ESO Common Pipeline Library * Copyright (C) 2001-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * $Author: yjung $ * $Date: 2007/11/22 08:48:48 $ * $Revision: 1.23 $ * $Name: $ */ /*----------------------------------------------------------------------------- Includes -----------------------------------------------------------------------------*/ #include #include "cpl_mask.h" #include "cpl_image_gen.h" #include "cpl_stats.h" #include "cpl_image_io.h" #include "cpl_memory.h" #include "cpl_msg.h" #include "cpl_tools.h" /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ int main(void) { cpl_image * imd ; double median, med_dist ; cpl_mask * bin1, * bin2, * bin1_dup ; int nb_sel1, nb_sel2 ; cpl_image * labels1, * labels2 ; double threshold ; int count ; int nb_objs1, nb_objs2 ; cpl_matrix * kernel ; double * ker_arr ; int new_pos[] = {1,2,3,4}; /* char outname[1024] ; */ const int nsize = 512; cpl_binary * bins ; cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING); /* Insert tests below */ /* Create test image */ cpl_msg_info("","Create a %dx%d test image", nsize, nsize); imd = cpl_image_fill_test_create(nsize, nsize) ; if (imd==NULL) { return cpl_test_end(1); } /* Compute the median and the av. dist. to the median of the loaded image */ median = cpl_image_get_median_dev(imd, &med_dist) ; /* Binarize the image with a first threshold */ cpl_msg_info("","Threshold the test image with median+2*sigma -> bin1") ; threshold = median + 2.0 * med_dist ; if ((bin1 = cpl_mask_threshold_image_create(imd, threshold, DBL_MAX)) == NULL) { cpl_image_delete(imd) ; return cpl_test_end(1); } cpl_test(cpl_mask_is_empty(bin1) == CPL_FALSE); /* Test wrap / unwrap */ bins = cpl_malloc(100*sizeof(cpl_binary)) ; bin2 = cpl_mask_wrap(10, 10, bins) ; cpl_mask_delete(bin2) ; bins = cpl_malloc(100*sizeof(cpl_binary)) ; bin2 = cpl_mask_wrap(10, 10, bins) ; cpl_mask_unwrap(bin2) ; cpl_free(bins) ; /* Binarize the image with a second threshold */ cpl_msg_info("","Threshold the test image with median+5*sigma -> bin2") ; threshold = median + 5.0 * med_dist ; if ((bin2 = cpl_mask_threshold_image_create(imd, threshold, DBL_MAX)) == NULL) { cpl_image_delete(imd) ; cpl_mask_delete(bin1) ; return cpl_test_end(1); } cpl_image_delete(imd) ; /* Test cpl_mask_get() */ /* Test cpl_mask_set() */ count = cpl_mask_count(bin1) ; if (cpl_mask_get(bin1, 1, 1) == CPL_BINARY_0) { cpl_mask_set(bin1, 1, 1, CPL_BINARY_1) ; if (cpl_mask_count(bin1) != count + 1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } } else { cpl_mask_set(bin1, 1, 1, CPL_BINARY_0) ; if (cpl_mask_count(bin1) != count - 1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } } /* Test cpl_mask_duplicate() */ bin1_dup = cpl_mask_duplicate(bin1) ; cpl_test(cpl_mask_is_empty(bin1) == cpl_mask_is_empty(bin1_dup)); cpl_test (cpl_mask_count(bin1_dup) == cpl_mask_count(bin1)); cpl_mask_delete(bin1_dup) ; /* Compute number of selected pixels */ if ((nb_sel1 = cpl_mask_count(bin1)) == -1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } if ((nb_sel2 = cpl_mask_count(bin2)) == -1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } if ((nb_sel1 = cpl_mask_count_window(bin1,1,1,1,1)) == -1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } if ((nb_sel2 = cpl_mask_count_window(bin2,1,1,1,1)) == -1) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test cpl_mask_turn() */ cpl_test(cpl_mask_count(bin1) != 0); bin1_dup = cpl_mask_duplicate(bin1); /* Rotate the mask all the way around */ cpl_test(cpl_mask_turn(bin1, 9) == CPL_ERROR_NONE); cpl_test(cpl_mask_turn(bin1, 6) == CPL_ERROR_NONE); cpl_test(cpl_mask_turn(bin1, -13) == CPL_ERROR_NONE); cpl_test(cpl_mask_turn(bin1, 10) == CPL_ERROR_NONE); cpl_test(cpl_mask_turn(bin1, 4) == CPL_ERROR_NONE); /* Compare to its copy */ cpl_test(cpl_mask_xor(bin1_dup, bin1) == CPL_ERROR_NONE); cpl_test(cpl_mask_count(bin1_dup) == 0); cpl_test(cpl_mask_is_empty(bin1_dup) == CPL_TRUE); cpl_mask_delete(bin1_dup); /* Build a 3x3 kernel */ ker_arr = cpl_malloc(9*sizeof(double)) ; ker_arr[0] = 1 ; ker_arr[1] = 1 ; ker_arr[2] = 1 ; ker_arr[3] = 1 ; ker_arr[4] = 1 ; ker_arr[5] = 1 ; ker_arr[6] = 1 ; ker_arr[7] = 1 ; ker_arr[8] = 1 ; kernel = cpl_matrix_wrap(3, 3, ker_arr) ; cpl_msg_info("","Apply a morphological closing on bin1") ; /* Test erosion function */ if (cpl_mask_erosion(bin1, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test dilation function */ if (cpl_mask_dilation(bin1, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_msg_info("","Apply a morphological opening on bin1") ; /* Test opening function */ if (cpl_mask_opening(bin1, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_msg_info("","Apply a morphological closing on bin2") ; /* Test closing function */ if (cpl_mask_closing(bin2, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_msg_info("","Apply a morphological opening on bin2") ; /* Test dilation function */ if (cpl_mask_dilation(bin2, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test erosion function */ if (cpl_mask_erosion(bin2, kernel) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_matrix_delete(kernel) ; /* Labelize the binary maps */ if ((labels1 = cpl_image_labelise_mask_create(bin1, &nb_objs1)) == NULL) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_msg_info("","Labelize bin1 ---> %d objects found", nb_objs1) ; if ((labels2 = cpl_image_labelise_mask_create(bin2, &nb_objs2)) == NULL) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_msg_info("","Labelize bin2 ---> %d objects found", nb_objs2) ; /* sprintf(outname, "labelized1.fits") ; */ /* cpl_image_save(labels1, outname, CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_DEFAULT) ; */ /* sprintf(outname, "labelized2.fits") ; */ /* cpl_image_save(labels2, outname, CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_DEFAULT) ; */ cpl_image_delete(labels1) ; cpl_image_delete(labels2) ; /* Test cpl_mask_and */ if (cpl_mask_and(bin1, bin2) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test cpl_mask_or */ if (cpl_mask_or(bin1, bin2) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test cpl_mask_not */ if (cpl_mask_not(bin2) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } /* Test cpl_mask_xor */ if (cpl_mask_xor(bin1, bin2) != CPL_ERROR_NONE) { cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; return cpl_test_end(1); } cpl_mask_delete(bin2) ; /* Test cpl_mask_collapse_create() */ bin2 = cpl_mask_collapse_create(bin1, 0) ; if (bin2==NULL) return cpl_test_end(1); cpl_mask_delete(bin2) ; /* Test cpl_mask_extract() */ bin2 = cpl_mask_extract(bin1, 1, 1, 1, 1) ; if (bin2==NULL) return cpl_test_end(1); cpl_mask_delete(bin2) ; /* Test cpl_mask_shift() */ if (cpl_mask_shift(bin1, 1, 1) != CPL_ERROR_NONE) return cpl_test_end(1); /* Test cpl_mask_flip() */ if (cpl_mask_flip(bin1, 0) != CPL_ERROR_NONE) return cpl_test_end(1); if (cpl_mask_flip(bin1, 1) != CPL_ERROR_NONE) return cpl_test_end(1); if (cpl_mask_flip(bin1, 2) != CPL_ERROR_NONE) return cpl_test_end(1); if (cpl_mask_flip(bin1, 3) != CPL_ERROR_NONE) return cpl_test_end(1); /* Test cpl_mask_copy() */ bin2 = cpl_mask_duplicate(bin1) ; if (cpl_mask_copy(bin1, bin2, 1, 1) != CPL_ERROR_NONE) return cpl_test_end(1); cpl_mask_delete(bin2) ; /* Test cpl_mask_move() */ if (cpl_mask_move(bin1, 2, new_pos) != CPL_ERROR_NONE) return cpl_test_end(1); /* Test cpl_mask_extract_subsample() */ bin2 = cpl_mask_extract_subsample(bin1, 2, 2) ; if (bin2 == NULL) return cpl_test_end(1); cpl_mask_delete(bin1) ; cpl_mask_delete(bin2) ; /* End of tests */ return cpl_test_end(0); }