Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AdaptiveThreshold.cs
2using System;
3
5{
9 public static partial class TransformationOperations
10 {
21 public static void AdaptiveThreshold(this Matrix<float> M, float Threshold,
22 int NeighborhoodWidth)
23 {
24 if (NeighborhoodWidth <= 0 || NeighborhoodWidth >= M.Width || NeighborhoodWidth >= M.Height)
25 throw new ArgumentOutOfRangeException(nameof(NeighborhoodWidth));
26
27 Matrix<float> Integral = M.Integral();
28
29 int From = -NeighborhoodWidth / 2;
30 int To = NeighborhoodWidth + From;
31 int y, h = M.Height;
32 int x, w = M.Width;
33 int Index = M.Start;
34 int Skip = M.Skip;
35 float[] Data = M.Data;
36 float[] Sums = Integral.Data;
37 float v, Avg;
38 int x1, y1, x2, y2, dx, dy;
39
40 for (y = 0; y < h; y++, Index += Skip)
41 {
42 y1 = y + From;
43 if (y1 < 0)
44 y1 = 0;
45
46 y2 = y + To;
47 if (y2 >= h)
48 y2 = h - 1;
49
50 dy = y2 - y1;
51
52 for (x = 0; x < w; x++)
53 {
54 x1 = x + From;
55 if (x1 < 0)
56 x1 = 0;
57
58 x2 = x + To;
59 if (x2 >= w)
60 x2 = w - 1;
61
62 dx = x2 - x1;
63
64 v = Data[Index];
65 Avg = (Sums[x2 + y2 * w] - Sums[x1 + y2 * w] - Sums[x2 + y1 * w] + Sums[x1 + y1 * w]) / (dx * dy);
66
67 Data[Index++] = v <= Avg - Threshold ? 0f : 1f;
68 }
69 }
70 }
71
82 public static void AdaptiveThreshold(this Matrix<int> M, int Threshold,
83 int NeighborhoodWidth)
84 {
85 if (NeighborhoodWidth <= 0 || NeighborhoodWidth >= M.Width || NeighborhoodWidth >= M.Height)
86 throw new ArgumentOutOfRangeException(nameof(NeighborhoodWidth));
87
88 Matrix<long> Integral = M.Integral();
89
90 int From = -NeighborhoodWidth / 2;
91 int To = NeighborhoodWidth + From;
92 int y, h = M.Height;
93 int x, w = M.Width;
94 int Index = M.Start;
95 int Skip = M.Skip;
96 int[] Data = M.Data;
97 long[] Sums = Integral.Data;
98 int v, Avg;
99 int x1, y1, x2, y2, dx, dy, dxdy;
100
101 for (y = 0; y < h; y++, Index += Skip)
102 {
103 y1 = y + From;
104 if (y1 < 0)
105 y1 = 0;
106
107 y2 = y + To;
108 if (y2 >= h)
109 y2 = h - 1;
110
111 dy = y2 - y1;
112
113 for (x = 0; x < w; x++)
114 {
115 x1 = x + From;
116 if (x1 < 0)
117 x1 = 0;
118
119 x2 = x + To;
120 if (x2 >= w)
121 x2 = w - 1;
122
123 dx = x2 - x1;
124 dxdy = dx * dy;
125
126 v = Data[Index];
127 Avg = (int)((Sums[x2 + y2 * w] - Sums[x1 + y2 * w] - Sums[x2 + y1 * w] + Sums[x1 + y1 * w] + (dxdy >> 1)) / dxdy);
128
129 Data[Index++] = v <= Avg - Threshold ? 0 : 0x01000000;
130 }
131 }
132 }
133 }
134}
Implements a Matrix, basic component for computations in Image Processing and Computer Vision.
Definition: Matrix.cs:12
int Height
Height of matrix (number of rows)
Definition: Matrix.cs:136
T[] Data
Underlying data on which the matrix is defined.
Definition: Matrix.cs:114
int Width
Width of matrix (number of columns)
Definition: Matrix.cs:120
int Skip
Number of elements to skip from the right edge in the underlying data to the left edge of the new row...
Definition: Matrix.cs:233
int Start
Start offset of matrix in underlying data.
Definition: Matrix.cs:228
Static class for Transformation Operations, implemented as extensions.
static void Threshold(this Matrix< float > M, float Threshold)
Creates a black and white image based on the threshold level provided in Threshold ....
Definition: Threshold.cs:16
static void AdaptiveThreshold(this Matrix< float > M, float Threshold, int NeighborhoodWidth)
Creates a black and white image based on the adaptive threshold levels provided. Each pixel is compar...
static void AdaptiveThreshold(this Matrix< int > M, int Threshold, int NeighborhoodWidth)
Creates a black and white image based on the adaptive threshold levels provided. Each pixel is compar...