C3204
2025-12-26 87a51c004242323d6bc80c470115ef69117bcb1b
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
using HalconDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Formats.Asn1;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;
 
namespace LB_VisionControl
{
    public enum RoiType { None, Rectangle2, Circle, Ellipse, Segment };
 
    public class ROI
    {
        // 你可以在父类中定义一些公共属性或方法
        public string Type = "ROI";  // 类型标识符
 
        public double Z = 0;
 
        public double Column = 0;
        public double X
        {
            get { return Column; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                Column = value;
            }
        }
 
        public double Row = 0;
        public double Y
        {
            get { return Row; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                Row = value;
            }
        }
 
        public double Phi = 0;
        public double Angle
        {
            get { return Phi * 180 / Math.PI; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                Phi = (value / 180 * Math.PI);
            }
        }
        public double Rotation
        {
            get { return Phi; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                Phi = value;
            }
        }
 
        public object Clone()
        { return MemberwiseClone(); }
    }
    [Serializable]
    public class HRectangle2 : ROI
    {
        public HRectangle2() { Type = "HRectangle2"; }
 
        //public HRectangle2(HTuple row, HTuple column, HTuple phi, HTuple length1, HTuple length2)
        //{
        //    Type = "HRectangle2";
        //    this.Row = row.TupleReal();
        //    this.Column = column.TupleReal();
        //    this.Phi = phi.TupleReal();
        //    this.SemiLength1 = length1.TupleReal();
        //    this.SemiLength2 = length2.TupleReal();
        //}
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HRectangle2(double X = 0, double Y = 0, double rotation = 0, double width = 800, double height = 800)
        {
            Type = "HRectangle2";
            this.X = X;
            this.Y = Y;
            Rotation = rotation;
            Width = width;
            Height = height;
        }
 
        public HObject GetHObject()
        {
            HOperatorSet.GenRectangle2(out HObject hObject, (HTuple)Row, (HTuple)Column, (HTuple)Phi, (HTuple)SemiLength1, (HTuple)SemiLength2);
            return hObject;
        }
 
        /// <summary>
        /// 矩形宽度的一半
        /// </summary>
        public double SemiLength1 = 200;
        /// <summary>
        /// 矩形宽度
        /// </summary>
        public double Width
        {
            get { return SemiLength1 * 2; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                SemiLength1 = (value / 2.0);
            }
        }
 
        /// <summary>
        /// 矩形高度的一半
        /// </summary>
        public double SemiLength2 = 200;
        public double Height
        {
            get { return SemiLength2 * 2; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                SemiLength2 = (value / 2.0);
            }
        }
 
        public HPoint[] Corners
        {
            get
            {
                // 计算未旋转矩形的角点相对于中心的坐标
                var corners = new HPoint[]
                {
                    new HPoint(-Width / 2, -Height / 2), // 左上角
                    new HPoint(Width / 2, -Height / 2),  // 右上角
                    new HPoint(Width / 2, Height / 2),   // 右下角
                    new HPoint(-Width / 2, Height / 2)   // 左下角
                };
 
                // 旋转矩形的角点
                for (int i = 0; i < corners.Length; i++)
                {
                    double x = corners[i].X;
                    double y = corners[i].Y;
 
                    // 旋转矩阵应用
                    double rotatedX = x * Math.Cos(Rotation) - y * Math.Sin(Rotation);
                    double rotatedY = x * Math.Sin(Rotation) + y * Math.Cos(Rotation);
 
                    // 移动到实际位置
                    corners[i] = new HPoint(rotatedX + X, rotatedY + Y);
                }
 
                return corners;
            }
        }
    }
 
    [Serializable]
    public class HCircle : ROI
    {
        public HCircle() { Type = "HCircle"; }
 
        //public HCircle(HTuple row, HTuple column, HTuple radius)
        //{
        //    Type = "HCircle";
        //    this.Row = row.TupleReal();
        //    this.Column = column.TupleReal();
        //    this.Radius = radius.TupleReal();
        //}
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HCircle(double X = 0, double Y = 0, double radius = 100)
        {
            Type = "HCircle";
            this.X = X;
            this.Y = Y;
            Radius = radius;
        }
 
        public double Radius = 100;
        public double Diameter
        {
            get { return Radius * 2; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                Radius = value / 2.0;
            }
        }
    }
 
    public class HEllipse : ROI
    {
        public HEllipse() { Type = "HEllipse"; }
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HEllipse(double x, double y, double phi, double radius1, double radius2, double start_angle = 0, double end_angle = Math.PI)
        {
            Type = "HEllipse";
            this.X = x;
            this.Y = y;
            this.Phi = phi;
            Radius1 = radius1;
            Radius2 = radius2;
            StartAngle = start_angle;
            EndAngle = end_angle;
        }
 
        public double Radius1 = 100;
        public double Radius2 = 100;
        public double StartAngle = 0;
        public double EndAngle = Math.PI;
    }
 
    [Serializable]
    public class HSegment : ROI
    {
        public HSegment() { Type = "HSegment"; }
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HSegment(double startX = 0, double startY = 0, double endX = 0, double endY = 0)
        {
            Type = "HSegment";
            StartX = startX;
            StartY = startY;
            EndX = endX;
            EndY = endY;
        }
 
        public HSegment(HPoint startPoint, HPoint endPoint)
        {
            Type = "HSegment";
            BeginRow = startPoint.Row;
            BeginColumn = startPoint.Column;
            EndRow = endPoint.Row;
            EndColumn = endPoint.Column;
        }
 
        public HObject GetHObject()
        {
            HOperatorSet.GenRegionLine(out HObject hObject, (HTuple)BeginRow, (HTuple)BeginColumn, (HTuple)EndRow, (HTuple)EndColumn);
            return hObject;
        }
 
        public HPoint StartPoint
        {
            get { return new HPoint(BeginColumn, BeginRow); }
            set { BeginRow = value.Row; BeginColumn = value.Column; }
        }
 
        public HPoint EndPoint
        {
            get { return new HPoint(EndColumn, EndRow); }
            set { EndRow = value.Row; EndColumn = value.Column; }
        }
 
        public double BeginColumn = 0;
        public double StartX
        {
            get { return BeginColumn; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                BeginColumn = value;
            }
        }
 
        public double BeginRow = 0;
        public double StartY
        {
            get { return BeginRow; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                BeginRow = value;
            }
        }
 
        public double EndColumn = 100;
        public double EndX
        {
            get { return EndColumn; }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                EndColumn = value;
            }
        }
 
        public double EndRow = 100;
        public double EndY
        {
            get
            {
                return EndRow;
            }
            set
            {
                double result = 0;
                value = double.TryParse(value.ToString(), out result) ? result : 0;
                EndRow = value;
            }
        }
 
        public double MidX
        {
            get { return (StartX + EndX) / 2; }
        }
 
        public double MidY
        {
            get { return (StartY + EndY) / 2; }
        }
 
        public double Length
        {
            get { return Math.Sqrt(Math.Pow(StartX - EndX, 2) + Math.Pow(StartX - EndY, 2)); }
        }
    }
 
    [Serializable]
    public class HLine : ROI
    {
        public HLine() { Type = "HLine"; }
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HLine(double x = 0, double y = 0, double phi = 0)
        {
            Type = "HLine";
            X = x;
            Y = y;
            Phi = phi;
        }
 
        public HLine(HPoint point, double phi = 0)
        {
            Type = "HLine";
            X = point.Row;
            Y = point.Column;
            Phi = phi;
        }
    }
 
    [Serializable]
    public class HPoint : ROI
    {
        public HPoint() { Type = "HPoint"; }
 
        // 带私有参数的构造函数
        [JsonConstructor]  // 标记为用于反序列化
        public HPoint(double x, double y, double z = 0)
        {
            Type = "HPoint";
            X = x;
            Y = y;
            Z = z;
        }
 
        public HPoint(OpenCvSharp.Point point)
        {
            Type = "HPoint";
            X = point.X;
            Y = point.Y;
        }
 
        public HPoint(HPoint point)
        {
            Type = "HPoint";
            X = point.X;
            Y = point.Y;
        }
 
        public HObject GetHObject()
        {
            HOperatorSet.GenRegionPoints(out HObject hObject, (HTuple)Row, (HTuple)Column);
            return hObject;
        }
 
        public Point2d ToPoint2d() { return new Point2d(X, Y); }
    }
 
    public class ROIConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(ROI);
        }
 
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jo = JObject.Load(reader);  // 只调用一次 Load(reader)
            if (jo != null)
            {
                var type = jo["Type"]?.ToString();  // 获取类型标识符
 
                // 根据类型标识符反序列化为具体类型
                switch (type)
                {
                    case "ROI":
                        return jo.ToObject<ROI>();
                    case "HRectangle2":
                        return jo.ToObject<HRectangle2>();
                    case "HCircle":
                        return jo.ToObject<HCircle>();
                    case "HSegment":
                        return jo.ToObject<HSegment>();
                    case "HLine":
                        return jo.ToObject<HLine>();
                    case "HPoint":
                        return jo.ToObject<HPoint>();
                    default:
                        // 其他类型的处理方式
                        throw new JsonSerializationException($"Unknown type: {type}");
                }
            }
            return null;
        }
 
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            try
            {
                if (value != null)
                {
                    // 创建一个 JObject 来序列化 ROI 对象
                    JObject jo = JObject.FromObject(value, serializer);
                    var type = value.GetType().Name;
                    // 根据类型标识符反序列化为具体类型
                    switch (type)
                    {
                        case "ROI":
                            ROI roi = value as ROI;
                            if (roi == null)
                                throw new JsonSerializationException($"Convert {type} to null");
 
                            // 开始写入 JSON 对象
                            writer.WriteStartObject();
 
                            writer.WritePropertyName("Type");
                            writer.WriteValue(roi.Type);
 
                            // 结束对象
                            writer.WriteEndObject();
                            break;
                        case "HRectangle2":
                            HRectangle2 hRectangle2 = value as HRectangle2;
                            if (hRectangle2 == null)
                                throw new JsonSerializationException($"Convert {type} to null");
 
                            // 开始写入 JSON 对象
                            writer.WriteStartObject();
 
                            writer.WritePropertyName("Type");
                            writer.WriteValue(hRectangle2.Type);
 
                            writer.WritePropertyName("X");
                            writer.WriteValue(hRectangle2.X);
 
                            writer.WritePropertyName("Y");
                            writer.WriteValue(hRectangle2.Y);
 
                            writer.WritePropertyName("Rotation");
                            writer.WriteValue(hRectangle2.Rotation);
 
                            writer.WritePropertyName("Width");
                            writer.WriteValue(hRectangle2.Width);
 
                            writer.WritePropertyName("Height");
                            writer.WriteValue(hRectangle2.Height);
 
                            // 结束对象
                            writer.WriteEndObject();
                            break;
                        case "HCircle":
                            HCircle hCircle = value as HCircle;
                            if (hCircle == null)
                                throw new JsonSerializationException($"Convert {type} to null");
 
                            // 开始写入 JSON 对象
                            writer.WriteStartObject();
 
                            writer.WritePropertyName("Type");
                            writer.WriteValue(hCircle.Type);
 
                            writer.WritePropertyName("X");
                            writer.WriteValue(hCircle.X);
 
                            writer.WritePropertyName("Y");
                            writer.WriteValue(hCircle.Y);
 
                            writer.WritePropertyName("Radius");
                            writer.WriteValue(hCircle.Radius);
 
                            // 结束对象
                            writer.WriteEndObject();
                            break;
                        case "HPoint":
                            HPoint hPoint = value as HPoint;
                            if (hPoint == null)
                                throw new JsonSerializationException($"Convert {type} to null");
 
                            // 开始写入 JSON 对象
                            writer.WriteStartObject();
 
                            writer.WritePropertyName("Type");
                            writer.WriteValue(hPoint.Type);
 
                            writer.WritePropertyName("X");
                            writer.WriteValue(hPoint.X);
 
                            writer.WritePropertyName("Y");
                            writer.WriteValue(hPoint.Y);
 
                            // 结束对象
                            writer.WriteEndObject();
                            break;
                        case "HSegment":
                            HSegment hSegment = value as HSegment;
                            if (hSegment == null)
                                throw new JsonSerializationException($"Convert {type} to null");
 
                            // 开始写入 JSON 对象
                            writer.WriteStartObject();
 
                            writer.WritePropertyName("Type");
                            writer.WriteValue(hSegment.Type);
 
                            writer.WritePropertyName("StartX");
                            writer.WriteValue(hSegment.StartX);
 
                            writer.WritePropertyName("StartY");
                            writer.WriteValue(hSegment.StartY);
 
                            writer.WritePropertyName("EndX");
                            writer.WriteValue(hSegment.EndX);
 
                            writer.WritePropertyName("EndY");
                            writer.WriteValue(hSegment.EndY);
 
                            // 结束对象
                            writer.WriteEndObject();
                            break;
                        case "HLine":
                        default:
                            // 其他类型的处理方式
                            throw new JsonSerializationException($"Unsupport convert {type}");
                    }
                }
            }
            catch (Exception ex)
            {
                // 输出异常信息进行调试
                Debug.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}