C3204
2025-12-18 d85cbf0ccd61d95b96695756e0c90db8b7679545
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
using HalconDotNet;
using System.Data.Common;
using LB_VisionProcesses.Alogrithms;
using System.Windows.Forms;
using OpenCvSharp;
using LB_VisionControl;
 
namespace LB_VisionProcesses.Alogrithms.OpenCvSharp
{
    public partial class FindLineToolEdit : TAlgorithmEdit
    {
        public FindLineToolEdit(FindLineTool subject = null)
        {
            if (subject != null && subject is FindLineTool)
                Subject = subject;
            else
                Subject = new FindLineTool();
 
            if (!(Subject.Params.ROI is HSegment))
                Subject.Params.ROI = new HSegment(0, 0, 250, 250);
 
            this.Dock = DockStyle.Fill;
            InitializeComponent();
        }
 
        /// <summary>
        /// 控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HFindLineToolEdit_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());
 
            //遍历可以选择的极性类型枚举
            foreach (var value in Enum.GetValues(typeof(Transition)))
                cmbTransition.Items.Add(value.ToString());
 
            //遍历可以选择的边缘类型枚举
            foreach (var value in Enum.GetValues(typeof(Selector)))
                cmbSelect.Items.Add(value.ToString());
 
            //遍历可以选择的Fixture枚举
            cmbFixture.Items.Add("");
            foreach (string value in IProcess.dicFixtures.Keys)
                cmbFixture.Items.Add(value.ToString());
 
            ckbDrawRoi.Checked = true;
            cmbTypeRoi.Text = RoiType.Segment.ToString();
            cmbTransition.Text = Transition.Ignore.ToString();
            cmbSelect.Text = Selector.Best.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["卡尺数量"] = int.TryParse(itxtCaliperCount.Text, out iResult) ? iResult : Subject.Params.Inputs["卡尺数量"];
            Subject.Params.Inputs["卡尺长度"] = double.TryParse(dtxtCaliperHeight.Text, out dResult) ? dResult : Subject.Params.Inputs["卡尺长度"];
            Subject.Params.Inputs["卡尺宽度"] = double.TryParse(dtxtCaliperWidth.Text, out dResult) ? dResult : Subject.Params.Inputs["卡尺宽度"];
 
            Subject.Params.Inputs["过滤一半像素"] = int.TryParse(itxtSigma.Text, out iResult) ? iResult : Subject.Params.Inputs["过滤一半像素"];
            Subject.Params.Inputs["对比度阈值"] = int.TryParse(itxtThreshold.Text, out iResult) ? iResult : Subject.Params.Inputs["对比度阈值"];
 
            Subject.Params.Inputs["极性"] = cmbTransition.SelectedItem.ToString();
            Subject.Params.Inputs["边缘位置"] = cmbSelect.SelectedItem.ToString();
 
            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(() =>
            {
                itxtCaliperCount.Text = Subject.Params.Inputs["卡尺数量"].ToString();
                dtxtCaliperHeight.Text = Subject.Params.Inputs["卡尺长度"].ToString();
                dtxtCaliperWidth.Text = Subject.Params.Inputs["卡尺宽度"].ToString();
                itxtSigma.Text = Subject.Params.Inputs["过滤一半像素"].ToString();
                itxtThreshold.Text = Subject.Params.Inputs["对比度阈值"].ToString();
                itxtIgnoreCount.Text = Subject.Params.Inputs["忽略点数"].ToString();
 
                cmbTransition.Text = Subject.Params.Inputs["极性"].ToString();
                cmbSelect.Text = Subject.Params.Inputs["边缘位置"].ToString();
 
                if (Subject.InputImage != null && Subject.InputImage is Mat)
                {
                    TAlgorithm.Mat2HObject((Mat)Subject.InputImage, out HObject image);
                    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;
                }
            }));
        }
 
        /// <summary>
        /// 更新输出结果
        /// </summary>
        public override void UpdataOutputs()
        {
            this.BeginInvoke(new Action(() =>
            {
                //存在反序列化错乱的情况需要使用自定义的转换器
                dtxtCenterX.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterX"]);
                dtxtCenterY.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterY"]);
                dtxtAngle.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Angle"]);
                dtxtWidth.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Width"]);
                dtxtHeight.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Height"]);
                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)
                {
                    TAlgorithm.Mat2HObject((Mat)Subject.InputImage, out HObject image);
                    HOperatorSet.GetImageSize(image, 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) { 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 Mat)
                    {
                        TAlgorithm.Mat2HObject((Mat)InputImage, out HObject image);
                        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 void btnShowROI_Click(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() =>
            {
                try
                {
                    imgTabControl.SelectedTab = tabPageInputImage;
 
                    inputImageHSmartWindowControl.ClearObj();
 
                    if (InputImage != null && InputImage is Mat)
                    {
                        TAlgorithm.Mat2HObject((Mat)InputImage, out HObject image);
                        inputImageHSmartWindowControl.ShowHoImage(image);
                    }
 
                    int hv_Elements = Convert.ToInt16(itxtCaliperCount.Text);
                    double hv_DetectHeight = Convert.ToDouble(dtxtCaliperHeight.Text);
                    double hv_DetectWidth = Convert.ToDouble(dtxtCaliperWidth.Text);
 
                    double hv_Row1 = Convert.ToDouble(((HSegment)inputImageHSmartWindowControl.oRoi).BeginRow + Subject.Params.Fixture.Row);
                    double hv_Column1 = Convert.ToDouble(((HSegment)inputImageHSmartWindowControl.oRoi).BeginColumn + Subject.Params.Fixture.Column);
                    double hv_Row2 = Convert.ToDouble(((HSegment)inputImageHSmartWindowControl.oRoi).EndRow + Subject.Params.Fixture.Row);
                    double hv_Column2 = Convert.ToDouble(((HSegment)inputImageHSmartWindowControl.oRoi).EndColumn + Subject.Params.Fixture.Column);
 
                    TAlgorithm.GetRakeRegions(null, out HObject ho_Regions, hv_Elements, hv_DetectHeight, hv_DetectWidth
                        , new HTuple(), new HTuple(), new HTuple(), new HTuple()
                        , hv_Row1, hv_Column1, hv_Row2, hv_Column2
                        , out HTuple hv_ResultRow, out HTuple hv_ResultColumn);
 
                    inputImageHSmartWindowControl.DispObj(ho_Regions);
                }
                catch { }
            }));
        }
    }
}