-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkeletBuild.cs
More file actions
158 lines (145 loc) · 5.39 KB
/
SkeletBuild.cs
File metadata and controls
158 lines (145 loc) · 5.39 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
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 SkeletBuild
{
//Скелетизация Зонга-Суня
public static bool[][] ZhangSuenThinning(bool[][] s)
{
bool[][] temp = ArrayClone(s);
int count = 0;
do
{
count = Step(1, temp, s);
temp = ArrayClone(s);
count += Step(2, temp, s);
temp = ArrayClone(s);
}
while (count > 0);
return s;
}
private static int Step(int stepNo, bool[][] temp, bool[][] s)
{
int count = 0;
for (int i = 1; i < temp.Length - 1; i++)
{
for (int j = 1; j < temp[0].Length - 1; j++)
{
if (SuenThinningAlg(i, j, temp, stepNo == 2))
{
// если изменения происходят
if (s[i][j]) count++;
s[i][j] = false;
}
}
}
return count;
}
private static bool SuenThinningAlg(int x, int y, bool[][] s, bool even)
{
bool p2 = s[x][y - 1];
bool p3 = s[x + 1][y - 1];
bool p4 = s[x + 1][y];
bool p5 = s[x + 1][y + 1];
bool p6 = s[x][y + 1];
bool p7 = s[x - 1][y + 1];
bool p8 = s[x - 1][y];
bool p9 = s[x - 1][y - 1];
int bp1 = NumberOfNonZeroNeighbors(x, y, s);
if (bp1 >= 2 && bp1 <= 6) //2nd условие
{
if (NumberOfZeroToOneTransitionFromP9(x, y, s) == 1)
{
if (even)
{
if (!((p2 && p4) && p8))
{
if (!((p2 && p6) && p8))
{
return true;
}
}
}
else
{
if (!((p2 && p4) && p6))
{
if (!((p4 && p6) && p8))
{
return true;
}
}
}
}
}
return false;
}
private static int NumberOfZeroToOneTransitionFromP9(int x, int y, bool[][] s)
{
bool p2 = s[x][y - 1];
bool p3 = s[x + 1][y - 1];
bool p4 = s[x + 1][y];
bool p5 = s[x + 1][y + 1];
bool p6 = s[x][y + 1];
bool p7 = s[x - 1][y + 1];
bool p8 = s[x - 1][y];
bool p9 = s[x - 1][y - 1];
int A = Convert.ToInt32((!p2 && p3)) + Convert.ToInt32((!p3 && p4)) +
Convert.ToInt32((!p4 && p5)) + Convert.ToInt32((!p5 && p6)) +
Convert.ToInt32((!p6 && p7)) + Convert.ToInt32((!p7 && p8)) +
Convert.ToInt32((!p8 && p9)) + Convert.ToInt32((!p9 && p2));
return A;
}
private static int NumberOfNonZeroNeighbors(int x, int y, bool[][] s)
{
int count = 0;
if (s[x - 1][y]) count++;
if (s[x - 1][y + 1]) count++;
if (s[x - 1][y - 1]) count++;
if (s[x][y + 1]) count++;
if (s[x][y - 1]) count++;
if (s[x + 1][y]) count++;
if (s[x + 1][y + 1]) count++;
if (s[x + 1][y - 1]) count++;
return count;
}
private static T[][] ArrayClone<T>(T[][] A)
{ return A.Select(a => a.ToArray()).ToArray(); }
//Построение скелета с помощью библиотеки Emgu
public static Bitmap SkelatanizeEmgu(Bitmap image)
{
Image<Gray, Byte> imgOld = image.ToImage<Gray, byte>();
Image<Gray, byte> img2 = (new Image<Gray, byte>(imgOld.Width, imgOld.Height, new Gray(255))).Sub(imgOld);
Image<Gray, byte> eroded = new Image<Gray, byte>(img2.Size);
Image<Gray, byte> temp = new Image<Gray, byte>(img2.Size);
Image<Gray, byte> skel = new Image<Gray, byte>(img2.Size);
skel.SetValue(0);
CvInvoke.Threshold(img2, img2, 127, 256, 0);
var element = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new System.Drawing.Point(-1, -1));
bool done = false;
while (!done)
{
CvInvoke.Erode(img2, eroded, element, new System.Drawing.Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
CvInvoke.Dilate(eroded, temp, element, new System.Drawing.Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
CvInvoke.Subtract(img2, temp, temp);
CvInvoke.BitwiseOr(skel, temp, skel);
eroded.CopyTo(img2);
if (CvInvoke.CountNonZero(img2) == 0) done = true;
}
return skel.ToBitmap();
}
}
}