-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoughTransform.cs
More file actions
105 lines (97 loc) · 3.98 KB
/
HoughTransform.cs
File metadata and controls
105 lines (97 loc) · 3.98 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace VKR
{
//трансформация Хафа
internal class HoughTransform
{
public static Bitmap Hough(Bitmap orig, int determination_accuracy)
{
Bitmap res = (Bitmap)orig.Clone();
int r_max = (int)Math.Sqrt(Math.Pow(orig.Width, 2) + Math.Pow(orig.Height, 2)) + 1;//диагональ, самая большая линия
int q_max = 180;//угол
int[,] accum = new int[r_max, q_max];//аккум
int[,] accumDraw = new int[r_max, q_max];
//Создание пространства Hough.
for (int x = 0; x < res.Width; x++)
{
for (int y = 0; y < res.Height; y++)
{
if (res.GetPixel(x, y) == Color.FromArgb(0, 0, 0))
{
for (int j_q = 0; j_q < q_max; j_q++)
{
int i_r = (int)(x * Math.Cos(j_q * Math.PI / 180) + y * Math.Sin(j_q * Math.PI / 180));
if (i_r > 0)
accum[i_r, j_q]++; //Draw sinusoids using the example of an accumay (increment the cell where the sinusoid passes)
}
}
}
}
//поиск локального максимума
int r = 0, q = 0, curMax = 0;
for (int k = 0; k <= determination_accuracy; k++)
{
for (int i_r = 0; i_r < r_max; i_r++)
{
for (int j_q = 0; j_q < q_max; j_q++)
{
if (accum[i_r, j_q] > curMax)
{
curMax = accum[i_r, j_q];
r = i_r;
q = j_q;
}
}
}
accum[r, q] = 0;
accumDraw[r, q] = curMax;//Максимумы занесены в отдельный массив
curMax = 0;
}
List<System.Drawing.Point> lis = new List<System.Drawing.Point>();
// пересчитать в декартову систему координат
for (int i_r = 0; i_r < r_max; i_r++)
{
for (int j_q = 0; j_q < q_max; j_q++)
{
if (accumDraw[i_r, j_q] != 0)
{
for (int x = 0; x < res.Width; x++)
{
int y = (int)((i_r - x * Math.Cos(j_q * Math.PI / 180)) / Math.Sin(j_q * Math.PI / 180));
if (y > 0 && y < res.Height)
{
res.SetPixel(x, y, Color.White);//горизонт
res.SetPixel(x, y + 1, Color.White);//толще
res.SetPixel(x, y - 1, Color.White);//толще
//lis.Add(new System.Drawing.Point(x, y));
}
}
for (int y = 0; y < res.Height; y++)
{
int x = (int)((i_r - y * Math.Sin(j_q * Math.PI / 180)) / Math.Cos(j_q * Math.PI / 180));
if (x > 0 && x < res.Width)
{
res.SetPixel(x, y, Color.White);
//lis.Add(new System.Drawing.Point(x, y));
}
}
}
}
}
return res;
}
}
}