aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-06 15:34:34 -0800
committerChristian C <cc@localhost>2025-03-06 15:34:34 -0800
commit9c6ca913d0b7f9b9ae95f72d6187944a8a940228 (patch)
tree2b6ffaa14fcce621aaff17aef27c29a19dad6ec5 /src/main.c
parentfa4a17e9ab5b55f8b8da3d52ee05ae62dde2e3a8 (diff)
Simplify execution
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c98
1 files changed, 6 insertions, 92 deletions
diff --git a/src/main.c b/src/main.c
index 4d19dc3..a18d602 100644
--- a/src/main.c
+++ b/src/main.c
@@ -133,110 +133,28 @@ int main(int argc, char** argv)
return 1;
}
+ uint16_t* new_masks;
// Regenerate contiguous labels
- starting_label = 1;
- uint16_t* 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++;
- }
- }
- }
- uint16_t *temp = labels;
- labels = masks;
- masks = temp;
- free(labels);
+ reduce_contiguous_regions(&masks, width, height, &starting_label);
if (!silent) {
printf("%u labels found\n", starting_label-1);
printf("Mask dimensions: %u %u\n", width, height);
}
- // 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);
- if (!silent) {
- printf("Information retrieval took %f ms\n", 1000*diff_time(&ts_info_end, &ts_info_start));
- }
-#ifdef AVL_INFO
- if (!silent) {
- 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);
-
- // 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;
- }
- }
- }
+ filter_small_masks(masks, width, height, MIN_AREA, MIN_PERIMETER);
TIME(ts_filter_end);
if (!silent) {
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);
+ reduce_contiguous_regions(&masks, width, height, &starting_label);
if (!silent) {
printf("%u remaining 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]);
- }
- }
- }
- }
+ root = get_mask_data(masks, width, height);
#ifdef AVL_INFO
if (!silent) {
printf("Inorder traversal of AVL tree: ");
@@ -248,11 +166,7 @@ int main(int argc, char** argv)
uint16_t *new_labels;
TIME(ts_start);
- new_labels = closeup(masks, width, height, N_DILATIONS);
- if (new_labels != NULL) {
- free(masks);
- masks = new_labels;
- }
+ closeup(&masks, width, height, N_DILATIONS);
TIME(ts_end);
if (!silent) {
printf("Closing up took %f ms\n", 1000*diff_time(&ts_end, &ts_start));