forked from CihanTopal/ED_Lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EDColor.cpp
624 lines (485 loc) · 16.7 KB
/
EDColor.cpp
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
#include "EDColor.h"
#include "ED.h"
using namespace cv;
using namespace std;
EDColor::EDColor(Mat srcImage, int gradThresh, int anchor_thresh , double sigma, bool validateSegments)
{
inputImage = srcImage.clone();
// check parameters for sanity
if (sigma < 1) sigma = 1;
if (gradThresh < 1) gradThresh = 1;
if (anchor_thresh < 0) anchor_thresh = 0;
if (validateSegments) { // setup for validation
anchor_thresh = 0;
divForTestSegment = 2.25;
}
// split channels (OpenCV uses BGR)
vector<Mat> bgr(3);
split(srcImage, bgr);
blueImg = bgr[0].data;
greenImg = bgr[1].data;
redImg = bgr[2].data;
height = srcImage.rows;
width = srcImage.cols;
// Allocate space for L*a*b color space
L_Img = new uchar[width*height];
a_Img = new uchar[width*height];
b_Img = new uchar[width*height];
// Convert RGB2Lab
MyRGB2LabFast();
// Allocate space for smooth channels
smooth_L = new uchar[width*height];
smooth_a = new uchar[width*height];
smooth_b = new uchar[width*height];
// Smooth Channels
smoothChannel(L_Img, smooth_L, sigma);
smoothChannel(a_Img, smooth_a, sigma);
smoothChannel(b_Img, smooth_b, sigma);
// Allocate space for direction and gradient images
dirImg = new uchar[width*height];
gradImg = new short[width*height];
// Compute Gradient & Edge Direction Maps
ComputeGradientMapByDiZenzo();
// Validate edge segments if the flag is set
if (validateSegments) {
// Get Edge Image using ED
ED edgeObj = ED(gradImg, dirImg, width, height, gradThresh, anchor_thresh, 1, 10, false);
segments = edgeObj.getSegments();
edgeImage = edgeObj.getEdgeImage();
sigma /= 2.5;
smoothChannel(L_Img, smooth_L, sigma);
smoothChannel(a_Img, smooth_a, sigma);
smoothChannel(b_Img, smooth_b, sigma);
edgeImg = edgeImage.data; // validation steps uses pointer to edgeImage
validateEdgeSegments();
// Extract the new edge segments after validation
extractNewSegments();
}
else {
ED edgeObj = ED(gradImg, dirImg, width, height, gradThresh, anchor_thresh);
segments = edgeObj.getSegments();
edgeImage = edgeObj.getEdgeImage();
segmentNo = edgeObj.getSegmentNo();
}
// Fix 1 pixel errors in the edge map
fixEdgeSegments(segments, 1);
// clean space
delete[] L_Img;
delete[] a_Img;
delete[] b_Img;
delete[] smooth_L;
delete[] smooth_a;
delete[] smooth_b;
delete[] gradImg;
delete[] dirImg;
}
cv::Mat EDColor::getEdgeImage()
{
return edgeImage;
}
std::vector<std::vector<cv::Point>> EDColor::getSegments()
{
return segments;
}
int EDColor::getSegmentNo()
{
return segmentNo;
}
int EDColor::getWidth()
{
return width;
}
int EDColor::getHeight()
{
return height;
}
void EDColor::MyRGB2LabFast()
{
// Inialize LUTs if necessary
if (!LUT_Initialized)
InitColorEDLib();
// First RGB 2 XYZ
double red, green, blue;
double x, y, z;
// Space for temp. allocation
double *L = new double[width*height];
double *a = new double[width*height];
double *b = new double[width*height];
for (int i = 0; i<width*height; i++) {
red = redImg[i] / 255.0;
green = greenImg[i] / 255.0;
blue = blueImg[i] / 255.0;
red = LUT1[(int)(red*LUT_SIZE + 0.5)];
green = LUT1[(int)(green*LUT_SIZE + 0.5)];
blue = LUT1[(int)(blue*LUT_SIZE + 0.5)];
red = red * 100;
green = green * 100;
blue = blue * 100;
//Observer. = 2°, Illuminant = D65
x = red*0.4124564 + green*0.3575761 + blue*0.1804375;
y = red*0.2126729 + green*0.7151522 + blue*0.0721750;
z = red*0.0193339 + green*0.1191920 + blue*0.9503041;
// Now xyz 2 Lab
double refX = 95.047;
double refY = 100.000;
double refZ = 108.883;
x = x / refX; //ref_X = 95.047 Observer= 2°, Illuminant= D65
y = y / refY; //ref_Y = 100.000
z = z / refZ; //ref_Z = 108.883
x = LUT2[(int)(x*LUT_SIZE + 0.5)];
y = LUT2[(int)(y*LUT_SIZE + 0.5)];
z = LUT2[(int)(z*LUT_SIZE + 0.5)];
L[i] = (116.0*y) - 16;
a[i] = 500 * (x / y);
b[i] = 200 * (y - z);
} //end-for
// Scale L to [0-255]
double min = 1e10;
double max = -1e10;
for (int i = 0; i<width*height; i++) {
if (L[i]<min) min = L[i];
else if (L[i]>max) max = L[i];
} //end-for
double scale = 255.0 / (max - min);
for (int i = 0; i<width*height; i++) { L_Img[i] = (unsigned char)((L[i] - min)*scale); }
// Scale a to [0-255]
min = 1e10;
max = -1e10;
for (int i = 0; i<width*height; i++) {
if (a[i]<min) min = a[i];
else if (a[i]>max) max = a[i];
} //end-for
scale = 255.0 / (max - min);
for (int i = 0; i<width*height; i++) { a_Img[i] = (unsigned char)((a[i] - min)*scale); }
// Scale b to [0-255]
min = 1e10;
max = -1e10;
for (int i = 0; i<width*height; i++) {
if (b[i]<min) min = b[i];
else if (b[i]>max) max = b[i];
} //end-for
scale = 255.0 / (max - min);
for (int i = 0; i<width*height; i++) { b_Img[i] = (unsigned char)((b[i] - min)*scale); }
// clean space
delete[] L;
delete[] a;
delete[] b;
}
void EDColor::ComputeGradientMapByDiZenzo()
{
memset(gradImg, 0, sizeof(short)*width*height);
int max = 0;
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
#if 1
// Prewitt for channel1
int com1 = smooth_L[(i + 1)*width + j + 1] - smooth_L[(i - 1)*width + j - 1];
int com2 = smooth_L[(i - 1)*width + j + 1] - smooth_L[(i + 1)*width + j - 1];
int gxCh1 = com1 + com2 + (smooth_L[i*width + j + 1] - smooth_L[i*width + j - 1]);
int gyCh1 = com1 - com2 + (smooth_L[(i + 1)*width + j] - smooth_L[(i - 1)*width + j]);
// Prewitt for channel2
com1 = smooth_a[(i + 1)*width + j + 1] - smooth_a[(i - 1)*width + j - 1];
com2 = smooth_a[(i - 1)*width + j + 1] - smooth_a[(i + 1)*width + j - 1];
int gxCh2 = com1 + com2 + (smooth_a[i*width + j + 1] - smooth_a[i*width + j - 1]);
int gyCh2 = com1 - com2 + (smooth_a[(i + 1)*width + j] - smooth_a[(i - 1)*width + j]);
// Prewitt for channel3
com1 = smooth_b[(i + 1)*width + j + 1] - smooth_b[(i - 1)*width + j - 1];
com2 = smooth_b[(i - 1)*width + j + 1] - smooth_b[(i + 1)*width + j - 1];
int gxCh3 = com1 + com2 + (smooth_b[i*width + j + 1] - smooth_b[i*width + j - 1]);
int gyCh3 = com1 - com2 + (smooth_b[(i + 1)*width + j] - smooth_b[(i - 1)*width + j]);
#else
// Sobel for channel1
int com1 = smooth_L[(i + 1)*width + j + 1] - smooth_L[(i - 1)*width + j - 1];
int com2 = smooth_L[(i - 1)*width + j + 1] - smooth_L[(i + 1)*width + j - 1];
int gxCh1 = com1 + com2 + 2 * (smooth_L[i*width + j + 1] - smooth_L[i*width + j - 1]);
int gyCh1 = com1 - com2 + 2 * (smooth_L[(i + 1)*width + j] - smooth_L[(i - 1)*width + j]);
// Sobel for channel2
com1 = smooth_a[(i + 1)*width + j + 1] - smooth_a[(i - 1)*width + j - 1];
com2 = smooth_a[(i - 1)*width + j + 1] - smooth_a[(i + 1)*width + j - 1];
int gxCh2 = com1 + com2 + 2 * (smooth_a[i*width + j + 1] - smooth_a[i*width + j - 1]);
int gyCh2 = com1 - com2 + 2 * (smooth_a[(i + 1)*width + j] - smooth_a[(i - 1)*width + j]);
// Sobel for channel3
com1 = smooth_b[(i + 1)*width + j + 1] - smooth_b[(i - 1)*width + j - 1];
com2 = smooth_b[(i - 1)*width + j + 1] - smooth_b[(i + 1)*width + j - 1];
int gxCh3 = com1 + com2 + 2 * (smooth_b[i*width + j + 1] - smooth_b[i*width + j - 1]);
int gyCh3 = com1 - com2 + 2 * (smooth_b[(i + 1)*width + j] - smooth_b[(i - 1)*width + j]);
#endif
int gxx = gxCh1*gxCh1 + gxCh2*gxCh2 + gxCh3*gxCh3;
int gyy = gyCh1*gyCh1 + gyCh2*gyCh2 + gyCh3*gyCh3;
int gxy = gxCh1*gyCh1 + gxCh2*gyCh2 + gxCh3*gyCh3;
#if 1
// Di Zenzo's formulas from Gonzales & Woods - Page 337
double theta = atan2(2.0*gxy, (double)(gxx - gyy)) / 2; // Gradient Direction
int grad = (int)(sqrt(((gxx + gyy) + (gxx - gyy)*cos(2 * theta) + 2 * gxy*sin(2 * theta)) / 2.0) + 0.5); // Gradient Magnitude
#else
// Koschan & Abidi - 2005 - Signal Processing Magazine
double theta = atan2(2.0*gxy, (double)(gxx - gyy)) / 2; // Gradient Direction
double cosTheta = cos(theta);
double sinTheta = sin(theta);
int grad = (int)(sqrt(gxx*cosTheta*cosTheta + 2 * gxy*sinTheta*cosTheta + gyy*sinTheta*sinTheta) + 0.5); // Gradient Magnitude
#endif
// Gradient is perpendicular to the edge passing through the pixel
if (theta >= -3.14159 / 4 && theta <= 3.14159 / 4)
dirImg[i*width + j] = EDGE_VERTICAL;
else
dirImg[i*width + j] = EDGE_HORIZONTAL;
gradImg[i*width + j] = grad;
if (grad > max) max = grad;
}
} // end outer for
// Scale the gradient values to 0-255
double scale = 255.0 / max;
for (int i = 0; i<width*height; i++)
gradImg[i] = (short)(gradImg[i] * scale);
}
void EDColor::smoothChannel(uchar *src, uchar *smooth, double sigma)
{
Mat srcImage = Mat(height, width, CV_8UC1, src);
Mat smoothImage = Mat(height, width, CV_8UC1, smooth);
if (sigma == 1.0)
GaussianBlur(srcImage, smoothImage, Size(5, 5), 1);
else if (sigma == 1.5)
GaussianBlur(srcImage, smoothImage, Size(7, 7), 1.5); // seems to be better?
else
GaussianBlur(srcImage, smoothImage, Size(), sigma);
}
//--------------------------------------------------------------------------------------------------------------------
// Validate the edge segments using the Helmholtz principle (for color images) channel1, channel2 and channel3 images
//
void EDColor::validateEdgeSegments()
{
int maxGradValue = MAX_GRAD_VALUE;
H = new double[maxGradValue];
memset(H, 0, sizeof(double)*maxGradValue);
memset(edgeImg, 0, width*height); // clear edge image
// Compute the gradient
memset(gradImg, 0, sizeof(short)*width*height); // reset gradient Image pixels to zero
int *grads = new int[maxGradValue];
memset(grads, 0, sizeof(int)*maxGradValue);
for (int i = 1; i<height - 1; i++) {
for (int j = 1; j<width - 1; j++) {
// Gradient for channel1
int com1 = smooth_L[(i + 1)*width + j + 1] - smooth_L[(i - 1)*width + j - 1];
int com2 = smooth_L[(i - 1)*width + j + 1] - smooth_L[(i + 1)*width + j - 1];
int gxCh1 = abs(com1 + com2 + (smooth_L[i*width + j + 1] - smooth_L[i*width + j - 1]));
int gyCh1 = abs(com1 - com2 + (smooth_L[(i + 1)*width + j] - smooth_L[(i - 1)*width + j]));
int ch1Grad = gxCh1 + gyCh1;
// Gradient for channel2
com1 = smooth_a[(i + 1)*width + j + 1] - smooth_a[(i - 1)*width + j - 1];
com2 = smooth_a[(i - 1)*width + j + 1] - smooth_a[(i + 1)*width + j - 1];
int gxCh2 = abs(com1 + com2 + (smooth_a[i*width + j + 1] - smooth_a[i*width + j - 1]));
int gyCh2 = abs(com1 - com2 + (smooth_a[(i + 1)*width + j] - smooth_a[(i - 1)*width + j]));
int ch2Grad = gxCh2 + gyCh2;
// Gradient for channel3
com1 = smooth_b[(i + 1)*width + j + 1] - smooth_b[(i - 1)*width + j - 1];
com2 = smooth_b[(i - 1)*width + j + 1] - smooth_b[(i + 1)*width + j - 1];
int gxCh3 = abs(com1 + com2 + (smooth_b[i*width + j + 1] - smooth_b[i*width + j - 1]));
int gyCh3 = abs(com1 - com2 + (smooth_b[(i + 1)*width + j] - smooth_b[(i - 1)*width + j]));
int ch3Grad = gxCh3 + gyCh3;
// Take average
int grad = (ch1Grad + ch2Grad + ch3Grad + 2) / 3;
gradImg[i*width + j] = grad;
grads[grad]++;
} //end-for
} //end-for
Mat gradImage = Mat(height, width, CV_16SC1, gradImg);
imwrite("newGrad.pgm", gradImage);
// Compute probability function H
int size = (width - 2)*(height - 2);
// size -= grads[0];
for (int i = maxGradValue - 1; i>0; i--)
grads[i - 1] += grads[i];
for (int i = 0; i<maxGradValue; i++)
H[i] = (double)grads[i] / ((double)size);
// Compute np: # of segment pieces
np = 0;
for (int i = 0; i<segments.size(); i++) {
int len = (int)segments[i].size();
np += (len*(len - 1)) / 2;
} //end-for
// Validate segments
for (int i = 0; i< segments.size(); i++) {
testSegment(i, 0, (int)segments[i].size() - 1);
} //end-for
// clear space
delete[] H;
delete[] grads;
}
//----------------------------------------------------------------------------------
// Resursive validation using half of the pixels as suggested by DMM algorithm
// We take pixels at Nyquist distance, i.e., 2 (as suggested by DMM)
//
void EDColor::testSegment(int i, int index1, int index2)
{
int chainLen = index2 - index1 + 1;
if (chainLen < MIN_PATH_LEN)
return;
// Test from index1 to index2. If OK, then we are done. Otherwise, split into two and
// recursively test the left & right halves
// First find the min. gradient along the segment
int minGrad = 1 << 30;
int minGradIndex;
for (int k = index1; k <= index2; k++) {
int r = segments[i][k].y;
int c = segments[i][k].x;
if (gradImg[r*width + c] < minGrad) { minGrad = gradImg[r*width + c]; minGradIndex = k; }
} //end-for
// Compute nfa
double nfa = NFA(H[minGrad], (int)(chainLen / divForTestSegment));
if (nfa <= EPSILON) {
for (int k = index1; k <= index2; k++) {
int r = segments[i][k].y;
int c = segments[i][k].x;
edgeImg[r*width + c] = 255;
} //end-for
return;
} //end-if
// Split into two halves. We divide at the point where the gradient is the minimum
int end = minGradIndex - 1;
while (end > index1) {
int r = segments[i][end].y;
int c = segments[i][end].x;
if (gradImg[r*width + c] <= minGrad) end--;
else break;
} //end-while
int start = minGradIndex + 1;
while (start < index2) {
int r = segments[i][start].y;
int c = segments[i][start].x;
if (gradImg[r*width + c] <= minGrad) start++;
else break;
} //end-while
testSegment(i, index1, end);
testSegment(i, start, index2);
}
//----------------------------------------------------------------------------------------------
// After the validation of the edge segments, extracts the valid ones
// In other words, updates the valid segments' pixel arrays and their lengths
//
void EDColor::extractNewSegments()
{
vector< vector<Point> > validSegments;
int noSegments = 0;
for (int i = 0; i < segments.size(); i++) {
int start = 0;
while (start < segments[i].size()) {
while (start < segments[i].size()) {
int r = segments[i][start].y;
int c = segments[i][start].x;
if (edgeImg[r*width + c]) break;
start++;
} //end-while
int end = start + 1;
while (end < segments[i].size()) {
int r = segments[i][end].y;
int c = segments[i][end].x;
if (edgeImg[r*width + c] == 0) break;
end++;
} //end-while
int len = end - start;
if (len >= 10) {
// A new segment. Accepted only only long enough (whatever that means)
//segments[noSegments].pixels = &map->segments[i].pixels[start];
//segments[noSegments].noPixels = len;
validSegments.push_back(vector<Point>());
vector<Point> subVec(&segments[i][start], &segments[i][end - 1]);
validSegments[noSegments] = subVec;
noSegments++;
} //end-else
start = end + 1;
} //end-while
} //end-for
// Update
segments = validSegments;
segmentNo = noSegments; // = validSegments.size()
}
double EDColor::NFA(double prob, int len)
{
double nfa = np;
for (int i = 0; i<len && nfa > EPSILON; i++)
nfa *= prob;
return nfa;
}
//---------------------------------------------------------
// Fix edge segments having one or two pixel fluctuations
// An example one pixel problem getting fixed:
// x
// x x --> xxx
//
// An example two pixel problem getting fixed:
// xx
// x x --> xxxx
//
void EDColor::fixEdgeSegments(std::vector<std::vector<cv::Point>> map, int noPixels)
{
/// First fix one pixel problems: There are four cases
for (int i = 0; i < map.size(); i++) {
int cp = (int)map[i].size() - 2; // Current pixel index
int n2 = 0; // next next pixel index
while (n2 < map[i].size()) {
int n1 = cp + 1; // next pixel
cp = cp % map[i].size(); // Roll back to the beginning
n1 = n1 % map[i].size(); // Roll back to the beginning
int r = map[i][cp].y;
int c = map[i][cp].x;
int r1 = map[i][n1].y;
int c1 = map[i][n1].x;
int r2 = map[i][n2].y;
int c2 = map[i][n2].x;
// 4 cases to fix
if (r2 == r - 2 && c2 == c) {
if (c1 != c) {
map[i][n1].x = c;
} //end-if
cp = n2;
n2 += 2;
}
else if (r2 == r + 2 && c2 == c) {
if (c1 != c) {
map[i][n1].x = c;
} //end-if
cp = n2;
n2 += 2;
}
else if (r2 == r && c2 == c - 2) {
if (r1 != r) {
map[i][n1].y = r;
} //end-if
cp = n2;
n2 += 2;
}
else if (r2 == r && c2 == c + 2) {
if (r1 != r) {
map[i][n1].y = r;
} //end-if
cp = n2;
n2 += 2;
}
else {
cp++;
n2++;
} //end-else
} //end-while
} // end-for
}
void EDColor::InitColorEDLib()
{
if (LUT_Initialized)
return;
double inc = 1.0 / LUT_SIZE;
for (int i = 0; i <= LUT_SIZE; i++) {
double d = i * inc;
if (d >= 0.04045) LUT1[i] = pow(((d + 0.055) / 1.055), 2.4);
else LUT1[i] = d / 12.92;
} //end-for
inc = 1.0 / LUT_SIZE;
for (int i = 0; i <= LUT_SIZE; i++) {
double d = i * inc;
if (d > 0.008856) LUT2[i] = pow(d, 1.0 / 3.0);
else LUT2[i] = (7.787*d) + (16.0 / 116.0);
} //end-for
LUT_Initialized = true;
}
bool EDColor::LUT_Initialized = false;
double EDColor::LUT1[LUT_SIZE + 1] = {0};
double EDColor::LUT2[LUT_SIZE + 1] = {0};