diff options
author | Christian C <cc@localhost> | 2025-03-05 20:18:59 -0800 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-05 20:18:59 -0800 |
commit | 9389fa47a068c167b8074d082fb3bdbeec79bdff (patch) | |
tree | 2e7123bc8cf45ce53005fc5d38ebf9b5849997d3 /src/main.c | |
parent | 5b3cb257506f3fbe8c5ad3ea8aedb87d51e4c87c (diff) |
Filter small labels
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 112 |
1 files changed, 86 insertions, 26 deletions
@@ -19,6 +19,9 @@ #define N_DILATIONS 3 +#define MIN_AREA 200 +#define MIN_PERIMETER 50 + int main(int argc, char** argv) { TIME(ts_g_start); @@ -60,6 +63,8 @@ int main(int argc, char** argv) fprintf(stderr, "No masks found!\n"); return 1; } + + // Regenerate contiguous labels starting_label = 1; uint16_t* labels = (uint16_t*)calloc(width*height, sizeof(uint16_t)); if (labels == NULL) { @@ -89,7 +94,87 @@ int main(int argc, char** argv) } TIME(ts_end); printf("Dilation took %f ms\n", 1000*diff_time(&ts_end, &ts_start)); - //free(masks); + // Get the area/ perimeter of each label + root = NULL; + TIME(ts_info_start); + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + if (masks[x + y*width] != 0) { + root = increase_label_area_alloc(root, masks[x + y*width]); + if (is_on_mask_boundary(masks, width, height, x, y)) { + increase_label_perimeter(root, masks[x + y*width]); + } + } + } + } + TIME(ts_info_end); + printf("Information retrieval took %f ms\n", 1000*diff_time(&ts_info_end, &ts_info_start)); +#ifdef AVL_INFO + printf("Inorder traversal of AVL tree: "); + print_label(root); + printf("\n"); +#endif + TIME(ts_filter_start); + // Get the smallest labels + struct AVLNode* small_label_tree = NULL; + small_label_tree = get_small_labels(NULL, root, MIN_AREA, MIN_PERIMETER); + + print_in_order_uint16_t(small_label_tree); + printf("\n"); + + // Remove the small labels + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + if (in_uint16_t_tree(small_label_tree, masks[x + y*width])) { + masks[x + y*width] = 0; + } + } + } + TIME(ts_filter_end); + printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start)); + free_avl_tree(small_label_tree); + free_avl_tree_nodes(root); + + // Regenerate contiguous labels + starting_label = 1; + labels = (uint16_t*)calloc(width*height, sizeof(uint16_t)); + if (labels == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free(masks); + } + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + size_t coord = x + y*width; + uint8_t channels = 2; + if (flood((uint8_t*)masks, labels, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) { + starting_label++; + } + } + } + temp = labels; + labels = masks; + masks = temp; + free(labels); + printf("%u labels found\n", starting_label-1); + printf("Mask dimensions: %u %u\n", width, height); + // Regenerate information after relabeling + root = NULL; + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + if (masks[x + y*width] != 0) { + root = increase_label_area_alloc(root, masks[x + y*width]); + if (is_on_mask_boundary(masks, width, height, x, y)) { + increase_label_perimeter(root, masks[x + y*width]); + } + } + } + } +#ifdef AVL_INFO + printf("Inorder traversal of AVL tree: "); + print_label(root); + printf("\n"); +#endif + //----------------------------------------------- #ifdef VISUAL @@ -180,31 +265,6 @@ int main(int argc, char** argv) CloseWindow(); #else if (masks != NULL) { - // Get the area/ perimeter of each label - TIME(ts_start); - for (size_t y = 0; y < height; y++) { - for (size_t x = 0; x < width; x++) { - if (masks[x + y*width] != 0) { - root = increase_label_area_alloc(root, masks[x + y*width]); - if (is_on_mask_boundary(masks, width, height, x, y)) { - increase_label_perimeter(root, masks[x + y*width]); - } - } - } - } - TIME(ts_end); - printf("Information retrieval took %f ms\n", 1000*diff_time(&ts_end, &ts_start)); -#ifdef AVL_INFO - printf("Inorder traversal of AVL tree: "); - print_label(root); - printf("\n"); -#endif - // Get the smallest labels - struct AVLNode* small_label_tree = NULL; - small_label_tree = get_small_labels(NULL, root, 50, 50); - free_avl_tree(small_label_tree); - free_avl_tree_nodes(root); - root = NULL; write_array("../out.bin", masks, width*height*sizeof(uint16_t)); free(masks); } |