-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeAlgorithm.cs
More file actions
279 lines (224 loc) · 12.3 KB
/
EdgeAlgorithm.cs
File metadata and controls
279 lines (224 loc) · 12.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace AutoPickingSys
{
class EdgeAlgorithm
{
public void
EdgeBitmapCalculate(Bitmap bitmap, int X, int Y, float R, out Bitmap EdgeBitmap,out int gatecolor, ref Dictionary<int, Pixel> Pixels)
{
Bitmap b = (Bitmap)bitmap.Clone();
int width = b.Width;
int height = b.Height;
long _edgeSum = 0;
int countA = 256; int[] dt = new int[257]; int AA; int countB = 256;
int countA1 = 256; int countB1 = 256; int sumA = 0, sumB = 0;
int theCount = 0;
for (int i = 0; i < 257; i++)
{
dt[i] = 0;
}
Bitmap edgeImage = new Bitmap(width, height);// (Bitmap)b.Clone();//
edgeImage = (Bitmap)bitmap.Clone();
try
{
if (bitmap != null)
{
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData edgeData = edgeImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* edge = (byte*)edgeData.Scan0;
byte* nsrc; byte* nedge;
int stride = srcData.Stride;
int offset = stride - width * 4;
int aHr, aHg, aHb, aVr, aVg, aVb, aH, aV;
double scale = 0.062;//gaiguo
double A, Ar, Ag, Ab;
int a0, a45, a90, a135, a180, a225, a270, a315;
double A1, A2, A3, A4, A5, A6, A7;
byte* nw; byte* n; byte* ne;
byte* w; byte* est;
byte* sw; byte* s; byte* se;
nsrc = src;//保存左上角当前指针
nedge = edge;
for (int i = 0; i < width; i++)//将画布涂黑
{
for (int j = 0; j < height; j++)
{
src = nsrc + i * 4 + j * stride;
edge = nedge + i * 4 + j * stride;
edge[0] = edge[1] = edge[2] = 255;
}
}
for (int i = 1; i < width - 1; i++)//
{
for (int j = 1; j < height - 1; j++)//从上往下搜索
{
src = nsrc + i * 4 + j * stride;
edge = nedge + i * 4 + j * stride;
nw = src - stride - 4; // (x-1, y-1)
n = src - stride; // (x , y-1)
ne = src - stride + 4; // (x+1, y-1)
w = src - 4; // (x-1, y)
est = src + 4; // (x+1, y)
sw = src + stride - 4; // (x-1, y+1)
s = src + stride; // (x , y+1)
se = src + stride + 4; // (x+1, y+1)
if ((i - X) * (i - X) + (j - Y) * (j - Y) < R * R)//如果x^2+y^2<r^2
{
a0 = Math.Abs((nw[0] + n[0] * 2 + ne[0]) - (sw[0] + s[0] * 2 + se[0]));
a45 = Math.Abs((w[0] + nw[0] * 2 + n[0]) - (s[0] + se[0] * 2 + est[0]));
a90 = Math.Abs((nw[0] + w[0] * 2 + sw[0]) - (ne[0] + est[0] * 2 + se[0]));
a135 = Math.Abs((w[0] + sw[0] * 2 + s[0]) - (n[0] + ne[0] * 2 + est[0]));
a180 = Math.Abs((sw[0] + s[0] * 2 + se[0]) - (nw[0] + n[0] * 2 + ne[0]));
a225 = Math.Abs((s[0] + se[0] * 2 + est[0]) - (w[0] + nw[0] * 2 + n[0]));
a270 = Math.Abs((ne[0] + est[0] * 2 + se[0]) - (nw[0] + w[0] * 2 + sw[0]));
a315 = Math.Abs((n[0] + ne[0] * 2 + est[0]) - (w[0] + sw[0] * 2 + s[0]));
//A = (aH + aV) / 2;
A1 = Math.Max(a0, a45);
A2 = Math.Max(A1, a90);
A3 = Math.Max(A2, a135);
A4 = Math.Max(A3, a180);
A5 = Math.Max(A4, a225);
A6 = Math.Max(A5, a270);
A7 = Math.Max(A6, a315);
A = A7;
if (A > 255)
{
A = 255;
}
if (A <= 20)
{
A = 0;
}
if (A > 0)
{
theCount++;
_edgeSum += (long)A;
}
_edgeSum += (long)A;
edge[0] = edge[1] = edge[2] = (byte)A;
Pixels[i + j * width].EdgeValue = (byte)A;
Pixels[i + j * width].BinaryzationValue = 1;
Pixels[i + j * width].FirstEdge = 0;
Pixels[i + j * width].SecondEdge = 0;
}
}
}
}
b.UnlockBits(srcData);
edgeImage.UnlockBits(edgeData);
b.Dispose();
}
}
catch (Exception ex)
{
GC.Collect();
}
EdgeBitmap = edgeImage;
gatecolor = (int)(_edgeSum / theCount);
}
public void EdgeBitmapCalculateRectangle(Bitmap bitmap, int X, int Y, int W, int H, out Bitmap EdgeBitmap, ref Dictionary<int, Pixel> Pixels)
{
Bitmap b = (Bitmap)bitmap.Clone();
int width = b.Width;
int height = b.Height;
long _edgeSum = 0;
double scaleR = 3.14;
Bitmap edgeImage = new Bitmap(width, height);// (Bitmap)b.Clone();//
edgeImage = (Bitmap)bitmap.Clone();
try
{
if (bitmap != null)//对源图像与目标图像进行锁存;
{
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData edgeData = edgeImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* src = (byte*)srcData.Scan0;//源图像左上角当前指针;
byte* edge = (byte*)edgeData.Scan0;//目标图像左上角当前指针;
byte* nsrc; byte* nedge;
int stride = srcData.Stride;//扫描宽度;
int offset = stride - width * 4;
int aHr, aHg, aHb, aVr, aVg, aVb, aH, aV;
double scale = 0.2;
double A;
byte* nw; byte* n; byte* ne;
byte* w; byte* est;
byte* sw; byte* s; byte* se;
nsrc = src;//保存左上角当前指针
nedge = edge;
for (int i = 0; i < width; i++)//将画布涂白
{
for (int j = 0; j < height; j++)
{
src = nsrc + i * 4 + j * stride;
edge = nedge + i * 4 + j * stride;
edge[0] = edge[1] = edge[2] = 255;
}
}
for (int i = 1; i < width; i++)//
{
for (int j = 1; j < height; j++)//从上往下搜索
{
src = nsrc + i * 4 + j * stride;
edge = nedge + i * 4 + j * stride;
nw = src - stride - 4; // (x-1, y-1)
n = src - stride; // (x , y-1)
ne = src - stride + 4; // (x+1, y-1)
w = src - 4; // (x-1, y)
est = src + 4; // (x+1, y)
sw = src + stride - 4; // (x-1, y+1)
s = src + stride; // (x , y+1)
se = src + stride + 4; // (x+1, y+1)
if ((i - X > 0) & (i - X < W) & (j - Y > 0) & (j - Y < H))
{
aHr = Math.Abs((nw[0] + w[0] * 2 + sw[0]) - (ne[0] + est[0] * 2 + se[0]));
//计算红色分量垂直灰度差
aVr = Math.Abs((nw[0] + n[0] * 2 + ne[0]) - (sw[0] + s[0] * 2 + se[0]));
//计算绿色分量水平灰度差
aHg = Math.Abs((nw[1] + w[1] * 2 + sw[1]) - (ne[1] + est[1] * 2 + se[1]));
//计算绿色分量垂直灰度差
aVg = Math.Abs((nw[1] + n[1] * 2 + ne[1]) - (sw[1] + s[1] * 2 + se[1]));
//计算蓝色分量水平灰度差
aHb = Math.Abs((nw[2] + w[2] * 2 + sw[2]) - (ne[2] + est[2] * 2 + se[2]));
//计算蓝色分量垂直灰度差
aVb = Math.Abs((nw[2] + n[2] * 2 + ne[2]) - (sw[2] + s[2] * 2 + se[2]));
//计算水平综合灰度差
aH = aHr + aHg + aHb;
//计算垂直综合灰度差
aV = aVr + aVg + aVb;
A = (aH + aV) / 2;
A = (int)(A * scale);//图像梯度值;
_edgeSum += (long)A;
edge[0] = edge[1] = edge[2] = (byte)A;
Pixels[i + j * width].EdgeValue = (byte)A;//把对应点的梯度值写入字典;
Pixels[i + j * width].BinaryzationValue = 1;//置对应点的二进制值为1;
}
}
}
}
b.UnlockBits(srcData);//解锁;
edgeImage.UnlockBits(edgeData);
b.Dispose();//释放;
}
}
catch (Exception ex)
{
GC.Collect();
}
EdgeBitmap = edgeImage;//得到图像直方图;
//GateColor = (int)(_edgeSum / (long)(W * H / scaleR));//得到边缘阈值为梯度平均值;
}
}
}