轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 06c627ec032b3f3876fd7db8a3ff0ff1a6614fa2
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LB_VisionControls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Reflection;
using System.Diagnostics.Contracts;
using System.Drawing.Drawing2D;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
using System.Diagnostics;
using OpenVinoSharp;
 
namespace LB_VisionProcesses.Alogrithms.Halcon
{
    public partial class HFindMultiModelToolEdit : TAlgorithmEdit
    {
        public List<HMulitModelControl> listModelToolControls = new List<HMulitModelControl>();
 
        public HFindMultiModelToolEdit(HFindMultiModelTool subject = null)
        {
            if (subject != null && subject is HFindMultiModelTool)
                Subject = subject;
            else
                Subject = new HFindMultiModelTool();
 
            this.Dock = DockStyle.Fill;
            InitializeComponent();
        }
 
        /// <summary>
        /// 控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HFindModelToolEdit_Load(object sender, EventArgs e)
        {
            pnlInputImage.Controls.Add(inputImageHSmartWindowControl);
            inputImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            pnlRecordImage.Controls.Add(recordImageHSmartWindowControl);
            recordImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            // 设置显示样式
            toolTip.AutoPopDelay = 5000;//提示信息的可见时间
            toolTip.InitialDelay = 500;//事件触发多久后出现提示
            toolTip.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
            toolTip.ShowAlways = true;//是否显示提示框
 
            //遍历可以选择的Roi类型枚举
            foreach (var value in Enum.GetValues(typeof(RoiType)))
                cmbTypeRoi.Items.Add(value.ToString());
 
            //遍历可以选择的Fixture枚举
            cmbFixture.Items.Add("");
            foreach (string value in IProcess.dicFixtures.Keys)
                cmbFixture.Items.Add(value.ToString());
 
            cmbTypeRoi.Text = RoiType.None.ToString();
            LoadParas();
 
            if (Subject.Result)
            {
                lblResult.BackColor = Color.Green;
                lblResult.Text = "True";
            }
            else
            {
                lblResult.BackColor = Color.Red;
                lblResult.Text = "False";
            }
 
            lblMsg.Text = Msg.Length > 50 ? Msg.Substring(0, 50) : Msg;
            lblMsgToolTip.SetToolTip(BtmStatusStrip, Msg);
            lblRunTime.Text = $"{Subject.RunTime}ms";
 
            UpdataInputsUI();
        }
 
        private System.Windows.Forms.ToolTip toolTip = new System.Windows.Forms.ToolTip();
        private void ShowToolTip(Control control, string message) => toolTip.SetToolTip(control, message);
 
        // 通过反射清除所有事件处理程序
        public void ClearAllEventHandlers()
        {
            // 获取事件字段
            FieldInfo field = typeof(Label).GetField("MouseHover", BindingFlags.NonPublic | BindingFlags.Instance);
 
            if (field != null)
            {
                // 将事件字段设为 null,移除所有事件处理程序
                field.SetValue(this, null);
            }
        }
 
        void UpdataInputsUI()
        {
            this.Invoke(() =>
            {
                #region 清空输入
                this.lblInputParam1.Visible = false;
                this.lblInputParam2.Visible = false;
                this.lblInputParam3.Visible = false;
                this.lblInputParam4.Visible = false;
                this.lblInputParam5.Visible = false;
                this.lblInputParam6.Visible = false;
                this.lblInputParam7.Visible = false;
                this.lblInputParam8.Visible = false;
                this.lblInputParam9.Visible = false;
                this.lblInputParam10.Visible = false;
                this.lblInputParam11.Visible = false;
                this.lblInputParam12.Visible = false;
                this.lblInputParam13.Visible = false;
                this.lblInputParam14.Visible = false;
                this.lblInputParam15.Visible = false;
                this.lblInputParam16.Visible = false;
                this.lblInputParam17.Visible = false;
                this.lblInputParam18.Visible = false;
 
                this.dtxtInputParam1.Visible = false;
                this.dtxtInputParam2.Visible = false;
                this.dtxtInputParam3.Visible = false;
                this.dtxtInputParam4.Visible = false;
                this.dtxtInputParam5.Visible = false;
                this.dtxtInputParam6.Visible = false;
                this.dtxtInputParam7.Visible = false;
                this.dtxtInputParam8.Visible = false;
                this.dtxtInputParam9.Visible = false;
                this.dtxtInputParam10.Visible = false;
                this.dtxtInputParam11.Visible = false;
                this.dtxtInputParam12.Visible = false;
                this.dtxtInputParam13.Visible = false;
                this.dtxtInputParam14.Visible = false;
                this.dtxtInputParam15.Visible = false;
                this.dtxtInputParam16.Visible = false;
                this.dtxtInputParam17.Visible = false;
                this.dtxtInputParam18.Visible = false;
                #endregion
 
                //ClearAllEventHandlers();
 
                //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI
                //, ScaleRMin, ScaleRMax
                //, ScaleCMin, ScaleCMax
                //, MinScore, NumMatches, MaxOverlap, SubPixel
                //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels)
                //, Greediness
                #region 输入参数
                //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI
                this.lblInputParam1.Text = "起始角度";
                this.dtxtInputParam1.Text = Subject.Params.Inputs["AngleStart"]?.ToString();
                this.lblInputParam1.Visible = true;
                this.dtxtInputParam1.Visible = true;
                this.dtxtInputParam1.Enabled = true;
                lblInputParam1.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的起始角度AngleStart");
 
                this.lblInputParam2.Text = "角度范围";
                this.dtxtInputParam2.Text = Subject.Params.Inputs["AngleExtent"]?.ToString();
                this.lblInputParam2.Visible = true;
                this.dtxtInputParam2.Visible = true;
                this.dtxtInputParam2.Enabled = true;
                lblInputParam2.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的角度范围AngleExtent,'0'表示无角度搜索");
 
                //, ScaleRMin, ScaleRMax
                this.lblInputParam3.Text = "纵向缩放最小值";
                this.dtxtInputParam3.Text = Subject.Params.Inputs["ScaleRMin"]?.ToString();
                this.lblInputParam3.Visible = true;
                this.dtxtInputParam3.Visible = true;
                this.dtxtInputParam3.Enabled = true;
                lblInputParam3.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最小值ScaleRMin");
 
                this.lblInputParam4.Text = "纵向缩放最大值";
                this.dtxtInputParam4.Text = Subject.Params.Inputs["ScaleRMax"]?.ToString();
                this.lblInputParam4.Visible = true;
                this.dtxtInputParam4.Visible = true;
                this.dtxtInputParam4.Enabled = true;
                lblInputParam4.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最大值ScaleRMax");
 
                //, ScaleCMin, ScaleCMax
                this.lblInputParam5.Text = "横向缩放最小值";
                this.dtxtInputParam5.Text = Subject.Params.Inputs["ScaleCMin"]?.ToString();
                this.lblInputParam5.Visible = true;
                this.dtxtInputParam5.Visible = true;
                this.dtxtInputParam5.Enabled = true;
                lblInputParam5.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最小值ScaleCMin");
 
                this.lblInputParam6.Text = "横向缩放最大值";
                this.dtxtInputParam6.Text = Subject.Params.Inputs["ScaleCMax"]?.ToString();
                this.lblInputParam6.Visible = true;
                this.dtxtInputParam6.Visible = true;
                this.dtxtInputParam6.Enabled = true;
                lblInputParam6.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最大值ScaleCMax");
 
                //, MinScore, NumMatches, MaxOverlap, ResultType
                this.lblInputParam7.Text = "最小分数";
                this.dtxtInputParam7.Text = Subject.Params.Inputs["MinScore"]?.ToString();
                this.lblInputParam7.Visible = true;
                this.dtxtInputParam7.Visible = true;
                this.dtxtInputParam7.Enabled = true;
                lblInputParam7.MouseHover += (s, e) => ShowToolTip((Control)s, "被找到的模板最小分数MinScore");
 
                this.lblInputParam8.Text = "期望个数";
                this.dtxtInputParam8.Text = Subject.Params.Inputs["NumMatches"]?.ToString();
                this.lblInputParam8.Visible = true;
                this.dtxtInputParam8.Visible = true;
                this.dtxtInputParam8.Enabled = true;
                lblInputParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "要找到的模板最多的实例数NumMatches,'0'则找到所有可能的匹配");
 
                this.lblInputParam9.Text = "最大重叠比例";
                this.dtxtInputParam9.Text = Subject.Params.Inputs["MaxOverlap"]?.ToString();
                this.lblInputParam9.Visible = true;
                this.dtxtInputParam9.Visible = true;
                this.dtxtInputParam9.Enabled = true;
                lblInputParam9.MouseHover += (s, e) => ShowToolTip((Control)s, "允许找到的模型实例的最大重叠比例MaxOverlap[较小的值会避免找到多个相似的匹配]");
 
                this.lblInputParam10.Text = "计算精度";
                this.dtxtInputParam10.Text = Subject.Params.Inputs["SubPixel"]?.ToString();
                this.lblInputParam10.Visible = true;
                this.dtxtInputParam10.Visible = true;
                this.dtxtInputParam10.Enabled = false;
                lblInputParam10.MouseHover += (s, e) => ShowToolTip((Control)s, "计算精度的设置SubPixel['none'最大误差为半个像素]");
 
                //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels)
                //, Greediness, , new HTuple(), new HTuple()
                this.lblInputParam11.Text = "搜索时金字塔的层级";
                this.dtxtInputParam11.Text = "0";
                this.lblInputParam11.Visible = true;
                this.dtxtInputParam11.Visible = true;
                this.dtxtInputParam11.Enabled = false;
                lblInputParam11.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时金字塔的层级");
 
                this.lblInputParam12.Text = "贪婪度";
                this.dtxtInputParam12.Text = Subject.Params.Inputs["Greediness"]?.ToString();
                this.lblInputParam12.Visible = true;
                this.dtxtInputParam12.Visible = true;
                this.dtxtInputParam12.Enabled = true;
                lblInputParam12.MouseHover += (s, e) => ShowToolTip((Control)s, "贪婪度,越高速度越快[一般都设为0.8,太大容易出现找不到的情况]");
 
                this.lblInputParam13.Text = "最小数量";
                this.dtxtInputParam13.Text = Subject.Params.Inputs["MinCount"]?.ToString();
                this.lblInputParam13.Visible = true;
                this.dtxtInputParam13.Visible = true;
                this.dtxtInputParam13.Enabled = true;
                lblInputParam13.MouseHover += (s, e) => ShowToolTip((Control)s, "匹配结果要求的最小数量");
 
                this.lblInputParam14.Text = "最大数量";
                this.dtxtInputParam14.Text = Subject.Params.Inputs["MaxCount"]?.ToString();
                this.lblInputParam14.Visible = true;
                this.dtxtInputParam14.Visible = true;
                this.dtxtInputParam14.Enabled = true;
                lblInputParam14.MouseHover += (s, e) => ShowToolTip((Control)s, "匹配结果要求的最大数量");
 
                this.lblInputParam15.Text = "模板滤波";
                this.dtxtInputParam15.Text = Subject.Params.Inputs["ModelMask"]?.ToString();
                this.lblInputParam15.Visible = true;
                this.dtxtInputParam15.Visible = true;
                this.dtxtInputParam15.Enabled = true;
                lblInputParam15.MouseHover += (s, e) => ShowToolTip((Control)s, "模板匹配前执行均值滤波");
                #endregion
 
                //UpdataInputs();
 
                //string NumLevels = "auto"
                //, double AngleStart = -5, double AngleExtent = 10, string AngleStep = "auto"
                //, double ScaleRMin = 0.9, double ScaleRMax = 1.1, string ScaleRStep = "auto"
                //, double ScaleCMin = 1.1, double ScaleCMax = 0.9, string ScaleCStep = "auto"
                //, string Optimization = "auto", string Metric = "ignore_local_polarity"
                //, string Contrast = "auto", int MinContrast = 10
            });
        }
 
        /// <summary>
        /// 更新运行参数
        /// </summary>
        public override void UpdataInputs()
        {
            List<HFindModelTool> hFindModelTools = new List<HFindModelTool>();
            foreach (var ModelToolControl in listModelToolControls)
                hFindModelTools.Add(ModelToolControl.HFindModelTool);
 
            ((HFindMultiModelTool)Subject).HFindModelTools = hFindModelTools;
 
            int iResult = 0;
            Subject.Params.Inputs["MinCount"] = int.TryParse(dtxtMinCount.Text, out iResult) ? iResult : Subject.Params.Inputs["MinCount"];
            Subject.Params.Inputs["MaxCount"] = int.TryParse(dtxtMaxCount.Text, out iResult) ? iResult : Subject.Params.Inputs["MinCount"];
 
            Subject.Params.Inputs["AngleStart"] = dtxtInputParam1.Text;
            Subject.Params.Inputs["AngleExtent"] = dtxtInputParam2.Text;
            Subject.Params.Inputs["ScaleRMin"] = dtxtInputParam3.Text;
            Subject.Params.Inputs["ScaleRMax"] = dtxtInputParam4.Text;
            Subject.Params.Inputs["ScaleCMin"] = dtxtInputParam5.Text;
            Subject.Params.Inputs["ScaleCMax"] = dtxtInputParam6.Text;
            Subject.Params.Inputs["MinScore"] = dtxtInputParam7.Text;
            Subject.Params.Inputs["NumMatches"] = dtxtInputParam8.Text;
            Subject.Params.Inputs["MaxOverlap"] = dtxtInputParam9.Text;
            Subject.Params.Inputs["SubPixel"] = dtxtInputParam10.Text;
            Subject.Params.Inputs["NumLevels"] = dtxtInputParam11.Text;
            Subject.Params.Inputs["Greediness"] = dtxtInputParam12.Text;
            Subject.Params.Inputs["MinCount"] = dtxtInputParam13.Text;
            Subject.Params.Inputs["MaxCount"] = dtxtInputParam14.Text;
            Subject.Params.Inputs["ModelMask"] = dtxtInputParam15.Text;
 
            Subject.Params.Outputs.Add("CenterX", new List<double>());
            Subject.Params.Outputs.Add("CenterY", new List<double>());
            Subject.Params.Outputs.Add("Angle", new List<double>());
            Subject.Params.Outputs.Add("Score", new List<double>());
            Subject.Params.Outputs.Add("Count", 0);
 
            if (cmbFixture.Text == "")
                Subject.Params.Fixture = new Fixture();
            else if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text))
                Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text];
 
            base.UpdataInputs();
        }
 
        /// <summary>
        /// 加载运行参数
        /// </summary>
        public override void LoadParas()
        {
            this.BeginInvoke(new Action(() =>
            {
                dtxtMinCount.Text = Subject.Params.Inputs["MinCount"]?.ToString();
                dtxtMaxCount.Text = Subject.Params.Inputs["MaxCount"]?.ToString();
 
                dtxtInputParam1.Text = Subject.Params.Inputs["AngleStart"]?.ToString();
                dtxtInputParam2.Text = Subject.Params.Inputs["AngleExtent"]?.ToString();
                dtxtInputParam3.Text = Subject.Params.Inputs["ScaleRMin"]?.ToString();
                dtxtInputParam4.Text = Subject.Params.Inputs["ScaleRMax"]?.ToString();
                dtxtInputParam5.Text = Subject.Params.Inputs["ScaleCMin"]?.ToString();
                dtxtInputParam6.Text = Subject.Params.Inputs["ScaleCMax"]?.ToString();
                dtxtInputParam7.Text = Subject.Params.Inputs["MinScore"]?.ToString();
                dtxtInputParam8.Text = Subject.Params.Inputs["NumMatches"]?.ToString();
                dtxtInputParam9.Text = Subject.Params.Inputs["MaxOverlap"]?.ToString();
                dtxtInputParam10.Text = Subject.Params.Inputs["SubPixel"]?.ToString();
                dtxtInputParam11.Text = Subject.Params.Inputs["NumLevels"]?.ToString();
                dtxtInputParam12.Text = Subject.Params.Inputs["Greediness"]?.ToString();
                dtxtInputParam13.Text = Subject.Params.Inputs["MinCount"]?.ToString();
                dtxtInputParam14.Text = Subject.Params.Inputs["MaxCount"]?.ToString();
                dtxtInputParam15.Text = Subject.Params.Inputs["ModelMask"]?.ToString();
 
                if (Subject.InputImage != null && Subject.InputImage is HObject)
                    inputImageHSmartWindowControl.ShowHoImage((HObject)Subject.InputImage);
 
                Type type = Subject.Params.ROI?.GetType();
                if (Subject.Params.ROI != null)
                {
                    switch (type)
                    {
                        case Type t when t == typeof(HRectangle2):
                            cmbTypeRoi.Text = RoiType.Rectangle2.ToString();
                            break;
                        case Type t when t == typeof(HCircle):
                            cmbTypeRoi.Text = RoiType.Circle.ToString();
                            break;
                        case Type t when t == typeof(HSegment):
                            cmbTypeRoi.Text = RoiType.Segment.ToString();
                            break;
                        default:
                            cmbTypeRoi.Text = RoiType.None.ToString();
                            break;
                    }
                    if (cmbTypeRoi.Text.ToString() != "None")
                        ckbDrawRoi.Checked = true;
                    else
                        ckbDrawRoi.Checked = false;
 
                    inputImageHSmartWindowControl.oRoi = Subject.Params.ROI;
                }
 
                if (Subject.Params.Fixture != null)
                    cmbFixture.Text = Subject.Params.Fixture.strName;
                else
                    cmbFixture.Text = "";
 
                switch (type)
                {
                    case Type t when t == typeof(HRectangle2):
                        inputImageHSmartWindowControl.oRoi
                             = new HRectangle2(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y
                             , Subject.Params.ROI.Phi + Subject.Params.Fixture.Phi, ((HRectangle2)Subject.Params.ROI).Width, ((HRectangle2)Subject.Params.ROI).Height);
                        break;
                    case Type t when t == typeof(HCircle):
                        inputImageHSmartWindowControl.oRoi
                        = new HCircle(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y
                        , ((HCircle)Subject.Params.ROI).Radius);
                        break;
                    case Type t when t == typeof(HSegment):
                        inputImageHSmartWindowControl.oRoi
                        = new HSegment(((HSegment)Subject.Params.ROI).StartX + Subject.Params.Fixture.X, ((HSegment)Subject.Params.ROI).StartY + Subject.Params.Fixture.Y
                        , ((HSegment)Subject.Params.ROI).EndX + Subject.Params.Fixture.X, ((HSegment)Subject.Params.ROI).EndY + Subject.Params.Fixture.Y);
                        break;
                    default:
                        inputImageHSmartWindowControl.oRoi = null;
                        break;
                }
 
                if (!string.IsNullOrEmpty(Subject.Params.Inputs["ModelsFullPath"]?.ToString()))
                    ShowModels((JArray.FromObject(Subject.Params.Inputs["ModelsFullPath"])).ToObject<List<string>>());
            }));
        }
 
        /// <summary>
        /// 更新输出结果
        /// </summary>
        public override void UpdataOutputs()
        {
            this.BeginInvoke(new Action(() =>
            {
                //存在反序列化错乱的情况需要使用自定义的转换器
                dtxtCenterX.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterX"]);
                dtxtCenterY.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterY"]);
                dtxtAngle.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Angle"]);
                dtxtScore.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Score"]);
                dtxtCount.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Count"]);
            }));
        }
 
        /// <summary>
        /// 点击运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnRun_Click(object sender, EventArgs e)
        {
            if (Subject.InputImage != null)
                InputImage = Subject.InputImage;
 
            DateTime StartTime = DateTime.Now;
            Run();
 
            //更新日志与结果
            this.BeginInvoke(new Action(() =>
            {
                if (Subject.Result)
                {
                    lblResult.BackColor = Color.Green;
                    lblResult.Text = "True";
                    recordImageHSmartWindowControl.SetColor("green");
                }
                else
                {
                    lblResult.BackColor = Color.Red;
                    lblResult.Text = "False";
                    recordImageHSmartWindowControl.SetColor("red");
                }
 
                lblMsg.Text = Msg.Length > 50 ? Msg.Substring(0, 50) : Msg;
                lblMsgToolTip.SetToolTip(BtmStatusStrip, Msg);
                lblRunTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
 
                UpdataOutputs();
                imgTabControl.SelectedTab = tabPageRecordImage;
 
                if (Subject.InputImage != null && Subject.InputImage is HObject)
                {
                    HOperatorSet.GetImageSize((HObject)Subject.InputImage, out HTuple ho_ImageWidth, out HTuple ho_ImageHeight);
                    recordImageHSmartWindowControl.ShowHoImage((HObject)Subject.InputImage);
                }
 
                //先判断子类再判断父类
                if (Subject.Record != null && Subject.Record is MsgRecord)
                {
                    MsgRecord MsgRecord = (MsgRecord)Subject.Record;
 
                    recordImageHSmartWindowControl.DispObj(MsgRecord.RecordObject_OK, true);
                    recordImageHSmartWindowControl.DispObj(MsgRecord.RecordObject_NG, false);
 
                    for (int i = 0; i < MsgRecord.Msg.Length; i++)
                        recordImageHSmartWindowControl.ShowMsg(MsgRecord.Msg[i], 1 == MsgRecord.Result[i] ? true : false
                            , MsgRecord.Column[i], MsgRecord.Row[i]);
                }
                else if (Subject.Record != null && Subject.Record is ObjectRecord record)
                {
                    recordImageHSmartWindowControl.DispObj(record.RecordObject_OK);
                    recordImageHSmartWindowControl.DispObj(record.RecordObject_NG);
                }
 
                GC.Collect();
            }));
        }
 
        public override void btnLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
 
            // 设置文件对话框的属性
            openFileDialog.Multiselect = false; // 不允许多选
            // 设置文件过滤器,支持多种文件类型
            openFileDialog.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|All Files (*.*)|*.*";
            // 显示文件对话框
            DialogResult result = openFileDialog.ShowDialog();
 
            // 处理对话框返回结果
            if (result == DialogResult.OK)
            {
                // 获取用户选择的文件名
                string[] selectedFiles = openFileDialog.FileNames;
                if (selectedFiles.Length > 0)
                {
                    HOperatorSet.ReadImage(out HObject ho_Image, selectedFiles[0]);
                    //判断是否为灰度图
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.CountChannels(ho_Image, out HTuple hv_Channels);
                        if (hv_Channels.TupleInt() != 1)
                        {
                            if (hv_Channels.TupleInt() == 4)
                            {
                                HOperatorSet.Decompose4(ho_Image, out HObject R, out HObject G, out HObject B, out _);
                                HOperatorSet.Compose3(R, G, B, out ho_Image);
                            }
                            HOperatorSet.Rgb1ToGray(ho_Image, out ho_Image);
                            //更新日志与结果
                            this.BeginInvoke(new Action(() =>
                            {
                                lblMsg.Text = "导入图片非灰度图,自动转换为灰度图";
                            }));
                        }
                        InputImage = ho_Image;
                        imgTabControl.SelectedTab = tabPageInputImage;
 
                        foreach (var ModelToolControl in listModelToolControls)
                            ModelToolControl.HFindModelTool.InputImage = ho_Image.CopyObj(1, -1);
 
                        inputImageHSmartWindowControl.oRoi = inputImageHSmartWindowControl.oRoi;
                    }
                }
            }
        }
 
        public override void btnSaveParas_Click(object sender, EventArgs e)
        {
            //保存前需要更新输入参数Inputs
            UpdataInputs();
 
            // 创建 SaveFileDialog 实例
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                // 设置对话框标题
                saveFileDialog.Title = "保存文件";
 
                // 设置默认路径
                saveFileDialog.InitialDirectory = Application.StartupPath;
 
                // 设置文件类型过滤器
                saveFileDialog.Filter = "文本文件 (*.json)|*.json|所有文件 (*.*)|*.*";
 
                // 设置默认文件名
                saveFileDialog.FileName = Subject.strProcessName + ".json";
 
                // 显示对话框并检查用户是否点击了保存按钮
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string fullPath = saveFileDialog.FileName;
                    Debug.WriteLine("选择的文件路径是: " + fullPath);
                    Subject.strProcessName = Path.GetFileNameWithoutExtension(fullPath);
                    ((HFindMultiModelTool)Subject).Save(Path.GetDirectoryName(fullPath));
                }
            }
        }
 
        public override void btnLoadParas_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
 
            // 设置文件对话框的属性
            openFileDialog.Multiselect = false; // 不允许多选
            // 设置文件过滤器,支持多种文件类型
            openFileDialog.Filter = "Ini Files (*.json)|*.json|All Files (*.*)|*.*";
            // 显示文件对话框
            DialogResult result = openFileDialog.ShowDialog();
 
            // 处理对话框返回结果
            if (result == DialogResult.OK)
            {
                // 获取用户选择的文件名
                string[] selectedFiles = openFileDialog.FileNames;
                if (selectedFiles.Length > 0)
                {
                    ((HFindMultiModelTool)Subject).Load(selectedFiles[0]);
                    LoadParas();
                }
            }
        }
 
        public override void ckbDrawRoi_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbDrawRoi.Checked)
            {
                inputImageHSmartWindowControl.bAollowDraw = true;
                imgTabControl.SelectedTab = tabPageInputImage;
            }
            else
            {
                inputImageHSmartWindowControl.bAollowDraw = false;
                Subject.Params.ROI = new ROI();
            }
        }
 
        public override void cmbTypeRoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (Enum.TryParse(cmbTypeRoi.Text.ToString(), out RoiType type))
                {
                    HTuple hv_imageWidth = 0;
                    HTuple hv_imageHeight = 0;
                    if (InputImage != null && InputImage is HObject)
                        HOperatorSet.GetImageSize((HObject)InputImage, out hv_imageWidth, out hv_imageHeight);
                    switch (type)
                    {
                        case RoiType.Rectangle2:
                            inputImageHSmartWindowControl.oRoi
                                = new HRectangle2(hv_imageWidth.TupleReal() / 2, hv_imageHeight.TupleReal() / 2, 0
                                , hv_imageWidth.TupleReal() / 4, hv_imageHeight.TupleReal() / 4);
                            break;
                        case RoiType.Circle:
                            inputImageHSmartWindowControl.oRoi
                                = new HCircle(hv_imageWidth.TupleReal() / 2, hv_imageHeight.TupleReal() / 2, hv_imageWidth.TupleReal() / 4);
                            break;
                        case RoiType.Segment:
                            inputImageHSmartWindowControl.oRoi
                                = new HSegment(0, 0, hv_imageWidth.TupleReal() / 4, hv_imageHeight.TupleReal() / 4);
                            break;
                        case RoiType.None:
                        default:
                            inputImageHSmartWindowControl.oRoi = null;
                            break;
                    }
                }
            }
            catch { }
        }
 
        public override void cmbFixture_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text))
                    Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text];
                else
                    Subject.Params.Fixture = new Fixture();
            }
            catch { }
        }
 
        public void ShowModels(List<string> ModelsFullPath)
        {
            try
            {
                if (ModelsFullPath == null)
                    return;
 
                splitContainer1.Panel1.Controls.Clear();
                listModelToolControls.Clear();
                foreach (string ModelFullPath in ModelsFullPath)
                {
                    int index = listModelToolControls.Count + 1;
                    string name = Path.GetFileNameWithoutExtension(ModelFullPath);
                    HMulitModelControl hMulitModelControl = new HMulitModelControl(index, name);
                    hMulitModelControl.HFindModelTool.Load(ModelFullPath);
 
                    if (InputImage != null && InputImage is HObject image && image.IsInitialized())
                        hMulitModelControl.HFindModelTool.InputImage = image.CopyObj(1, -1);
                    splitContainer1.Panel1.Controls.Add(hMulitModelControl);
                    hMulitModelControl.Dock = DockStyle.Top;
                    listModelToolControls.Add(hMulitModelControl);
 
                    //刷新显示
                    hMulitModelControl.UpdataModelUI();
                }
            }
            catch { }
        }
 
        private void btnAddModel_Click(object sender, EventArgs e)
        {
            int index = listModelToolControls.Count + 1;
            string name = "模板" + index.ToString();
            HMulitModelControl hMulitModelControl = new HMulitModelControl(index, name);
            splitContainer1.Panel1.Controls.Add(hMulitModelControl);
            hMulitModelControl.Dock = DockStyle.Top;
            listModelToolControls.Add(hMulitModelControl);
 
            //刷新显示
            if (InputImage != null && InputImage is HObject)
                hMulitModelControl.HFindModelTool.InputImage = ((HObject)InputImage).CopyObj(1, -1);
 
            ((HFindMultiModelTool)Subject).HFindModelTools.Add(hMulitModelControl.HFindModelTool);
        }
 
        private void btnDelModel_Click(object sender, EventArgs e)
        {
            int index = listModelToolControls.Count - 1;
            splitContainer1.Panel1.Controls.Remove(listModelToolControls[index]);
            listModelToolControls[index].Dispose();
            listModelToolControls.RemoveAt(index);
            ((HFindMultiModelTool)Subject).HFindModelTools.RemoveAt(index);
        }
 
        private void HFindMultiModelToolEdit_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                //刷新显示
                foreach (HMulitModelControl ModelToolControl in listModelToolControls)
                    ModelToolControl.UpdataModelUI();
            }
            catch { }
        }
    }
}