1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include <lib/mem/galloc.h>
#include <lib/seg/mask_data.h>
#include <lib/seg/util.h>
#include <stdio.h>
// Allocate Mask Data for Label
MaskData *create_mask_data(MaskData_t label) {
MaskData *data = (MaskData *)g_malloc(sizeof(MaskData));
data->label = label;
data->area = 0;
data->perimeter = 0;
return data;
}
// Compare mask data labels
bool_t compare_labels(MaskData *left, MaskData *right) {
return left->label < right->label;
}
// Create AVL Mask node
AVLNode *create_avl_mask_node(MaskData *data) {
return create_avl_node((void *)data, (AvlComparator)compare_labels);
}
// Insert MaskData into the AVL Tree
Result insert_mask(AVLNode *node, MaskData *data) {
return avl_insert(node, (void *)data, (AvlComparator)compare_labels);
}
// Allocate a label's Mask data in a tree
// If it already exists, skip the allocation
AVLNode *insert_mask_alloc(AVLNode *node, MaskData_t label) {
MaskData *data = create_mask_data(label);
Result result = insert_mask(node, data);
if (!result.success) {
g_free(data);
}
return (AVLNode *)result.data;
}
// Print AVL Node Mask Data Label
void print_label(AVLNode *root) {
if (root != NULL) {
print_label(root->left);
MaskData *data = root->data;
printf("%d: (%zu, %zu) ", data->label, data->area, data->perimeter);
print_label(root->right);
}
}
// Increase the label's area
bool_t increase_label_area(AVLNode *root, MaskData_t label) {
if (root == NULL) {
return FALSE;
}
MaskData *data = (MaskData *)root->data;
if (data->label == label) {
data->area++;
} else if (data->label > label) {
return increase_label_area(root->left, label);
} else if (data->label < label) {
return increase_label_area(root->right, label);
}
return TRUE;
}
// Increase the label's perimeter
bool_t increase_label_perimeter(AVLNode *root, MaskData_t label) {
if (root == NULL) {
return FALSE;
}
MaskData *data = (MaskData *)root->data;
if (data->label == label) {
data->perimeter++;
} else if (data->label > label) {
return increase_label_perimeter(root->left, label);
} else if (data->label < label) {
return increase_label_perimeter(root->right, label);
}
return TRUE;
}
// Increase the label's area
// Create an AVL node if it doesn't exist
AVLNode *increase_label_area_alloc(AVLNode *root, MaskData_t label) {
AVLNode *new_root = root;
bool_t success = increase_label_area(new_root, label);
if (success == FALSE) {
new_root = insert_mask_alloc(new_root, label);
increase_label_area(new_root, label);
}
return new_root;
}
// Increase the label's perimeter
// Create an AVL node if it doesn't exist
AVLNode *increase_label_perimeter_alloc(AVLNode *root, MaskData_t label) {
AVLNode *new_root = root;
bool_t success = increase_label_perimeter(new_root, label);
if (success == FALSE) {
new_root = insert_mask_alloc(new_root, label);
increase_label_perimeter(new_root, label);
}
return new_root;
}
// Comparison of MaskData_ts
bool_t compare_image_mask_data_t(MaskData_t *s1, MaskData_t *s2) {
return *s1 < *s2;
}
// In-order traversal print pointer
void print_in_order_image_mask_data_t(AVLNode *root) {
if (root != NULL) {
print_in_order_image_mask_data_t(root->left);
printf("%d ", *((MaskData_t *)root->data));
print_in_order_image_mask_data_t(root->right);
}
}
// Check if MaskData_t in AVLTree with MaskData_t* data
bool_t in_image_mask_data_t_tree(AVLNode *root, MaskData_t value) {
if (root == NULL) {
return FALSE;
}
if (*((MaskData_t *)root->data) == value) {
return TRUE;
} else if (value < *((MaskData_t *)root->data)) {
return in_image_mask_data_t_tree(root->left, value);
} else {
return in_image_mask_data_t_tree(root->right, value);
}
}
// Filter out small masks
// Assumption: Contiguous labeling
AVLNode *get_small_labels(AVLNode *removal_tree, AVLNode *label_tree,
size_t min_area, size_t min_perimeter) {
AVLNode *return_tree = removal_tree;
if (label_tree != NULL) {
return_tree = get_small_labels(return_tree, label_tree->left, min_area,
min_perimeter);
MaskData *node_data = (MaskData *)label_tree->data;
if ((node_data->area < min_area) ||
(node_data->perimeter < min_perimeter)) {
// Insert
Result result =
avl_insert(return_tree, &node_data->label,
(bool_t(*)(void *, void *))compare_image_mask_data_t);
if (result.success) {
return_tree = result.data;
}
}
return_tree = get_small_labels(return_tree, label_tree->right, min_area,
min_perimeter);
}
return return_tree;
}
// Get mask label data
AVLNode *get_mask_data(Mask *mask) {
uint32_t width = mask->width;
uint32_t height = mask->height;
AVLNode *root = NULL;
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
if (mask->image[y][x] != 0) {
root = increase_label_area_alloc(root, mask->image[y][x]);
if (is_on_mask_boundary(mask, x, y)) {
increase_label_perimeter(root, mask->image[y][x]);
}
}
}
}
return root;
}
// Filter out small masks in mask
void filter_small_masks(Mask *mask, size_t min_area, size_t min_perimeter) {
uint32_t width = mask->width;
uint32_t height = mask->height;
AVLNode *root = get_mask_data(mask);
AVLNode *small_label_tree = NULL;
small_label_tree = get_small_labels(NULL, root, min_area, min_perimeter);
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
if (in_image_mask_data_t_tree(small_label_tree, mask->image[y][x])) {
mask->image[y][x] = 0;
}
}
}
free_avl_tree(small_label_tree);
free_avl_tree_nodes(root);
}
|