-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureDetector.cs
More file actions
268 lines (237 loc) · 8.46 KB
/
FeatureDetector.cs
File metadata and controls
268 lines (237 loc) · 8.46 KB
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
//Ebner Lena - MMTB2019
using System;
using System.Drawing;
using System.Collections.Generic;
public sealed class FeatureDetector
{
private RGBChannels image;
private static FeatureDetector _instance = null;
private static readonly object instancelock = new object();
private FeatureDetector()
{
}
public static FeatureDetector Instance()
{
if(_instance == null)
{
lock (instancelock)
{
if(_instance == null)
{
_instance = new FeatureDetector();
}
}
}
return _instance;
}
public RGBChannels HarrisCornerDetection(RGBChannels img)
{
image = img;
ImageProcessor ip = new ImageProcessor();
RGBChannels sobelX = ip.ApplyFilter(img, FilterType.SobelX);
RGBChannels sobelY = ip.ApplyFilter(img, FilterType.SobelY);
RGBChannels gaus = ip.ApplyFilter(img, FilterType.Gaussian);
RGBChannels a = new RGBChannels(img.Width, img.Height);
RGBChannels b = new RGBChannels(img.Width, img.Height);
RGBChannels c = new RGBChannels(img.Width, img.Height);
RGBChannels f = new RGBChannels(img.Width, img.Height);
double valueA = 0;
double valueB = 0;
double valueC = 0;
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
valueA = Math.Pow(sobelX.R[y, x], 2) * gaus.R[y, x];
valueB = Math.Pow(sobelY.R[y, x], 2) * gaus.R[y, x];
valueC = sobelX.R[y, x] * sobelY.R[y, x] * gaus.R[y, x];
a.SetValueToAllChannels(valueA, x, y);
b.SetValueToAllChannels(valueB, x, y);
c.SetValueToAllChannels(valueC, x, y);
var lambdas = GetEigenValues(a.R[y, x], b.R[y, x], c.R[y, x]);
f.SetValueToAllChannels(GetFValue(lambdas.lambda1, lambdas.lambda2), x, y);
}
}
return f;
}
private (double lambda1, double lambda2) GetEigenValues(double a, double b, double c)
{
double lambda1 = 0;
double lambda2 = 0;
double root = Math.Sqrt((Math.Pow(a, 2.0)) - (2.0 * a * b) + (Math.Pow(b, 2.0)) + (4.0 * Math.Pow(c, 2)));
lambda1 = (1.0 / 2.0) * (a + b + root);
lambda2 = (1.0 / 2.0) * (a + b - root);
return (lambda1, lambda2);
}
private static double GetFValue(double lambda1, double lambda2)
{
double f = 0;
if (lambda1 >= lambda2 && lambda1 > 4.0)
f = (lambda1 * lambda2) / (lambda1 + lambda2);
return f;
}
public List<HarrisNode> GetMaximas(int dimX, int dimY, RGBChannels f)
{
double[,] maximas = new double[f.R.GetLength(0), f.R.GetLength(1)];
List<HarrisNode> maxis = new List<HarrisNode>();
double[,] localMax = new double[9, 9];
for (int x = 0; x < f.R.GetLength(0) - 8; x += dimX)
{
for (int y = 0; y < f.R.GetLength(1) - 8; y += dimY)
{
for (int i = 0; i < dimX; i++)
{
for (int j = 0; j < dimY; j++)
{
localMax[i, j] = f.R[x + i, y + j];
}
}
SetLocalMaximas(ref maxis, localMax, x, y);
}
}
return maxis;
}
private void SetLocalMaximas(ref List<HarrisNode> maxis, double[,] localMaximas, int x, int y)
{
int maxIndexX = 0;
int maxIndexY = 0;
double maxValue = localMaximas[0, 0];
//find maxima
for (int i = 0; i < localMaximas.GetLength(0); i++)
{
for (int j = 0; j < localMaximas.GetLength(1); j++)
{
if (localMaximas[i, j] > maxValue)
{
maxValue = localMaximas[i, j];
maxIndexX = i;
maxIndexY = j;
}
}
}
//apply 1 for maximum, 0 for rest
for (int i = 0; i < localMaximas.GetLength(0); i++)
{
for (int j = 0; j < localMaximas.GetLength(1); j++)
{
if (i == maxIndexX && j == maxIndexY && localMaximas[i, j] != 0)
{
maxis.Add(new HarrisNode(localMaximas[i, j], (i + x), (j + y)));
}
}
}
}
public List<HarrisNode> ThresholdMaximas(List<HarrisNode> maximas, double threshold)
{
List<HarrisNode> realMaxis = new List<HarrisNode>();
foreach (HarrisNode max in maximas)
{
List<HarrisNode> neighbours = maximas.FindAll(m => m.X > max.X - 4 && m.X < max.X + 4 && m.Y > max.Y - 4 && m.Y < max.Y + 4);
HarrisNode maximum = neighbours[0];
foreach (HarrisNode n in neighbours)
{
if (n.Value > maximum.Value)
{
maximum = n;
}
}
if (maximum.Value > threshold)
realMaxis.Add(maximum);
}
return realMaxis;
}
public List<HarrisNode> GetOrientationBin(RGBChannels orientation, List<HarrisNode> maximas)
{
foreach (HarrisNode max in maximas)
{
List<HarrisNode> neighbours = maximas.FindAll(m => m.X > max.X - 4 && m.X < max.X + 4 && m.Y > max.Y - 4 && m.Y < max.Y + 4);
int[] or = new int[4];
foreach (HarrisNode n in neighbours)
{
if (orientation.R[n.X, n.Y] >= 90)
{
or[0]++;
}
else if (orientation.R[n.X, n.Y] >= 0)
{
or[1]++;
}
else if (orientation.R[n.X, n.Y] >= -90)
{
or[2]++;
}
else
{
or[3]++;
}
}
//get maximum of 4bin
int maxOrIndex = 0;
int maxOr = 0;
for (int i = 0; i < or.Length; i++)
{
if (or[i] > maxOr)
{
maxOr = or[i];
maxOrIndex = i;
}
}
if (maxOrIndex == 0)
max.OrientationColor = Color.Blue;
else if (maxOrIndex == 1)
max.OrientationColor = Color.Red;
else if (maxOrIndex == 2)
max.OrientationColor = Color.Yellow;
else
max.OrientationColor = Color.Green;
}
return maximas;
}
public RGBChannels GetColorFromFValue(RGBChannels f, List<HarrisNode> maximas)
{
RGBChannels result = new RGBChannels(f.Width, f.Height);
for (int x = 0; x < f.Width; x++)
{
for (int y = 0; y < f.Height; y++)
{
HarrisNode node = maximas.Find( max => max.X == x && max.Y == y);
if (node != null)
{
result.R[y,x] = 255;
result.G[y,x] = 0;
result.B[y,x] = 0;
}
else {
result.R[y,x] = image.R[y,x];
result.G[y,x] = image.G[y,x];
result.B[y,x] = image.B[y,x];
}
}
}
return result;
}
public RGBChannels GetColorFromOrientation(RGBChannels f, List<HarrisNode> maximas)
{
RGBChannels result = new RGBChannels(f.Width, f.Height);
for (int x = 0; x < f.Width; x++)
{
for (int y = 0; y < f.Height; y++)
{
HarrisNode node = maximas.Find(max => max.X == x && max.Y == y);
if (node != null)
{
result.R[y,x] = node.OrientationColor.R;
result.G[y,x] = node.OrientationColor.G;
result.B[y,x] = node.OrientationColor.B;
}
else
{
result.R[y,x] = image.R[y,x];
result.G[y,x] = image.G[y,x];
result.B[y,x] = image.B[y,x];
}
}
}
return result;
}
}