轮胎外观检测添加思谋语义分割模型检测工具
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
using HalconDotNet;
using LB_VisionControls;
using Newtonsoft.Json.Linq;
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 OpenCvSharp;
using System.Diagnostics;
 
namespace LB_VisionProcesses.Alogrithms
{
    public partial class TAlgorithmEdit : UserControl
    {
        public IProcess Subject;
        public UserHSmartWindowControl inputImageHSmartWindowControl = new UserHSmartWindowControl();
        public UserHSmartWindowControl recordImageHSmartWindowControl = new UserHSmartWindowControl();
 
        public ToolTip lblMsgToolTip = new ToolTip();
 
        public bool Result
        {
            get
            {
                if (Subject == null)
                {
                    return false;
                }
                return Subject.Result;
            }
            set
            {
                if (Subject != null)
                {
                    Subject.Result = value;
                }
            }
        }
 
        public string Msg
        {
            get
            {
                if (Subject == null)
                {
                    return "算法为空";
                }
                return Subject.Msg.Trim();
            }
        }
 
        public Object OutputImage
        {
            get
            {
                if (Subject == null)
                {
                    return null;
                }
                return Subject.OutputImage;
            }
        }
 
        public Object InputImage
        {
            get
            {
                if (Subject == null)
                {
                    return null;
                }
                return Subject.InputImage;
            }
            set
            {
                Subject.InputImage = value;
 
                if (InputImage == null)
                {
                    return;
                }
 
                if (InputImage is HObject)
                {
                    inputImageHSmartWindowControl.ShowHoImage((HObject)value);
                }
                else if (InputImage is Bitmap)
                {
                    TAlgorithm.Bitmap2HObject((Bitmap)value, out HObject image);
                    inputImageHSmartWindowControl.ShowHoImage(image);
                }
                else if (InputImage is Mat)
                {
                    TAlgorithm.Mat2HObject((Mat)value, out HObject image);
                    inputImageHSmartWindowControl.ShowHoImage(image);
                }
            }
        }
 
        public TAlgorithmEdit() { }
 
        public TAlgorithmEdit(TAlgorithm subject = null)
        {
            if (subject != null)
            {
                Subject = subject;
            }
            else
            {
                Subject = new TAlgorithm();
            }
            this.Dock = DockStyle.Fill;
 
            lblMsgToolTip.AutoPopDelay = 5000; // 提示显示5秒后消失
            lblMsgToolTip.InitialDelay = 500;  // 鼠标悬停500毫秒后显示提示
 
            InitializeComponent();
        }
 
        /// <summary>
        /// 运行算子
        /// </summary>
        /// <param name="minThreshold"></param>
        /// <param name="maxThreshold"></param>
        /// <param name="minArea"></param>
        /// <param name="maxArea"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public virtual bool Run()
        {
            //运行前需要更新输入参数Paras
            UpdataInputs();
            Subject.Run();
            return Subject.Result;
        }
 
        /// <summary>
        /// 更新运行参数
        /// </summary>
        public virtual void UpdataInputs()
        {
            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(HEllipse):
                    {
                        HEllipse hEllipse = (HEllipse)inputImageHSmartWindowControl.oRoi;
                        Subject.Params.ROI
                            = new HEllipse(hEllipse.X - Subject.Params.Fixture.X, hEllipse.Y - Subject.Params.Fixture.Y
                            , hEllipse.Phi, hEllipse.Radius1, hEllipse.Radius2, hEllipse.StartPhi, hEllipse.EndPhi);
                        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 virtual void LoadParas()
        {
            Type type = Subject.Params.ROI?.GetType();
            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(HEllipse):
                    {
                        inputImageHSmartWindowControl.oRoi
                    = new HEllipse(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y
                    , ((HEllipse)Subject.Params.ROI).Phi, ((HEllipse)Subject.Params.ROI).Radius1, ((HEllipse)Subject.Params.ROI).Radius2
                    , ((HEllipse)Subject.Params.ROI).StartAngle, ((HEllipse)Subject.Params.ROI).EndAngle);
                        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;
                    }
            }
        }
 
        /// <summary>
        /// 更新运行结果
        /// </summary>
        public virtual void UpdataOutputs() { }
 
        public virtual void btnRun_Click(object sender, EventArgs e) { }
 
        public virtual 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]);
                    InputImage = ho_Image;
                }
            }
        }
 
        public virtual void btnSaveParas_Click(object sender, EventArgs e)
        {
            UpdataInputs();
            // 创建 SaveFileDialog 实例
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                // 设置对话框标题
                saveFileDialog.Title = "保存文件";
 
                // 设置默认路径
                saveFileDialog.InitialDirectory = Application.StartupPath;
 
                // 设置文件类型过滤器
                saveFileDialog.Filter = "文本文件 (*.json)|*.json|所有文件 (*.*)|*.*";
 
                // 设置默认文件名
                saveFileDialog.FileName = "Algorithm_1.json";
 
                // 显示对话框并检查用户是否点击了保存按钮
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string fullPath = saveFileDialog.FileName;
                    Debug.WriteLine("选择的文件路径是: " + fullPath);
                    Subject.strProcessName = Path.GetFileNameWithoutExtension(fullPath);
                    //Subject.oRoi = inputImageHSmartWindowControl.oRoi;
                    Subject.Save(System.IO.Path.GetDirectoryName(fullPath));
                }
            }
        }
 
        public virtual 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)
                {
                    Subject.Load(selectedFiles[0]);
                    LoadParas();
                }
            }
        }
 
        public virtual void ckbDrawRoi_CheckedChanged(object sender, EventArgs e) { }
 
        public virtual void cmbTypeRoi_SelectedIndexChanged(object sender, EventArgs e) { }
 
        public virtual void cmbFixture_SelectedIndexChanged(object sender, EventArgs e) { }
    }
}