-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
409 lines (325 loc) · 11.3 KB
/
MainForm.cs
File metadata and controls
409 lines (325 loc) · 11.3 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
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
using MathNet.Numerics.LinearAlgebra;
using System.Text.Json;
namespace HomographyApp;
public partial class MainForm : Form
{
enum PointsInsert
{
original,
reference,
}
private PointsInsert pointsInsertState = PointsInsert.original;
private Bitmap? bitmapOrig;
private Bitmap? bitmapRef;
private Bitmap? bitmapFinal;
private readonly Point[] pointsOrig =
[
new(117, 24),
new(73, 448),
new(552, 444),
new(517, 20)
];
private readonly Point[] pointsRef =
[
new(765, 30),
new(765, 446),
new(1159, 446),
new(1159, 30)
];
private readonly Point[] pointsFinal = new Point[4];
private readonly List<Vector<float>> pointsOrigTransformed = [];
private readonly List<Vector<float>> pointsRefTransformed = [];
private readonly List<Vector<float>> pointsFinalTransformed = [];
private int currentIndexOrig = 0;
private int currentIndexRef = 0;
private readonly CoordTrans ctOrig = new();
private readonly CoordTrans ctRef = new();
private readonly CoordTrans ctFinal = new();
private readonly CoordTrans ctImageNormalisedOrig = new();
private readonly CoordTrans ctImageNormalisedRef = new();
private Matrix<float>? H;
public MainForm()
{
InitializeComponent();
// coord trans original
ctOrig.Xmin = -3.25f;
ctOrig.Xmax = 3.25f;
ctOrig.Ymin = 0f;
ctOrig.Ymax = 4.6f;
ctOrig.Umin = 0;
ctOrig.Umax = 639;
ctOrig.Vmin = 479;
ctOrig.Vmax = 0;
// coord trans reference
ctRef.Xmin = -3.25f;
ctRef.Xmax = 3.25f;
ctRef.Ymin = 0f;
ctRef.Ymax = 4.6f;
ctRef.Umin = 640;
ctRef.Umax = 1279;
ctRef.Vmin = 479;
ctRef.Vmax = 0;
// coord trans final
ctFinal.Xmin = -3.25f;
ctFinal.Xmax = 3.25f;
ctFinal.Ymin = 0f;
ctFinal.Ymax = 4.6f;
ctFinal.Umin = 0;
ctFinal.Umax = 639;
ctFinal.Vmin = 959;
ctFinal.Vmax = 480;
// coord trans Image Normalised origina;
ctImageNormalisedOrig.Xmin = 0f;
ctImageNormalisedOrig.Xmax = 1f;
ctImageNormalisedOrig.Ymin = 0f;
ctImageNormalisedOrig.Ymax = 1f;
ctImageNormalisedOrig.Umin = 0;
ctImageNormalisedOrig.Umax = 639;
ctImageNormalisedOrig.Vmin = 479;
ctImageNormalisedOrig.Vmax = 0;
// coord trans Image Normalised reference
ctImageNormalisedRef.Xmin = 0f;
ctImageNormalisedRef.Xmax = 1f;
ctImageNormalisedRef.Ymin = 0f;
ctImageNormalisedRef.Ymax = 1f;
ctImageNormalisedRef.Umin = 640;
ctImageNormalisedRef.Umax = 1279;
ctImageNormalisedRef.Vmin = 479;
ctImageNormalisedRef.Vmax = 0;
bitmapOrig = new("../../../Assets/imageOrig.png");
bitmapRef = new("../../../Assets/imageRef.png");
bitmapFinal = (Bitmap)bitmapOrig.Clone();
RefreshData();
}
/// <summary>
/// PanelDrawing_MouseDown
/// </summary>
private void PanelDrawing_MouseDown(object sender, MouseEventArgs e)
{
Point p = e.Location;
if (pointsInsertState == PointsInsert.original)
{
pointsOrig[currentIndexOrig] = p;
currentIndexOrig++;
if (currentIndexOrig > 3)
currentIndexOrig = 0;
}
else
{
pointsRef[currentIndexRef] = p;
currentIndexRef++;
if (currentIndexRef > 3)
currentIndexRef = 0;
}
RefreshData();
panelDrawing.Invalidate();
}
/// <summary>
/// PanelDrawing_Paint
/// </summary>
private void PanelDrawing_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (bitmapOrig != null)
g.DrawImage(bitmapOrig, 0, 0);
if (bitmapRef != null)
g.DrawImage(bitmapRef, 640, 0);
if (bitmapFinal != null)
g.DrawImage(bitmapFinal, 0, 480);
using Pen pOrig = new(Brushes.Red, 3f);
using Pen pRef = new(Brushes.Yellow, 3f);
using Pen pFinal = new(Brushes.LightGreen, 3f);
g.DrawLines(pOrig, pointsOrig);
g.DrawLine(pOrig, pointsOrig[3], pointsOrig[0]);
g.DrawLines(pRef, pointsRef);
g.DrawLine(pRef, pointsRef[3], pointsRef[0]);
g.DrawLines(pFinal, pointsFinal);
g.DrawLine(pFinal, pointsFinal[3], pointsFinal[0]);
}
/// <summary>
/// ButtonPointsOrig_Click
/// </summary>
private void ButtonPointsOrig_Click(object sender, EventArgs e)
{
pointsInsertState = PointsInsert.original;
}
/// <summary>
/// ButtonPointsRef_Click
/// </summary>
private void ButtonPointsRef_Click(object sender, EventArgs e)
{
pointsInsertState |= PointsInsert.reference;
}
/// <summary>
/// Refresh text box
/// </summary>
private void RefreshData()
{
ArgumentNullException.ThrowIfNull(bitmapOrig);
textBoxInfo.Clear();
pointsOrigTransformed.Clear();
pointsRefTransformed.Clear();
// point transformation
for (int i = 0; i < 4; i++)
{
pointsOrigTransformed.Add(ctOrig.FromUVtoXYVectorFloat(pointsOrig[i]));
pointsRefTransformed.Add(ctRef.FromUVtoXYVectorFloat(pointsRef[i]));
}
textBoxInfo.AppendText("Original [U,V]:\r\n");
foreach (var pt in pointsOrig)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
textBoxInfo.AppendText("\r\nOriginal [X,Y]:\r\n");
foreach (var pt in pointsOrigTransformed)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
textBoxInfo.AppendText("\r\nReference [U,V]:\r\n");
foreach (var pt in pointsRef)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
textBoxInfo.AppendText("\r\nReference [X,Y]:\r\n");
foreach (var pt in pointsRefTransformed)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
// calculate homography for real world coordinates
H = Homography.Calculate(pointsOrigTransformed, pointsRefTransformed);
textBoxInfo.AppendText("\r\nHomography Matrix H:\r\n");
textBoxInfo.AppendText(H.ToMatrixString());
pointsFinalTransformed.Clear();
for (int i = 0; i < 4; i++)
{
var result = H.Multiply(pointsOrigTransformed[i]);
result /= result[2];
pointsFinalTransformed.Add(result);
}
textBoxInfo.AppendText("\r\nFinal [X,Y]:\r\n");
foreach (var pt in pointsFinalTransformed)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
// point transformation
for (int i = 0; i < 4; i++)
pointsFinal[i] = ctFinal.FromXYVectorFtoUV(pointsFinalTransformed[i]);
textBoxInfo.AppendText("\r\nFinal [U,V]:\r\n");
foreach (var pt in pointsFinal)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
List<Vector<float>> pointOrigList = [];
List<Vector<float>> pointRefList = [];
// point transformation
for (int i = 0; i < 4; i++)
{
pointOrigList.Add(ctImageNormalisedOrig.FromUVtoXYVectorFloat(pointsOrig[i]));
pointRefList.Add(ctImageNormalisedRef.FromUVtoXYVectorFloat(pointsRef[i]));
}
textBoxInfo.AppendText("\r\nOriginal normalised [X,Y]:\r\n");
foreach (var pt in pointOrigList)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
textBoxInfo.AppendText("\r\nReference normalised [X,Y]:\r\n");
foreach (var pt in pointRefList)
textBoxInfo.AppendText(pt.ToString() + "\r\n");
var HImage = Homography.Calculate(pointOrigList, pointRefList);
textBoxInfo.AppendText("\r\nHomography Matrix HImage:\r\n");
textBoxInfo.AppendText(HImage.ToMatrixString());
bitmapFinal = Homography.ComputeOutputImage(bitmapOrig, HImage, ctImageNormalisedOrig);
}
/// <summary>
/// ButtonLoadOrigImage_Click
/// </summary>
private void ButtonLoadOrigImage_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new()
{
InitialDirectory = @"Desktop",
RestoreDirectory = true,
Title = "Browse PNG Files",
DefaultExt = "png",
Filter = "png files (*.png)|*.png|All files (*.*)|*.*",
FilterIndex = 2,
CheckFileExists = true,
CheckPathExists = true
};
openFileDialog.ShowDialog();
string bitmapPath = openFileDialog.FileName;
if (string.IsNullOrEmpty(bitmapPath))
return;
try
{
bitmapOrig = new(bitmapPath);
}
catch (Exception ex)
{
MessageBox.Show("Error loading bitmap" + ex.Message);
return;
}
panelDrawing.Invalidate();
}
/// <summary>
/// ButtonSaveMatrixH_Click
/// </summary>
private void ButtonSaveMatrixH_Click(object sender, EventArgs e)
{
if (H == null)
{
MessageBox.Show("Matrix does not exist.");
return;
}
using SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = @"Desktop",
Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*",
Title = "Save matrix as JSON",
DefaultExt = "json",
FileName = "MatrixH.json"
};
saveFileDialog.ShowDialog();
string filePath = saveFileDialog.FileName;
if (string.IsNullOrEmpty(filePath))
return;
try
{
// Convert to jagged array
float[][] jagged = new float[H.RowCount][];
for (int i = 0; i < H.RowCount; i++)
{
jagged[i] = [.. H.Row(i)];
}
var json = JsonSerializer.Serialize(jagged, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(saveFileDialog.FileName, json);
MessageBox.Show("Matrix saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error saving matrix to file" + ex.Message);
return;
}
}
/// <summary>
/// ButtonRef_Click
/// </summary>
private void ButtonRef_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new()
{
InitialDirectory = @"Desktop",
RestoreDirectory = true,
Title = "Browse PNG Files",
DefaultExt = "png",
Filter = "png files (*.png)|*.png|All files (*.*)|*.*",
FilterIndex = 2,
CheckFileExists = true,
CheckPathExists = true
};
openFileDialog.ShowDialog();
string bitmapPath = openFileDialog.FileName;
if (string.IsNullOrEmpty(bitmapPath))
return;
try
{
bitmapRef = new(bitmapPath);
bitmapFinal = (Bitmap)bitmapRef.Clone();
}
catch (Exception ex)
{
MessageBox.Show("Error loading bitmap" + ex.Message);
return;
}
panelDrawing.Invalidate();
}
}