-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsptestview.mm
412 lines (335 loc) · 14.8 KB
/
dsptestview.mm
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
//
// dsptestview.m
// dsptest1
//
// Created by Lieven Govaerts on 08/02/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "dsptestview.h"
#import "tessocr.h"
#import "fuzzy_search_db.h"
#import "recognizer.h"
#include <stdlib.h>
#include <math.h>
#include "util.h"
#include "graphics.h"
@implementation dsptestview
static void
draw_bounding_boxes(unsigned char *outptr, const NSArray *comps,
int width, int height, int bitsPerPixel)
{
for(conn_box_t *box in comps)
{
if (box->img)
{
int boxwidth = box->xmax - box->xmin + 1;
int boxheight = box->ymax - box->ymin + 1;
/* Copy box image to out image */
for (int y = 0; y < boxheight; y++) {
int yloc = (box->ymin + y) * width * bitsPerPixel / 8;
for (int x = 0; x < boxwidth; x++) {
int xloc = (box->xmin + x) * bitsPerPixel / 8;
unsigned char *inptr = box->img + y * boxwidth + x;
unsigned char *curout = outptr + xloc + yloc;
*curout++ = *inptr; // r
*curout++ = *inptr; // g
*curout++ = *inptr; // b
*curout++ = 0;
}
}
}
// draw a red bounding box
for (int x = box->xmin; x <= box->xmax; x++) {
int xloc = x * bitsPerPixel / 8;
// top
int yloc = box->ymin * width * bitsPerPixel / 8;
*(outptr + xloc + yloc) = 255;
*(outptr + xloc + yloc + 1) = 0;
*(outptr + xloc + yloc + 2) = 0;
// bottom
yloc = box->ymax * width * bitsPerPixel / 8;
*(outptr + xloc + yloc) = 255;
*(outptr + xloc + yloc + 1) = 0;
*(outptr + xloc + yloc + 2) = 0;
}
for (int y = box->ymin; y <= box->ymax; y++) {
int yloc = y * width * bitsPerPixel / 8;
int xloc = box->xmin * bitsPerPixel / 8;
*(outptr + xloc + yloc) = 255;
*(outptr + xloc + yloc + 1) = 0;
*(outptr + xloc + yloc + 2) = 0;
xloc = box->xmax * bitsPerPixel / 8;
*(outptr + xloc + yloc) = 255;
*(outptr + xloc + yloc + 1) = 0;
*(outptr + xloc + yloc + 2) = 0;
}
}
}
- (void) awakeFromNib
{
// NSString* imageName = @"/Users/lgo/macdev/dsptest1/OcrTest/images/A_wonb.jpg";
NSString* imageName = @"/Users/lgo/macdev/dsptest1/OcrTest/images/P1180863-800x600.jpg";
NSData* fileData = [NSData dataWithContentsOfFile:imageName];
inImageRep = [NSBitmapImageRep
imageRepWithData:fileData];
if (inImageRep){
inImage = [[NSImage alloc] init];
[inImage addRepresentation:inImageRep];
inputImgBytes = [inImageRep bitmapData];
[imageView setImage:inImage];
}
}
NSBitmapImageRep *cloneImageRep(NSBitmapImageRep* inImageRep)
{
return [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:[inImageRep pixelsWide]
pixelsHigh:[inImageRep pixelsHigh]
bitsPerSample:[inImageRep bitsPerSample]
samplesPerPixel:[inImageRep samplesPerPixel]
hasAlpha:[inImageRep hasAlpha]
isPlanar:[inImageRep isPlanar]
colorSpaceName:[inImageRep colorSpaceName]
bytesPerRow:[inImageRep bytesPerRow]
bitsPerPixel:[inImageRep bitsPerPixel]];
}
#if 0
- (IBAction)edgeDetection:(id)sender
{
int bitsPerPixel = [inImageRep bitsPerPixel];
int width = [inImageRep pixelsWide];
int height = [inImageRep pixelsHigh];
int avg_slope = 0;
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lumbuf = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lum_edge = (unsigned char*)malloc(width * height * sizeof(unsigned char));
NSBitmapImageRep *outImageRep = cloneImageRep(inImageRep);
NSImage* outImage = [[[NSImage alloc] init] autorelease];
[outImage addRepresentation:outImageRep];
outputImgBytes = [outImageRep bitmapData];
/*** Step 1: Edge detection ***/
rgb_convert_to_lum(inputImgBytes, lumin, width, height, bitsPerPixel);
canny_edge_detection(lumin, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
// sobel_edge_detection(lumbuf, lum_edge, width, height);
/* Finished */
[imageView setImage:outImage];
free(lumin);
free(lum_edge);
free(lumbuf);
}
#endif
- (NSBitmapImageRep *)rotate:(NSImage *)image
width:(int)width
height:(int)height
angle:(double)angle
imageRep:(NSBitmapImageRep *)imageRep
{
/**
* Apply the following transformations:
*
* - with the rotation at the left corner (0,0).
* - rotate it by 90 degrees, either clock or counter clockwise.
*/
NSSize size = NSMakeSize(width, height);
NSImage *rotatedImage = [[NSImage alloc] initWithSize:size];
NSBitmapImageRep *rotatedImageRep = cloneImageRep(imageRep);
[rotatedImage addRepresentation:rotatedImageRep];
[rotatedImage lockFocus];
NSAffineTransform *rotateTF = [NSAffineTransform transform];
[rotateTF rotateByDegrees:2];
[rotateTF concat];
NSRect r1 = NSMakeRect(0, 0, width, height);
[imageRep drawInRect:r1];
[rotatedImage unlockFocus];
return rotatedImageRep;
}
- (IBAction)connComps:(id)sender
{
int bitsPerPixel = [inImageRep bitsPerPixel];
int width = [inImageRep pixelsWide];
int height = [inImageRep pixelsHigh];
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lum_edge = (unsigned char*)malloc(width * height * sizeof(unsigned char));
double avg_slope = 0;
NSBitmapImageRep *outImageRep = cloneImageRep(inImageRep);
NSImage* outImage = [[[NSImage alloc] init] autorelease];
[outImage addRepresentation:outImageRep];
unsigned char* outputImgBytes = [outImageRep bitmapData];
/*** Step 1: Edge detection ***/
rgb_convert_to_lum(inputImgBytes, lumin, width, height, bitsPerPixel);
canny_edge_detection(lumin, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
/*** Step 2a: if slope, rotate image first ***/
if (avg_slope > -180)
{
rotate(lumin, lum_edge, width, height, avg_slope);
canny_edge_detection(lum_edge, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
}
/*** Step 2: Get small Bounding Boxes ***/
// canny returns only 4 colors + black =-> any color > 0 should be white.
rgb_convert_canny_to_code(outputImgBytes, lum_edge, width, height, bitsPerPixel);
const NSArray *comps = connected_binary(lum_edge, width, height);
/*** Step 2b: Group into characters ***/
const int maxCharDeltaX = 2;
const int maxCharDeltaY= 2;
comps = group_into_characters(comps, maxCharDeltaX, maxCharDeltaY);
const int minWidth = 6;
const int minHeight = 6;
comps = remove_too_small(comps, minWidth, minHeight);
dsptest_log(LOG_BB, __FILE__, "Log connected components\n");
dsptest_log(LOG_BB, __FILE__, "===========================================\n");
log_bounding_boxes(comps);
dsptest_log(LOG_BB, __FILE__, "===========================================\n");
// draw bounding boxes on screen.
// lum_convert_to_rgb(lum_edge, outputImgBytes, width, height, bitsPerPixel);
draw_bounding_boxes(outputImgBytes, comps, width, height, bitsPerPixel);
/* Finished */
[imageView setImage:outImage];
free(lumin);
free(lum_edge);
}
- (IBAction)groupBoundingBoxes:(id)sender
{
int bitsPerPixel = [inImageRep bitsPerPixel];
int width = [inImageRep pixelsWide];
int height = [inImageRep pixelsHigh];
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lum_edge = (unsigned char*)malloc(width * height * sizeof(unsigned char));
double avg_slope = 0;
NSBitmapImageRep *outImageRep = cloneImageRep(inImageRep);
NSImage* outImage = [[[NSImage alloc] init] autorelease];
[outImage addRepresentation:outImageRep];
unsigned char* outputImgBytes = [outImageRep bitmapData];
/*** Step 1: Edge detection ***/
rgb_convert_to_lum(inputImgBytes, lumin, width, height, bitsPerPixel);
canny_edge_detection(lumin, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
/*** Step 2a: if slope, rotate image first ***/
if (avg_slope > -180)
{
rotate(lumin, lum_edge, width, height, avg_slope);
canny_edge_detection(lum_edge, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
}
/*** Step 2: Get small Bounding Boxes ***/
// canny returns only 4 colors + black =-> any color > 0 should be white.
rgb_convert_canny_to_code(outputImgBytes, lum_edge, width, height, bitsPerPixel);
NSArray *bounding_boxes = connected_binary(lum_edge, width, height);
// NSArray *bounding_boxes = connected_div_and_conq(lum_edge, width, height);
/*** Step 3: Group bounding boxes ***/
bounding_boxes = group_into_lines(bounding_boxes, width, height);
dsptest_log(LOG_BB, __FILE__, "Log grouped connected components\n");
dsptest_log(LOG_BB, __FILE__, "===========================================\n");
log_bounding_boxes(bounding_boxes);
dsptest_log(LOG_BB, __FILE__, "===========================================\n");
// draw bounding boxes on screen.
// lum_convert_to_rgb(lum_edge, outputImgBytes, width, height, bitsPerPixel);
draw_bounding_boxes(outputImgBytes, bounding_boxes, width, height, bitsPerPixel);
/* Finished */
[imageView setImage:outImage];
}
- (IBAction)binarization:(id)sender
{
int bitsPerPixel = [inImageRep bitsPerPixel];
int width = [inImageRep pixelsWide];
int height = [inImageRep pixelsHigh];
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lum_edge = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lumbuf = (unsigned char*)malloc(width * height * sizeof(unsigned char));
double avg_slope = 0;
NSBitmapImageRep *outImageRep = cloneImageRep(inImageRep);
NSImage* outImage = [[[NSImage alloc] init] autorelease];
[outImage addRepresentation:outImageRep];
unsigned char *outputImgBytes = [outImageRep bitmapData];
/*** Step 1: Edge detection ***/
rgb_convert_to_lum(inputImgBytes, lumin, width, height, bitsPerPixel);
canny_edge_detection(lumin, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
/*** Step 2a: if slope, rotate image first ***/
if (avg_slope > -180)
{
rotate(lumin, lum_edge, width, height, avg_slope);
canny_edge_detection(lum_edge, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
}
/*** Step 2: Get small Bounding Boxes ***/
// canny returns only 4 colors + black =-> any color > 0 should be white.
rgb_convert_to_bw_treshold(outputImgBytes, lum_edge, width, height, bitsPerPixel, 1);
NSArray *bounding_boxes = connected_binary(lum_edge, width, height);
// NSArray *bounding_boxes = connected_div_and_conq(lum_edge, width, height);
/*** Step 3: Group bounding boxes ***/
bounding_boxes = group_into_lines(bounding_boxes, width, height);
/*** Step 4: Binarization of interior of bounding boxes ***/
binarization_bounding_boxes(lumin, bounding_boxes, width, height);
// binarization_threshold(lum_edge, lum_edge, 0, 0, width, width, height, 0, 0, width, 1);
make_boxes_black_on_white_bg(bounding_boxes);
// draw bounding boxes on screen.
lum_convert_to_rgb(lumbuf, outputImgBytes, width, height, bitsPerPixel);
draw_bounding_boxes(outputImgBytes, bounding_boxes, width, height, bitsPerPixel);
/* Finished */
[imageView setImage:outImage];
}
- (IBAction)ocr:(id)sender
{
tessocr *ocr = [[tessocr alloc] init];
recognizer *rec = [[recognizer alloc] init];
int bitsPerPixel = [inImageRep bitsPerPixel];
int width = [inImageRep pixelsWide];
int height = [inImageRep pixelsHigh];
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* lum_edge = (unsigned char*)malloc(width * height * sizeof(unsigned char));
double avg_slope = 0;
NSBitmapImageRep *outImageRep = cloneImageRep(inImageRep);
NSImage* outImage = [[[NSImage alloc] init] autorelease];
[outImage addRepresentation:outImageRep];
unsigned char *outputImgBytes = [outImageRep bitmapData];
/*** Step 1: Edge detection ***/
rgb_convert_to_lum(inputImgBytes, lumin, width, height, bitsPerPixel);
canny_edge_detection(lumin, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
/*** Step 2a: if slope, rotate image first ***/
if (avg_slope > -180)
{
unsigned char* lumbuf = (unsigned char*)malloc(width * height * sizeof(unsigned char));
rotate(lumin, lumbuf, width, height, avg_slope);
canny_edge_detection(lumbuf, outputImgBytes, width, height, bitsPerPixel, &avg_slope);
free(lumin);
lumin = lumbuf;
}
/*** Step 2: Get small Bounding Boxes ***/
// canny returns only 4 colors + black =-> any color > 0 should be white.
rgb_convert_to_bw_treshold(outputImgBytes, lum_edge, width, height, bitsPerPixel, 1);
// binarization_threshold(lum_edge, lum_edge, 0, 0, width, width, height, 0, 0, width, 1);
NSArray *bounding_boxes = connected_binary(lum_edge, width, height);
// NSArray *bounding_boxes = connected_div_and_conq(lum_edge, width, height);
/*** Step 3: Group bounding boxes ***/
bounding_boxes = group_into_lines(bounding_boxes, width, height);
/*** Step 4: Binarization of interior of bounding boxes ***/
binarization_bounding_boxes(lumin, bounding_boxes, width, height);
make_boxes_black_on_white_bg(bounding_boxes);
// draw bounding boxes on screen.
lum_convert_to_rgb(lumin, outputImgBytes, width, height, bitsPerPixel);
draw_bounding_boxes(outputImgBytes, bounding_boxes, width, height, bitsPerPixel);
[imageView setImage:outImage];
/*** Step 5: OCR of binarized bounding boxes ***/
fuzzy_search_db *db = [[fuzzy_search_db alloc] init_db:@"/tmp/ramdisc/ratings"];
NSMutableArray *result = [[NSMutableArray alloc] init];
for(conn_box_t *box in bounding_boxes) {
const char* ocr_text = [ocr run_tesseract:box];
char* text = filter_ocr_string(ocr_text);
if (text)
{
NSString *str = [NSString stringWithUTF8String:text];
NSArray *matches = [db find_best_matching_movies:str
topk:20];
for (NSString *m in matches)
{
int dist = [rec calc_editDistance:str b:m caseInsensitive:true];
if (dist < 25)
{
if (! [result containsObject:m])
[result addObject:m];
}
}
}
}
for (NSString *str in result)
dsptest_log(LOG_OCR, __FILE__, "found line: %s\n", [str UTF8String]);
// NSString *str = [[NSString alloc] initWithUTF8String:text];
// [lbl setStringValue:str];
}
@end