Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OtsuThreshold.cs
2
4{
8 public static partial class TransformationOperations
9 {
18 public static float OtsuThreshold(this Matrix<float> M)
19 {
20 return M.OtsuThreshold(256);
21 }
22
32 public static float OtsuThreshold(this Matrix<float> M, int Buckets)
33 {
34 return M.OtsuThreshold(Buckets, 0, 1f);
35 }
36
48 public static float OtsuThreshold(this Matrix<float> M, int Buckets, float Min, float Max)
49 {
50 int[] Histogram = M.Histogram(Buckets, Min, Max);
51 int NrPixels = M.Width * M.Height;
52 int i, j, c = Buckets;
53
54 long SumB = 0;
55 long WSumHistogram = 0;
56 float t, mF, wF, wB = 0;
57 float BestT = 0;
58 float MaxT = 0;
59
60 for (i = 1; i < c; i++)
61 WSumHistogram += i * Histogram[i];
62
63 for (i = 0; i < c; i++)
64 {
65 wF = NrPixels - wB;
66 if (wB > 0 && wF > 0)
67 {
68 mF = (WSumHistogram - SumB) / wF;
69 t = (SumB / wB) - mF;
70 t = wB * wF * t * t;
71 if (t >= MaxT)
72 {
73 BestT = i;
74 MaxT = t;
75 }
76 }
77
78 j = Histogram[i];
79 wB += j;
80 SumB += i * j;
81 }
82
83 return BestT * (Max - Min) / Buckets + Min;
84 }
85
94 public static int OtsuThreshold(this Matrix<int> M)
95 {
96 return M.OtsuThreshold(256);
97 }
98
108 public static int OtsuThreshold(this Matrix<int> M, int Buckets)
109 {
110 return M.OtsuThreshold(Buckets, 0, 0x01000000);
111 }
112
124 public static int OtsuThreshold(this Matrix<int> M, int Buckets, int Min, int Max)
125 {
126 int[] Histogram = M.Histogram(Buckets, Min, Max);
127 int NrPixels = M.Width * M.Height;
128 int i, j, c = Buckets;
129
130 long SumB = 0;
131 long WSumHistogram = 0;
132 float t, mF, wF, wB = 0;
133 float BestT = 0;
134 float MaxT = 0;
135
136 for (i = 1; i < c; i++)
137 WSumHistogram += i * Histogram[i];
138
139 for (i = 0; i < c; i++)
140 {
141 wF = NrPixels - wB;
142 if (wB > 0 && wF > 0)
143 {
144 mF = (WSumHistogram - SumB) / wF;
145 t = (SumB / wB) - mF;
146 t = wB * wF * t * t;
147 if (t >= MaxT)
148 {
149 BestT = i;
150 MaxT = t;
151 }
152 }
153
154 j = Histogram[i];
155 wB += j;
156 SumB += i * j;
157 }
158
159 return (int)(BestT * (Max - Min) / Buckets + Min + 0.5);
160 }
161 }
162}
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
static int OtsuThreshold(this Matrix< int > M)
Calculates the threshold level using Otsu's method.
static float OtsuThreshold(this Matrix< float > M, int Buckets, float Min, float Max)
Calculates the threshold level using Otsu's method.
static int OtsuThreshold(this Matrix< int > M, int Buckets)
Calculates the threshold level using Otsu's method.
static float OtsuThreshold(this Matrix< float > M)
Calculates the threshold level using Otsu's method.
static int OtsuThreshold(this Matrix< int > M, int Buckets, int Min, int Max)
Calculates the threshold level using Otsu's method.
static float OtsuThreshold(this Matrix< float > M, int Buckets)
Calculates the threshold level using Otsu's method.