-
Notifications
You must be signed in to change notification settings - Fork 2
/
segmentation.c
436 lines (379 loc) · 11.3 KB
/
segmentation.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <stdlib.h>
#include <stdio.h>
#include "image.h"
#include "linkedlist.h"
#include "segmentation.h"
#include <assert.h>
#include <stdbool.h>
#include <stdbool.h>
LinkedList *segmentation(Img *source, bool whitespaces)
{
Block *block = img_make_block(source);
LinkedList *paragraphs = block_split_vertical(source, block, true);
LinkedList *chars = list_init();
Node *p = paragraphs->start;
while (p)
{
LinkedList *lines = line_split(source, p->data);
Node *l = lines->start;
while (l)
{
chars = list_concat(chars, character_split(source, l->data, whitespaces));
if (whitespaces)
{
Block *spacing = block_init();
spacing->label = ' ';
list_insert(chars, node_init(spacing));
}
l = l->next;
}
if (whitespaces && p->next)
{
Block *newline = block_init();
newline->label = '\n';
list_insert(chars, node_init(newline));
list_insert(chars, node_init(newline));
}
// Maybe free lines with content & blocks ?
p = p->next;
}
// Maybe free paragraphs with content & blocks ?
return chars;
}
Block *block_init()
{
Block *block = malloc(sizeof(Block));
block->x = 0;
block->y = 0;
block->width = 0;
block->height = 0;
block->label = '\0';
return block;
}
void block_delete(Block *block)
{
assert(block != NULL);
free(block);
}
Block *img_make_block(Img *image)
{
Block *block = block_init();
block->label = image->label;
block->x = 0;
block->y = 0;
block->width = image->width;
block->height = image->height;
return block;
}
Img *img_from_block(Img *source, Block *block)
{
Img *res = img_init(block->width, block->height);
for (int y = 0; y < block->height; y++)
{
for (int x = 0; x < block->width; x++)
{
res->pixels[y * block->width + x] = source->pixels[(block->y + y) * source->width + block->x + x];
}
}
return res;
}
double threshold = 0.99;
void remove_white_margin(Img *img, Block *block)
{
remove_top_margin(img, block);
remove_bottom_margin(img, block);
remove_left_margin(img, block);
remove_right_margin(img, block);
}
Block *remove_top_margin(Img *img, Block *block)
{
// Remove top margin
int y = block->y;
while (horizontal_white_rate(img, block, y) > threshold && y < block->y + block->height)
{
y++;
}
block->height = block->height - (y - block->y);
block->y = y;
return block;
}
Block *remove_bottom_margin(Img *img, Block *block)
{
// Remove bottom margin
int y = block->y + block->height - 1;
while (horizontal_white_rate(img, block, y) > threshold && y > 0)
y--;
block->height = y - block->y + 1;
return block;
}
Block *remove_left_margin(Img *img, Block *block)
{
// Remove left margin
int x = block->x;
// While the Xth column is white
while (vertical_white_rate(img, block, x) > threshold && x < block->x + block->width)
x++;
block->width = block->width - (x - block->x);
block->x = x;
return block;
}
Block *remove_right_margin(Img *img, Block *block)
{
// Remove right margin
int x = block->x + block->width - 1;
// While the Xth column is white
while (vertical_white_rate(img, block, x) > threshold && x > 0)
x--;
block->width = x - block->x + 1;
return block;
}
double vertical_white_rate(Img *image, Block *block, int x)
{
// Compute the average rate of white on the column
double rate = 0.0;
for (int y = 0; y < block->height; y++)
{
// Get the coordinate of the pixel in the entire image
int pixelIndex = (block->y + y) * image->width + x;
rate += image->pixels[pixelIndex];
}
// Average rate of white
rate /= block->height;
return rate;
}
double vertical_char_white_rate(Img *image, Block *block, int x)
{
// Compute the average rate of white on the column
double rate = 0.0;
for (int y = 0; y < block->height; y++)
{
// Get the coordinate of the pixel in the entire image
int pixelIndex = (block->y + y) * image->width + x;
// Check if pixel at x & x - 1 is black
rate += image->pixels[pixelIndex] || (x != block->x && image->pixels[pixelIndex - 1]);
}
// Average rate of white
rate /= block->height;
return rate;
}
double horizontal_white_rate(Img *image, Block *block, int y)
{
// Compute the average rate of white on the column
double rate = 0.0;
for (int x = 0; x < block->width; x++)
{
int pixelIndex = y * image->width + (block->x + x);
rate += image->pixels[pixelIndex];
}
// Average rate of white
rate /= block->width;
return rate;
}
LinkedList *block_split_vertical(Img *image, Block *block, bool shouldRetry)
{
/* Split the block in two, put them in a linked list */
// Remove margins
remove_white_margin(image, block);
int x = block->x;
int blackWidth = 0;
int whiteWidth = 0;
int currentColor = 0;
if (block->width <= 0 || block->height <= 0)
return NULL;
// Go through columns until finding a white column
while (whiteWidth <= 20 && x < block->x + block->width)
{
double rate = vertical_white_rate(image, block, x);
// If line is black
if (rate < threshold)
{
blackWidth++;
// If the last line was white
if (currentColor == 1)
{
whiteWidth = 0;
blackWidth = 1;
}
currentColor = 0;
}
else if (blackWidth > 5)
{
whiteWidth++;
currentColor = 1;
}
x++;
}
// If no cut is needed
if (x == block->x + block->width)
{
if (shouldRetry)
return block_split_horizontal(image, block, false);
else
{
LinkedList *res = list_init();
// Put the main block in the list
Node *node = node_init(block);
res->start = node;
res->end = node;
return res;
}
}
// If the block has been cut
Block *res1 = block_init();
Block *res2 = block_init();
// Cut the image
res1->x = block->x;
res1->width = x - block->x;
res2->x = x;
res2->width = (block->x + block->width) - x;
// Set y attributes
res1->y = block->y;
res1->height = block->height;
res2->y = block->y;
res2->height = block->height;
LinkedList *child1 = block_split_horizontal(image, res1, false);
LinkedList *child2 = block_split_vertical(image, res2, true);
if (child1 != NULL && child2 != NULL)
return list_concat(child1, child2);
else if(child2 == NULL)
return child1;
else
return child2;
}
LinkedList *block_split_horizontal(Img *image, Block *block, bool shouldRetry)
{
/* Split the block in two, put child blocks in a linked list */
// Remove margins
remove_white_margin(image, block);
int y = block->y;
int blackHeight = 0;
int whiteHeight = 0;
int currentColor = 0;
if (block->width <= 0 || block->height <= 0)
return NULL;
while (whiteHeight <= blackHeight * 1.5 && y < block->y + block->height)
{
double rate = horizontal_white_rate(image, block, y);
// If the line is black
if (rate < threshold)
{
blackHeight++;
// If the last line was white
if (currentColor == 1)
{
whiteHeight = 0;
blackHeight = 1;
}
currentColor = 0;
} // If the line is white
else if (blackHeight > 5)
{
whiteHeight++;
currentColor = 1;
}
y++;
}
// Remove last white lines
y -= whiteHeight;
// If no cut is needed
if (y == block->y + block->height)
{
if (shouldRetry)
return block_split_vertical(image, block, false);
else
{
LinkedList *res = list_init();
// Put the main block in the list
Node *node = node_init(block);
res->start = node;
res->end = node;
return res;
}
}
// If the block has been cut
Block *res1 = block_init();
Block *res2 = block_init();
// Cut the image
res1->y = block->y;
res1->height = y - block->y;
res2->y = y;
res2->height = block->height - y;
// Set y attributes
res1->x = block->x;
res1->width = block->width;
res2->x = block->x;
res2->width = block->width;
LinkedList *child1 = block_split_horizontal(image, res1, false);
LinkedList *child2 = block_split_vertical(image, res2, true);
if (child1 != NULL && child2 != NULL)
return list_concat(child1, child2);
else if(child2 == NULL)
return child1;
else
return child2;
}
LinkedList *line_split(Img *image, Block *block)
{
LinkedList *res = list_init();
Block *current = NULL;
for (int y = block->y; y < block->y + block->height; y++)
{
double rate = horizontal_white_rate(image, block, y);
// If the line is white and previous line is black
if (current != NULL && (rate > threshold || y == block->y + block->height - 1))
{
current->height = y - current->y;
list_insert(res, node_init(current));
current = NULL;
}
// If the line is black and the previous line is white (no block defined)
else if (rate < threshold && current == NULL)
{
// Create a new block
current = block_init();
current->y = y;
current->x = block->x;
current->width = block->width;
}
}
return res;
}
LinkedList *character_split(Img *image, Block *block, bool whitespaces)
{
LinkedList *res = list_init();
Block *current = NULL;
for (int x = block->x; x < block->x + block->width; x++)
{
double rate = vertical_char_white_rate(image, block, x);
// If the column is white and previous column is black
if (current != NULL && (rate > threshold || x == block->x + block->width - 1))
{
current->width = x - current->x;
remove_white_margin(image, current);
list_insert(res, node_init(current));
current = NULL;
}
// If the column is black and the previous column is white (no block defined)
else if (rate < threshold && current == NULL)
{
Block *last = res->end != NULL ? res->end->data : NULL;
// Insert whitespace if needed
if (whitespaces && last != NULL && x > last->x + last->width + block->height / 3)
{
Block *space = block_init();
space->x = last->x + last->width;
space->width = x - space->x;
space->y = block->y;
space->height = block->height;
space->label = ' ';
list_insert(res, node_init(space));
}
// Create a new block
current = block_init();
current->x = x - 1;
current->y = block->y;
current->height = block->height;
}
}
return res;
}