Form1.cs
17.4 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace ImgCheckReel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ConfigHelper.Config.PropertyBind("CamTestReel_totalcover", txtTotalCover, "Text", "TextChanged", "100");
ConfigHelper.Config.PropertyBind("CamTestReel_threshold", txtthreshold, "Text", "TextChanged", "0.6");
ConfigHelper.Config.PropertyBind("CamTestReel_HL", txtHL, "Value", "Scroll", "40");
ConfigHelper.Config.PropertyBind("CamTestReel_HH", txtHH, "Value", "Scroll", "70");
ConfigHelper.Config.PropertyBind("CamTestReel_LL", txtLL, "Value", "Scroll", "15");
ConfigHelper.Config.PropertyBind("CamTestReel_LH", txtLH, "Value", "Scroll", "100");
ConfigHelper.Config.PropertyBind("CamTestReel_SL", txtSL, "Value", "Scroll", "20");
ConfigHelper.Config.PropertyBind("CamTestReel_SH", txtSH, "Value", "Scroll", "100");
UpdateValue();
}
[HandleProcessCorruptedStateExceptions]
public bool? TestHasReel(string filename, out string msg)
{
msg = "";
DateTime startTime = DateTime.Now;
Bitmap bmp = null;
textBox1.Text = "";
pictureBox1.Image = null;
pictureBox2.Image = null;
using (var s = File.Open(filename, FileMode.Open))
{
bmp = (Bitmap)Bitmap.FromStream(s);
}
try
{
if (bmp == null)
{
return false;
}
pictureBox1.Image = (Image)bmp.Clone();
var b = new Bitmap(bmp.Width / 2, bmp.Height / 2, bmp.PixelFormat);
bool hasReel = false;
using (var g = Graphics.FromImage(b))
{
g.InterpolationMode = InterpolationMode.Low;
g.DrawImage(bmp, 0, 0, b.Width, b.Height);
ImageLockMode imageLockMode = ImageLockMode.ReadWrite;
var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), imageLockMode, b.PixelFormat);
var rois = TestRoi.GetRois();
if (rois == null || rois.Count == 0)
{
rois = new List<TestRoi>() { new TestRoi() };
rois[0].CoverCount = totalcover;
rois[0].RoiPath.Add(new Point(0, 0));
rois[0].RoiPath.Add(new Point(bmp.Width, 0));
rois[0].RoiPath.Add(new Point(bmp.Width, bmp.Height));
rois[0].RoiPath.Add(new Point(0, bmp.Height));
TestRoi.SaveConfig(rois);
}
for (int i = 0; i < rois.Count; i++)
{
// rois[i].CoverCount = totalcover;
double maskcout = 0;
var roi = rois[i];
var rp = roi.RoiPath.Select(r => new Point(r.X / 2, r.Y / 2)).ToList();
// rp.ForEach(s => g.DrawString("新的角点", new Font("黑体", 9), new SolidBrush(Color.FromArgb(0, 255, 0)), s));
var fp = GetFilledPoints(rp);
var nfp = fp.Where(f => IsPointInPath(f.X, f.Y, rp)).ToList();
foreach (var xy in nfp)
{
int x = (xy.Y * bd.Width + xy.X) * 3;
//Marshal.WriteByte(bd.Scan0, x, 0);
//Marshal.WriteByte(bd.Scan0, x + 1, 0);
//Marshal.WriteByte(bd.Scan0, x + 2, 255);
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
var cb = (int)Marshal.ReadByte(bd.Scan0, x);
var h = RgbToHsv(new ColorRGB(cr, cg, cb));
if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh)
{
maskcout++;
Marshal.WriteByte(bd.Scan0, x, 0);
Marshal.WriteByte(bd.Scan0, x + 1, 0);
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
else
{
Marshal.WriteByte(bd.Scan0, x, 255);
Marshal.WriteByte(bd.Scan0, x + 1, 255);
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
}
double calc = maskcout / (double)roi.CoverCount;
textBox1.Text += $" 检测到Roi[{i}]覆盖面积计数:maskcout:{maskcout}/{roi.CoverCount}={calc}<{threshold},result:{(calc <= threshold ? "有料" : "无料")}\r\n";
if (calc <= threshold)
{
hasReel = true;
//break;
}
}
b.UnlockBits(bd);
pictureBox2.Image = (Image)b.Clone();
}
b.Dispose();
return hasReel;
}
catch (AccessViolationException e)
{
MessageBox.Show(" 扫码出现AccessViolationException异常:" + e.ToString());
return null;
}
catch (Exception ex)
{
MessageBox.Show(" 扫码出错:" + ex.ToString());
return null;
}
finally
{
if (bmp != null)
bmp.Dispose();
}
}
int totalcover, hh, hl, ll, lh, sl, sh;
double threshold;
string fName;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "图片|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog.FileName;
TestHasReel(fName, out string msg);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fName))
{
TestHasReel(fName, out string msg);
}
}
void UpdateValue()
{
lblHL.Text = $"CamTestReel_HL(色相):{txtHL.Value}";
hl = txtHL.Value;
lblHH.Text = $"CamTestReel_HH(色相):{txtHH.Value}";
hh = txtHH.Value;
lblLL.Text = $"CamTestReel_LL(亮度):{txtLL.Value}";
ll = txtLL.Value;
lblLH.Text = $"CamTestReel_LH(亮度):{txtLH.Value}";
lh = txtLH.Value;
lblSL.Text = $"CamTestReel_SL(饱和度):{txtSL.Value}";
sl = txtSL.Value;
lblSH.Text = $"CamTestReel_SH(饱和度):{txtSH.Value}";
sh = txtSH.Value;
}
private void button2_Click_1(object sender, EventArgs e)
{
TestRoi.SaveConfig(textBox2.Text);
TestHasReel(fName, out string msg);
}
private void txtHL_Scroll(object sender, EventArgs e)
{
UpdateValue();
button2_Click(null, null);
}
private void txtHH_Scroll(object sender, EventArgs e)
{
UpdateValue();
button2_Click(null, null);
}
private void txtLL_Scroll(object sender, EventArgs e)
{
UpdateValue();
button2_Click(null, null);
}
private void txtLH_Scroll(object sender, EventArgs e)
{
UpdateValue();
button2_Click(null, null);
}
private void button3_Click(object sender, EventArgs e)
{
textBox2.Text = TestRoi.LoadConfig();
}
private void txtthreshold_TextChanged(object sender, EventArgs e)
{
double.TryParse(txtthreshold.Text, out threshold);
button2_Click(null, null);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtSL_Scroll(object sender, EventArgs e)
{
UpdateValue();
button2_Click(null, null);
}
private void txtSH_Scroll(object sender, EventArgs e)
{
UpdateValue(); ;
button2_Click(null, null);
}
private void txtTotalCover_TextChanged(object sender, EventArgs e)
{
int.TryParse(txtTotalCover.Text, out totalcover);
button2_Click(null, null);
}
private static List<Point> GetFilledPoints(List<Point> points)
{
List<Point> filledPoints = new List<Point>();
// 获取路径的边界框
int minX = int.MaxValue;
int minY = int.MaxValue;
int maxX = int.MinValue;
int maxY = int.MinValue;
foreach (Point point in points)
{
if (point.X < minX)
{
minX = point.X;
}
if (point.Y < minY)
{
minY = point.Y;
}
if (point.X > maxX)
{
maxX = point.X;
}
if (point.Y > maxY)
{
maxY = point.Y;
}
}
// 遍历边界框内的每个像素点
for (int y = minY; y <= maxY; y++)
{
for (int x = minX; x <= maxX; x++)
{
if (IsPointInPath(x, y, points))
{
filledPoints.Add(new Point(x, y));
}
}
}
return filledPoints;
}
private static bool IsPointInPath(int x, int y, List<Point> points)
{
bool inside = false;
for (int i = 0, j = points.Count - 1; i < points.Count; j = i++)
{
if (((points[i].Y > y) != (points[j].Y > y)) &&
(x < (points[j].X - points[i].X) * (y - points[i].Y) / (points[j].Y - points[i].Y) + points[i].X))
{
inside = !inside;
}
}
return inside;
}
/// <summary>
/// RGB转换HSV
/// </summary>
/// <param name="rgb"></param>
/// <returns></returns>
public static ColorHSV RgbToHsv(ColorRGB rgb)
{
float min, max, tmp, H, S, V;
float R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
tmp = Math.Min(R, G);
min = Math.Min(tmp, B);
tmp = Math.Max(R, G);
max = Math.Max(tmp, B);
// H
H = 0;
if (max == min)
{
H = 0;
}
else if (max == R && G > B)
{
H = 60 * (G - B) * 1.0f / (max - min) + 0;
}
else if (max == R && G < B)
{
H = 60 * (G - B) * 1.0f / (max - min) + 360;
}
else if (max == G)
{
H = H = 60 * (B - R) * 1.0f / (max - min) + 120;
}
else if (max == B)
{
H = H = 60 * (R - G) * 1.0f / (max - min) + 240;
}
// S
if (max == 0)
{
S = 0;
}
else
{
S = (max - min) * 1.0f / max;
}
// V
V = max;
return new ColorHSV((int)H, (int)(S * 100), (int)(V * 100));
}
}
/// <summary>
/// 类 名:ColorRGB
/// 功 能:R 红色 \ G 绿色 \ B 蓝色 颜色模型
/// 所有颜色模型的基类,RGB是用于输出到屏幕的颜色模式,所以所有模型都将转换成RGB输出
/// 日 期:2015-01-22
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public class ColorRGB
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
public ColorRGB(int r, int g, int b)
{
this._r = r;
this._g = g;
this._b = b;
}
private int _r;
private int _g;
private int _b;
/// <summary>
/// 红色
/// </summary>
public int R
{
get { return this._r; }
set
{
this._r = value;
this._r = this._r > 255 ? 255 : this._r;
this._r = this._r < 0 ? 0 : this._r;
}
}
/// <summary>
/// 绿色
/// </summary>
public int G
{
get { return this._g; }
set
{
this._g = value;
this._g = this._g > 255 ? 255 : this._g;
this._g = this._g < 0 ? 0 : this._g;
}
}
/// <summary>
/// 蓝色
/// </summary>
public int B
{
get { return this._b; }
set
{
this._b = value;
this._b = this._b > 255 ? 255 : this._b;
this._b = this._b < 0 ? 0 : this._b;
}
}
/// <summary>
/// 获取实际颜色
/// </summary>
/// <returns></returns>
public Color GetColor()
{
return Color.FromArgb(this._r, this._g, this._b);
}
}
internal class TestRoi
{
public int CoverCount = 1;
public List<Point> RoiPath = new List<Point>();
const string roifile = "Config\\coverroi.json";
static List<TestRoi> listroi = null;
public static List<TestRoi> GetRois()
{
return listroi;
}
static TestRoi()
{
ReloadConfig();
}
public static string LoadConfig()
{
if (!File.Exists(roifile))
return "";
return File.ReadAllText(roifile);
}
public static void ReloadConfig()
{
if (!File.Exists(roifile))
return;
var roidata = File.ReadAllText(roifile);
listroi = JsonConvert.DeserializeObject<List<TestRoi>>(roidata);
return;
}
public static void SaveConfig(List<TestRoi> data)
{
if (!File.Exists(roifile))
return;
var dataStr = JsonConvert.SerializeObject(data);
File.WriteAllText(roifile, dataStr);
ReloadConfig();
}
public static void SaveConfig(string data)
{
if (!File.Exists(roifile))
return;
File.WriteAllText(roifile, data);
ReloadConfig();
}
}
/// <summary>
/// 类 名:ColorHSV
/// 功 能:H 色相 \ S 饱和度(纯度) \ V 明度 颜色模型
/// 日 期:2015-01-22
/// 修 改:2015-03-20
/// 作 者:ls9512
/// </summary>
public class ColorHSV
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="h"></param>
/// <param name="s"></param>
/// <param name="v"></param>
public ColorHSV(int h, int s, int v)
{
this._h = h;
this._s = s;
this._v = v;
}
private int _h;
private int _s;
private int _v;
/// <summary>
/// 色相
/// </summary>
public int H
{
get { return this._h; }
set
{
this._h = value;
this._h = this._h > 360 ? 360 : this._h;
this._h = this._h < 0 ? 0 : this._h;
}
}
/// <summary>
/// 饱和度(纯度)
/// </summary>
public int S
{
get { return this._s; }
set
{
this._s = value;
this._s = this._s > 255 ? 255 : this._s;
this._s = this._s < 0 ? 0 : this._s;
}
}
/// <summary>
/// 明度
/// </summary>
public int V
{
get { return this._v; }
set
{
this._v = value;
this._v = this._v > 255 ? 255 : this._v;
this._v = this._v < 0 ? 0 : this._v;
}
}
}
}