C3204
2025-12-29 fec341de45f4b3fd1825807f0b3261143fa13caa
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
using HalconDotNet;
using LB_VisionControl;
using OpenCvSharp;
using System.Diagnostics;
using System.Reflection;
using static LB_VisionProcesses.Alogrithms.OpenCvSharp.FindModelTool;
 
namespace LB_VisionProcesses.Alogrithms.OpenCvSharp
{
    public partial class FindModelToolEdit : TAlgorithmEdit
    {
        public FindModelToolEdit(FindModelTool subject = null)
        {
            if (subject != null && subject is FindModelTool)
                Subject = subject;
            else
                Subject = new FindModelTool();
 
            this.Dock = DockStyle.Fill;
            InitializeComponent();
        }
 
        public UserHSmartWindowControl modelImageHSmartWindowControl = new UserHSmartWindowControl();
        public UserHSmartWindowControl createModelImageHSmartWindowControl = new UserHSmartWindowControl();
 
        /// <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;
 
            pnlCreateModelImage.Controls.Add(createModelImageHSmartWindowControl);
            createModelImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            grpModel.Controls.Add(modelImageHSmartWindowControl);
            modelImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            //cmbModelType.SelectedIndex = cmbModelType.FindString("形状");
            UpdataInputsUI();
 
            // 设置显示样式
            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());
 
            //遍历可以选择的Model类型枚举
            foreach (var value in Enum.GetValues(typeof(ModelType)))
                cmbModelType.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";
        }
 
        /// <summary>
        /// 更新运行参数
        /// </summary>
        public override void UpdataInputs()
        {
            //设置运行参数
            double dResult = 0;
            int iResult = 0;
            cmbModelType.Text = ((FindModelTool)Subject).ModelID.Type.ToString();
            if (Enum.TryParse(cmbModelType.SelectedItem.ToString(), out ModelType modelType))
            {
                switch (modelType)
                {
                    case ModelType.灰度匹配:
                        //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI
                        //, ScaleRMin, ScaleRMax
                        //, ScaleCMin, ScaleCMax
                        //, MinScore, NumMatches, MaxOverlap
                        //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels)
                        //, Greediness, ResultType, new HTuple(), new HTuple()
                        //, out hv_Score, out hv_CenterRow, out hv_CenterColumn);
                        #region 输入参数
                        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["ResultType"] = 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;
                        #endregion
                        break;
                    default:
                        //, 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 输入参数
                        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;
                        #endregion
                        break;
                }
 
                //if (cmbFixture.Text == "")
                //    Subject.Params.Fixture = new Fixture();
                //else if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text))
                //    Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text];
 
                Type type = inputImageHSmartWindowControl.oRoi?.GetType();
                switch (type)
                {
                    case Type t when t == typeof(HRectangle2):
                        HRectangle2 hRectangle2 = (HRectangle2)inputImageHSmartWindowControl.oRoi;
                        Subject.Params.ROI
                            = new HRectangle2(hRectangle2.X - Subject.Params.Fixture.X, hRectangle2.Y - Subject.Params.Fixture.Y
                            , hRectangle2.Phi - Subject.Params.Fixture.Phi, hRectangle2.Width, hRectangle2.Height);
                        break;
                    case Type t when t == typeof(HCircle):
                        HCircle hCircle = (HCircle)inputImageHSmartWindowControl.oRoi;
                        Subject.Params.ROI
                            = new HCircle(hCircle.X - Subject.Params.Fixture.X, hCircle.Y - Subject.Params.Fixture.Y, hCircle.Radius);
                        break;
                    case Type t when t == typeof(HSegment):
                        HSegment hSegment = (HSegment)inputImageHSmartWindowControl.oRoi;
                        Subject.Params.ROI
                            = new HSegment(hSegment.StartX - Subject.Params.Fixture.X, hSegment.StartY - Subject.Params.Fixture.Y
                            , hSegment.EndX - Subject.Params.Fixture.X, hSegment.EndY - Subject.Params.Fixture.Y);
                        break;
                    default:
                        Subject.Params.ROI = new ROI();
                        break;
                }
 
            }
        }
 
        /// <summary>
        /// 加载运行参数
        /// </summary>
        public override void LoadParas()
        {
            this.BeginInvoke(new Action(() =>
            {
                cmbModelType.Text = ((FindModelTool)Subject).ModelID.Type.ToString();
                if (Enum.TryParse(cmbModelType.Text, out ModelType modelType))
                {
                    switch (modelType)
                    {
                        case ModelType.灰度匹配:
                            //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI
                            //, ScaleRMin, ScaleRMax
                            //, ScaleCMin, ScaleCMax
                            //, MinScore, NumMatches, MaxOverlap
                            //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels)
                            //, Greediness, ResultType, new HTuple(), new HTuple()
                            #region 输入参数
                            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["ResultType"].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();
                            #endregion
                            break;
                        default:
                            #region 输入参数
                            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["ResultType"].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();
                            #endregion
                            break;
                    }
                }
 
                if (Subject.InputImage != null && Subject.InputImage is Mat)
                {
                    using (HImage image = TAlgorithm.Mat2HImage((Mat)Subject.InputImage))
                    {
                        inputImageHSmartWindowControl.ShowHoImage(image);
                        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;
                        }
 
                        ShowModel(((FindModelTool)Subject).ModelID);
 
                    }
                }
            }));
        }
 
        /// <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"]);
                dtxtPhi.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Phi"]);
                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 Mat)
                {
                    using (HImage image = TAlgorithm.Mat2HImage((Mat)Subject.InputImage))
                    {
                        image.GetImageSize(out HTuple ho_ImageWidth, out HTuple ho_ImageHeight);
                        recordImageHSmartWindowControl.ShowHoImage(image);
                        //先判断子类再判断父类
                        if (Subject.Record != null && Subject.Record is MsgRecord msgRecord)
                        {
                            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 objRecord)
                        {
                            recordImageHSmartWindowControl.DispObj(objRecord.RecordObject_OK, true);
                            recordImageHSmartWindowControl.DispObj(objRecord.RecordObject_NG, false);
                        }
 
                    }
                }
 
                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)
                {
                    Mat mat = Cv2.ImRead(selectedFiles[0]);
                    //判断是否为灰度图
                    if (mat.Channels() != 1)
                    {
                        Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY);
                        //更新日志与结果
                        this.BeginInvoke(new Action(() =>
                        {
                            lblMsg.Text = "导入图片非灰度图,自动转换为灰度图";
                        }));
                    }
                    InputImage = mat;
                    imgTabControl.SelectedTab = tabPageInputImage;
                    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 = System.Windows.Forms.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);
                    ((FindModelTool)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)
                {
                    ((FindModelTool)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 Mat)
                    {
                        using (HImage image = TAlgorithm.Mat2HImage((Mat)InputImage))
                        {
                            HOperatorSet.GetImageSize(image, 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 { }
        }
 
        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);
            }
        }
 
        private void cmbModelType_TextUpdate(object sender, EventArgs e)
        {
            cmbModelType_SelectedIndexChanged(sender, e);
        }
 
        private void cmbModelType_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdataInputsUI();
        }
 
        void UpdataInputsUI()
        {
            this.BeginInvoke(() =>
            {
                #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
 
                #region 清空模板
                this.lblModelParam1.Visible = false;
                this.lblModelParam2.Visible = false;
                this.lblModelParam3.Visible = false;
                this.lblModelParam4.Visible = false;
                this.lblModelParam5.Visible = false;
                this.lblModelParam6.Visible = false;
                this.lblModelParam7.Visible = false;
                this.lblModelParam8.Visible = false;
                this.lblModelParam9.Visible = false;
                this.lblModelParam10.Visible = false;
                this.lblModelParam11.Visible = false;
                this.lblModelParam12.Visible = false;
                this.lblModelParam13.Visible = false;
                this.lblModelParam14.Visible = false;
                this.lblModelParam15.Visible = false;
                this.lblModelParam16.Visible = false;
                this.lblModelParam17.Visible = false;
                this.lblModelParam18.Visible = false;
 
                this.dtxtModelParam1.Visible = false;
                this.dtxtModelParam2.Visible = false;
                this.dtxtModelParam3.Visible = false;
                this.dtxtModelParam4.Visible = false;
                this.dtxtModelParam5.Visible = false;
                this.dtxtModelParam6.Visible = false;
                this.dtxtModelParam7.Visible = false;
                this.dtxtModelParam8.Visible = false;
                this.dtxtModelParam9.Visible = false;
                this.dtxtModelParam10.Visible = false;
                this.dtxtModelParam11.Visible = false;
                this.dtxtModelParam12.Visible = false;
                this.dtxtModelParam13.Visible = false;
                this.dtxtModelParam14.Visible = false;
                this.dtxtModelParam15.Visible = false;
                this.dtxtModelParam16.Visible = false;
                this.dtxtModelParam17.Visible = false;
                this.dtxtModelParam18.Visible = false;
                #endregion
 
                ClearAllEventHandlers();
 
                if (Enum.TryParse(cmbModelType.Text, out ModelType modelType))
                {
                    switch (modelType)
                    {
                        case ModelType.灰度匹配:
                            //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI
                            //, ScaleRMin, ScaleRMax
                            //, ScaleCMin, ScaleCMax
                            //, MinScore, NumMatches, MaxOverlap
                            //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels)
                            //, Greediness, ResultType, new HTuple(), new HTuple()
 
                            #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 = "ResultType";
                            this.dtxtInputParam10.Text = Subject.Params.Inputs["ResultType"].ToString();
                            this.lblInputParam10.Visible = true;
                            this.dtxtInputParam10.Visible = true;
                            this.dtxtInputParam10.Enabled = false;
                            lblInputParam10.MouseHover += (s, e) => ShowToolTip((Control)s, "计算精度的设置ResultType['1'得到亚像素精度]");
 
                            //, 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 = "2";
                            this.lblInputParam12.Visible = true;
                            this.dtxtInputParam12.Visible = true;
                            this.dtxtInputParam12.Enabled = true;
                            lblInputParam12.MouseHover += (s, e) => ShowToolTip((Control)s, "贪婪度,越小速度越快[一般都设为2,太小容易出现找不到的情况]");
 
                            this.lblInputParam13.Text = "最小数量";
                            this.dtxtInputParam13.Text = "0";
                            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 = "9999";
                            this.lblInputParam14.Visible = true;
                            this.dtxtInputParam14.Visible = true;
                            this.dtxtInputParam14.Enabled = true;
                            lblInputParam14.MouseHover += (s, e) => ShowToolTip((Control)s, "匹配结果要求的最大数量");
                            #endregion
                            //UpdataInputs();
 
                            //NumLevels == "auto" ? NumLevels : Convert.ToInt16(NumLevels)
                            //   , AngleStart, AngleExtent, AngleStep == "auto" ? AngleStep : Convert.ToDouble(AngleStep)
                            //   , ScaleRMin, ScaleRMax, ScaleRStep == "auto" ? ScaleRStep : Convert.ToDouble(ScaleRStep)
                            //   , ScaleCMin, ScaleCMax, ScaleCStep == "auto" ? ScaleCStep : Convert.ToDouble(ScaleCStep)
                            //   , Optimization, Metric
                            //   , Contrast, MinContrast
 
                            #region 模板参数
                            //NumLevels == "auto" ? NumLevels : Convert.ToInt16(NumLevels)
                            this.lblModelParam1.Text = "金字塔的层级";
                            this.dtxtModelParam1.Text = "auto";
                            this.lblModelParam1.Visible = true;
                            this.dtxtModelParam1.Visible = true;
                            this.dtxtModelParam1.Enabled = false;
                            lblModelParam1.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时金字塔的层级NumLevels,'0'表示不使用金字塔");
 
                            //   , AngleStart, AngleExtent, AngleStep == "auto" ? AngleStep : Convert.ToDouble(AngleStep)
                            this.lblModelParam2.Text = "角度步长";
                            this.dtxtModelParam2.Text = "auto";
                            this.lblModelParam2.Visible = true;
                            this.dtxtModelParam2.Visible = true;
                            this.dtxtModelParam2.Enabled = false;
                            lblModelParam2.MouseHover += (s, e) => ShowToolTip((Control)s, "角度范围内进行旋转的步长AngleStep,'auto'表示由系统自动选择合适的步长");
 
                            this.lblModelParam3.Text = "起始角度";
                            this.dtxtModelParam3.Text = "-5";
                            this.lblModelParam3.Visible = true;
                            this.dtxtModelParam3.Visible = true;
                            this.dtxtModelParam3.Enabled = true;
                            lblModelParam3.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的起始角度AngleStart");
 
                            this.lblModelParam4.Text = "角度范围";
                            this.dtxtModelParam4.Text = "10";
                            this.lblModelParam4.Visible = true;
                            this.dtxtModelParam4.Visible = true;
                            this.dtxtModelParam4.Enabled = true;
                            lblModelParam4.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的角度范围AngleExtent,'0'表示无角度搜索");
 
                            //, double ScaleRMin = 0.9, double ScaleRMax = 1.1, string ScaleRStep = "auto"
                            this.lblModelParam5.Text = "纵向缩放最小值";
                            this.dtxtModelParam5.Text = "0.9";
                            this.lblModelParam5.Visible = true;
                            this.dtxtModelParam5.Visible = true;
                            this.dtxtModelParam5.Enabled = true;
                            lblModelParam5.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最小值ScaleRMin");
 
                            this.lblModelParam6.Text = "纵向缩放最大值";
                            this.dtxtModelParam6.Text = "1.1";
                            this.lblModelParam6.Visible = true;
                            this.dtxtModelParam6.Visible = true;
                            this.dtxtModelParam6.Enabled = true;
                            lblModelParam6.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最大值ScaleRMax");
 
                            //, double ScaleCMin = 1.1, double ScaleCMax = 0.9, string ScaleCStep = "auto"
                            this.lblModelParam7.Text = "横向缩放最小值";
                            this.dtxtModelParam7.Text = "0.9";
                            this.lblModelParam7.Visible = true;
                            this.dtxtModelParam7.Visible = true;
                            this.dtxtModelParam7.Enabled = true;
                            lblModelParam7.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最小值ScaleCMin");
 
                            this.lblModelParam8.Text = "横向缩放最大值";
                            this.dtxtModelParam8.Text = "1.1";
                            this.lblModelParam8.Visible = true;
                            this.dtxtModelParam8.Visible = true;
                            this.dtxtModelParam8.Enabled = true;
                            lblModelParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最大值ScaleCMax");
 
                            //, string Optimization = "auto", string Metric = "ignore_local_polarity"
                            this.lblModelParam9.Text = "优化";
                            this.dtxtModelParam9.Text = "none";
                            this.lblModelParam9.Visible = true;
                            this.dtxtModelParam9.Visible = true;
                            this.dtxtModelParam9.Enabled = false;
                            lblModelParam9.MouseHover += (s, e) => ShowToolTip((Control)s, "影响形状模型创建时的速度和精度的优化选项Optimization");
 
                            this.lblModelParam10.Text = "极性";
                            this.dtxtModelParam10.Text = "ignore_local_polarity";
                            this.lblModelParam10.Visible = true;
                            this.dtxtModelParam10.Visible = true;
                            this.dtxtModelParam10.Enabled = false;
                            lblModelParam10.MouseHover += (s, e) => ShowToolTip((Control)s, "极性信息的度量方法Metric");
 
                            //, string Contrast = "auto", int MinContrast = 10
                            this.lblModelParam11.Text = "对比度参数";
                            this.dtxtModelParam11.Text = "auto";
                            this.lblModelParam11.Visible = true;
                            this.dtxtModelParam11.Visible = true;
                            this.dtxtModelParam11.Enabled = false;
                            lblModelParam11.MouseHover += (s, e) => ShowToolTip((Control)s, "创建模板时的对比度Contrast");
 
                            this.lblModelParam12.Text = "最小对比度参数";
                            this.dtxtModelParam12.Text = "10";
                            this.lblModelParam12.Visible = true;
                            this.dtxtModelParam12.Visible = true;
                            this.dtxtModelParam12.Enabled = true;
                            lblModelParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "模型匹配过程中考虑的最小对比度MinContrast");
                            #endregion
                            break;
                        default:
                            //, 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 = "SubPixel";
                            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 = "2";
                            this.lblInputParam12.Visible = true;
                            this.dtxtInputParam12.Visible = true;
                            this.dtxtInputParam12.Enabled = true;
                            lblInputParam12.MouseHover += (s, e) => ShowToolTip((Control)s, "贪婪度,越小速度越快[一般都设为2,太小容易出现找不到的情况]");
 
                            this.lblInputParam13.Text = "最小数量";
                            this.dtxtInputParam13.Text = "0";
                            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 = "9999";
                            this.lblInputParam14.Visible = true;
                            this.dtxtInputParam14.Visible = true;
                            this.dtxtInputParam14.Enabled = true;
                            lblInputParam14.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
 
                            #region 模板参数
                            //NumLevels == "auto" ? NumLevels : Convert.ToInt16(NumLevels)
                            this.lblModelParam1.Text = "金字塔的层级";
                            this.dtxtModelParam1.Text = "auto";
                            this.lblModelParam1.Visible = true;
                            this.dtxtModelParam1.Visible = true;
                            this.dtxtModelParam1.Enabled = false;
                            lblModelParam1.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时金字塔的层级NumLevels,'0'表示不使用金字塔");
 
                            //   , AngleStart, AngleExtent, AngleStep == "auto" ? AngleStep : Convert.ToDouble(AngleStep)
                            this.lblModelParam2.Text = "角度步长";
                            this.dtxtModelParam2.Text = "auto";
                            this.lblModelParam2.Visible = true;
                            this.dtxtModelParam2.Visible = true;
                            this.dtxtModelParam2.Enabled = false;
                            lblModelParam2.MouseHover += (s, e) => ShowToolTip((Control)s, "角度范围内进行旋转的步长AngleStep,'auto'表示由系统自动选择合适的步长");
 
                            this.lblModelParam3.Text = "起始角度";
                            this.dtxtModelParam3.Text = "-5";
                            this.lblModelParam3.Visible = true;
                            this.dtxtModelParam3.Visible = true;
                            this.dtxtModelParam3.Enabled = true;
                            lblModelParam3.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的起始角度AngleStart");
 
                            this.lblModelParam4.Text = "角度范围";
                            this.dtxtModelParam4.Text = "10";
                            this.lblModelParam4.Visible = true;
                            this.dtxtModelParam4.Visible = true;
                            this.dtxtModelParam4.Enabled = true;
                            lblModelParam4.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的角度范围AngleExtent,'0'表示无角度搜索");
 
                            //, double ScaleRMin = 0.9, double ScaleRMax = 1.1, string ScaleRStep = "auto"
                            this.lblModelParam5.Text = "纵向缩放最小值";
                            this.dtxtModelParam5.Text = "0.9";
                            this.lblModelParam5.Visible = true;
                            this.dtxtModelParam5.Visible = true;
                            this.dtxtModelParam5.Enabled = true;
                            lblModelParam5.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最小值ScaleRMin");
 
                            this.lblModelParam6.Text = "纵向缩放最大值";
                            this.dtxtModelParam6.Text = "1.1";
                            this.lblModelParam6.Visible = true;
                            this.dtxtModelParam6.Visible = true;
                            this.dtxtModelParam6.Enabled = true;
                            lblModelParam6.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最大值ScaleRMax");
 
                            //, double ScaleCMin = 1.1, double ScaleCMax = 0.9, string ScaleCStep = "auto"
                            this.lblModelParam7.Text = "横向缩放最小值";
                            this.dtxtModelParam7.Text = "0.9";
                            this.lblModelParam7.Visible = true;
                            this.dtxtModelParam7.Visible = true;
                            this.dtxtModelParam7.Enabled = true;
                            lblModelParam7.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最小值ScaleCMin");
 
                            this.lblModelParam8.Text = "横向缩放最大值";
                            this.dtxtModelParam8.Text = "1.1";
                            this.lblModelParam8.Visible = true;
                            this.dtxtModelParam8.Visible = true;
                            this.dtxtModelParam8.Enabled = true;
                            lblModelParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最大值ScaleCMax");
 
                            //, string Optimization = "auto", string Metric = "ignore_local_polarity"
                            this.lblModelParam9.Text = "优化";
                            this.dtxtModelParam9.Text = "auto";
                            this.lblModelParam9.Visible = true;
                            this.dtxtModelParam9.Visible = true;
                            this.dtxtModelParam9.Enabled = false;
                            lblModelParam9.MouseHover += (s, e) => ShowToolTip((Control)s, "影响形状模型创建时的速度和精度的优化选项Optimization");
 
                            this.lblModelParam10.Text = "极性";
                            this.dtxtModelParam10.Text = "ignore_local_polarity";
                            this.lblModelParam10.Visible = true;
                            this.dtxtModelParam10.Visible = true;
                            this.dtxtModelParam10.Enabled = false;
                            lblModelParam10.MouseHover += (s, e) => ShowToolTip((Control)s, "极性信息的度量方法Metric");
 
                            //, string Contrast = "auto", int MinContrast = 10
                            this.lblModelParam11.Text = "对比度参数";
                            this.dtxtModelParam11.Text = "auto";
                            this.lblModelParam11.Visible = true;
                            this.dtxtModelParam11.Visible = true;
                            this.dtxtModelParam11.Enabled = false;
                            lblModelParam11.MouseHover += (s, e) => ShowToolTip((Control)s, "创建模板时的对比度Contrast");
 
                            this.lblModelParam12.Text = "最小对比度参数";
                            this.dtxtModelParam12.Text = "10";
                            this.lblModelParam12.Visible = true;
                            this.dtxtModelParam12.Visible = true;
                            this.dtxtModelParam12.Enabled = true;
                            lblModelParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "模型匹配过程中考虑的最小对比度MinContrast");
                            #endregion
                            break;
                    }
                }
 
            });
        }
 
        private void btnCreateModel_Click(object sender, EventArgs e)
        {
            if (InputImage != null && InputImage is Mat)
            {
                imgTabControl.SelectedTab = tabPageModelImage;
                using (HImage hoDomainImage = TAlgorithm.Mat2HImage((Mat)InputImage))
                {
                    HOperatorSet.GetImageSize(hoDomainImage, out HTuple hv_imageWidth, out HTuple hv_imageHeight);
                    createModelImageHSmartWindowControl.ShowHoImage(hoDomainImage);
                    createModelImageHSmartWindowControl.bAollowDraw = true;
                    createModelImageHSmartWindowControl.oRoi = new HRectangle2(hv_imageWidth.TupleReal() / 2, hv_imageHeight.TupleReal() / 2
                        , 0, hv_imageWidth.TupleReal() / 4, hv_imageHeight.TupleReal() / 4);
                }
            }
        }
 
        private void btnSaveModel_Click(object sender, EventArgs e)
        {
            try
            {
                if (InputImage != null && InputImage is Mat)
                {
                    double result = 0;
                    if (Enum.TryParse(cmbModelType.Text, out ModelType modelType))
                    {
                        string NumLevels = this.dtxtModelParam1.Text;
                        string AngleStep = this.dtxtModelParam2.Text;
 
                        double AngleStart = Convert.ToDouble(this.dtxtModelParam3.Text);
                        double AngleExtent = Convert.ToDouble(this.dtxtModelParam4.Text);
                        double ScaleRMin = Convert.ToDouble(this.dtxtModelParam5.Text);
                        double ScaleRMax = Convert.ToDouble(this.dtxtModelParam6.Text);
                        double ScaleCMin = Convert.ToDouble(this.dtxtModelParam7.Text);
                        double ScaleCMax = Convert.ToDouble(this.dtxtModelParam8.Text);
 
                        string Optimization = this.dtxtModelParam9.Text;
                        string Metric = this.dtxtModelParam10.Text;
                        string Contrast = this.dtxtModelParam11.Text;
                        int MinContrast = Convert.ToInt16(this.dtxtModelParam12.Text);
                        HRectangle2 ROI = (HRectangle2)createModelImageHSmartWindowControl.oRoi;
                        using (HRegion hRectangle = new HRegion())
                        {
                            hRectangle.GenRectangle2(ROI.Row, ROI.Column, ROI.Phi, ROI.SemiLength1, ROI.SemiLength2);
                            using (HImage hoDomainImage = TAlgorithm.Mat2HImage((Mat)InputImage))
                            {
                                HTuple hv_Channels = new HTuple();
                                //判断是否为灰度图
                                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                                {
                                    try
                                    {
                                        //hv_Channels = hoDomainImage.CountChannels();
                                        //if (hv_Channels.TupleInt() != 1)
                                        //    HOperatorSet.Rgb1ToGray(hoDomainImage, out hoDomainImage);
 
                                        ////转换后再次检查是否为灰度图
                                        //HOperatorSet.CountChannels(hoDomainImage, out hv_Channels);
                                        //if (hv_Channels.TupleInt() != 1)
                                        //    HOperatorSet.Rgb1ToGray(hoDomainImage, out hoDomainImage);
                                    }
                                    catch { }
                                }
                                HObject hoImageReduced = hoDomainImage.Rgb1ToGray().ReduceDomain(hRectangle);
                                HOperatorSet.CropDomain(hoImageReduced, out hoImageReduced);
                                TAlgorithm.HObject2Mat(hoImageReduced, out Mat hoModelImage);
                                bool bCreateModel = false;
 
                                switch (modelType)
                                {
                                    case ModelType.灰度匹配:
                                        bCreateModel = ((FindModelTool)Subject).CreateModel
                                            (hoModelImage, modelType, NumLevels
                                            , AngleStart, AngleExtent, AngleStep
                                            , ScaleRMin, ScaleRMax, "auto"
                                            , ScaleCMin, ScaleCMax, "auto"
                                            , Optimization, Metric, Contrast, MinContrast);
                                        break;
                                    default:
                                        bCreateModel = ((FindModelTool)Subject).CreateModel
                                            (hoModelImage, modelType, NumLevels
                                            , AngleStart, AngleExtent, AngleStep
                                            , ScaleRMin, ScaleRMax, "auto"
                                            , ScaleCMin, ScaleCMax, "auto"
                                            , Optimization, Metric, Contrast, MinContrast);
                                        break;
                                }
 
                                if (bCreateModel)
                                {
                                    using (HObject ho_ModelContours = new HObject())
                                    {
                                        //switch (((FindModelTool)Subject).ModelID.Type)
                                        //{
                                        //    case ModelType.局部变形模板:
                                        //        HOperatorSet.GetDeformableModelContours(out ho_ModelContours, ((HFindModelTool)Subject).ModelID.hvModel, 1);
                                        //        break;
                                        //    case ModelType.各向异形模板:
                                        //    default:
                                        //        HOperatorSet.GetShapeModelContours(out ho_ModelContours, ((HFindModelTool)Subject).ModelID.hvModel, 1);
                                        //        break;
                                        //}
                                        //HOperatorSet.GetImageSize(((HFindModelTool)Subject).ModelID.hoImage, out HTuple hv_Width, out HTuple hv_Height);
                                        //HOperatorSet.VectorAngleToRigid(-hv_Height / 2, -hv_Width / 2, 0, 0, 0, 0, out HTuple hv_HomMat2D);
                                        //HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ModelContours, hv_HomMat2D);
 
                                        //建模成功导航到子页
                                        parasTabControl.SelectedTab = tabPageRunParas;
                                        //modelImageHSmartWindowControl.ShowHoImage(((HFindModelTool)Subject).ModelID.hoImage);
                                        using (HImage modelImage = TAlgorithm.Mat2HImage(((FindModelTool)Subject).ModelID.hoImage))
                                        {
                                            modelImageHSmartWindowControl.ShowHoImage(modelImage);
                                            modelImageHSmartWindowControl.DispObj(ho_ModelContours, false);
                                            modelImageHSmartWindowControl.ShowMsg("创建模板成功", true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
 
        public void ShowModel(OModel ModelID)
        {
            try
            {
                ((FindModelTool)Subject).ModelID = ModelID;
 
                if (ModelID != null && ModelID.Width > 0)
                {
                    //HOperatorSet.GenEmptyObj(out HObject ho_ModelContours);
                    using (HRegion ho_ModelContours = new HRegion())
                    {
                        //switch (ModelID.Type)
                        //{
                        //    case ModelType.各向异形模板:
                        //        HOperatorSet.GetDeformableModelContours(out ho_ModelContours, ModelID.hvModel, 1);
                        //        break;
                        //    default:
                        //        HOperatorSet.GetShapeModelContours(out ho_ModelContours, ModelID.hvModel, 1);
                        //        break;
                        //}
 
                        //HOperatorSet.GetImageSize(ModelID.hoImage, out HTuple hv_Width, out HTuple hv_Height);
                        //HOperatorSet.VectorAngleToRigid(-hv_Height / 2, -hv_Width / 2, 0, 0, 0, 0, out HTuple hv_HomMat2D);
                        //HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ModelContours, hv_HomMat2D);
 
                        modelImageHSmartWindowControl.ClearObj();
                        using (HImage hoImage = TAlgorithm.Mat2HImage(ModelID.hoImage))
                        {
                            //modelImageHSmartWindowControl.ShowHoImage(ModelID.hoImage);
                            modelImageHSmartWindowControl.ShowHoImage(hoImage);
                            modelImageHSmartWindowControl.DispObj(ho_ModelContours, false);
                            modelImageHSmartWindowControl.ShowMsg("创建模板成功", true);
                        }
                    }
                }
            }
            catch { }
        }
 
        private void HFindModelToolEdit_SizeChanged(object sender, EventArgs e)
        {
            ShowModel(((FindModelTool)Subject).ModelID);
        }
    }
}