-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.mm
702 lines (581 loc) · 21.9 KB
/
graphics.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//
// graphics.mm
// dsptest1
//
// Created by Lieven Govaerts on 16/12/12.
//
//
#include <stdio.h>
#include "graphics.h"
#include "util.h"
static const int sharpen[3][3] = { { -1, -1, -1 },
{ -1, 8, -1 },
{ -1, -1, -1 } };
static const int smoothen[3][3] = { { 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 } };
static const int LoG[9][9] = {
{ 0, 1, 1, 2, 2, 2, 1, 1, 0 },
{ 1, 2, 4, 5, 5, 5, 4, 2, 1 },
{ 1, 4, 5, 3, 0, 3, 5, 4, 1 },
{ 2, 5, 3, -12, -24, -12, 3, 5, 2 },
{ 2, 5, 0, -24, -40, -24, 0, 5, 2 },
{ 2, 5, 3, -12, -24, -12, 3, 5, 2 },
{ 1, 4, 5, 3, 0, 3, 5, 4, 1 },
{ 1, 4, 5, 3, 0, 3, 5, 4, 1 },
{ 0, 1, 1, 2, 2, 2, 1, 1, 0 } };
static const int Gaussian[5][5] = {
{ 1, 4, 7, 4, 1 },
{ 4, 16, 26, 16, 4 },
{ 7, 26, 41, 26, 7 },
{ 4, 16, 26, 16, 4 },
{ 1, 4, 7, 4, 1 },
};
static const int GaussianDivider = 273;
static const int SobelHorizontal[3][3] = {
{ -1, 0, +1 },
{ -2, 0, +2 },
{ -1, 0, +1 },
};
static const int SobelVertical[3][3] = {
{ +1, +2, +1 },
{ 0, 0, 0 },
{ -1, -2, -1 },
};
void rgb_convert_to_lum(const unsigned char *inbuf, unsigned char *lumbuf,
int width, int height, int bitsPerPixel)
{
int rowPixels = width * bitsPerPixel / 8;
for (int y = 0; y < height; y++) {
int yloc = y * rowPixels;
unsigned char *lumptr = lumbuf + y * width;
for (int x = 0; x < width; x++) {
int xloc = x * bitsPerPixel / 8;
const unsigned char *curin = inbuf + yloc + xloc;
unsigned char r = *curin++, g = *curin++, b = *curin++;
// calculate luminance from rgb
float lum = 0.3 * r + 0.59 * g + 0.11 * b;
*lumptr++ = lum; // r
}
}
}
void lum_convert_to_rgb(const unsigned char *lumbuf, unsigned char *outbuf,
int width, int height, int bitsPerPixel)
{
int rowPixels = width * bitsPerPixel / 8;
for (int y = 0; y < height; y++) {
int yloc = y * rowPixels;
const unsigned char *lumptr = lumbuf + y * width;
for (int x = 0; x < width; x++) {
int xloc = x * bitsPerPixel / 8;
unsigned char *curout = outbuf + yloc + xloc;
*curout++ = *lumptr; // r
*curout++ = *lumptr; // g
*curout++ = *lumptr++; // b
*curout++ = 0;
}
}
}
void histogram(const unsigned char *inbuf, unsigned int *histogram,
int inleft, int intop,
int inwidth,
int boxwidth, int boxheight)
{
memset(histogram, 0, 256 * sizeof(unsigned int));
for (int y = 0; y < boxheight; y++) {
for (int x = 0; x < boxwidth; x++) {
const unsigned char *curin = inbuf + (intop + y) * inwidth + (x + inleft);
(*(histogram+(unsigned int)*curin))++;
}
}
}
void rgb_convert_to_bw_treshold(const unsigned char *inbuf, unsigned char *lumbuf,
int width, int height, int bitsPerPixel,
int treshold)
{
int rowPixels = width * bitsPerPixel / 8;
for (int y = 0; y < height; y++) {
int yloc = y * rowPixels;
unsigned char *lumptr = lumbuf + y * width;
for (int x = 0; x < width; x++) {
int xloc = x * bitsPerPixel / 8;
const unsigned char *curin = inbuf + yloc + xloc;
unsigned char r = *curin++, g = *curin++, b = *curin++;
// calculate luminance from rgb
float lum = 0.3 * r + 0.59 * g + 0.11 * b;
if (lum > treshold)
*lumptr++ = 255;
else
*lumptr++ = 0;
}
}
}
// both x and y in bytes.
// outptr points to x,y point in output buffer.
void convolution(const unsigned char *lumin, int *outptr,
int x, int y,
int width, int height,
int *matrix, int matrixSize,
int matrix_divider,
int *min, int *max)
{
int offset = matrixSize / 2; // should be 1 for a matrix of size 3.
const unsigned char *incur;
int lum = 0;
// calculate convolution of one pixel!
for (int i = 0; i < matrixSize; i++) {
int yloc = (y + (i - offset)) * width;
if (yloc < 0 || (y + (i - offset)) >= height) {
yloc = (y + ((matrixSize / 2) - offset)) * width;
}
for (int j= 0; j < matrixSize; j++) {
int xloc = x + (j - offset);
if (xloc < 0 || (x + (j - offset)) >= width) {
xloc = x + ((matrixSize / 2) - offset);
}
incur = lumin + yloc + xloc;
int m = *(matrix + (i * matrixSize) + j);
lum += (int)*incur * m;
}
}
lum /= matrix_divider;
if (lum < *min)
*min = lum;
if (lum > *max)
*max = lum;
*outptr = lum;
}
void convolution_in_range(const unsigned char *lumin, unsigned char *lumout,
int x, int y,
int width, int height,
int *matrix, int matrixSize,
int matrix_divider)
{
int offset = matrixSize / 2; // should be 1 for a matrix of size 3.
const unsigned char *incur;
int lum = 0;
// calculate convolution of one pixel!
for (int i = 0; i < matrixSize; i++) {
int yloc = (y + (i - offset)) * width;
if (yloc < 0 || (y + (i - offset)) >= height) continue;
for (int j= 0; j < matrixSize; j++) {
int xloc = x + (j - offset);
if (xloc < 0 || (x + (j - offset)) >= width) continue;
incur = lumin + yloc + xloc;
int m = *(matrix + (i * matrixSize) + j);
lum += (int)*incur * m;
}
}
lum /= matrix_divider;
*lumout = lum;
}
void filter_and_convert_to_gray(unsigned char *inbuf, unsigned char *outbuf,
int width, int height, int bitsPerPixel)
{
unsigned char* lumin = (unsigned char*)malloc(width * height * sizeof(unsigned char));
int* lumbuf = (int*)malloc(width * height * sizeof(int));
int* lumptr;
int min = 0, max = 0;
unsigned char *curout;
rgb_convert_to_lum(inbuf, lumin, width, height, bitsPerPixel);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
lumptr = lumbuf + y * width + x;
convolution(lumin, lumptr,
x, y, width, height,
(int*)sharpen, 3, 1,
&min, &max);
}
}
// minimum and maximum luminance is stored in min and max respectively.
// copy intermediate luminance buffer to output luminance buffer,
// adapt values to range.
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
lumptr = lumbuf + y * width + x;
int lum = *lumptr;
float gray = ((lum - min) / ((max - min)/255));
curout = outbuf + y * width + x;
*curout++ = (unsigned char)gray;
}
}
}
void gaussian_blur(const unsigned char *inlum, unsigned char *outlum,
int width, int height)
{
unsigned char *lumptr;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
lumptr = outlum + y * width + x;
convolution_in_range(inlum, lumptr,
x, y, width, height,
(int*)Gaussian, 5, GaussianDivider);
}
}
}
#define PI 3.14159265
void sobel_edge_detection(const unsigned char *inlum, unsigned char *outlum,
int width, int height)
{
int lumx, lumy, lumsum;
int min = 0, max = 0;
unsigned char *lumptr;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
lumptr = outlum + y * width + x;
convolution(inlum, &lumy,
x, y, width, height,
(int*)SobelVertical, 3, 1,
&min, &max);
convolution(inlum, &lumx,
x, y, width, height,
(int*)SobelHorizontal, 3, 1,
&min, &max);
// edge strength
lumsum = sqrt(lumx * lumx + lumy * lumy);
if (lumsum > 255) lumsum = 255;
if (lumsum < 0) lumsum = 0;
*lumptr = lumsum;
}
}
}
typedef enum canny_codes_t {
black = 0,
yellow = 1,
red = 2,
green = 3,
blue = 4
} canny_codes_t;
void rgb_convert_canny_to_code(const unsigned char *inbuf, unsigned char *lumbuf,
int width, int height, int bitsPerPixel)
{
int rowPixels = width * bitsPerPixel / 8;
for (int y = 0; y < height; y++) {
int yloc = y * rowPixels;
unsigned char *lumptr = lumbuf + y * width;
for (int x = 0; x < width; x++) {
int xloc = x * bitsPerPixel / 8;
const unsigned char *curin = inbuf + yloc + xloc;
unsigned char r = *curin++, g = *curin++, b = *curin++;
canny_codes_t code;
if (b == 255)
code = blue;
else if (r == 255 && g == 255)
code = yellow;
else if (r == 255)
code = red;
else if (g == 255)
code = green;
else
code = black;
*lumptr++ = (unsigned char)code;
}
}
}
// http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/can_tut.html
void canny_edge_detection(const unsigned char *inlum, unsigned char *outbuf,
int width, int height, int bitsperpixel,
double *avg_slope)
{
unsigned char* templum = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* gradiants = (unsigned char*)malloc(width * height * sizeof(unsigned char));
unsigned char* strengths = (unsigned char*)malloc(width * height * sizeof(unsigned char));
// possible method to choose tresholds:
// http://www.kerrywong.com/2009/05/07/canny-edge-detection-auto-thresholding/
int low_treshold = 20;
int high_treshold = 200;
// step 1: gaussian blur
// templum = inlum;
gaussian_blur(inlum, templum, width, height);
// step 2: sobel horizontal & vertical edge filtering
int Gx, Gy, strength;
int min = 0, max = 0;
unsigned char *outptr, *strengthptr, *gradptr;
double slope = 0, slope_y = 0, slope_r = 0, slope_g = 0, slope_b = 0;
long cur_y = 0, cur_r = 0, cur_g = 0, cur_b = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
outptr = outbuf + y * (width * bitsperpixel / 8) + x * (bitsperpixel / 8);
strengthptr = strengths + y * width + x;
gradptr = gradiants + y * width + x;
convolution(inlum, &Gy,
x, y, width, height,
(int*)SobelVertical, 3, 1,
&min, &max);
convolution(inlum, &Gx,
x, y, width, height,
(int*)SobelHorizontal, 3, 1,
&min, &max);
// edge strength
strength = sqrt(Gx * Gx + Gy * Gy);
if (strength > 255) strength = 255;
if (strength < 0) strength = 0;
double edge_dir;
if (strength > low_treshold) {
// edge direction
if (Gx == 0 && Gy == 0) {
edge_dir = 0;
}
edge_dir = atan2(Gx, Gy) * 180 / PI;
/* Calculate the general slope of the image:
Take the average angle for each major orientation.
*/
// assign edge to range
if (((edge_dir < 22.5) && (edge_dir > -22.5)) || (edge_dir > 157.5) || (edge_dir < -157.5)) {
edge_dir += 270; edge_dir = fmod(edge_dir, 180);
slope_y = ((slope_y * cur_y) + edge_dir) / (++cur_y);
edge_dir = 0;
*outptr++ = 255; *outptr++ = 255; *outptr++ = 0; *outptr = 0; // yellow
} else if (((edge_dir > 22.5) && (edge_dir < 67.5)) || ((edge_dir < -112.5) && (edge_dir > -157.5))) {
edge_dir = 45;
*outptr++ = 0; *outptr++ = 255; *outptr++ = 0; *outptr = 0; // green
} else if (((edge_dir > 67.5) && (edge_dir < 112.5)) || ((edge_dir < -67.5) && (edge_dir > -112.5))) {
if (edge_dir > 0) edge_dir = edge_dir; // do nothing
else edge_dir = 180 + edge_dir;
slope_b = ((slope_b * cur_b) + edge_dir) / (++cur_b);
edge_dir = 90;
*outptr++ = 0; *outptr++ = 0; *outptr++ = 255; *outptr = 0; // blue
} else if (((edge_dir > 112.5) && (edge_dir < 157.5)) || ((edge_dir < -22.5) && (edge_dir > -67.5))) {
edge_dir = 135;
*outptr++ = 255; *outptr++ = 0; *outptr++ = 0; *outptr = 0; // red
}
} else {
edge_dir = 255;
*outptr++ = 0; *outptr++ = 0; *outptr++ = 0; *outptr = 0;
}
*strengthptr = strength;
*gradptr = (int)edge_dir;
}
}
/* calculate slope as angle where positive = from 0 (right) to bottom. */
slope_y -= 90; slope_b -= 90;
if (slope_y * slope_b > 0 && // same sign?
abs(slope_b - slope_y) < 2)
{
slope = ((slope_y * cur_y) + (slope_b * cur_b)) / (cur_y + cur_b);
dsptest_log(1, __FILE__, "Uniform slope found: %f (%f,%f)\n", slope, slope_y, slope_b);
*avg_slope = slope;
} else {
*avg_slope = -1000;
}
// step 3: non-maximum suppression
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width -1; x++) {
strengthptr = strengths + y * width + x;
gradptr = gradiants + y * width + x;
outptr = outbuf + y * (width * bitsperpixel / 8) + x * (bitsperpixel / 8);
switch (*gradptr) {
case 0: {
unsigned char *strength_top = strengths + (y-1) * width + x;
unsigned char *strength_bottom = strengths + (y+1) * width + x;
strength = *strength_bottom > *strength_top ? *strength_bottom : *strength_top;
break;
}
case 45: {
unsigned char *strength_righttop = strengths + (y-1) * width + x + 1;
unsigned char *strength_leftbottom = strengths + (y+1) * width + x - 1;
strength = *strength_leftbottom > *strength_righttop ? *strength_leftbottom : *strength_righttop;
break;
}
case 90: {
unsigned char *strength_left = strengths + y * width + x - 1;
unsigned char *strength_right = strengths + y * width + x + 1;
strength = *strength_right > *strength_left ? *strength_right : *strength_left;
break;
}
case 135: {
unsigned char *strength_lefttop = strengths + (y-1) * width + x - 1;
unsigned char *strength_rightbottom = strengths + (y+1) * width + x + 1;
strength = *strength_rightbottom > *strength_lefttop ? *strength_rightbottom : *strength_lefttop;
break;
}
default:
break;
}
if (*strengthptr <= strength) {
*strengthptr = 0;
*outptr++ = 0; *outptr++ = 0; *outptr++ = 0; *outptr = 0;
}
}
}
// step 4: tresholding +
// step 5: edge tracking by hysteris: following the edge, enhance all edges above the low threshold.
int nr_iters = 1;
for (int i = 0; i < nr_iters; i++) {
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width -1; x++) {
strengthptr = strengths + y * width + x;
gradptr = gradiants + y * width + x;
outptr = outbuf + y * (width * bitsperpixel / 8) + x * (bitsperpixel / 8);
if (*strengthptr < low_treshold) {
strength = 0;
} else if (*strengthptr > high_treshold) {
strength = 255;
} else {
if (i == nr_iters - 1)
strength = 0;
else {
strength = *strengthptr;
}
// not so strong edge, only keep it when connected to strong edge.
int strong_lefttop = *(strengths + (y-1) * width + x - 1) > high_treshold;
int strong_top = *(strengths + (y-1) * width + x) > high_treshold;
int strong_righttop = *(strengths + (y-1) * width + x + 1) > high_treshold;
int strong_left = *(strengths + y * width + x - 1) > high_treshold;
int strong_right = *(strengths + y * width + x + 1) > high_treshold;
int strong_leftbottom = *(strengths + (y+1) * width + x - 1) > high_treshold;
int strong_bottom = *(strengths + (y+1) * width + x) > high_treshold;
int strong_rightbottom = *(strengths + (y+1) * width + x + 1) > high_treshold;
switch (*gradptr) {
case 0: {
if (strong_lefttop || strong_left || strong_leftbottom || strong_right ||
strong_rightbottom || strong_righttop) {
strength = 255;
}
break;
}
case 45: {
if (strong_lefttop || strong_left || strong_top || strong_right ||
strong_rightbottom || strong_bottom) {
strength = 255;
}
break;
}
case 90: {
if (strong_lefttop || strong_top || strong_righttop || strong_leftbottom ||
strong_bottom || strong_rightbottom ) {
strength = 255;
}
break;
}
case 135: {
if (strong_leftbottom || strong_left || strong_top || strong_right ||
strong_righttop || strong_bottom) {
strength = 255;
}
break;
}
default:
break;
}
}
*strengthptr = strength;
if (strength == 0) {
*outptr++ = 0; *outptr++ = 0; *outptr++ = 0; *outptr = 0;
}
}
}
}
free(templum);
}
void binarization_threshold(const unsigned char* inlum, unsigned char* outlum,
int inleft, int intop,
int inwidth,
int boxwidth, int boxheight,
int outleft, int outtop,
int outwidth,
int threshold)
{
const unsigned char *curin;
unsigned char *curout;
for (int y = 0; y < boxheight; y++) {
for (int x = 0; x < boxwidth; x++) {
curin = inlum + (((y + intop) * inwidth) + (x + inleft));
curout = outlum + (((y + outtop) * outwidth) + (x + outleft));
unsigned char lum = *curin;
if (lum >= threshold)
*curout = 255;
else
*curout = 0;
}
}
}
void binarization(const unsigned char* inlum, unsigned char* outlum,
int inleft, int intop,
int inwidth,
int boxwidth, int boxheight,
int outleft, int outtop,
int outwidth)
{
unsigned int histbuf[256];
// histogram
histogram(inlum, histbuf, inleft, intop, inwidth, boxwidth, boxheight);
// calculate tresholds with otsu
unsigned int threshold = 0;
// http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html
// Total number of pixels
int total = boxwidth * boxheight;
float sum = 0;
for (int t=0 ; t<256 ; t++) sum += t * histbuf[t];
float sumB = 0;
int wB = 0;
int wF = 0;
float varMax = 0;
for (int t = 0; t < 256; t++) {
wB += histbuf[t]; // Weight Background
if (wB == 0) continue;
wF = total - wB; // Weight Foreground
if (wF == 0) break;
sumB += (float) (t * histbuf[t]);
float mB = sumB / wB; // Mean Background
float mF = (sum - sumB) / wF; // Mean Foreground
// Calculate Between Class Variance
float varBetween = (float)wB * (float)wF * (mB - mF) * (mB - mF);
// Check if new maximum found
if (varBetween > varMax) {
varMax = varBetween;
threshold = t;
}
}
binarization_threshold(inlum, outlum, inleft, intop, inwidth, boxwidth, boxheight, outleft, outtop, outwidth, threshold);
}
#define T 5
#define SHADES 32
// ensure that pixels of similar color will have the exact same color.
void prepare(const unsigned char *inlum, unsigned char *outlum,
int width, int height, int bitsPerPixel)
{
unsigned char lum, prevlum;
const unsigned char* cur;
int newlum = 255;
for (int y = 0; y < height; y++) {
// setup the first pixel on the left.
// ...
for (int x = 1; x < width; x++) {
int prevx = (x - 1);
cur = inlum + y * width + prevx;
prevlum = *cur++;
lum = *cur++;
unsigned char delta = abs(prevlum - lum);
if (delta < T) {
cur = outlum + y * width + prevx;
prevlum = *cur++;
newlum = (prevlum / SHADES) * SHADES;
} else
{
newlum = (lum / SHADES) * SHADES;
}
*(outlum + y * width + x) = newlum;
}
}
}
void
rotate(const unsigned char *lumbuf, unsigned char *outlum,
int width, int height, double slope)
{
double angle = slope * M_PI / 180;
double ca = cos(angle); double sa = sin(angle);
for (int y = 0; y < height; y++) {
int yloc = y * width;
for (int x = 0; x < width; x++) {
unsigned char *curout = outlum + yloc + x;
int lx = x * ca - y * sa;
int ly = x * sa + y * ca;
const unsigned char *lumptr = lumbuf + ly * width + lx;
if (lx < 0 || lx >= width || ly < 0 || ly >= height)
*curout = 255;
else
*curout = *lumptr;
}
}
}