轮胎外观检测添加思谋语义分割模型检测工具
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
using HalconDotNet;
using MyControls;
using System.Data.Common;
using MyProcesses.Alogrithms;
using System.Windows.Forms;
using OpenCvSharp;
 
namespace MyProcesses.Alogrithms
{
    public partial class TestToolEdit : TAlgorithmEdit
    {
        public TestToolEdit(TestTool subject = null)
        {
            if (subject != null && subject is TestTool)
                Subject = subject;
            else
                Subject = new TestTool();
 
            this.Dock = DockStyle.Fill;
            InitializeComponent();
        }
 
        /// <summary>
        /// 控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestToolEdit_Load(object sender, EventArgs e)
        {
            pnlInputImage.Controls.Add(inputImageHSmartWindowControl);
            inputImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            pnlRecordImage.Controls.Add(recordImageHSmartWindowControl);
            recordImageHSmartWindowControl.Dock = DockStyle.Fill;
 
            //遍历可以选择的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());
 
            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;
 
            Subject.Params.Inputs["输入"] = stxtInput.Text;
 
            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(() =>
            {
                stxtInput.Text = Subject.Params.Inputs["输入"].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 = "";
 
                base.LoadParas();
            }));
        }
 
        /// <summary>
        /// 更新输出结果(需要修改)
        /// </summary>
        public override void UpdataOutputs()
        {
            this.BeginInvoke(new Action(() =>
            {
                //存在反序列化错乱的情况需要使用自定义的转换器
                stxtOutput.Text = Subject.Params.Outputs["输出"]?.ToString();
 
            }));
        }
 
        /// <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.OutputImage != null)
                {
                    if (Subject.OutputImage is Mat)
                    {
                        TAlgorithm.Mat2HObject((Mat)Subject.OutputImage, out HObject image);
                        recordImageHSmartWindowControl.ShowHoImage(image);
                    }
                    else if (Subject.OutputImage is Bitmap)
                    {
                        TAlgorithm.Bitmap2HObject((Bitmap)Subject.OutputImage, out HObject image);
                        recordImageHSmartWindowControl.ShowHoImage(image);
                    }
                    else if (Subject.OutputImage is HObject)
                        recordImageHSmartWindowControl.ShowHoImage((HObject)Subject.OutputImage);
                }
 
                //先判断子类再判断父类
                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)
                {
                    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)
                        {
                            HOperatorSet.Rgb1ToGray(ho_Image, out ho_Image);
                            //更新日志与结果
                            this.BeginInvoke(new Action(() =>
                            {
                                lblMsg.Text = "导入图片非灰度图,自动转换为灰度图";
                            }));
                        }
                        InputImage = ho_Image;
                        imgTabControl.SelectedTab = tabPageInputImage;
                        inputImageHSmartWindowControl.oRoi = inputImageHSmartWindowControl.oRoi;
                    }
                }
            }
        }
 
        public override void btnSaveParas_Click(object sender, EventArgs e) { base.btnSaveParas_Click(sender, e); }
 
        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 { }
        }
    }
}