-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (97 loc) · 3.89 KB
/
Program.cs
File metadata and controls
110 lines (97 loc) · 3.89 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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Recognizer
{
internal static class Program
{
private const int ResizeRate = 2;
private static Bitmap ConvertToBitmap(int width, int height, Func<int, int, Color> getPixelColor)
{
var bmp = new Bitmap(ResizeRate * width, ResizeRate * height);
using (var g = Graphics.FromImage(bmp))
{
for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
g.FillRectangle(new SolidBrush(getPixelColor(x, y)),
ResizeRate * x,
ResizeRate * y,
ResizeRate,
ResizeRate
);
}
return bmp;
}
private static Bitmap ConvertToBitmap(Pixel[,] array)
{
return ConvertToBitmap(array.GetLength(0), array.GetLength(1),
(x, y) => Color.FromArgb(array[x, y].R, array[x, y].G, array[x, y].B));
}
private static Bitmap ConvertToBitmap(double[,] array)
{
return ConvertToBitmap(array.GetLength(0), array.GetLength(1), (x, y) =>
{
var gray = (int) (255 * array[x, y]);
gray = Math.Min(gray, 255);
gray = Math.Max(gray, 0);
return Color.FromArgb(gray, gray, gray);
});
}
private static PictureBox CreateBox(Bitmap bmp)
{
return new PictureBox
{
Size = bmp.Size,
Dock = DockStyle.Fill,
Image = bmp
};
}
public static Pixel[,] LoadPixels(Bitmap bmp)
{
var pixels = new Pixel[bmp.Width, bmp.Height];
for (var x = 0; x < bmp.Width; x++)
for (var y = 0; y < bmp.Height; y++)
pixels[x, y] = new Pixel(bmp.GetPixel(x, y));
return pixels;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
var bmp = (Bitmap) Image.FromFile("eurobot.bmp");
var pixels = LoadPixels(bmp);
var form = new Form
{
ClientSize = new Size(3 * ResizeRate * bmp.Width, 2 * ResizeRate * bmp.Height)
};
var panel = new TableLayoutPanel
{
RowCount = 2,
ColumnCount = 3,
Dock = DockStyle.Fill
};
form.Controls.Add(panel);
panel.Controls.Add(CreateBox(ConvertToBitmap(pixels)), 0, 0);
var grayscale = Grayscale.ToGrayscale(pixels);
panel.Controls.Add(CreateBox(ConvertToBitmap(grayscale)), 1, 0);
var clear = MedianFiltere.MedianFilter(grayscale);
panel.Controls.Add(CreateBox(ConvertToBitmap(clear)), 2, 0);
var sobell = SobelFilterClass.SobelFilter(clear, new double[,] {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}});
panel.Controls.Add(CreateBox(ConvertToBitmap(sobell)), 0, 1);
var trashhold = ThresholdFilterClass.ThresholdFilter(sobell, 0.1);
panel.Controls.Add(CreateBox(ConvertToBitmap(trashhold)), 1, 1);
var bitmap = ConvertToBitmap(sobell);
using (var g = Graphics.FromImage(bitmap))
{
var lines = HoughTransform.HoughAlgorithm(sobell);
var pen = new Pen(Color.Red, 2);
foreach (var e in lines)
g.DrawLine(pen, e.X0 * ResizeRate, e.Y0 * ResizeRate, e.X1 * ResizeRate, e.Y1 * ResizeRate);
}
panel.Controls.Add(CreateBox(bitmap), 2, 1);
Application.Run(form);
}
}
}