-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautoCropFoldout.c
326 lines (240 loc) · 9.42 KB
/
autoCropFoldout.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
/*
Copyright(c)2008-2013 Internet Archive. Software license GPL version 2.
run with:
autoCropFoldout filein.jpg
*/
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
#include <assert.h>
#include <math.h> //for sqrt
#include <float.h> //for DBL_MAX
#include <limits.h> //for INT_MAX
#include "autoCropCommon.h"
#include "autocrop_remove_bg.h"
#define debugstr printf
//#define debugstr
//#define WRITE_DEBUG_IMAGES 1
#define black_pixel_percentage_foldout 0.95
static const l_float32 deg2rad = 3.1415926535 / 180.;
static inline l_int32 min (l_int32 a, l_int32 b) {
return b + ((a-b) & (a-b)>>31);
}
static inline l_int32 max (l_int32 a, l_int32 b) {
return a - ((a-b) & (a-b)>>31);
}
/// main()
///____________________________________________________________________________
int main(int argc, char **argv) {
PIX *pixs, *pixd, *pixg;
char *filein;
static char mainName[] = "autoCropFoldout";
FILE *fp;
long int should_deskew;
if ((argc < 2) || (argc > 3)) {
exit(ERROR_INT(" Syntax: autoCropFoldout filein.jpg [should_deskew=1]",
mainName, 1));
}
filein = argv[1];
if (argc == 3) {
should_deskew = strtol(argv[2], NULL, 10);
} else {
should_deskew = 1;
}
if ((fp = fopenReadStream(filein)) == NULL) {
exit(ERROR_INT("image file not found", mainName, 1));
}
debugstr("Opened file handle\n");
if ((pixs = pixReadStreamJpeg(fp, 0, 8, NULL, 0)) == NULL) {
exit(ERROR_INT("pixs not made", mainName, 1));
}
debugstr("Read jpeg\n");
//we don't rotate foldouts
pixd = pixs;
#ifdef WRITE_DEBUG_IMAGES
pixWrite(DEBUG_IMAGE_DIR "out.jpg", pixd, IFF_JFIF_JPEG);
#endif
l_int32 grayChannel;
pixg = ConvertToGray(pixd, &grayChannel);
debugstr("Converted to gray\n");
l_int32 histmax;
l_int32 threshInitial = CalculateTreshInitial(pixg, &histmax);
debugstr("threshInitial is %d\n", threshInitial);
float delta;
l_int32 cropT=-1, cropB=-1, cropR=-1, cropL=-1;
float deltaT, deltaB, deltaV1, deltaV2, deltaBinding, deltaOuter;
l_uint32 threshOuter, threshT, threshB;
/* create bitonal image */
PIX *pixb;
l_int32 w_8, h_8, d;
pixGetDimensions(pixg, &w_8, &h_8, &d);
pixOtsuAdaptiveThreshold(pixg,
w_8,
h_8,
1,
1,
0.1,
NULL,
&pixb);
#ifdef WRITE_DEBUG_IMAGES
{
pixWrite(DEBUG_IMAGE_DIR "outbininit.png", pixb, IFF_PNG);
}
#endif
//Perform quick removal of background
l_int32 topEdge = remove_bg_top(pixb, 0, black_pixel_percentage_foldout);
debugstr("topEdge is %d\n", topEdge);
l_int32 bottomEdge = remove_bg_bottom(pixb, 0, black_pixel_percentage_foldout);
debugstr("bottomEdge is %d\n", bottomEdge);
assert(bottomEdge>topEdge);
l_int32 rightEdge = remove_bg_outer(pixb, 1, topEdge, bottomEdge, black_pixel_percentage_foldout);
l_int32 leftEdge = remove_bg_outer(pixb, -1, topEdge, bottomEdge, black_pixel_percentage_foldout);
debugstr("rightEdge is %d\n", rightEdge);
debugstr("leftEdge is %d\n", leftEdge);
threshInitial = 158;
BOX *box;
l_int32 boxW10 = (l_int32)(0.10*(rightEdge-leftEdge)*8);
l_int32 boxH10 = (l_int32)(0.10*(bottomEdge-topEdge)*8);
box = boxCreate(leftEdge*8+boxW10, topEdge*8+boxH10, (rightEdge-leftEdge)*8-2*boxW10, (bottomEdge-topEdge)*8-2*boxH10);
double skewScore, skewConf;
PIX *pixBig;
startTimer();
if ((pixBig = pixRead(filein)) == NULL) {
exit(ERROR_INT("pixBig not made", mainName, 1));
}
printf("opened large jpg in %7.3f sec\n", stopTimer());
PIX *pixBigG;
if (kGrayModeThreeChannel != grayChannel) {
pixBigG = pixConvertRGBToGray (pixBig, (0==grayChannel), (1==grayChannel), (2==grayChannel));
} else {
pixBigG = pixConvertRGBToGray (pixBig, 0.30, 0.60, 0.10);
}
PIX *pixBigBFull; // = pixThresholdToBinary (pixBigC, threshInitial);
l_int32 w = pixGetWidth(pixBigG);
l_int32 h = pixGetHeight(pixBigG);
pixOtsuAdaptiveThreshold(pixBigG,
w,
h,
50,
50,
0.1,
NULL,
&pixBigBFull);
PIX *pixBigB = pixClipRectangle(pixBigBFull, box, NULL);
debugstr("croppedWidth = %d, croppedHeight=%d\n", pixGetWidth(pixBigB), pixGetHeight(pixBigB));
#ifdef WRITE_DEBUG_IMAGES
{
pixWrite(DEBUG_IMAGE_DIR "outbinfull.png", pixBigBFull, IFF_PNG);
pixWrite(DEBUG_IMAGE_DIR "outbinbig.png", pixBigB, IFF_PNG);
}
#endif
l_float32 angle, conf, textAngle;
if (should_deskew) {
debugstr("calling pixFindSkew\n");
if (pixFindSkew(pixBigB, &textAngle, &conf)) {
/* an error occured! */
debugstr("textAngle=%.2f\ntextConf=%.2f\n", 0.0, -1.0);
} else {
debugstr("textAngle=%.2f\ntextConf=%.2f\n", textAngle, conf);
}
} else {
angle = textAngle = conf = 0.0;
}
//Deskew(pixbBig, cropL*8, cropR*8, cropT*8, cropB*8, &skewScore, &skewConf);
l_int32 skewMode;
if (conf >= 2.0) {
printf("skewMode: text\n");
angle = textAngle;
skewMode = kSkewModeText;
} else {
printf("skewMode: none\n");
angle = 0.0;
skewMode = kSkewModeNone;
}
debugstr("rotating by %f\n", angle);
PIX *pixBigT = pixRotate(pixBigG,
deg2rad*angle,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
cropT = topEdge*8;
cropB = bottomEdge*8;
cropL = leftEdge*8;
cropR = rightEdge*8;
l_int32 outerCropL = cropL;
l_int32 outerCropR = cropR;
l_int32 outerCropT = cropT;
l_int32 outerCropB = cropB;
debugstr("finding clean lines...\n");
w = pixGetWidth(pixBigT);
h = pixGetHeight(pixBigT);
l_int32 limitLeft = calcLimitLeft(w,h,angle);
l_int32 limitTop = calcLimitTop(w,h,angle);
PIX *pixBigTbin;
pixOtsuAdaptiveThreshold(pixBigT,
w,
h,
50,
50,
0.1,
NULL,
&pixBigTbin);
#ifdef WRITE_DEBUG_IMAGES
{
pixWrite(DEBUG_IMAGE_DIR "outbinT.png", pixBigTbin, IFF_PNG);
}
#endif
//redo the rough crop on the full-resolution rotated image
cropT = remove_bg_top(pixBigTbin, 0, black_pixel_percentage_foldout);
debugstr("new cropT is %d\n", cropT);
cropB = remove_bg_bottom(pixBigTbin, 0, black_pixel_percentage_foldout);
debugstr("bottomEdge is %d\n", cropB);
assert(bottomEdge>topEdge);
cropR = remove_bg_outer(pixBigTbin, 1, cropT, cropB, black_pixel_percentage_foldout);
cropL = remove_bg_outer(pixBigTbin, -1, cropT, cropB, black_pixel_percentage_foldout);
debugstr("rightEdge is %d\n", rightEdge);
debugstr("leftEdge is %d\n", leftEdge);
debugstr("adjusted: cL=%d, cR=%d, cT=%d, cB=%d limitLeft=%d, limitTop=%d\n", cropL, cropR, cropT, cropB, limitLeft, limitTop);
printf("angle: %.2f\n", angle);
printf("conf: %.2f\n", conf); //TODO: this is the text deskew angle, but what if we are deskewing using the binding mode?
PrintKeyValue_int32("OuterCropL", cropL);
PrintKeyValue_int32("OuterCropR", cropR);
PrintKeyValue_int32("OuterCropT", cropT);
PrintKeyValue_int32("OuterCropB", cropB);
//debugstr("finding inner crop box (text block)...\n");
//l_int32 innerCropT, innerCropB, innerCropL, innerCropR;
//FindInnerCrop(pixBigT, threshInitial, cropL, cropR, cropT, cropB, &innerCropL, &innerCropR, &innerCropT, &innerCropB);
#ifdef WRITE_DEBUG_IMAGES
{
//BOX *boxCrop = boxCreate(cropL/8, cropT/8, (cropR-cropL)/8, (cropB-cropT)/8);
PIX *p = pixCopy(NULL, pixd);
PIX *pixFinalR = pixRotate(p,
deg2rad*angle,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
//pixRenderBoxArb(pixFinalR, boxCrop, 1, 255, 0, 0);
Box *boxOuterCrop = boxCreate(cropL/8, cropT/8, (cropR-cropL)/8, (cropB-cropT)/8);
pixRenderBoxArb(pixFinalR, boxOuterCrop, 1, 0, 0, 255);
/*
if ((-1 != innerCropL) & (-1 != innerCropR) & (-1 != innerCropT) & (-1 != innerCropB)) {
BOX *boxCropVar = boxCreate(innerCropL/8, innerCropT/8, (innerCropR-innerCropL)/8, (innerCropB-innerCropT)/8);
pixRenderBoxArb(pixFinalR, boxCropVar, 1, 0, 255, 0);
boxDestroy(&boxCropVar);
}
*/
pixWrite(DEBUG_IMAGE_DIR "outbox.jpg", pixFinalR, IFF_JFIF_JPEG);
pixDestroy(&pixFinalR);
pixDestroy(&p);
p = pixCopy(NULL, pixd);
PIX *pixFinalR2 = pixRotate(p,
deg2rad*angle,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
PIX *pixFinalC = pixClipRectangle(pixFinalR2, boxOuterCrop, NULL);
pixWrite(DEBUG_IMAGE_DIR "outcrop.jpg", pixFinalC, IFF_JFIF_JPEG);
pixDestroy(&p);
pixDestroy(&pixFinalC);
//boxDestroy(&boxCrop);
boxDestroy(&boxOuterCrop);
}
#endif
}