-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
617 lines (449 loc) · 20.9 KB
/
Form1.cs
File metadata and controls
617 lines (449 loc) · 20.9 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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using drawPoint = System.Drawing.Point;
//using System.Windows.Forms.
namespace INFOIBV
{
public partial class INFOIBV : Form
{
private Bitmap InputImage;
private Bitmap OutputImage;
Color[,] Image;
Color[,] newImage;
float[,] Kx;
float[,] Ky;
// Variabelen die voor de preprocessing pipeline nodig zijn
Color[,] greyscaleImage;
int leftUpperBbX;
int leftUpperBbY;
int maxx;
int maxy;
int currentRegions;
int regionCount;
int optimalThreshold;
bool[,] optimalLabel;
public INFOIBV()
{
InitializeComponent();
}
private void LoadImageButton1_Click(object sender, EventArgs e) { LoadAndCropImage(); }
private void applyButton_Click(object sender, EventArgs e)
{
resetForApply();
if (InputImage == null)
{
MessageBox2.Text = "Please Load an image first.";
return;
}
if (BoundaryRadio.Checked)
{
drawPoint[] boundary = TraceBoundary(Image, getBackgroundColor());
kernelInput.Text = WritedrawPointArr(boundary);
BoundaryToOutput(boundary, Image);
}
else if (cornerDetRadio.Checked)
{
int n = 0;
CreateSobelKernel(n, ref Kx, ref Ky);
List<Corner> corners = HarrisCornerDetection(Kx, Ky, Image);
CornersToImage(corners);
kernelInput.Text = WritedrawPointArr(CornerListToArray(corners));
}
if (ValueRadio.Checked)
kernelInput.Text = "Unique values: " + valueCount(generateHistogram(Image));
else if (thresholdRadio.Checked)
toOutputBitmap(ApplyThresholdFilter(Image, thresholdTrackbar.Value));
else if (edgeDetection.Checked)
toOutputBitmap(ApplyEdgeDetection(Image));
else if (findCentroid.Checked)
{
try
{
drawPoint[] point = new drawPoint[1];
drawPoint centroid = FindCentroid(ColorArrayToDrawPoints(Image));
point[0] = centroid;
drawPointsToImage(point, Image);
}
catch (Exception error) { MessageBox2.Text = error.Message; }
}
else if (greyscaleRadio.Checked)
toOutputBitmap(ApplyGreyscale(Image));
else if (preprocessingRadio.Checked)
toOutputBitmap(PreprocessingPipeline(Image));
else if (pipelineRadio.Checked)
{
drawPoint[] conDefList = new drawPoint[0];
drawPoint leftUpperBoundingBox = new drawPoint(0, 0);
float[] angleList = new float[0];
try
{
Color[,] pipelineImage = PreprocessingPipeline(Image);
CreateSobelKernel(0, ref Kx, ref Ky);
List<Corner> cornerList = HarrisCornerDetection(Kx, Ky, pipelineImage);
drawPoint[] cornerArray = CornerListToArray(cornerList);
try
{
//calculate the convex hull, by drawing a 'border' around the outer pixels, and add the inward points close to the far center in the hand
conDefList = AddConvexDefects(cornerArray, ConvexHull(cornerList), pipelineImage);
//calculate the angles between each of the neigbouring points in the hand.
angleList = cornerOfConvex(conDefList);
leftUpperBoundingBox = new drawPoint(leftUpperBbX, leftUpperBbY);
}
catch (Exception)
{
//if only two points were found, no border can be drawn, and output will be given to determine why not.
toOutputBitmap(pipelineImage);
AddDrawPointsToImage(pipelineImage, cornerArray); //show the preprocessed structure with found corners.
throw new Exception("Largest area after preprocessing does not have enough corners to proceed. This is not a hand, or only the sleeve has been processed");
}
try
{
//the most corners from convexhull and defects are likely found in the hand
//scan over the image and find the box that contains the most points
//this can be used to calculate a more accurate centroid, and get more better defects.
Tuple<Color[,], List<Corner>, drawPoint> hand = isolateHand(pipelineImage, conDefList, cornerArray);
Console.WriteLine("Size hand: " + hand.Item1.GetLength(0) + "x" + hand.Item1.GetLength(1));
Console.WriteLine("Number of corners: " + hand.Item2.Count);
Console.WriteLine("Upperleft corner: (" + (hand.Item3.X + leftUpperBbX) + ", " + (hand.Item3.Y + leftUpperBbY) + ")");
//calculate the convex d
drawPoint[] handConDefList = AddConvexDefects(CornerListToArray(hand.Item2), ConvexHull(hand.Item2), hand.Item1);
angleList = cornerOfConvex(handConDefList);
drawPoint handLeftUpperBoundingBox = hand.Item3;
conDefList = handConDefList;
leftUpperBoundingBox = new drawPoint(handLeftUpperBoundingBox.X + leftUpperBbX, handLeftUpperBoundingBox.Y + leftUpperBbY);
}
catch { throw new Exception("Hand isolation failed - continuing with full region."); }
}
catch (Exception error) { MessageBox2.Text = error.Message; }
kernelInput.Text = WriteAnglesDistances(angleList, conDefList, 8, 40);
drawPoint[] oImageConDef = OriginalImagePoints(conDefList, leftUpperBoundingBox);
Color drawColor = StateToColor(determineObject(angleList, 8, 40));
toOutputBitmap(CrossesInImage(oImageConDef, DrawLinesBetwPoints(oImageConDef, greyscaleImage, Color.CadetBlue), drawColor));
//kernelInput.Text = WritedrawPointArr(AddConvexDefects(CornerListToArray(cornerList), ConvexHull(cornerList), pipelineImage));
// catch (Exception error) { MessageBox2.Text = "Error - please try another image."; }
}
else if (ErosionRadio.Checked)
toOutputBitmap(ApplyErosionDilationFilter(Image, true));
else if (DilationRadio.Checked)
toOutputBitmap(ApplyErosionDilationFilter(Image, false));
else if (OpeningRadio.Checked)
toOutputBitmap(ApplyOpeningClosingFilter(Image, true));
else if (ClosingRadio.Checked)
toOutputBitmap(ApplyOpeningClosingFilter(Image, false));
//toOutputBitmap(newImage);
//greyscale, region labelling, opening closing (dus erosion dilation),
//weg: Fourier, complement WritedrawVectArr
}
Color[,] AddDrawPointsToImage(Color[,] InputImage, drawPoint[] points)
{
foreach (drawPoint p in points)
InputImage[p.X, p.Y] = Color.Red;
return InputImage;
}
Color[,] DrawLinesBetwPoints(drawPoint[] points, Color[,] InputImage, Color drawColor)
{
Stack<drawPoint> sPoints = new Stack<drawPoint>(points);
drawPoint firstPoint = sPoints.Peek();
while(sPoints.Count > 1)
{
drawPoint p1 = sPoints.Pop();
drawPoint p2 = sPoints.Pop();
InputImage = DrawLine(p1, p2, InputImage, drawColor);
sPoints.Push(p2);
}
InputImage = DrawLine(firstPoint, sPoints.Pop(), InputImage, drawColor);
return InputImage;
}
Color[,] DrawLine(drawPoint A, drawPoint B, Color[,] InputImage, Color drawColor)
{
Color foreGrColor = drawColor;
float x0 = A.X;
float y0 = A.Y;
float deltaX = B.X - A.X;
float deltaY = B.Y - A.Y;
float maximum = Math.Max(Math.Abs(deltaX), Math.Abs(deltaY));
deltaX /= maximum;
deltaY /= maximum;
for (float n = 0; n < maximum; ++n)
{
// draw pixel at ( A.X, A.Y )
x0 += deltaX; y0 += deltaY;
InputImage[(int)x0, (int)y0] = foreGrColor;
}
return InputImage;
}
drawPoint[] ColorArrayToDrawPoints(Color[,] image)
{
Color backgroundColor = getBackgroundColor();
List<drawPoint> drawpoints = new List<drawPoint>();
for (int u = 0; u < image.GetLength(0); u++)
for (int v = 0; v < image.GetLength(1); v++)
{
if (image[u, v] != backgroundColor)
drawpoints.Add(new drawPoint(u, v));
}
return drawpoints.ToArray();
}
drawPoint[] CornerListToArray(List<Corner> cornerList)
{
drawPoint[] cornerArray = new drawPoint[cornerList.Count];
for (int i = 0; i < cornerList.Count; i++)
cornerArray[i] = new drawPoint(cornerList[i].U, cornerList[i].V);
return cornerArray;
}
Color getBackgroundColor()
{
if (checkBlackBackground.Checked)
return Color.FromArgb(255, 0, 0, 0);
return Color.FromArgb(255, 255, 255, 255);
}
Color getForegroundColor()
{
if (checkBlackBackground.Checked)
return Color.FromArgb(255, 255, 255, 255);
return Color.FromArgb(255, 0, 0, 0);
}
String WritedrawVectArr(Vector[] Cn)
{
String output = "{";
for (int n = 0; n < Cn.Length; n++)
{
output = output + "(" + Cn[n].X + "," + Cn[n].Y + "), ";
}
output += "}";
return output;
}
String WritedrawPointArr(drawPoint[] drawPoints)
{
String output = "";
for (int n = 0; n < drawPoints.Length; n++)
{
output = output + drawPoints[n].X + "\t" + drawPoints[n].Y + "\r\n";
}
return output;
}
String WriteAnglesDistances(float[] angles, drawPoint[] points, int lowerThr, int upperThr)
{
String output = "";
if (angles[0] > lowerThr && angles[0] < upperThr)
output = output + angles[0] + "\t" + SqDistancePoints(points[points.Length - 1], points[0]) + "\t" + SqDistancePoints(points[0], points[1]) + "\r\n";
for (int n = 1; n < points.Length - 1; n++)
{
if(angles[n] > lowerThr && angles[n] < upperThr)
{
output = output + angles[n] + "\t" + SqDistancePoints(points[n], points[n - 1]) + "\t" + SqDistancePoints(points[n], points[n + 1]) + "\r\n";
}
}
if (angles[0] > lowerThr && angles[0] < upperThr)
output = output + angles[points.Length - 1] + "\t" + SqDistancePoints(points[points.Length - 2], points[points.Length -1]) + "\t" + SqDistancePoints(points[points.Length - 1], points[0]) + "\r\n";
return output;
}
String WritedrawFloatArr(float[] fs)
{
String output = "";
for (int i = 0; i < fs.Length; i++)
{
output = output + fs[i] + "\r\n";
}
return output;
}
float Pi = (float)Math.PI;
private void saveButton_Click(object sender, EventArgs e)
{
if (OutputImage == null) return; // Get out if no output image
if (saveImageDialog.ShowDialog() == DialogResult.OK)
OutputImage.Save(saveImageDialog.FileName); // Save the output image
}
void resetForApply()
{
MessageBox2.Text = "";
if (InputImage == null) return; // Get out if no input image
if (OutputImage != null)
{
if (RightAsInput.Checked)
{
InputImage.Dispose();
InputImage = new Bitmap(OutputImage.Size.Width, OutputImage.Size.Height);
for (int x = 0; x < InputImage.Size.Width; x++)
{
for (int y = 0; y < InputImage.Size.Height; y++)
{
InputImage.SetPixel(x, y, OutputImage.GetPixel(x, y)); // Set the pixel color at coordinate (x,y)
//OutputImage.SetPixel(x, y, newImage[x, y]);
}
}
pictureBox1.Image = InputImage;
}
OutputImage.Dispose(); // Reset output image
} // Reset output image
OutputImage = new Bitmap(InputImage.Size.Width, InputImage.Size.Height); // Create new output image
Image = new Color[InputImage.Size.Width, InputImage.Size.Height]; // Create array to speed-up operations (Bitmap functions are very slow)
newImage = new Color[InputImage.Size.Width, InputImage.Size.Height];
greyscaleImage = new Color[InputImage.Size.Width, InputImage.Size.Height];
// Setup progress bar
progressBar.Visible = true;
progressBar.Minimum = 1;
progressBar.Maximum = InputImage.Size.Width * InputImage.Size.Height;
progressBar.Value = 1;
progressBar.Step = 1;
Image = BitmapToArray(InputImage);
}
Color[,] BitmapToArray(Bitmap InputImage)
{
Color[,] OutputImage = new Color[InputImage.Size.Width, InputImage.Size.Height];
for (int x = 0; x < InputImage.Size.Width; x++)
{
for (int y = 0; y < InputImage.Size.Height; y++)
{
OutputImage[x, y] = InputImage.GetPixel(x, y); // Set pixel color in array at (x,y)
}
}
return OutputImage;
}
/// <summary>
/// Generates an image in picturBox2 based on the color matrix newImage.
/// </summary>
void toOutputBitmap(Color[,] newImage)
{
//Image = newImage;
// Copy array to output Bitmap
OutputImage.Dispose(); // Reset output image
OutputImage = new Bitmap(newImage.GetLength(0), newImage.GetLength(1));
for (int x = 0; x < OutputImage.Size.Width; x++)
{
for (int y = 0; y < OutputImage.Size.Height; y++)
{
//OutputImage1.SetPixel(x, y, Image[x, y]); // Set the pixel color at coordinate (x,y)
OutputImage.SetPixel(x, y, newImage[x, y]);
}
}
outputBox1.Image = (Image)OutputImage; // Display output image
progressBar.Visible = false; // Hide progress bar
}
/// <summary>
/// Reads the input of textbox1 and returns a matrix generated from the input.
/// </summary>
/// <returns></returns>
int[,] ParseMatrix()
{
try
{
// split the rows
string input = kernelInput.Text;
string[] rows = input.Split(new string[] { "\r\n" }, StringSplitOptions.None);
// split the columns and add to a 2D array
int[,] matrix = new int[rows.Length, rows.Length]; //creer M x M matrix afhankelijk van ingevoerde values
for (int i = 0; i < rows.Length; i++)
{
// alle 3 de rijen parsen
int[] column = Array.ConvertAll(rows[i].Split(' '), int.Parse);
if (column.Length != rows.Length)
{
throw new Exception("Provide a square matrix, with equal number of rows and columns");
}
if (column.Length % 2 == 0)
throw new Exception("Provide a square matrix, with an odd number of columns and rows");
// deze kolom op de goede plek in de matrix zetten
for (int j = 0; j < rows.Length; j++)
matrix[i, j] = column[j];
}
return matrix;
//de matrix is geparsed en de waardes zijn nu op te halen
}
catch (Exception e)
{
MessageBox2.Text = e.Message;
return null;
}
}
/// <summary>
/// Checks if a number is binary (used to check the valuecount of a histogram for binary images).
/// </summary>
Tuple<Color[,], Boolean> MaybeBinary(Color[,] InputImage, int input)
{
if (checkBinary.Checked)
{
InputImage = ApplyThresholdFilter(InputImage);
return new Tuple<Color[,], Boolean>(InputImage, true);
}
if (input == 2) return new Tuple<Color[,], Boolean>(InputImage, true);
return new Tuple<Color[,], bool>(InputImage, false);
}
/// <summary>
/// Takes an integer value and clamps it to either the minimum or maximum RGB-value (0-255).
/// </summary>
int clamp(int i)
{
if (i < 0) return 0;
else if (i > 255) return 255;
else return i;
}
// -------------------------------------------- TRACE BOUNDARY CODE --------------------------------------------
Color[,] ApplyEdgeDetection(Color[,] InputImage)
{
Color[,] OutputImage = new Color[InputImage.GetLength(0), InputImage.GetLength(1)];
//
float[,] Hx = new float[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
float[,] Hy = new float[,] { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
int halfboxsize = Hx.GetLength(0) / 2;
for (int x = halfboxsize; x < InputImage.GetLength(0) - halfboxsize; x++)
{
for (int y = halfboxsize; y < InputImage.GetLength(1) - halfboxsize; y++)
{
float u = CalculateNewColor(x, y, Hx, halfboxsize, InputImage, false) / 8; //apply Hx to the image pixel
float v = CalculateNewColor(x, y, Hy, halfboxsize, InputImage, false) / 8; //apply Hy to the image pixel
int edgeStrength = (int)Math.Sqrt(u * u + v * v); //calculate edgestrength by calculating the length of vector [Hx, Hy]
//clamp to max nad min values
if (edgeStrength > 255)
edgeStrength = 255;
if (edgeStrength < 0)
edgeStrength = 0;
edgeStrength = 255 - edgeStrength;
Color updatedColor = Color.FromArgb(edgeStrength, edgeStrength, edgeStrength);
OutputImage[x, y] = updatedColor;
}
progressBar.PerformStep();
}
return OutputImage;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void BoundaryRadio_CheckedChanged(object sender, EventArgs e)
{
}
private void ValueRadio_CheckedChanged(object sender, EventArgs e)
{
}
private void thresholdRadio_CheckedChanged(object sender, EventArgs e)
{
if (thresholdRadio.Checked)
thresholdTrackbar.Enabled = true;
else
thresholdTrackbar.Enabled = false;
}
private void thresholdTrackbar_Scroll(object sender, EventArgs e)
{
thresholdValue.Text = thresholdTrackbar.Value.ToString();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void INFOIBV_Load(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
}
private void kernelInput_TextChanged(object sender, EventArgs e)
{
}
private void progressBar_Click(object sender, EventArgs e)
{
}
}
}