forked from rajbot/autocrop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocrop_remove_bg.c
239 lines (178 loc) · 6.8 KB
/
autocrop_remove_bg.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
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
#include <math.h> //for sqrt
#include <assert.h>
#include <float.h> //for DBL_MAX
#include <limits.h> //for INT_MAX
#include "autoCropCommon.h"
#include "autocrop_remove_bg.h"
/* This file contains quick-and-dirty background removal code. It operates on images
which have already been bitonalized. We are using these functions with low resolution
images (scaled down by 8x) in order to set a rough crop box.
These functions first set an inital crop box based on 80% of the width of the image
and 100% of the height. We set the inital crop to the outer edge of the book,
since there might be bookmarks and other pieces of paper inserted on the binding side.
We then shrink the initial crop box by removing black lines. We consider a line to
be black if the number of black pixels in the line meets or exceeds black_pixel_percentage.
For scribe books, we pass in black_pixel_percentage=0.90. This allows for bookmarks
or other narrow pieces of paper to be sticking outside the edge of the book.
For foldouts, we want to crop to the very outside edge of the folio, so we pass in
a black_pixel_percentage of 95%.
*/
/// remove_bg_top()
///____________________________________________________________________________
l_int32 remove_bg_top(PIX *pixb, l_int32 rotDir, float black_pixel_percentage) {
l_int32 w, h, d;
pixGetDimensions(pixb, &w, &h, &d);
assert(pixGetDepth(pixb) == 1);
l_uint32 a;
l_uint32 limitL, limitR, limitB;
if (1 == rotDir) {
limitL = (l_uint32)(0.20*w);
limitR = w-1;
} else if (-1 == rotDir) {
limitL = 1;
limitR = (l_uint32)(0.80*w);
} else if (0 == rotDir) {
limitL = 1;
limitR = w-1;
} else {
assert(0);
}
limitB = l_uint32(0.80*h);
//printf("T: limitL=%d, limitR=%d, limitB=%d\n", limitL, limitR, limitB);
//number of black pels required for this line to be considered part of the background
l_uint32 numBlackRequired = (l_uint32)(black_pixel_percentage*(limitR-limitL));
l_uint32 i, j;
for(j=0; j<=limitB; j++) {
l_uint32 numBlackPels = 0;
for (i=limitL; i<=limitR; i++) {
l_int32 retval = pixGetPixel(pixb, i, j, &a);
assert(0 == retval);
if (PEL_IS_BLACK == a) {
numBlackPels++;
}
}
//printf("T %d: numBlack=%d\n", j, numBlackPels);
if (numBlackPels<numBlackRequired) {
//printf("break at %d!\n", j);
return j;
}
}
return 0;
}
/// remove_bg_bottom()
///____________________________________________________________________________
l_int32 remove_bg_bottom(PIX *pixb, l_int32 rotDir, float black_pixel_percentage) {
l_int32 w, h, d;
pixGetDimensions(pixb, &w, &h, &d);
assert(pixGetDepth(pixb) == 1);
l_uint32 a;
l_int32 limitL, limitR, limitT;
if (1 == rotDir) {
limitL = (l_uint32)(w*0.20);
limitR = w-1;
} else if (-1 == rotDir) {
limitL = 1;
limitR = (l_uint32)(w*0.80);
} else if (0 == rotDir) {
limitL = 1;
limitR = w-1;
} else {
assert(0);
}
limitT = l_uint32(0.20*h);
//printf("B: limitL=%d, limitR=%d, limitT=%d\n", limitL, limitR, limitT);
//number of black pels required for this line to be considered part of the background
l_uint32 numBlackRequired = (l_uint32)(black_pixel_percentage*(limitR-limitL));
l_int32 i, j;
for(j=h-1; j>=limitT; j--) {
l_uint32 numBlackPels = 0;
for (i=limitL; i<=limitR; i++) {
l_int32 retval = pixGetPixel(pixb, i, j, &a);
assert(0 == retval);
if (PEL_IS_BLACK == a) {
numBlackPels++;
}
}
//printf("B %d: numBlack=%d\n", j, numBlackPels);
if (numBlackPels<numBlackRequired) {
//printf("break!\n");
return j;
}
}
return h-1;
}
/// remove_bg_outer_L()
///____________________________________________________________________________
l_int32 remove_bg_outer_L(PIX *pixb, l_int32 iStart, l_int32 iEnd, l_int32 limitT, l_int32 limitB, l_uint32 numBlackRequired) {
l_uint32 a;
l_int32 i, j;
for(i=iStart; i<=iEnd; i++) {
l_uint32 numBlackPels = 0;
for (j=limitT; j<=limitB; j++) {
l_int32 retval = pixGetPixel(pixb, i, j, &a);
assert(0 == retval);
if (PEL_IS_BLACK == a) {
numBlackPels++;
}
}
//debugstr("O %d: numBlack=%d\n", i, numBlackPels);
if (numBlackPels<numBlackRequired) {
//debugstr("remove_bg_outer_L break! (thresh=%d)\n", numBlackRequired);
return i;
}
}
return iStart;
}
/// remove_bg_outer_R()
///____________________________________________________________________________
l_int32 remove_bg_outer_R(PIX *pixb, l_int32 iStart, l_int32 iEnd, l_int32 limitT, l_int32 limitB, l_uint32 numBlackRequired) {
l_uint32 a;
l_int32 i, j;
for(i=iStart; i>=iEnd; i--) {
l_uint32 numBlackPels = 0;
for (j=limitT; j<=limitB; j++) {
l_int32 retval = pixGetPixel(pixb, i, j, &a);
assert(0 == retval);
if (PEL_IS_BLACK == a) {
numBlackPels++;
}
}
//debugstr("R %d: numBlack=%d\n", i, numBlackPels);
if (numBlackPels<numBlackRequired) {
//debugstr("remove_bg_outer_R break! (thresh=%d)\n", numBlackRequired);
return i;
}
}
return iStart;
}
/// remove_bg_outer()
///____________________________________________________________________________
l_int32 remove_bg_outer(PIX *pixb, l_int32 rotDir, l_uint32 topEdge, l_uint32 bottomEdge, float black_pixel_percentage) {
l_int32 w, h, d;
pixGetDimensions(pixb, &w, &h, &d);
assert(pixGetDepth(pixb) == 1);
l_uint32 kernelHeight10 = (l_uint32)(0.10*(bottomEdge-topEdge));
l_int32 limitT, limitB;
limitT = topEdge+kernelHeight10;
limitB = bottomEdge-kernelHeight10;
l_int32 step;
l_int32 iStart, iEnd;
//l_int32 initialBlackThresh = 140;
l_uint32 numBlackRequired = (l_uint32)(black_pixel_percentage*(limitB-limitT));
if (1 == rotDir) {
iStart = w-1;
iEnd = (l_int32)(w*0.20);
//debugstr("R: iStart=%d, iEnd=%d, limitT=%d, limitB=%d\n", iStart, iEnd, limitT, limitB);
return remove_bg_outer_R(pixb, iStart, iEnd, limitT, limitB, numBlackRequired);
} else if (-1 == rotDir) {
iStart = 0;
iEnd = (l_uint32)(w*0.80);
//debugstr("L: iStart=%d, iEnd=%d, limitT=%d, limitB=%d\n", iStart, iEnd, limitT, limitB);
return remove_bg_outer_L(pixb, iStart, iEnd, limitT, limitB, numBlackRequired);
} else {
assert(0);
}
}