轮胎外观检测添加思谋语义分割模型检测工具
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
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
using HalconDotNet;
using LB_VisionControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_VisionProcesses.Alogrithms.Halcon
{
    public class HFindCode2dTool : TAlgorithm
    {
        public enum Code2dType { None, QRCode, BarCode };
 
        HTuple hv_QRCodeHandle = new HTuple();//二维码模板
        HTuple hv_BarCodeHandle = new HTuple();//条码模板
        public HFindCode2dTool()
        {
            strProcessClass = "LB_VisionProcesses.Alogrithms.Halcon.HFindCode2dTool";
            strProcessName = "Halcon2D_读码工具";
 
            //创建一个条码模型
            hv_BarCodeHandle = CreateCodeMode();
            hv_QRCodeHandle = CreateQRCodeMode();
 
            Params.Inputs.Add("Code2dType", "BarCode");
            Params.Inputs.Add("CodeHead", "");
            Params.Inputs.Add("CodeLength", 0);
            Params.Inputs.Add("MinThreshold", 80);
            Params.Inputs.Add("MaxThreshold", 180);
 
            Params.Inputs.Add("MinCount", 0);
            Params.Inputs.Add("MaxCount", 9999);
 
            Params.Outputs.Add("Count", 0);
            Params.Outputs.Add("CodeStrings", new List<string>());
        }
 
        private static readonly object lockObj = new object();
 
        public override bool Run()
        {
            DateTime StartTime = DateTime.Now;
 
            InitRunParams();
            HOperatorSet.GenEmptyObj(out HObject EmptyObj);
            OutputImage = EmptyObj;
 
            // 创建并启动任务
            TAlgorithmMain();
            RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
            return Result;
        }
        /// <summary>
        /// 算子逻辑
        /// </summary>
        public override void TAlgorithmMain()
        {
            lock (lockObj)
            {
                #region 初始化变量
                HOperatorSet.GenEmptyObj(out HObject ho_SymbolXLDs);
                HOperatorSet.GenEmptyObj(out HObject ho_SymbolRegions);
                HTuple hv_DecodedDataStrings = new HTuple();
                HTuple hv_DecodedDataTypes = new HTuple();
 
                //创建一个条码模型
                hv_BarCodeHandle = CreateCodeMode();
                hv_QRCodeHandle = CreateQRCodeMode();
                #endregion
 
                try
                {
                    if (InputImage == null || !(InputImage is HObject))
                    {
                        Msg = "输入图片为空";
                        Result = false;
                        return;
                    }
                    //if (InputImage is Bitmap)
                    //{
                    //    try
                    //    {
                    //        using (HImage hImage = new HImage())
                    //        {
                    //            Rectangle rect = new Rectangle(0, 0, ((Bitmap)InputImage).Width, ((Bitmap)InputImage).Height);
                    //            BitmapData srcBmpData = ((Bitmap)InputImage).LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                    //            hImage.GenImageInterleaved(srcBmpData.Scan0, "rgbx", ((Bitmap)InputImage).Width, ((Bitmap)InputImage).Height, 0, "byte", ((Bitmap)InputImage).Width, ((Bitmap)InputImage).Height, 0, 0, -1, 0);
                    //            ((Bitmap)InputImage).UnlockBits(srcBmpData);
                    //            //((Bitmap)InputImage).Dispose();
                    //            InputImage = null;
                    //            InputImage = hImage.Clone();
                    //        }
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //    }
                    //}
                    if (!(InputImage is HObject))
                    {
                        Msg = "输入图片格式不为HObject";
                        Result = false;
                        return;
                    }
 
                    object DomainImage = null;
 
                    #region 裁剪区域
                    if (!ReduceDomainImage(InputImage, ref DomainImage))
                    {
                        Msg = "裁剪区域失败";
                        Result = false;
                        return;
                    }
                    this.Record = new MsgRecord();
                    #endregion
 
                    #region 算子逻辑
                    HObject hoDomainImage = DomainImage as HObject;
                    HTuple hv_Channels = new HTuple();
                    //判断是否为灰度图
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        try
                        {
                            HOperatorSet.CountChannels(hoDomainImage, out hv_Channels);
                            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);
                                Msg = "输入图片不为灰度图";
                                Result = false;
                                return;
                            }
                        }
                        catch
                        {
                            Msg = "输入图片不为灰度图且转换失败";
                            Result = false;
                            return;
                        }
                    }
 
                    Code2dType eCode2dType = Code2dType.None;
                    string CodeHead = (ProcessParams.ConvertToString(Params.Inputs["CodeHead"])).TrimEnd();
                    int CodeLength = ProcessParams.ConvertToInt32(Params.Inputs["CodeLength"]);
                    int MinThreshold = ProcessParams.ConvertToInt32(Params.Inputs["MinThreshold"]);
                    int MaxThreshold = ProcessParams.ConvertToInt32(Params.Inputs["MaxThreshold"]);
 
                    // 尝试转换字符串为枚举类型
                    if (!Enum.TryParse(ProcessParams.ConvertToString(Params.Inputs["Code2dType"]), out eCode2dType))
                        eCode2dType = Code2dType.None;
 
                    if (string.IsNullOrEmpty(CodeHead) || CodeHead.Trim() == "")
                    {
                        switch (eCode2dType)
                        {
                            case Code2dType.None:
                                break;
                            case Code2dType.QRCode:
                                HOperatorSet.FindDataCode2d(hoDomainImage, out ho_SymbolXLDs, hv_QRCodeHandle, new HTuple(), new HTuple(), out hv_BarCodeHandle, out hv_DecodedDataStrings);
                                break;
                            case Code2dType.BarCode:
                                HOperatorSet.FindBarCode(hoDomainImage, out ho_SymbolRegions, hv_BarCodeHandle, "auto", out hv_DecodedDataStrings);
                                HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, "all", "decoded_types", out hv_DecodedDataTypes);
                                break;
                        }
                    }
                    else
                    {
                        //使用图像增强算法找到想要的条码
                        for (int i = 0; i <= ProcessParams.ConvertToInt32(Params.Inputs["MinThreshold"]); i = i + 20)
                        {
                            for (int j = 255; j >= ProcessParams.ConvertToInt32(Params.Inputs["MaxThreshold"]); j = j - 40)
                            {
                                //色阶映射,增强图像对比度
                                scale_gray_map(hoDomainImage, out HObject ho_Image2, i, j);
                                switch (eCode2dType)
                                {
                                    case Code2dType.None:
                                        break;
                                    case Code2dType.QRCode:
                                        HOperatorSet.FindDataCode2d(ho_Image2, out ho_SymbolXLDs, hv_QRCodeHandle, new HTuple(), new HTuple(), out hv_BarCodeHandle, out hv_DecodedDataStrings);
                                        hv_DecodedDataTypes = null;
                                        break;
                                    case Code2dType.BarCode:
                                        HOperatorSet.FindBarCode(ho_Image2, out ho_SymbolRegions, hv_BarCodeHandle, "auto", out hv_DecodedDataStrings);
                                        HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, "all", "decoded_types", out hv_DecodedDataTypes);
                                        break;
                                }
 
                                if ((int)(new HTuple(((hv_DecodedDataStrings.TupleStrlen())).TupleGreater(0))) != 0)
                                {
                                    HTuple row = new HTuple();
                                    HTuple col = new HTuple();
                                    HTuple phi = new HTuple();
                                    HTuple length1 = new HTuple();
                                    HTuple length2 = new HTuple();
 
                                    List<string> listCodeStrings = new List<string>();
 
                                    for (int k = 0; k < hv_DecodedDataStrings.Length; k++)
                                    {
                                        string strBarCode = (string)hv_DecodedDataStrings[k];
                                        if (strBarCode.StartsWith(CodeHead))
                                        {
                                            //CodeLength大于0时开启长度校验
                                            if (CodeLength > 0 && strBarCode.Length != CodeLength)
                                                continue;
 
                                            switch (eCode2dType)
                                            {
                                                case Code2dType.None:
                                                    break;
                                                case Code2dType.QRCode:
                                                    HOperatorSet.SelectObj(ho_SymbolXLDs, out HObject ho_SymbolXLD, k + 1);
                                                    HOperatorSet.SmallestRectangle2Xld(ho_SymbolXLD, out row, out col, out phi, out length1, out length2);
                                                    HOperatorSet.GenRectangle2(out HObject ho_SymbolXLDRegion, row, col, phi, length1, length2);
                                                    ((MsgRecord)Record).AddRecord(strBarCode, row, col, ho_SymbolXLDRegion);
                                                    break;
                                                case Code2dType.BarCode:
                                                    HOperatorSet.SelectObj(ho_SymbolRegions, out HObject ho_SymbolRegion, k + 1);
                                                    HOperatorSet.SmallestRectangle2(ho_SymbolRegion, out row, out col, out phi, out length1, out length2);
                                                    ((MsgRecord)Record).AddRecord(strBarCode, row, col, ho_SymbolRegion);
                                                    break;
                                            }
                                            OutputImage = hoDomainImage;
                                            listCodeStrings.Add(strBarCode);
                                            Params.Outputs["CodeStrings"] = listCodeStrings;
                                            Params.Outputs["Count"] = 1;
                                            Msg = "运行成功";
                                            Result = true;
                                            return;
                                        }
                                    }
                                }
                            }
                        }
 
                        OutputImage = null;
                        Params.Outputs["CodeStrings"] = null;
                        Params.Outputs["Count"] = 0;
                        if (CodeLength > 0)
                            Msg = string.Format("没有找到指定头为\"{0}\"且长度为\"{1}\"的码", CodeHead, CodeLength);
                        else
                            Msg = string.Format("没有找到指定头为\"{0}\"码", CodeHead);
                        Result = false;
                        return;
                    }
                    #endregion
 
                    #region 结果处理
                    this.Record = new ObjectRecord();
                    List<string> CodeStrings = new List<string>();
                    if ((int)(new HTuple(((hv_DecodedDataStrings.TupleStrlen())).TupleGreater(0))) != 0)
                    {
                        for (int k = 0; k < hv_DecodedDataStrings.Length; k++)
                            CodeStrings.Add((string)hv_DecodedDataStrings[k]);
 
                        switch (eCode2dType)
                        {
                            case Code2dType.None:
                                break;
                            case Code2dType.QRCode:
                                HOperatorSet.SmallestRectangle2Xld(ho_SymbolXLDs, out HTuple row, out HTuple col, out HTuple phi, out HTuple length1, out HTuple length2);
                                HOperatorSet.GenRectangle2(out HObject ho_SymbolXLDRegion, row, col, phi, length1, length2);
                                ((ObjectRecord)Record).AddRecord(ho_SymbolXLDRegion);
                                break;
                            case Code2dType.BarCode:
                                HOperatorSet.DilationRectangle1(ho_SymbolRegions, out HObject Regiondilation, 9, 9);
                                ((ObjectRecord)Record).AddRecord(Regiondilation);
                                break;
                        }
                    }
 
                    Params.Outputs["CodeStrings"] = CodeStrings;
                    Params.Outputs["Count"] = CodeStrings.Count;
                    #endregion
 
                    #region 生成OutputImage给后续处理
                    try
                    {
                        OutputImage = hoDomainImage;
                    }
                    catch (Exception ex)
                    {
                        Msg = "生成OutputImage失败,原因是:" + ex.ToString();
                        Result = false;
                        return;
                    }
                    #endregion
 
                    if (Msg == "运行超时")
                    {
                        Result = false;
                        return;
                    }
 
                    int MinCount = ProcessParams.ConvertToInt32(Params.Inputs["MinCount"]);
                    int MaxCount = ProcessParams.ConvertToInt32(Params.Inputs["MaxCount"]);
 
                    if (CodeStrings.Count < MinCount || CodeStrings.Count > MaxCount)
                    {
                        Msg = string.Format("结果个数超出范围({0},{1})", MinCount, MaxCount);
                        Result = false;
                        return;
                    }
 
                    Msg = "运行成功";
                    Result = true;
                    return;
                }
                catch (Exception ex)
                {
                    Msg = "运行失败,原因是:" + ex.ToString().TrimEnd();
                    Params.Outputs.Add("Count", 0);
                    Params.Outputs.Add("CodeStrings", new List<string>());
                    OutputImage = null;
                    Result = false;
                    return;
                }
                finally
                {
                    bCompleted = true;
 
                    #region 内存释放
                    ho_SymbolXLDs.Dispose();
                    ho_SymbolRegions.Dispose();
                    #endregion
                }
            }
        }
 
        /// <summary>
        /// 创建条码类型
        /// </summary>
        public HTuple CreateCodeMode()
        {
            //条码模型句柄
            HTuple hv_BarCodeHandle = null;
 
            //创建条码模型
            HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle);
            HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "orientation", 0);
            HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "orientation_tol", 90);
            return hv_BarCodeHandle;
        }
 
        public HTuple CreateQRCodeMode()
        {
            //条码模型句柄
            HTuple hv_QRCodeHandle = null;
            HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_QRCodeHandle);
            return hv_QRCodeHandle;
        }
    }
}