-
Notifications
You must be signed in to change notification settings - Fork 2
/
DistanceTransformImage.cs
1011 lines (922 loc) · 42.7 KB
/
DistanceTransformImage.cs
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Vision.NET 2.1 Computer Vision Library
* Copyright (C) 2009 Matthew Johnson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra.Single;
namespace VisionNET
{
/// <summary>
/// Performs an Euclidean distance transform on an input binary image.
/// </summary>
public class DistanceTransformImage : IMultichannelImage<int>
{
private IntegerArrayHandler _handler;
private string _label;
/// <summary>
/// Label for the image.
/// </summary>
public string ID
{
get
{
return _label;
}
set
{
_label = value;
}
}
/// <summary>
/// Constructor.
/// </summary>
public DistanceTransformImage()
{
_handler = new IntegerArrayHandler();
}
/// <summary>
/// Returns a Bitmap version of this image using the computed minimum and maximum values.
/// </summary>
/// <returns>A Bitmap representing this image</returns>
public unsafe BitmapSource ToBitmap()
{
return ToGrayscale().ToBitmap();
}
/// <summary>
/// Converts the distance transform image to a grayscale representation.
/// </summary>
/// <returns>A grayscale representation of the distance transform</returns>
public unsafe GrayscaleImage ToGrayscale()
{
GrayscaleImage gray = new GrayscaleImage(Rows, Columns);
fixed (float* dst = gray.RawArray)
{
fixed (int* src = RawArray)
{
int* srcPtr = src;
float* dstPtr = dst;
for (int length = Rows * Columns; length > 0; length--, dstPtr++, srcPtr += Channels)
*dstPtr = *srcPtr;
}
}
return gray;
}
private const int INFINITY = int.MaxValue;
private struct Curve
{
public int Row;
public int End;
public int B;
public int Column;
public override string ToString()
{
return string.Format("Start={0} End={1} B={2} Row={3} Column={4}", Row, End, B, Row, Column);
}
}
private static int[] _squareLookup;
private static void initLookup(int size)
{
_squareLookup = new int[size];
int x_last = 0;
for (int i = 1; i < size; i++)
{
int x = x_last + (i << 1) - 1;
_squareLookup[i] = x;
x_last = x;
}
}
/// <summary>
/// Finds the nearest edgels to list of locations.
/// </summary>
/// <param name="locations">The locations to test</param>
/// <returns>The nearest edgels</returns>
public Dictionary<Vector,Vector> FindNearestEdges(List<Vector> locations)
{
int[,,] data = RawArray;
Dictionary<Vector, Vector> nearest = new Dictionary<Vector, Vector>();
foreach (Vector loc in locations)
{
int r = (int)loc[1];
int c = (int)loc[0];
nearest[new DenseVector(new float[]{c,r})] = new DenseVector(new float[]{data[r, c, 2], data[r, c, 1]});
}
return nearest;
}
internal static unsafe DistanceTransformImage ComputeNaive(BinaryImage edges, bool computeSquareRoot)
{
int rows = edges.Rows;
int columns = edges.Columns;
List<ImageCell> cells = edges.GetCells();
DistanceTransformImage dt = new DistanceTransformImage();
dt.SetDimensions(edges.Rows, edges.Columns, 3);
for(int r=0; r<rows; r++)
for (int c = 0; c < columns; c++)
{
var distances = from index in cells
select new
{
Distance = distance(index.Row, index.Column, r, c),
Index = index
};
var nearest = distances.OrderBy(o => o.Distance).First();
if (computeSquareRoot)
dt[r, c, 0] = (int)Math.Sqrt(nearest.Distance);
else dt[r, c, 0] = nearest.Distance;
dt[r, c, 1] = nearest.Index.Row;
dt[r, c, 2] = nearest.Index.Column;
}
return dt;
}
/// <summary>
/// Computes the Chamfer distance match between the provided template and this distance transform at the provided index.
/// </summary>
/// <param name="row">Row to compare at</param>
/// <param name="column">Column to compare at</param>
/// <param name="template">The template to compare</param>
/// <param name="maxDistance">Maximum distance allowed</param>
/// <returns>Chamfer match value</returns>
public float ComputeChamferMatch(int row, int column, List<Vector> template, int maxDistance)
{
long sum = 0;
int[,,] data = RawArray;
int count = 0;
int rows = Rows;
int columns = Columns;
foreach (var point in template)
{
int r = (int)(point[1] + row);
int c = (int)(point[0] + column);
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int dist = data[r, c, 0];
dist = dist < maxDistance ? dist : maxDistance;
sum += dist;
count++;
}
}
return (float)sum / (count*maxDistance);
}
/// <summary>
/// Computes the Chamfer distance match between the provided template and this distance transform at the provided index.
/// </summary>
/// <param name="template">The template to compare</param>
/// <param name="maxDistance">Maximum distance allowed</param>
/// <returns>Chamfer match value</returns>
public float ComputeChamferMatch(List<Vector> template, int maxDistance)
{
long sum = 0;
int[, ,] data = RawArray;
int count = 0;
int rows = Rows;
int columns = Columns;
foreach (var point in template)
{
int r = (int)point[1];
int c = (int)point[0];
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int dist = data[r, c, 0];
dist = dist < maxDistance ? dist : maxDistance;
sum += dist;
count++;
}
}
return (float)sum / (count * maxDistance);
}
/// <summary>
/// Computes the Chamfer distance match between the provided template and this distance transform at the provided index.
/// </summary>
/// <param name="row">Row to compare at</param>
/// <param name="column">Column to compare at</param>
/// <param name="template">The template to compare</param>
/// <param name="maxDistance">Maximum distance allowed</param>
/// <param name="scale">Scaling factor for the match</param>
/// <returns>Chamfer match value</returns>
public float ComputeChamferMatch(int row, int column, List<Vector> template, int maxDistance, Vector scale)
{
long sum = 0;
int[, ,] data = RawArray;
int count = 0;
int rows = Rows;
int columns = Columns;
float scaleX = scale[0];
float scaleY = scale[1];
foreach (var point in template)
{
int r = (int)(point[1]*scaleY + row);
int c = (int)(point[0]*scaleX + column);
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int dist = data[r, c, 0];
dist = dist < maxDistance ? dist : maxDistance;
sum += dist;
count++;
}
}
return (float)sum / (count*maxDistance);
}
/// <summary>
/// Computes the match between the template orientations and the orientations of the nearest edges in the image at the provided offset index.
/// The Z value of each feature in the template should be the orientation of the template edge at that template index, and all orientations should
/// be in the range from 0 to Pi.
/// </summary>
/// <param name="row">The row to compare at</param>
/// <param name="column">The column to compare at</param>
/// <param name="template">The template to compare</param>
/// <param name="orientations">Orientations from 0 to Pi for each index in the image</param>
/// <returns>Orientation match value</returns>
public float ComputeOrientationMatch(int row, int column, List<Vector> template, float[,] orientations)
{
int rows = Rows;
int columns = Columns;
if (orientations.GetLength(0) != rows || orientations.GetLength(1) != columns)
throw new ArgumentException("orientations must have the same dimensionality as the image");
double halfPI = Math.PI / 2;
double sum = 0;
int count = 0;
int[,,] data = RawArray;
foreach (Vector point in template)
{
int r = (int)(point[1] + row);
int c = (int)(point[0] + column);
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int rr = data[r, c, 1];
int cc = data[r, c, 2];
double diff = Math.Abs(orientations[rr, cc] - point[2]);
if (diff > Math.PI)
diff -= Math.PI;
if (diff > halfPI)
diff = Math.PI - diff;
sum += diff;
count++;
}
}
return (float)(sum / (halfPI*count));
}
/// <summary>
/// Computes the match between the template orientations and the orientations of the nearest edges in the image at the provided offset index.
/// The Z value of each feature in the template should be the orientation of the template edge at that template index, and all orientations should
/// be in the range from 0 to Pi.
/// </summary>
/// <param name="template">The template to compare</param>
/// <param name="orientations">Orientations from 0 to Pi for each index in the image</param>
/// <returns>Orientation match value</returns>
public float ComputeOrientationMatch(List<Vector> template, float[,] orientations)
{
int rows = Rows;
int columns = Columns;
if (orientations.GetLength(0) != rows || orientations.GetLength(1) != columns)
throw new ArgumentException("orientations must have the same dimensionality as the image");
double halfPI = Math.PI / 2;
double sum = 0;
int count = 0;
int[, ,] data = RawArray;
foreach (Vector point in template)
{
int r = (int)point[1];
int c = (int)point[0];
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int rr = data[r, c, 1];
int cc = data[r, c, 2];
double diff = Math.Abs(orientations[rr, cc] - point[2]);
if (diff > Math.PI)
diff -= Math.PI;
if (diff > halfPI)
diff = Math.PI - diff;
sum += diff;
count++;
}
}
return (float)(sum / (halfPI * count));
}
/// <summary>
/// Computes the match between the template orientations and the orientations of the nearest edges in the image at the provided offset index.
/// The Z value of each feature in the template should be the orientation of the template edge at that template index, and all orientations should
/// be in the range from 0 to Pi.
/// </summary>
/// <param name="row">The row to compare at</param>
/// <param name="column">The column to compare at</param>
/// <param name="template">The template to compare</param>
/// <param name="orientations">Orientations from 0 to Pi for each index in the image</param>
/// <param name="scale">Scaling factor for the match</param>
/// <returns>Orientation match value</returns>
public float ComputeOrientationMatch(int row, int column, List<Vector> template, float[,] orientations, Vector scale)
{
int[, ,] data = RawArray;
int rows = Rows;
int columns = Columns;
if (orientations.GetLength(0) != rows || orientations.GetLength(1) != columns)
throw new ArgumentException("orientations must have the same dimensionality as the image");
double halfPI = Math.PI / 2;
double sum = 0;
int count = 0;
float scaleX = scale[0];
float scaleY = scale[1];
foreach (Vector point in template)
{
int r = (int)(point[1]*scaleY + row);
int c = (int)(point[0]*scaleX + column);
if (r >= 0 && r < rows && c >= 0 && c < columns)
{
int rr = data[r, c, 1];
int cc = data[r, c, 2];
double diff = Math.Abs(orientations[rr, cc] - point[2]);
if (diff > Math.PI)
diff -= Math.PI;
if (diff > halfPI)
diff = Math.PI - diff;
sum += diff;
count++;
}
}
return (float)(sum / (halfPI*count));
}
private static int distance(int row1, int column1, int row2, int column2)
{
int dr = row1 - row2;
int dc = column1 - column2;
return dr * dr + dc * dc;
}
/// <summary>
/// Computes the distance transform using an efficient Euclidean distance transform. If <paramref name="computeSquareRoot"/>
/// is not set, the squared distance will be stored.
/// </summary>
/// <param name="edges">Edge image</param>
/// <param name="computeSquareRoot">Whether to compute the actual distance from the squared distance</param>
/// <returns>Distance transform</returns>
public static unsafe DistanceTransformImage Compute(BinaryImage edges, bool computeSquareRoot)
{
int i, j, k;
int r, c;
int I_1, I_2, r_1, r_2, dr, intersection;
int I2new, I2val, I2column, dtVal;
int rows = edges.Rows;
int columns = edges.Columns;
int channels = 3;
int stride = columns * channels;
Curve[] curves = new Curve[256];
Curve* bottomPtr, curve0Ptr, curve1Ptr;
int curvesCount = 0;
int[, ,] I2 = new int[rows, columns, channels];
DistanceTransformImage dt = new DistanceTransformImage();
dt.SetDimensions(rows, columns, channels);
initLookup(Math.Max(rows, columns));
fixed (bool* edgesBuf = edges.RawArray)
{
fixed (int* I2Buf = I2, squareBuf = _squareLookup, dtBuf = dt.RawArray)
{
fixed (Curve* curvesBuf = curves)
{
bool* edgesPtr = edgesBuf;
int* I2Ptr = I2Buf;
int* squarePtr = squareBuf;
int lastEdge, width;
bool* edgesScan;
int* I2RevPtr, I2LagPtr, dtPtr, dtScan, I2Scan;
for (r = 0, i = rows; i != 0; i--, r++)
{
lastEdge = INFINITY;
if (*edgesPtr)
{
I2Ptr[0] = 0;
I2Ptr[1] = r;
I2Ptr[2] = 0;
lastEdge = 0;
squarePtr = squareBuf;
}
else *I2Ptr = INFINITY;
I2LagPtr = I2Ptr;
edgesPtr++;
I2Ptr += channels;
for (j = columns - 1, c = 1; j != 0; j--, c++, edgesPtr++, I2LagPtr += channels, I2Ptr += channels)
{
if (*edgesPtr)
{
if (lastEdge == INFINITY)
{
for (k = c + 1, I2RevPtr = I2Ptr, squarePtr = squareBuf; k != 0; k--, I2RevPtr -= channels, squarePtr++)
{
I2RevPtr[0] = *squarePtr;
I2RevPtr[1] = r;
I2RevPtr[2] = c;
}
}
else
{
width = c - lastEdge;
width = width >> 1;
for (k = width + 1, I2RevPtr = I2Ptr, squarePtr = squareBuf; k != 0; k--, I2RevPtr -= channels, squarePtr++)
{
I2RevPtr[0] = *squarePtr;
I2RevPtr[1] = r;
I2RevPtr[2] = c;
}
}
lastEdge = c;
squarePtr = squareBuf;
}
else
{
if (*I2LagPtr == INFINITY)
*I2Ptr = INFINITY;
else
{
I2Ptr[0] = *(++squarePtr);
I2Ptr[1] = r;
I2Ptr[2] = lastEdge;
}
}
}
}
for (
edgesPtr = edgesBuf,
dtPtr = dtBuf,
I2Ptr = I2Buf,
c = 0,
i = columns;
i != 0;
I2Ptr += channels,
edgesPtr++,
dtPtr += channels,
c++,
i--)
{
curvesCount = 0;
bottomPtr = null;
curve0Ptr = null;
curve1Ptr = null;
for (
I2Scan = I2Ptr,
edgesScan = edgesPtr,
dtScan = dtPtr,
j = rows,
r = 0;
j != 0;
I2Scan += stride,
edgesScan += columns,
dtScan += stride,
r++,
j--)
{
if (*edgesScan)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = rows;
bottomPtr->B = 0;
bottomPtr->Column = c;
dtScan[1] = r;
dtScan[2] = c;
continue;
}
I2new = INFINITY;
I2val = I2Scan[0];
I2column = I2Scan[2];
if (I2val != INFINITY)
{
if (curvesCount > 0)
{
dr = r - bottomPtr->Row;
I2new = _squareLookup[dr] + bottomPtr->B;
if (I2val < I2new)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = rows;
bottomPtr->B = I2val;
bottomPtr->Column = I2column;
dtScan[0] = I2val;
dtScan[1] = r;
dtScan[2] = I2column;
Debug.Assert(dtScan[0] == distance(r, c, dtScan[1], dtScan[2]));
continue;
}
}
for (; ; )
{
if (curvesCount == 0)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = rows;
bottomPtr->B = I2val;
bottomPtr->Column = I2column;
break;
}
if (I2val < curve1Ptr->B)
{
curvesCount--;
if (curvesCount > 0)
{
curve1Ptr--;
if (curvesCount > 1)
curve0Ptr--;
else curve0Ptr = null;
}
else
{
curve1Ptr = null;
bottomPtr = null;
curve0Ptr = null;
}
continue;
}
I_1 = curve1Ptr->B;
I_2 = I2val;
r_1 = curve1Ptr->Row;
r_2 = r;
dr = r_2 - r_1;
intersection = r_2 + ((I_2 - I_1 - _squareLookup[dr]) / (dr << 1));
if (intersection >= rows)
{
*I2Scan = INFINITY;
break;
}
if (curve0Ptr == null || intersection > curve0Ptr->End)
{
curvesCount++;
curve1Ptr->End = intersection;
curve0Ptr = curve1Ptr;
curve1Ptr++;
curve1Ptr->Row = r;
curve1Ptr->End = rows;
curve1Ptr->B = I_2;
curve1Ptr->Column = I2column;
break;
}
curvesCount--;
if (curvesCount > 0)
{
curve1Ptr--;
if (curvesCount > 1)
curve0Ptr--;
else curve0Ptr = null;
}
else
{
curve1Ptr = null;
bottomPtr = null;
curve0Ptr = null;
}
}
}
if (curvesCount == 0)
continue;
if (I2new == INFINITY)
{
dr = r - bottomPtr->Row;
I2new = _squareLookup[dr] + bottomPtr->B;
}
dtScan[0] = I2new;
dtScan[1] = bottomPtr->Row;
dtScan[2] = bottomPtr->Column;
Debug.Assert(dtScan[0] == distance(r, c, dtScan[1], dtScan[2]));
if (I2new < I2val)
*I2Scan = INFINITY;
if (bottomPtr->End == r)
{
curvesCount--;
bottomPtr++;
}
}
//continue;
curvesCount = 0;
bottomPtr = null;
curve0Ptr = null;
curve1Ptr = null;
I2Scan -= stride;
dtScan -= stride;
edgesScan -= columns;
for (
j = rows,
r = rows - 1;
j != 0;
I2Scan -= stride,
dtScan -= stride,
edgesScan -= columns,
r--,
j--)
{
if (*edgesScan)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = -1;
bottomPtr->B = 0;
bottomPtr->Column = c;
continue;
}
I2new = INFINITY;
I2val = I2Scan[0];
I2column = I2Scan[2];
dtVal = *dtScan;
if (I2val != INFINITY)
{
if (curvesCount > 0)
{
dr = bottomPtr->Row - r;
I2new = _squareLookup[dr] + bottomPtr->B;
if (dtVal < I2new && dtVal < I2val)
{
curvesCount = 0;
curve1Ptr = null;
bottomPtr = null;
curve0Ptr = null;
continue;
}
if (I2val < I2new)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = -1;
bottomPtr->B = I2val;
bottomPtr->Column = I2column;
dtScan[0] = I2val;
dtScan[1] = r;
dtScan[2] = I2column;
Debug.Assert(dtScan[0] == distance(r, c, dtScan[1], dtScan[2]));
continue;
}
}
for (; ; )
{
if (curvesCount == 0)
{
curvesCount = 1;
bottomPtr = curvesBuf;
curve1Ptr = curvesBuf;
curve0Ptr = null;
bottomPtr->Row = r;
bottomPtr->End = -1;
bottomPtr->B = I2val;
bottomPtr->Column = I2column;
break;
}
if (I2val < curve1Ptr->B)
{
curvesCount--;
if (curvesCount > 0)
{
curve1Ptr--;
if (curvesCount > 1)
curve0Ptr--;
else curve0Ptr = null;
}
else
{
curve1Ptr = null;
bottomPtr = null;
curve0Ptr = null;
}
continue;
}
I_1 = curve1Ptr->B;
I_2 = I2val;
r_1 = curve1Ptr->Row;
r_2 = r;
dr = r_1 - r_2;
intersection = r_2 - ((I_2 - I_1 - _squareLookup[dr]) / (dr << 1));
if (intersection < 0)
{
break;
}
if (curve0Ptr == null || intersection < curve0Ptr->End)
{
curvesCount++;
curve1Ptr->End = intersection;
curve0Ptr = curve1Ptr;
curve1Ptr++;
curve1Ptr->Row = r;
curve1Ptr->End = -1;
curve1Ptr->B = I_2;
curve1Ptr->Column = I2column;
break;
}
curvesCount--;
if (curvesCount > 0)
{
curve1Ptr--;
if (curvesCount > 1)
curve0Ptr--;
else curve0Ptr = null;
}
else
{
curve1Ptr = null;
bottomPtr = null;
curve0Ptr = null;
}
}
}
if (curvesCount == 0)
continue;
if (I2new == INFINITY)
{
dr = bottomPtr->Row - r;
I2new = _squareLookup[dr] + bottomPtr->B;
}
if ((I2val == INFINITY && dtVal == 0) || I2new < dtVal)
{
dtScan[0] = I2new;
dtScan[1] = bottomPtr->Row;
dtScan[2] = bottomPtr->Column;
Debug.Assert(dtScan[0] == distance(r, c, dtScan[1], dtScan[2]));
}
if (bottomPtr->End == r)
{
curvesCount--;
bottomPtr++;
}
}
}
if (computeSquareRoot)
{
dtPtr = dtBuf;
for (r = rows; r != 0; r--)
for (c = columns; c != 0; c--, dtPtr += channels)
{
*dtPtr = (int)Math.Sqrt(*dtPtr);
}
}
}
}
}
return dt;
}
/// <summary>
/// Width of the image (equivalent to <see cref="P:Columns" />)
/// </summary>
public int Width
{
get { return _handler.Columns; }
}
/// <summary>
/// Height of the image (equivalment to <see cref="P:Rows" />)
/// </summary>
public int Height
{
get { return _handler.Rows; }
}
/// <summary>
/// Sets whether this array is an integral array. This property influences how the rectangle sum will be computed.
/// </summary>
public bool IsIntegral
{
get { return _handler.IsIntegral; }
set { _handler.IsIntegral = value; }
}
/// <summary>
/// Computes a sum of the values in the array within the rectangle starting at (<paramref name="startRow" />, <paramref name="startColumn"/>) in <paramref name="channel"/>
/// with a size of <paramref name="rows"/>x<paramref name="columns"/>.
/// </summary>
/// <param name="startRow">Starting row</param>
/// <param name="startColumn">Starting column</param>
/// <param name="rows">Number of rows in the rectangle</param>
/// <param name="columns">Number of columns in the rectangle</param>
/// <param name="channel">Channel to draw values from</param>
/// <returns>The sum of all values in the rectangle</returns>
public int ComputeRectangleSum(int startRow, int startColumn, int rows, int columns, int channel)
{
return _handler.ComputeRectangleSum(startRow, startColumn, rows, columns, channel);
}
/// <summary>
/// Computes a sum of the values in the array starting at (<paramref name="row"/>, <paramref name="column"/>) in <paramref name="channel" />
/// in a rectangle described by the offset and size in <paramref name="rect"/>.
/// </summary>
/// <param name="row">Reference row</param>
/// <param name="column">Reference column</param>
/// <param name="channel">Channel to draw values from</param>
/// <param name="rect">Offset and size of the rectangle</param>
/// <returns>The sum of all values in the rectangle</returns>
public int ComputeRectangleSum(int row, int column, int channel, Rectangle rect)
{
return _handler.ComputeRectangleSum(row, column, channel, rect);
}
/// <summary>
/// Number of rows in the array.
/// </summary>
public int Rows
{
get { return _handler.Rows; }
}
/// <summary>
/// Number of columns in the array.
/// </summary>
public int Columns
{
get { return _handler.Columns; }
}
/// <summary>
/// Number of channels in the array.
/// </summary>
public int Channels
{
get { return _handler.Channels; }
}
/// <summary>
/// Sets the data of the array to <paramref name="data"/>. This new array will replace the current one. No copy is created.
/// </summary>
/// <param name="data">Array to handle</param>
public void SetData(int[, ,] data)
{
_handler.SetData(data);
}
/// <summary>
/// Sets the dimensions of the underlying array. The resulting new array will replace the old array completely, no data will be copied over.
/// </summary>
/// <param name="rows">Number of desired rows in the new array.</param>
/// <param name="columns">Number of desired columns in the new array.</param>
/// <param name="channels">Number of desired channels in the new array.</param>
public void SetDimensions(int rows, int columns, int channels)
{
_handler.SetDimensions(rows, columns, channels);
}
/// <summary>
/// Extracts a portion of the array defined by the parameters.
/// </summary>
/// <param name="startRow">Starting row</param>
/// <param name="startColumn">Starting column</param>
/// <param name="rows">Number of rows in the portion</param>
/// <param name="columns">Number of columns in the portion</param>
/// <returns>A portion of the array</returns>
public int[, ,] ExtractRectangle(int startRow, int startColumn, int rows, int columns)
{
return _handler.ExtractRectangle(startRow, startColumn, rows, columns);
}
/// <summary>
/// Indexes the underlying array.
/// </summary>
/// <param name="row">Desired row</param>
/// <param name="column">Desired column</param>
/// <param name="channel">Desired column</param>
/// <returns>Value at (<paramref name="row"/>, <paramref name="column"/>, <paramref name="channel"/>) within the array.</returns>
public int this[int row, int column, int channel]
{
get
{
return _handler[row, column, channel];
}
set
{
_handler[row, column, channel] = value;
}
}
/// <summary>
/// Extracts an entire channel from the array.
/// </summary>
/// <param name="channel">Channel to extract</param>
/// <returns>Extracted channel</returns>
public int[,] ExtractChannel(int channel)
{
return _handler.ExtractChannel(channel);
}
/// <summary>
/// The underlying array. Breaks capsulation to allow operations using pointer arithmetic.
/// </summary>
public int[, ,] RawArray
{
get { return _handler.RawArray; }