diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/seg/mask_data.c | 15 | ||||
-rw-r--r-- | src/main.c | 112 |
2 files changed, 101 insertions, 26 deletions
diff --git a/src/lib/seg/mask_data.c b/src/lib/seg/mask_data.c index 2ff58a8..2f3a51a 100644 --- a/src/lib/seg/mask_data.c +++ b/src/lib/seg/mask_data.c @@ -190,6 +190,21 @@ void print_in_order_uint16_t(struct AVLNode* root) } } +// Check if uint16_t in AVLTree with uint16_t* data +bool_t in_uint16_t_tree(struct AVLNode* root, uint16_t value) +{ + if (root == NULL) { + return FALSE; + } + if (*((uint16_t*)root->data) == value) { + return TRUE; + } else if (value < *((uint16_t*)root->data)) { + return in_uint16_t_tree(root->left, value); + } else { + return in_uint16_t_tree(root->right, value); + } +} + // Filter out small masks // Assumption: Contiguous labeling struct AVLNode* get_small_labels(struct AVLNode* removal_tree, struct AVLNode* label_tree, size_t min_area, size_t min_perimeter) @@ -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); } |