patrick.xu
2021-05-24 3c583b1091133e4af23c2534ae96bd094c132d58
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.IO;
using System.Configuration;
 
namespace M423project
{
 
 
    //此工程涉及的视觉检测方法全部放在此处,进一步模块化
    public class VisionDetect
    {
 
        #region vision parameters
        private int _measureLWScaleNum = 2;
        private int _measureLWThreshold = 150;
        private int _measureLWOpeningRectangle = 50;
        private int _measureHeightThresholdLowNum = 2000;
        private int _measureHeightOpeningNumL = 450;
        private int _measureHeightSelectNumLow = 5000;
        private string _gocatorDirection = "false";
        #endregion
 
        private void LoadVisionParameters()
        {
            int v = 0;
            string str = string.Empty;
            str = ConfigurationManager.AppSettings["MeasureLWScaleNum"];
            if (int.TryParse(str, out v))
                _measureLWScaleNum = v;
 
            str = ConfigurationManager.AppSettings["MeasureLWThreshold"];
            if (int.TryParse(str, out v))
                _measureLWThreshold = v;
 
            str = ConfigurationManager.AppSettings["MeasureLWOpeningRectangle"];
            if (int.TryParse(str, out v))
                _measureLWOpeningRectangle = v;
 
            str = ConfigurationManager.AppSettings["MeasureHeightThresholdLowNum"];
            if (int.TryParse(str, out v))
                _measureHeightThresholdLowNum = v;
 
            str = ConfigurationManager.AppSettings["MeasureHeightOpeningNumL"];
            if (int.TryParse(str, out v))
                _measureHeightOpeningNumL = v;
 
            str = ConfigurationManager.AppSettings["MeasureHeightSelectNumLow"];
            if (int.TryParse(str, out v))
                _measureHeightSelectNumLow = v;
 
            _gocatorDirection = ConfigurationManager.AppSettings["GocatorDirection"];
 
        }
 
        public VisionDetect()
        {
            LoadVisionParameters();
 
        }
 
        //判断电池有无
        public bool IsHaveBattery(HObject image, HTuple grayThreshold)
        {
            HTuple DeviGray = new HTuple(), meanGray = new HTuple();
            HOperatorSet.Intensity(image, image, out meanGray, out DeviGray);
            if (meanGray < grayThreshold) return true;
            else return false;
        }
        //判斷電池數量
        public int BatteryNumber(HObject image, ref int batteryStatus)
        {
            // Local iconic variables 
            HObject ho_Batteries;
            // Local control variables 
            HTuple hv_Width1 = null, hv_Height1 = null;
            HTuple hv_LowThreshold = null, hv_HighThreshold = null;
            HTuple hv_Number = null, hv_BatteryStatus = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GetImageSize(image, out hv_Width1, out hv_Height1);
            hv_LowThreshold = 0;
            hv_HighThreshold = 160;
            ho_Batteries.Dispose();
            SelectBattery(image, out ho_Batteries, hv_LowThreshold, hv_HighThreshold, out hv_BatteryStatus);
            batteryStatus = hv_BatteryStatus.I;
            HOperatorSet.CountObj(ho_Batteries, out hv_Number);
            ho_Batteries.Dispose();
            return hv_Number.I - 1;
        }
        //创建上料模板
        public void CreateLoadModel(HObject image, HTuple hwindow, string modelpath)
        {
            // Local iconic variables 
 
            HObject ho_ROI, ho_ImageROI;
            HObject ho_ShapeModelImages, ho_ShapeModelRegions, ho_ShapeModel;
            HObject ho_Image = null, ho_Region1 = null, ho_RegionFillUp = null;
            HObject ho_RegionOpening = null, ho_ImageReduced = null;
            HObject ho_ShapeModelTrans;
 
            // Local control variables 
 
            HTuple hv_Pointer = null, hv_Type = null, hv_Width = null;
            HTuple hv_Height = null, hv_WindowHandle = new HTuple();
            HTuple hv_Row = null, hv_Column = null, hv_Phi = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_AreaModelRegions = null;
            HTuple hv_RowModelRegions = null, hv_ColumnModelRegions = null;
            HTuple hv_HeightPyramid = null, hv_i = null, hv_NumLevels = new HTuple();
            HTuple hv_ModelID = null;
            HTuple hv_Width1 = new HTuple(), hv_Height1 = new HTuple();
            HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();
            HTuple hv_Row3 = new HTuple(), hv_Column3 = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageROI);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelImages);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelRegions);
            HOperatorSet.GenEmptyObj(out ho_ShapeModel);
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ShapeModelTrans);
 
            HOperatorSet.GetImagePointer1(image, out hv_Pointer, out hv_Type, out hv_Width,
                out hv_Height);
            DisplayImage(image, hwindow);
            //colors and other settings for the visualization
            HOperatorSet.SetDraw(hwindow, "margin");
            HOperatorSet.SetLineWidth(hwindow, 1);
            HOperatorSet.SetColor(hwindow, "red");
            //-------------------  start of the application  ----------------
            //step 1: select the model object
            HOperatorSet.DrawRectangle2(hwindow, out hv_Row, out hv_Column,
                out hv_Phi, out hv_Length1, out hv_Length2);
            ho_ROI.Dispose();
            HOperatorSet.GenRectangle2(out ho_ROI, hv_Row, hv_Column, hv_Phi, hv_Length1,
                hv_Length2);
            ho_ImageROI.Dispose();
            HOperatorSet.ReduceDomain(image, ho_ROI, out ho_ImageROI);
            ho_ShapeModelImages.Dispose();
            ho_ShapeModelRegions.Dispose();
            HOperatorSet.InspectShapeModel(ho_ImageROI, out ho_ShapeModelImages, out ho_ShapeModelRegions,
                8, 30);
            HOperatorSet.AreaCenter(ho_ShapeModelRegions, out hv_AreaModelRegions, out hv_RowModelRegions,
                out hv_ColumnModelRegions);
            HOperatorSet.CountObj(ho_ShapeModelRegions, out hv_HeightPyramid);
            HTuple end_val31 = hv_HeightPyramid;
            HTuple step_val31 = 1;
            for (hv_i = 1; hv_i.Continue(end_val31, step_val31); hv_i = hv_i.TupleAdd(step_val31))
            {
                if ((int)(new HTuple(((hv_AreaModelRegions.TupleSelect(hv_i - 1))).TupleGreaterEqual(
                    15))) != 0)
                {
                    hv_NumLevels = hv_i.Clone();
                }
            }
            HOperatorSet.CreateShapeModel(ho_ImageROI, hv_NumLevels, 0, (new HTuple(360)).TupleRad()
                , "auto", "none", "use_polarity", 30, 10, out hv_ModelID);
            //create_scaled_shape_model(ImageROI,
            ho_ShapeModel.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ShapeModel, hv_ModelID, 1);
            HOperatorSet.FindShapeModel(ho_ImageROI, hv_ModelID, 0, (new HTuple(360)).TupleRad()
             , 0.7, 1, 0.5, "least_squares", 0, 0.4, out hv_Row3, out hv_Column3, out hv_Angle, out hv_Score);
            HTuple homat2didentity = null, homat2dtranslate = null, homat2drotate = null;
            HOperatorSet.HomMat2dIdentity(out homat2didentity);
            HOperatorSet.HomMat2dTranslate(homat2didentity, hv_Row3, hv_Column3, out homat2dtranslate);
            HOperatorSet.HomMat2dRotate(homat2dtranslate, hv_Angle, hv_Row3, hv_Column3, out homat2drotate);
            HOperatorSet.AffineTransContourXld(ho_ShapeModel, out ho_ShapeModelTrans, homat2dtranslate);
            HOperatorSet.DispObj(ho_ShapeModelTrans, hwindow);
            HOperatorSet.WriteShapeModel(hv_ModelID, modelpath);
            ho_ROI.Dispose();
            ho_ImageROI.Dispose();
            ho_ShapeModelImages.Dispose();
            ho_ShapeModelRegions.Dispose();
            ho_ShapeModel.Dispose();
            ho_Image.Dispose();
            ho_Region1.Dispose();
            ho_RegionFillUp.Dispose();
            ho_RegionOpening.Dispose();
            ho_ImageReduced.Dispose();
        }
        //创建二维码模板
        public void Create2DDataCodeModel(HObject image, HTuple hwindow, string modelpath, out string sn)
        {
            // Local iconic variables 
 
            HObject ho_ROI, ho_ImageROI;
            HObject ho_SymbolXLDs = null;
            // Local control variables 
 
            HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple();
            HTuple hv_Row = null, hv_Column = null, hv_Phi = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_DataCodeType = null;
            HTuple hv_DataCodeHandle = null, hv_resultshandles = null, strings = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageROI);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
 
            //抓取图片
            HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
            //dev_close_window(...);
            //dev_open_window(...);
            DisplayImage(image, hwindow);
            HOperatorSet.SetColor(hwindow, "red");
            HOperatorSet.DrawRectangle2(hwindow, out hv_Row, out hv_Column,
                out hv_Phi, out hv_Length1, out hv_Length2);
            ho_ROI.Dispose();
            HOperatorSet.GenRectangle2(out ho_ROI, hv_Row, hv_Column, hv_Phi, hv_Length1,
                hv_Length2);
            ho_ImageROI.Dispose();
            HOperatorSet.ReduceDomain(image, ho_ROI, out ho_ImageROI);
            hv_DataCodeType = new HTuple();
            hv_DataCodeType[0] = "Data Matrix ECC 200";
            hv_DataCodeType[1] = "QR Code";
            hv_DataCodeType[2] = "Micro QR Code";
            hv_DataCodeType[3] = "PDF417";
            hv_DataCodeType[4] = "Aztec Code";
            hv_DataCodeType[5] = "GS1 DataMatrix";
            hv_DataCodeType[6] = "GS1 QR Code";
            hv_DataCodeType[7] = "GS1 Aztec Code";
            HOperatorSet.CreateDataCode2dModel(hv_DataCodeType.TupleSelect(0), new HTuple(),
                new HTuple(), out hv_DataCodeHandle);
            HOperatorSet.FindDataCode2d(ho_ImageROI, out ho_SymbolXLDs, hv_DataCodeHandle, "train", "all", out hv_resultshandles, out strings);
            HOperatorSet.DispObj(ho_SymbolXLDs, hwindow);
            HOperatorSet.WriteDataCode2dModel(hv_DataCodeHandle, modelpath + "\\ModelDataCode.dcm");
            sn = strings.ToString();
            ho_ROI.Dispose();
            ho_ImageROI.Dispose();
            ho_SymbolXLDs.Dispose();
        }
        //上料模板匹配
        public void SearchLoadModel(HObject image, string modelPath, ref HTuple totalScore, ref HTuple totalScore1)
        {
            // Local iconic variables 
            HObject ho_ShapeModel, ho_Batteries = null;
            HObject ho_Battery = null, ho_Rectangle = null, ho_ImageReduced = null;
            // Local control variables 
 
            HTuple hv_ModelID = null;
            HTuple hv_TotalScore = new HTuple();
            HTuple hv_TotalScore1 = new HTuple(), hv_Width1 = new HTuple();
            HTuple hv_Height1 = new HTuple(), hv_LowThreshold = new HTuple();
            HTuple hv_HighThreshold = new HTuple(), hv_Index1 = new HTuple();
            HTuple hv_BatteryArea = new HTuple(), hv_CenterRow = new HTuple();
            HTuple hv_CenterColumn = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_rect_row = new HTuple(), hv_rect_column = new HTuple();
            HTuple hv_rect_phi = new HTuple(), hv_rect_length1 = new HTuple();
            HTuple hv_rect_length2 = new HTuple(), hv_Row3 = new HTuple();
            HTuple hv_Column3 = new HTuple(), hv_Angle = new HTuple();
            HTuple hv_Score = new HTuple(), hv_ModelID1 = new HTuple();
            HTuple hv_Score1 = new HTuple();
            HTuple hv_Number = new HTuple(), hv_BatteryStatus = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ShapeModel);
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
 
            try
            {
                HOperatorSet.ReadShapeModel(modelPath + "ModelThreeRects.shm", out hv_ModelID);
                HOperatorSet.ReadShapeModel(modelPath + "ModelTriangle.shm", out hv_ModelID1);
                ho_Batteries.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Batteries);
                hv_TotalScore = new HTuple();
                hv_TotalScore1 = new HTuple();
                hv_LowThreshold = 0;
                hv_HighThreshold = 120;
                ho_Batteries.Dispose();
                SelectBattery(image, out ho_Batteries, hv_LowThreshold, hv_HighThreshold, out hv_BatteryStatus);
                HOperatorSet.CountObj(ho_Batteries, out hv_Number);
                for (hv_Index1 = 1; (int)hv_Index1 <= hv_Number; hv_Index1 = (int)hv_Index1 + 1)
                {
                    ho_Battery.Dispose();
                    HOperatorSet.SelectObj(ho_Batteries, out ho_Battery, hv_Index1);
                    HOperatorSet.AreaCenter(ho_Battery, out hv_BatteryArea, out hv_CenterRow,
                        out hv_CenterColumn);
                    if ((int)(new HTuple(hv_BatteryArea.TupleEqual(0))) != 0)
                    {
                        continue;
                    }
                    HOperatorSet.SmallestRectangle2(ho_Battery, out hv_Row2, out hv_Column2,
                        out hv_Phi, out hv_Length1, out hv_Length2);
                    hv_rect_row = hv_CenterRow.Clone();
                    hv_rect_column = hv_CenterColumn + 0.8 * hv_Length2;
                    hv_rect_phi = hv_Phi.Clone();
                    hv_rect_length1 = 0.5 * (hv_Length1 - 10);
                    hv_rect_length2 = hv_Length2 - 20;
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_rect_row, hv_rect_column,
                        hv_rect_phi, hv_rect_length1, hv_rect_length2);
                    ho_ImageReduced.Dispose();
                    HOperatorSet.ReduceDomain(image, ho_Rectangle, out ho_ImageReduced);
                    HOperatorSet.FindShapeModel(ho_ImageReduced, hv_ModelID, 0, (new HTuple(360)).TupleRad()
                        , 0.5, 1, 0.5, "least_squares", 0, 0.4, out hv_Row3, out hv_Column3,
                        out hv_Angle, out hv_Score);
                    hv_TotalScore = hv_TotalScore.TupleConcat(hv_Score);
                    if ((int)(new HTuple(hv_Score.TupleEqual(new HTuple()))) != 0)
                    {
                        ho_ShapeModel.Dispose();
                        //HOperatorSet.GetShapeModelContours(out ho_ShapeModel, hv_ModelID1, 1);
                        HOperatorSet.FindShapeModel(ho_ImageReduced, hv_ModelID1, 0, (new HTuple(360)).TupleRad()
                            , 0.5, 1, 0.5, "least_squares", 0, 0.4, out hv_Row3, out hv_Column3,
                            out hv_Angle, out hv_Score1);
                        hv_TotalScore1 = hv_TotalScore1.TupleConcat(hv_Score1);
                    }
                }
                totalScore = hv_TotalScore;
                totalScore1 = hv_TotalScore1;
            }
            catch (HalconException)
            {
                ho_ShapeModel.Dispose();
                ho_Batteries.Dispose();
                ho_Battery.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
            }
            ho_ShapeModel.Dispose();
            ho_Batteries.Dispose();
            ho_Battery.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageReduced.Dispose();
        }
        //可以显示
        public void SearchDataCode(string modelpath, HObject image, HTuple hwindow, ref string sn)
        {
            // Local iconic variables 
            HObject ho_Region, ho_ImageReduced = null;
            HObject ho_SymbolXLDs = null;
            // Local control variables 
 
            HTuple hv_DataCodeHandle = null;
            HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();
            HTuple hv_WindowHandle = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
 
            try
            {
                ho_Region.Dispose();
                HOperatorSet.ReadDataCode2dModel(modelpath, out hv_DataCodeHandle);
                DisplayImage(image, hwindow);
                ho_SymbolXLDs.Dispose();
                HOperatorSet.FindDataCode2d(image, out ho_SymbolXLDs, hv_DataCodeHandle,
                    "train", "all", out hv_ResultHandles, out hv_DecodedDataStrings);
                HOperatorSet.SetColor(hwindow, "green");
                HOperatorSet.DispObj(ho_SymbolXLDs, hwindow);
                string s = "N/A";
                sn = ((int)(new HTuple(hv_DecodedDataStrings.TupleEqual(new HTuple()))) != 0) ? s : (hv_DecodedDataStrings.ToString());
            }
            catch (HalconException)
            {
                sn = "N/A";
                HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_SymbolXLDs.Dispose();
            }
            HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
            ho_Region.Dispose();
            ho_ImageReduced.Dispose();
            ho_SymbolXLDs.Dispose();
        }
        //不显示
        public void SearchDataCode(string modelpath, HObject image, ref string sn)
        {
            // Local iconic variables 
            HObject ho_Region, ho_ImageReduced = null, ho_Rectangle;
            HObject ho_SymbolXLDs = null;
            // Local control variables 
 
            HTuple hv_DataCodeHandle = null;
            HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();
            HTuple hv_WindowHandle = new HTuple();
            HTuple hv_Row1 = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_Column2 = new HTuple();
 
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
 
            try
            {
                ho_Region.Dispose();
 
                hv_Row1 = 0;
                hv_Column1 = 0;
                hv_Row2 = 1000;
                hv_Column2 = 1000;
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2,
                    hv_Column2);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(image, ho_Rectangle, out ho_ImageReduced);
 
                HOperatorSet.ReadDataCode2dModel(modelpath, out hv_DataCodeHandle);
                ho_SymbolXLDs.Dispose();
                HOperatorSet.FindDataCode2d(ho_ImageReduced, out ho_SymbolXLDs, hv_DataCodeHandle,
                    "train", "all", out hv_ResultHandles, out hv_DecodedDataStrings);
                string s = "N/A";
                sn = ((int)(new HTuple(hv_DecodedDataStrings.TupleEqual(new HTuple()))) != 0) ? s : (hv_DecodedDataStrings.ToString());
 
                // Halcon 12
                if (sn.Length == 19)
                {
                    sn = sn.Substring(1, 17);
                }
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_SymbolXLDs.Dispose();
                ho_Rectangle.Dispose();
                HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
            }
            catch (HalconException)
            {
                ;
            }
        }
        //Gocator测电池高度
        public void MeasureHeight1(HObject image, double baseHeight, ref double[] height)
        {
            // Local iconic variables 
 
            HObject ho_HeightMap = null, ho_Intensity = null;
            HObject ho_Z = null, ho_ImageMean = null, ho_Regions = null, ho_RegionOpening = null;
            HObject ho_ConnectedRegions1 = null, ho_Battery = null, ho_RegionFillUp = null;
            HObject ho_ROI = null, ho_ImageReduced = null, ho_Region = null, ho_SelectedRegions = null;
            // Local control variables 
 
            HTuple hv_base_height = null;
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_frameCount = new HTuple(), hv_timestamp = new HTuple();
            HTuple hv_encoderPosition = new HTuple(), hv_encoderIndex = new HTuple();
            HTuple hv_inputs = new HTuple(), hv_xOffset = new HTuple();
            HTuple hv_xResolution = new HTuple(), hv_yOffset = new HTuple();
            HTuple hv_yResolution = new HTuple(), hv_zOffset = new HTuple();
            HTuple hv_zResolution = new HTuple(), hv_width = new HTuple();
            HTuple hv_height = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Indices = new HTuple(), hv_Number = new HTuple();
            HTuple hv_z = new HTuple(), hv_erosionSize = new HTuple();
            HTuple hv_thresholdSize = new HTuple(), hv_Index1 = new HTuple();
            HTuple hv_Rows = new HTuple(), hv_Columns = new HTuple();
            HTuple hv_Arch = new HTuple(), hv_Grayvals = new HTuple();
            HTuple hv_zOffsetInt = new HTuple(), hv_Grayval = null, hv_Rows1 = new HTuple(), hv_Columns1 = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_HeightMap);
            HOperatorSet.GenEmptyObj(out ho_Intensity);
            HOperatorSet.GenEmptyObj(out ho_Z);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
 
            hv_base_height = baseHeight;
            try
            {
                HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                Go2GenTL_ParseData(image, out ho_HeightMap, out ho_Intensity, out hv_frameCount,
                    out hv_timestamp, out hv_encoderPosition, out hv_encoderIndex, out hv_inputs,
                    out hv_xOffset, out hv_xResolution, out hv_yOffset, out hv_yResolution,
                    out hv_zOffset, out hv_zResolution, out hv_width, out hv_height);
                //
                ho_Z.Dispose();
                HOperatorSet.ConvertImageType(ho_HeightMap, out ho_Z, "real");
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_Intensity, out ho_ImageMean, 15, 15);
                ho_Regions.Dispose();
                HOperatorSet.Threshold(ho_ImageMean, out ho_Regions, 2000, 80000);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Regions, out ho_RegionOpening, 100, 40);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1);
                HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.TupleSort(hv_Area, out hv_Indices);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions, "area",
                    "and", 15000,//hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 2)) - 10,
                    1000000);//hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 1)) + 10);
                HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
                ho_Battery.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Battery);
                hv_z = new HTuple();
                hv_erosionSize = new HTuple();
                hv_erosionSize[0] = 20;
                hv_erosionSize[1] = 100;
                hv_thresholdSize = new HTuple();
                hv_thresholdSize[0] = 10000;
                hv_thresholdSize[1] = 10000;
                for (hv_Index1 = 1; (int)hv_Index1 <= 2; hv_Index1 = (int)hv_Index1 + 1)
                {
                    try
                    {
                        ho_Battery.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedRegions, out ho_Battery, hv_Index1);
                        ho_RegionFillUp.Dispose();
                        HOperatorSet.FillUp(ho_Battery, out ho_RegionFillUp);
                        ho_ROI.Dispose();
                        HOperatorSet.ErosionRectangle1(ho_RegionFillUp, out ho_ROI, hv_erosionSize.TupleSelect(
                            hv_Index1 - 1), hv_erosionSize.TupleSelect(hv_Index1 - 1));
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_Intensity, ho_ROI, out ho_ImageReduced
                            );
                        HOperatorSet.GetRegionPoints(ho_ROI, out hv_Rows1, out hv_Columns1);
                        hv_Grayval = new HTuple();
                        HOperatorSet.GetGrayval(ho_ImageReduced, hv_Rows1, hv_Columns1, out hv_Grayval);
                        ho_Region.Dispose();
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0.85 * (hv_Grayval.TupleMax()), 100000);
                        HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns);
                        HOperatorSet.GetSystem("halcon_arch", out hv_Arch);
                        HOperatorSet.GetGrayval(ho_Z, hv_Rows, hv_Columns, out hv_Grayvals);
                        if ((int)(new HTuple(hv_Arch.TupleEqual("x64-win64"))) != 0)
                        {
                            hv_zOffsetInt = (hv_zOffset.TupleSelect(0)) + (((hv_zOffset.TupleSelect(1))).TupleLsh(
                                32));
                        }
                        else
                        {
                            hv_zOffsetInt = hv_zOffset[0];
                        }
                        hv_height = (((hv_Grayvals * (hv_zResolution.TupleSelect(0))) * 0.000001) + (hv_zOffsetInt * 0.000001)) + hv_base_height;
                        hv_z = hv_z.TupleConcat(hv_height.TupleMean());
                    }
                    catch (HalconException)
                    {
                        hv_z[hv_Index1 - 1] = 999.999;
                    }
                }
                height[0] = hv_z[0].D;
                height[1] = hv_z[1].D;
            }
            catch (HalconException)
            {
                height[0] = 999.999;
                height[1] = 999.999;
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                ho_Z.Dispose();
                ho_ImageMean.Dispose();
                ho_Regions.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Battery.Dispose();
                ho_RegionFillUp.Dispose();
                ho_ROI.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
            }
            ho_HeightMap.Dispose();
            ho_Intensity.Dispose();
            ho_Z.Dispose();
            ho_ImageMean.Dispose();
            ho_Regions.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Battery.Dispose();
            ho_RegionFillUp.Dispose();
            ho_ROI.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
        }
 
        /*
        public void MeasureHeight(HObject image, double baseHeight, ref double[] height)
        {
            // Local iconic variables 
 
            HObject ho_HeightMap = null, ho_Intensity = null;
            HObject ho_Z = null, ho_ImageMean = null, ho_Regions = null, ho_RegionOpening = null;
            HObject ho_ConnectedRegions1 = null, ho_SelectedRegions = null;
            HObject ho_SortedRegions = null, ho_Battery = null, ho_RegionFillUp = null;
            HObject ho_ROI = null, ho_ImageReduced = null, ho_Region = null;
 
 
            // Local control variables 
 
            HTuple hv_base_height = null;
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_frameCount = new HTuple(), hv_timestamp = new HTuple();
            HTuple hv_encoderPosition = new HTuple(), hv_encoderIndex = new HTuple();
            HTuple hv_inputs = new HTuple(), hv_xOffset = new HTuple();
            HTuple hv_xResolution = new HTuple(), hv_yOffset = new HTuple();
            HTuple hv_yResolution = new HTuple(), hv_zOffset = new HTuple();
            HTuple hv_zResolution = new HTuple(), hv_width = new HTuple();
            HTuple hv_height = new HTuple(), hv_Number = new HTuple();
            HTuple hv_z = new HTuple(), hv_erosionSize = new HTuple();
            HTuple hv_thresholdSize = new HTuple(), hv_Index1 = new HTuple();
            HTuple hv_Rows1 = new HTuple(), hv_Columns1 = new HTuple();
            HTuple hv_Grayval = new HTuple(), hv_Rows = new HTuple();
            HTuple hv_Columns = new HTuple(), hv_Arch = new HTuple();
            HTuple hv_Grayvals = new HTuple(), hv_zOffsetInt = new HTuple();
            HTuple hv_Sorted = new HTuple(), hv_SortNum = new HTuple(), hv_DeleteLowNum = new HTuple(), hv_DeleteHighNum = new HTuple();
 
            HTuple hv_Sequence = new HTuple(), hv_NewSortNum = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_HeightMap);
            HOperatorSet.GenEmptyObj(out ho_Intensity);
            HOperatorSet.GenEmptyObj(out ho_Z);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
 
            hv_base_height = baseHeight;
            try
            {
                HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                Go2GenTL_ParseData(image, out ho_HeightMap, out ho_Intensity, out hv_frameCount,
                    out hv_timestamp, out hv_encoderPosition, out hv_encoderIndex, out hv_inputs,
                    out hv_xOffset, out hv_xResolution, out hv_yOffset, out hv_yResolution,
                    out hv_zOffset, out hv_zResolution, out hv_width, out hv_height);
                //
                ho_Z.Dispose();
                HOperatorSet.ConvertImageType(ho_HeightMap, out ho_Z, "real");
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_Intensity, out ho_ImageMean, 5, 5);
                ho_Regions.Dispose();
                int _measureHeightThresholdLowNum = 4000;
                HOperatorSet.Threshold(ho_ImageMean, out ho_Regions, _measureHeightThresholdLowNum, 80000);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Regions, out ho_RegionOpening, _measureHeightOpeningNumL, 30);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions, "area",
                    "and", _measureHeightSelectNumLow, 10000000);
 
                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "first_point",
                    _gocatorDirection, "row");
                HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
                ho_Battery.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Battery);
                hv_z = new HTuple();
                hv_erosionSize = new HTuple();
                hv_erosionSize[0] = 200;
                hv_erosionSize[1] = 20;
                hv_thresholdSize = new HTuple();
                hv_thresholdSize[0] = 40000;
                hv_thresholdSize[1] = 18000;
                for (hv_Index1 = 1; (int)hv_Index1 <= 2; hv_Index1 = (int)hv_Index1 + 1)
                {
                    try
                    {
                        ho_Battery.Dispose();
                        HOperatorSet.SelectObj(ho_SortedRegions, out ho_Battery, hv_Index1);
                        ho_RegionFillUp.Dispose();
                        HOperatorSet.FillUp(ho_Battery, out ho_RegionFillUp);
                        ho_ROI.Dispose();
                        HOperatorSet.ErosionRectangle1(ho_RegionFillUp, out ho_ROI, hv_erosionSize.TupleSelect(
                            hv_Index1 - 1), hv_erosionSize.TupleSelect(hv_Index1 - 1));
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_Intensity, ho_ROI, out ho_ImageReduced);
                        HOperatorSet.GetRegionPoints(ho_ROI, out hv_Rows1, out hv_Columns1);
                        hv_Grayval = new HTuple();
                        HOperatorSet.GetGrayval(ho_ImageReduced, hv_Rows1, hv_Columns1, out hv_Grayval);
                        ho_Region.Dispose();
                        //Adam2017.4.30修改
                        //HOperatorSet.GenRectangle2(out ho_Region, 472.469, 264.696, (new HTuple(0)).TupleRad(), 226.738, 377.07);
                        
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0.3 * (hv_Grayval.TupleMax()), 1000000);
                        
                        HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns);
                        HOperatorSet.GetSystem("halcon_arch", out hv_Arch);
                        HOperatorSet.GetGrayval(ho_Z, hv_Rows, hv_Columns, out hv_Grayvals);
                        
                        
                        //Adam 2018.1.20添加(去除数组中的极大值和极小值)
                        HOperatorSet.TupleSort(hv_Grayvals, out hv_Sorted);
                        hv_SortNum = new HTuple(hv_Sorted.TupleLength());
                        hv_DeleteLowNum = hv_SortNum * 0.45;
                        hv_DeleteHighNum = hv_SortNum * 0.55;
                        HOperatorSet.TupleInt(hv_DeleteLowNum, out hv_DeleteLowNum);
                        HOperatorSet.TupleInt(hv_DeleteHighNum, out hv_DeleteHighNum);
                        //选择tuple数据,剔除tuple前面的5%和tuple后面的5%,目前还没有找到合适的算子
                        HOperatorSet.TupleGenSequence(hv_DeleteLowNum, hv_DeleteHighNum, 1, out hv_Sequence);
                        HOperatorSet.TupleSelect(hv_Sorted, hv_Sequence, out hv_NewSortNum);
                        /////////////////////////////////////////////////////////////////////////////////////////////end
 
 
                        if ((int)(new HTuple(hv_Arch.TupleEqual("x64-win64"))) != 0)
                        {
                            hv_zOffsetInt = (hv_zOffset.TupleSelect(0)) + (((hv_zOffset.TupleSelect(1))).TupleLsh(
                                32));
                        }
                        else
                        {
                            hv_zOffsetInt = hv_zOffset[0];
                        }
                        hv_height = (((hv_NewSortNum * (hv_zResolution.TupleSelect(0))) * 0.000001) + (hv_zOffsetInt * 0.000001)) + hv_base_height;
                        hv_z = hv_z.TupleConcat(hv_height.TupleMean());
                    }
                    catch (HalconException)
                    {
                        hv_z[hv_Index1 - 1] = 999.999;
                    }
                }
 
                //Shawn添加判断
                if (hv_z[0] == 999.999 && hv_z[1] != 999.999)
                    hv_z[0] = hv_z[1] - 0.2 * CommonUtil.random.NextDouble();
                if (hv_z[1] == 999.999 && hv_z[0] != 999.999)
                    hv_z[1] = hv_z[0] + 0.2 * CommonUtil.random.NextDouble();
 
                height[0] = hv_z[0].D + 4.5705;
                height[1] = hv_z[1].D - 0.45 + 4.5705;
            }
            catch (HalconException)
            {
                height[0] = 999.999;
                height[1] = 999.999;
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                ho_Z.Dispose();
                ho_ImageMean.Dispose();
                ho_Regions.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_Battery.Dispose();
                ho_RegionFillUp.Dispose();
                ho_ROI.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
            }
            ho_HeightMap.Dispose();
            ho_Intensity.Dispose();
            ho_Z.Dispose();
            ho_ImageMean.Dispose();
            ho_Regions.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            ho_SortedRegions.Dispose();
            ho_Battery.Dispose();
            ho_RegionFillUp.Dispose();
            ho_ROI.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
        }
        */
        //图像显示
        //2018.10.12Adam添加
        public void MeasureHeight(HObject image, double baseHeight, ref double[] height)
        {
            // Local iconic variables 
 
            HObject ho_HeightMap = null, ho_Intensity = null;
            HObject ho_Z = null, ho_ImageMean = null, ho_Regions = null, ho_RegionOpening = null;
            HObject ho_ConnectedRegions1 = null, ho_SelectedRegions = null;
            HObject ho_SortedRegions = null, ho_Battery = null, ho_RegionFillUp = null;
            HObject ho_ROI = null, ho_ImageReduced = null, ho_Region = null;
 
 
            // Local control variables 
 
            HTuple hv_base_height = null;
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_frameCount = new HTuple(), hv_timestamp = new HTuple();
            HTuple hv_encoderPosition = new HTuple(), hv_encoderIndex = new HTuple();
            HTuple hv_inputs = new HTuple(), hv_xOffset = new HTuple();
            HTuple hv_xResolution = new HTuple(), hv_yOffset = new HTuple();
            HTuple hv_yResolution = new HTuple(), hv_zOffset = new HTuple();
            HTuple hv_zResolution = new HTuple(), hv_width = new HTuple();
            HTuple hv_height = new HTuple(), hv_Number = new HTuple();
            HTuple hv_z = new HTuple(), hv_erosionSize = new HTuple();
            HTuple hv_thresholdSize = new HTuple(), hv_Index1 = new HTuple();
            HTuple hv_Rows1 = new HTuple(), hv_Columns1 = new HTuple();
            HTuple hv_Grayval = new HTuple(), hv_Rows = new HTuple();
            HTuple hv_Columns = new HTuple(), hv_Arch = new HTuple();
            HTuple hv_Grayvals = new HTuple(), hv_zOffsetInt = new HTuple();
            HTuple hv_Sorted = new HTuple(), hv_SortNum = new HTuple(), hv_DeleteLowNum = new HTuple(), hv_DeleteHighNum = new HTuple();
 
            HTuple hv_Sequence = new HTuple(), hv_NewSortNum = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_HeightMap);
            HOperatorSet.GenEmptyObj(out ho_Intensity);
            HOperatorSet.GenEmptyObj(out ho_Z);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
 
            hv_base_height = baseHeight;
            try
            {
                HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                Go2GenTL_ParseData(image, out ho_HeightMap, out ho_Intensity, out hv_frameCount,
                    out hv_timestamp, out hv_encoderPosition, out hv_encoderIndex, out hv_inputs,
                    out hv_xOffset, out hv_xResolution, out hv_yOffset, out hv_yResolution,
                    out hv_zOffset, out hv_zResolution, out hv_width, out hv_height);
                //
                ho_Z.Dispose();
                HOperatorSet.ConvertImageType(ho_HeightMap, out ho_Z, "real");
                ho_ImageMean.Dispose();
                //将ho_Intensity修改为ho_Z
                HOperatorSet.MeanImage(ho_Z, out ho_ImageMean, 5, 5);
                ho_Regions.Dispose();
                int _measureHeightThresholdLowNum = 4000;
                HOperatorSet.Threshold(ho_ImageMean, out ho_Regions, _measureHeightThresholdLowNum, 80000);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Regions, out ho_RegionOpening, _measureHeightOpeningNumL, 30);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions, "area",
                    "and", _measureHeightSelectNumLow, 10000000);
 
                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "first_point",
                    _gocatorDirection, "row");
                HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
                ho_Battery.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Battery);
                hv_z = new HTuple();
                hv_erosionSize = new HTuple();
                hv_erosionSize[0] = 100;
                hv_erosionSize[1] = 20;
                hv_thresholdSize = new HTuple();
                hv_thresholdSize[0] = 50000;
                hv_thresholdSize[1] = 18000;
                for (hv_Index1 = 1; (int)hv_Index1 <= hv_Number; hv_Index1 = (int)hv_Index1 + 1)
                {
                    try
                    {
                        ho_Battery.Dispose();
                        HOperatorSet.SelectObj(ho_SortedRegions, out ho_Battery, hv_Index1);
                        ho_RegionFillUp.Dispose();
                        HOperatorSet.FillUp(ho_Battery, out ho_RegionFillUp);
                        ho_ROI.Dispose();
                        HOperatorSet.ErosionRectangle1(ho_RegionFillUp, out ho_ROI, hv_erosionSize.TupleSelect(
                            hv_Index1 - 1), hv_erosionSize.TupleSelect(hv_Index1 - 1));
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_Intensity, ho_ROI, out ho_ImageReduced);
                        HOperatorSet.GetRegionPoints(ho_ROI, out hv_Rows1, out hv_Columns1);
                        hv_Grayval = new HTuple();
                        HOperatorSet.GetGrayval(ho_ImageReduced, hv_Rows1, hv_Columns1, out hv_Grayval);
                        ho_Region.Dispose();
                        //Adam2017.4.30修改
                        //HOperatorSet.GenRectangle2(out ho_Region, 472.469, 264.696, (new HTuple(0)).TupleRad(), 226.738, 377.07);
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0.3 * (hv_Grayval.TupleMax()), 1000000);
                        HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns);
                        HOperatorSet.GetSystem("halcon_arch", out hv_Arch);
                        HOperatorSet.GetGrayval(ho_Z, hv_Rows, hv_Columns, out hv_Grayvals);
 
                        //Adam 2018.1.20添加(去除数组中的极大值和极小值)
                        HOperatorSet.TupleSort(hv_Grayvals, out hv_Sorted);
                        hv_SortNum = new HTuple(hv_Sorted.TupleLength());
                        hv_DeleteLowNum = hv_SortNum * 0.45;
                        hv_DeleteHighNum = hv_SortNum * 0.55;
                        HOperatorSet.TupleInt(hv_DeleteLowNum, out hv_DeleteLowNum);
                        HOperatorSet.TupleInt(hv_DeleteHighNum, out hv_DeleteHighNum);
                        //选择tuple数据,剔除tuple前面的5%和tuple后面的5%,目前还没有找到合适的算子
                        HOperatorSet.TupleGenSequence(hv_DeleteLowNum, hv_DeleteHighNum, 1, out hv_Sequence);
                        HOperatorSet.TupleSelect(hv_Sorted, hv_Sequence, out hv_NewSortNum);
                        /////////////////////////////////////////////////////////////////////////////////////////////end
                        if ((int)(new HTuple(hv_Arch.TupleEqual("x64-win64"))) != 0)
                        {
                            hv_zOffsetInt = (hv_zOffset.TupleSelect(0)) + (((hv_zOffset.TupleSelect(1))).TupleLsh(32));
                        }
                        else
                        {
                            hv_zOffsetInt = hv_zOffset[0];
                        }
                        hv_height = (((hv_NewSortNum * (hv_zResolution.TupleSelect(0))) * 0.000001) + (hv_zOffsetInt * 0.000001)) + hv_base_height;
                        hv_z = hv_z.TupleConcat(hv_height.TupleMean());
                    }
                    catch (HalconException)
                    {
                        hv_z[hv_Index1 - 1] = 999.999;
                    }
                }
 
                //Shawn添加判断
                /*
                if (hv_z[0] == 999.999 && hv_z[1] != 999.999)
                    hv_z[0] = hv_z[1] - 0.2 * CommonUtil.random.NextDouble();
                if (hv_z[1] == 999.999 && hv_z[0] != 999.999)
                    hv_z[1] = hv_z[0] + 0.2 * CommonUtil.random.NextDouble();
                */
 
                height[0] = hv_z[0].D;
                //当hv_z.Length == 1时,测试标准块;当hv_z.Length > 1时,测试电池。
                height[1] = hv_z.Length > 1 ? hv_z[1].D : 999.999;
            }
            catch (HalconException)
            {
                height[0] = hv_z[0].D;
                height[1] = hv_z.Length > 1 ? hv_z[1].D : 999.999;
                ho_HeightMap.Dispose();
                ho_Intensity.Dispose();
                ho_Z.Dispose();
                ho_ImageMean.Dispose();
                ho_Regions.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_Battery.Dispose();
                ho_RegionFillUp.Dispose();
                ho_ROI.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
            }
            ho_HeightMap.Dispose();
            ho_Intensity.Dispose();
            ho_Z.Dispose();
            ho_ImageMean.Dispose();
            ho_Regions.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            ho_SortedRegions.Dispose();
            ho_Battery.Dispose();
            ho_RegionFillUp.Dispose();
            ho_ROI.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
        }
 
        public static void DisplayImage(HObject image, HTuple hwindow)
        {
            HTuple imageheight = new HTuple(), imagewidth = new HTuple();
            HOperatorSet.GetImageSize(image, out imagewidth, out imageheight);
            HOperatorSet.SetPart(hwindow, 0, 0, imageheight - 1, imagewidth - 1);
            HOperatorSet.DispObj(image, hwindow);
        }
        //读取图像
        public static void ReadImage(string imagePath, out HObject image)
        {
            HOperatorSet.ReadImage(out image, (HTuple)imagePath);
        }
        //同步抓拍图像
        public static void GrabImage(HTuple acqHandle, out HObject image)
        {
            //拍照
            try
            {
                HOperatorSet.GrabImage(out image, acqHandle);
            }
            catch (Exception)
            {
                image = null;
            }
        }
 
        public static void myExcel(string path, string text)
        {
            if (!File.Exists(path)) Directory.CreateDirectory(path);
            path += "\\" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
            if (!File.Exists(path))
            {
                string str = "Time,Condition" + ",\r\n";
                File.AppendAllText(path, str);
            }
            File.AppendAllText(path, text);
        }
 
        public static void GrabImageAsync(HTuple acqHandle, HTuple MaxDelay, out HObject image)
        {
            HOperatorSet.GrabImageAsync(out image, acqHandle, MaxDelay);
 
        }
        //图像原始图像保存
        public static string SaveImage(HObject image, string imageType, string directoryPath)
        {
            //string path = "D:\\图像1\\" + DateTime.Now.ToString("yyyyMMdd") + path1;
            if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
            string time = DateTime.Now.ToString("HHmmssfff");
            HOperatorSet.WriteImage(image, imageType, 0, directoryPath + "\\" + time);
            return directoryPath + "\\" + time + "." + imageType;
        }
 
        public static string SaveImageAs(HObject image, string fileName)
        {
            string imageType = "png";
            try
            {
                HOperatorSet.WriteImage(image, imageType, 0, fileName);
 
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, "保存图片失败:" + ex.ToString());
            }
            return fileName + "." + imageType;
        }
 
        public static void SaveImage(HObject image, string imageType, string directoryPath, int imageIndex)
        {
            if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
            HOperatorSet.WriteImage(image, imageType, 0, directoryPath + "\\" + imageIndex.ToString());
        }
        //保存窗口检测截图
        public static string SaveWindowImage(HTuple hwindow, string imageType, string path)
        {
            string fileName = string.Empty;
            HObject image = new HObject();
            HOperatorSet.DumpWindowImage(out image, hwindow);
            fileName = SaveImage(image, imageType, path);
            image.Dispose();
            return fileName;
        }
 
        public static string SaveWindowImageAs(HTuple hwindow, string fileName)
        {
            HObject image = new HObject();
            HOperatorSet.DumpWindowImage(out image, hwindow);
            string fn = SaveImageAs(image, fileName);
            image.Dispose();
            return fn;
        }
 
        //保存相机内参
        public void SaveCamParam(HTuple CamParam, string CamParamFileName)
        {
            //string CamParamFileName = "";
            HOperatorSet.WriteCamPar(CamParam, CamParamFileName);
            //return CamParamFileName;
        }
        //保存相机位姿
        public void SaveCamPose(HTuple CamPose, string CamPoseFileName)
        {
            //string CamPoseFileName = "";
            HOperatorSet.WritePose(CamPose, CamPoseFileName);
            //return CamPoseFileName;
        }
        //判定是否成功标定
        public void IsCaltabCalibrate(HObject Image, string CaltabName, HTuple StartCameraParam, HTuple WindowHandle,
                                      out bool IsCaltabCalibrate)
        {
            HObject ho_Caltab = new HObject();
            HTuple hv_RCoord = new HTuple(), hv_CCoord = new HTuple(), hv_StartPose = new HTuple();
            HTuple hv_SizeGauss, hv_MarkThresh, hv_MinDiamMarks, hv_StartThresh, hv_DeltaThresh, hv_MinThresh, hv_Alpha, hv_MinContLength, hv_MaxDiamMarks;
 
            HObject ho_Cross = null;
 
            HOperatorSet.GenEmptyObj(out ho_Cross);
 
            hv_SizeGauss = 3;
            hv_MarkThresh = 120;
            hv_MinDiamMarks = 5;
            hv_StartThresh = 128;
            hv_DeltaThresh = 10;
            hv_MinThresh = 18;
            hv_Alpha = 0.9;
            hv_MinContLength = 15;
            hv_MaxDiamMarks = 100;
            try
            {
                ho_Caltab.Dispose();
                HOperatorSet.FindCaltab(Image, out ho_Caltab, CaltabName, hv_SizeGauss, hv_MarkThresh, hv_MinDiamMarks);
                HOperatorSet.FindMarksAndPose(Image, ho_Caltab, CaltabName, StartCameraParam,
                        hv_StartThresh, hv_DeltaThresh, hv_MinThresh, hv_Alpha, hv_MinContLength,
                        hv_MaxDiamMarks, out hv_RCoord, out hv_CCoord, out hv_StartPose);
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RCoord, hv_CCoord, 16, 0.6);
                DisplayImage(Image, WindowHandle);
                HOperatorSet.SetDraw(WindowHandle, "margin");
                HOperatorSet.SetColor(WindowHandle, "green");
                HOperatorSet.DispObj(ho_Caltab, WindowHandle);
                HOperatorSet.SetColor(WindowHandle, "yellow");
                HOperatorSet.DispObj(ho_Cross, WindowHandle);
                //HOperatorSet.DispCaltab(WindowHandle, CaltabName, StartCameraParam, hv_StartPose, 1);
                IsCaltabCalibrate = true;
            }
            catch (HalconException)
            {
                DisplayImage(Image, WindowHandle);
                IsCaltabCalibrate = false;
            }
 
        }
        //单目相机标定及精度换算(精度为mm单位)
        public void CameraCalibrate(HTuple ImagePath, string CaltabFileName, HTuple StartCamParam, HTuple WindowHandle,
                                    ref HTuple CameraParam, ref HTuple NFinalPose, ref HTuple Error, ref HTuple Pixscale)
        {
            // Local iconic variables 
            HObject ho_Image = null, ho_Caltab = null;
            // Local control variables 
 
            HTuple hv_WindowHandle = new HTuple(), hv_CaltabFile = null;
            HTuple hv_X = null, hv_Y = null, hv_Z = null, hv_StartCamPar = null;
            HTuple hv_SizeGauss = null, hv_MarkThresh = null, hv_MinDiamMarks = null;
            HTuple hv_StartThresh = null, hv_DeltaThresh = null, hv_MinThresh = null;
            HTuple hv_Alpha = null, hv_MinContLength = null, hv_MaxDiamMarks = null;
            HTuple hv_Rows = null, hv_Cols = null, hv_StartPoses = null;
            HTuple hv_Pixscales = null, hv_Index = null;
            HTuple hv_RCoord = new HTuple(), hv_CCoord = new HTuple();
            HTuple hv_StartPose = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_Pixscale = new HTuple(), hv_CameraParam = null;
            HTuple hv_NFinalPose = null, hv_Errors = null, hv_pixScale = null;
            HObject ho_Cross = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Caltab);
 
            try
            {
                //***halcon标定板标定像素精度******
                //Read the model calibration points.
                hv_CaltabFile = CaltabFileName;
                HOperatorSet.CaltabPoints(hv_CaltabFile, out hv_X, out hv_Y, out hv_Z);
                //Set the initial values for the internal camera parameters
                //***
                //**如果是远心镜头,第一个参数为0
                hv_StartCamPar = new HTuple();
                hv_StartCamPar[0] = StartCamParam[0];
                hv_StartCamPar[1] = StartCamParam[1];
                hv_StartCamPar[2] = StartCamParam[2];
                hv_StartCamPar[3] = StartCamParam[3];
                hv_StartCamPar[4] = StartCamParam[4];
                hv_StartCamPar[5] = StartCamParam[5];
                hv_StartCamPar[6] = StartCamParam[6];
                hv_StartCamPar[7] = StartCamParam[7];
                //Parameter settings for find_caltab and find_marks_and_pose
                hv_SizeGauss = 3;
                hv_MarkThresh = 120;
                hv_MinDiamMarks = 5;
                hv_StartThresh = 128;
                hv_DeltaThresh = 10;
                hv_MinThresh = 18;
                hv_Alpha = 0.9;
                hv_MinContLength = 15;
                hv_MaxDiamMarks = 100;
                //Create the tuples in which the image coordinates of the
                //calibration marks and the initial poses will be accumulated
                hv_Rows = new HTuple();
                hv_Cols = new HTuple();
                hv_StartPoses = new HTuple();
                hv_Pixscales = new HTuple();
                //Image Acquisition 01: Code generated by Image Acquisition 01
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(ImagePath.TupleLength()
                    )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    ho_Image.Dispose();
                    HOperatorSet.ReadImage(out ho_Image, ImagePath.TupleSelect(hv_Index));
                    //Search for the calibration plate
                    ho_Caltab.Dispose();
                    HOperatorSet.FindCaltab(ho_Image, out ho_Caltab, hv_CaltabFile, hv_SizeGauss,
                        hv_MarkThresh, hv_MinDiamMarks);
                    HOperatorSet.FindMarksAndPose(ho_Image, ho_Caltab, hv_CaltabFile, hv_StartCamPar,
                        hv_StartThresh, hv_DeltaThresh, hv_MinThresh, hv_Alpha, hv_MinContLength,
                        hv_MaxDiamMarks, out hv_RCoord, out hv_CCoord, out hv_StartPose);
                    //HOperatorSet.SetColor(WindowHandle, "green");
                    //DisplayImage(ho_Image, WindowHandle);
                    //HOperatorSet.DispCaltab(WindowHandle, hv_CaltabFile, hv_StartCamPar,
                    //    hv_StartPose, 1);
 
                    HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RCoord, hv_CCoord, 16, 0.6);
                    DisplayImage(ho_Image, WindowHandle);
                    HOperatorSet.SetDraw(WindowHandle, "margin");
                    HOperatorSet.SetColor(WindowHandle, "green");
                    HOperatorSet.DispObj(ho_Caltab, WindowHandle);
                    HOperatorSet.SetColor(WindowHandle, "yellow");
                    HOperatorSet.DispObj(ho_Cross, WindowHandle);
 
                    hv_Rows = hv_Rows.TupleConcat(hv_RCoord);
                    hv_Cols = hv_Cols.TupleConcat(hv_CCoord);
                    hv_StartPoses = hv_StartPoses.TupleConcat(hv_StartPose);
                    HOperatorSet.DistancePp(hv_RCoord.TupleSelect(0), hv_CCoord.TupleSelect(0),
                        hv_RCoord.TupleSelect(6), hv_CCoord.TupleSelect(6), out hv_Distance);
                    hv_Pixscale = ((hv_X.TupleSelect(6)) - (hv_X.TupleSelect(0))) / hv_Distance;
                    hv_Pixscales = hv_Pixscales.TupleConcat(hv_Pixscale);
                }
                //Perform the actual calibration
                HOperatorSet.CameraCalibration(hv_X, hv_Y, hv_Z, hv_Rows, hv_Cols, hv_StartCamPar,
                    hv_StartPoses, "all", out hv_CameraParam, out hv_NFinalPose, out hv_Errors);
                hv_pixScale = hv_Pixscales.TupleMean();
 
                CameraParam = hv_CameraParam;
                NFinalPose = hv_NFinalPose;
                Error = hv_Errors;
                Pixscale = hv_pixScale[0] * 1000;
            }
            catch (HalconException)
            {
                ho_Image.Dispose();
                ho_Caltab.Dispose();
            }
            ho_Image.Dispose();
            ho_Caltab.Dispose();
        }
        //提取M*N点阵中心点
        public void ExtractDotCenters(HObject ho_Image, HTuple hv_Threshold, HTuple hv_RowCount, HTuple hv_ColumnCount, ref HTuple hv_RowCenters, ref HTuple hv_ColumnCenters)
        {
            // Local iconic variables 
 
            HObject ho_Region, ho_ConnectedRegions, ho_Rectangle = null;
            HObject ho_ImageReduced = null, ho_Region1 = null, ho_ConnectedRegions1 = null;
            HObject ho_SortedRegions = null;
 
 
            // Local control variables 
 
            HTuple hv_Area = null, hv_Row = null, hv_Column = null;
            HTuple hv_DotRadius = null, hv_RowDis = null, hv_ColumnDis = null;
            HTuple hv_Index = null, hv_Area1 = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
 
            hv_RowCenters = new HTuple();
            hv_ColumnCenters = new HTuple();
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_Image, out ho_Region, 0, hv_Threshold);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
            HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row, out hv_Column);
            hv_DotRadius = (((hv_Area.TupleSelect(0)) / 3.1415926)).TupleSqrt();
            hv_RowDis = (hv_Row.TupleSelect(4)) - (hv_Row.TupleSelect(0));
            hv_ColumnDis = (hv_Column.TupleSelect(3)) - (hv_Column.TupleSelect(0));
            HTuple end_val8 = hv_RowCount - 1;
            HTuple step_val8 = 1;
            for (hv_Index = 0; hv_Index.Continue(end_val8, step_val8); hv_Index = hv_Index.TupleAdd(step_val8))
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, ((hv_Row.TupleSelect(hv_Index * 4)) - hv_DotRadius) - 20,
                    ((hv_Column.TupleSelect(hv_Index * 4)) - hv_DotRadius) - 20, ((hv_Row.TupleSelect(
                    (hv_Index * 4) + 3)) + hv_DotRadius) + 20, ((hv_Column.TupleSelect((hv_Index * 4) + 3)) + hv_DotRadius) + 20);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, hv_Threshold);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1);
                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_ConnectedRegions1, out ho_SortedRegions, "first_point",
                    "true", "column");
                HOperatorSet.AreaCenter(ho_SortedRegions, out hv_Area1, out hv_Row1, out hv_Column1);
                hv_RowCenters = hv_RowCenters.TupleConcat(hv_Row1);
                hv_ColumnCenters = hv_ColumnCenters.TupleConcat(hv_Column1);
            }
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region1.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SortedRegions.Dispose();
 
            return;
        }
        //判断点阵中心是否成功提取
        public void isDotCalibrate(HObject Image, HTuple Threshold, HTuple hv_RowCount, HTuple hv_ColumnCount, ref HTuple RowCenters, ref HTuple ColumnCenters, out bool isDotCalibrate)
        {
            try
            {
                ExtractDotCenters(Image, Threshold, hv_RowCount, hv_ColumnCount, ref RowCenters, ref ColumnCenters);
                isDotCalibrate = true;
            }
            catch (HalconException)
            {
                isDotCalibrate = false;
            }
        }
 
        //九宫格标定中心坐标
        public void NineCalibrate(HObject Image, HTuple Threshold, ref HTuple RowCenters, ref HTuple ColumnCenters)
        {
            // Local iconic variables 
 
            HObject ho_StructElement, ho_Dark, ho_RegionOpening;
            HObject ho_Light, ho_RegionOpening1;
 
            // Local control variables 
 
            HTuple hv_Area = null, hv_RowDark = null, hv_ColumnDark = null;
            HTuple hv_Area1 = null, hv_RowLight = null, hv_ColumnLight = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_StructElement);
            HOperatorSet.GenEmptyObj(out ho_Dark);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_Light);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening1);
 
            ho_StructElement.Dispose();
            HOperatorSet.GenStructElements(out ho_StructElement, "noise", 2, 2);
            //**提取黑格
            ho_Dark.Dispose();
            HOperatorSet.Threshold(Image, out ho_Dark, 0, Threshold);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningSeg(ho_Dark, ho_StructElement, out ho_RegionOpening);
            HOperatorSet.AreaCenter(ho_RegionOpening, out hv_Area, out hv_RowDark, out hv_ColumnDark);
            //**提取白格
            ho_Light.Dispose();
            HOperatorSet.Threshold(Image, out ho_Light, Threshold, 255);
            ho_RegionOpening1.Dispose();
            HOperatorSet.OpeningSeg(ho_Light, ho_StructElement, out ho_RegionOpening1);
            HOperatorSet.AreaCenter(ho_RegionOpening1, out hv_Area1, out hv_RowLight, out hv_ColumnLight);
            RowCenters = new HTuple();
            RowCenters = RowCenters.TupleConcat(hv_RowDark.TupleSelect(
                0));
            RowCenters = RowCenters.TupleConcat(hv_RowLight.TupleSelect(
                0));
            RowCenters = RowCenters.TupleConcat(hv_RowDark.TupleSelect(
                1));
            RowCenters = RowCenters.TupleConcat(hv_RowLight.TupleSelect(
                1));
            RowCenters = RowCenters.TupleConcat(hv_RowDark.TupleSelect(
                2));
            RowCenters = RowCenters.TupleConcat(hv_RowLight.TupleSelect(
                2));
            RowCenters = RowCenters.TupleConcat(hv_RowDark.TupleSelect(
                3));
            RowCenters = RowCenters.TupleConcat(hv_RowLight.TupleSelect(
                3));
            RowCenters = RowCenters.TupleConcat(hv_RowDark.TupleSelect(
                4));
            ColumnCenters = new HTuple();
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnDark.TupleSelect(
                0));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnLight.TupleSelect(
                0));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnDark.TupleSelect(
                1));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnLight.TupleSelect(
                1));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnDark.TupleSelect(
                2));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnLight.TupleSelect(
                2));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnDark.TupleSelect(
                3));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnLight.TupleSelect(
                3));
            ColumnCenters = ColumnCenters.TupleConcat(hv_ColumnDark.TupleSelect(
                4));
            ho_StructElement.Dispose();
            ho_Dark.Dispose();
            ho_RegionOpening.Dispose();
            ho_Light.Dispose();
            ho_RegionOpening1.Dispose();
 
            return;
        }
        //判断九宫格是否成功标定
        public void isNineCalibrate(HObject Image, HTuple Threshold, ref HTuple RowCenters, ref HTuple ColumnCenters, out bool isNineCalibrate)
        {
            try
            {
                NineCalibrate(Image, Threshold, ref RowCenters, ref ColumnCenters);
                isNineCalibrate = true;
            }
            catch (HalconException)
            {
                isNineCalibrate = false;
            }
        }
 
        public void SelectBattery1(HObject ho_Image, out HObject ho_Batteries, HTuple hv_LowThreshold, HTuple hv_HighThreshold)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_Region1, ho_RegionFillUp, ho_ConnectedRegions;
            HObject ho_SelectedRegions, ho_Battery1 = null, ho_Battery2 = null;
 
 
            // Local control variables 
 
            HTuple hv_Area = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Area1 = null, hv_Row = null, hv_Column = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Battery1);
            HOperatorSet.GenEmptyObj(out ho_Battery2);
 
            ho_Batteries.Dispose();
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            ho_Region1.Dispose();
            HOperatorSet.Threshold(ho_Image, out ho_Region1, hv_LowThreshold, hv_HighThreshold);
            ho_RegionFillUp.Dispose();
            HOperatorSet.FillUp(ho_Region1, out ho_RegionFillUp);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
            HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row1, out hv_Column1);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                "and", 180000, 250000);
            HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area1, out hv_Row, out hv_Column);
            if ((int)(new HTuple(hv_Area1.TupleNotEqual(new HTuple()))) != 0)
            {
                ho_Battery1.Dispose();
                HOperatorSet.SelectShape(ho_SelectedRegions, out ho_Battery1, "column", "and",
                    (hv_Column.TupleMax()) - 30, (hv_Column.TupleMax()) + 30);
                HOperatorSet.ConcatObj(ho_Batteries, ho_Battery1, out OTemp[0]);
                ho_Batteries.Dispose();
                ho_Batteries = OTemp[0];
                ho_Battery2.Dispose();
                HOperatorSet.Difference(ho_SelectedRegions, ho_Battery1, out ho_Battery2);
                HOperatorSet.ConcatObj(ho_Batteries, ho_Battery2, out OTemp[0]);
                ho_Batteries.Dispose();
                ho_Batteries = OTemp[0];
            }
            ho_Region1.Dispose();
            ho_RegionFillUp.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Battery1.Dispose();
            ho_Battery2.Dispose();
 
            return;
        }
        public void SelectBattery(HObject ho_Image, out HObject ho_Batteries, HTuple hv_LowThreshold, HTuple hv_HighThreshold, out HTuple hv_BatteryStatus)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_Region1, ho_RegionFillUp, ho_ConnectedRegions;
            HObject ho_SelectedRegions, ho_Battery1 = null, ho_Battery2 = null;
 
 
            // Local control variables 
 
            HTuple hv_Width = null, hv_Height = null, hv_Area = null;
            HTuple hv_Row1 = null, hv_Column1 = null, hv_Area1 = null;
            HTuple hv_Row = null, hv_Column = null, hv_Length = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Battery1);
            HOperatorSet.GenEmptyObj(out ho_Battery2);
 
            hv_BatteryStatus = new HTuple();
            ho_Batteries.Dispose();
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_Region1.Dispose();
            HOperatorSet.Threshold(ho_Image, out ho_Region1, hv_LowThreshold, hv_HighThreshold);
            ho_RegionFillUp.Dispose();
            HOperatorSet.FillUp(ho_Region1, out ho_RegionFillUp);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
            HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row1, out hv_Column1);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                "and", 120000, 250000);
            HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area1, out hv_Row, out hv_Column);
            HOperatorSet.TupleLength(hv_Column, out hv_Length);
            if ((int)(new HTuple(hv_Length.TupleEqual(2))) != 0)
            {
                hv_BatteryStatus = 3;
            }
            else if ((int)(new HTuple(hv_Length.TupleEqual(0))) != 0)
            {
                hv_BatteryStatus = 10;
            }
            else if ((int)(new HTuple(hv_Column.TupleLess(0.5 * hv_Width))) != 0)
            {
                hv_BatteryStatus = 1;
            }
            else if ((int)(new HTuple(hv_Column.TupleGreater(0.5 * hv_Width))) != 0)
            {
                hv_BatteryStatus = 2;
            }
            if ((int)(new HTuple(hv_Area1.TupleNotEqual(new HTuple()))) != 0)
            {
                ho_Battery1.Dispose();
                HOperatorSet.SelectShape(ho_SelectedRegions, out ho_Battery1, "column", "and",
                    (hv_Column.TupleMax()) - 30, (hv_Column.TupleMax()) + 30);
                HOperatorSet.ConcatObj(ho_Batteries, ho_Battery1, out OTemp[0]);
                ho_Batteries.Dispose();
                ho_Batteries = OTemp[0];
                ho_Battery2.Dispose();
                HOperatorSet.Difference(ho_SelectedRegions, ho_Battery1, out ho_Battery2);
                HOperatorSet.ConcatObj(ho_Batteries, ho_Battery2, out OTemp[0]);
                ho_Batteries.Dispose();
                ho_Batteries = OTemp[0];
            }
            ho_Region1.Dispose();
            ho_RegionFillUp.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Battery1.Dispose();
            ho_Battery2.Dispose();
 
            return;
        }
        //halcon处理Gocator相机采集到的图像
        public void Go2GenTL_ParseData(HObject ho_Image, out HObject ho_HeightMap, out HObject ho_Intensity,
                                       out HTuple hv_FrameCount, out HTuple hv_Timestamp, out HTuple hv_EncoderPosition,
                                       out HTuple hv_EncoderIndex, out HTuple hv_Inputs, out HTuple hv_xOffset, out HTuple hv_xResolution,
                                       out HTuple hv_yOffset, out HTuple hv_yResolution, out HTuple hv_zOffset, out HTuple hv_zResolution,
                                       out HTuple hv_Width, out HTuple hv_Length)
        {
            // Local iconic variables 
            HObject ho_Stamps;
            // Local control variables 
            HTuple hv_ChannelCount = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_HeightMap);
            HOperatorSet.GenEmptyObj(out ho_Intensity);
            HOperatorSet.GenEmptyObj(out ho_Stamps);
 
            HOperatorSet.CountChannels(ho_Image, out hv_ChannelCount);
            ho_HeightMap.Dispose();
            ho_Intensity.Dispose();
            ho_Stamps.Dispose();
            HOperatorSet.Decompose3(ho_Image, out ho_HeightMap, out ho_Intensity, out ho_Stamps
                );
            Go2GenTL_Stamp(ho_Stamps, 1, out hv_FrameCount);
            Go2GenTL_Stamp(ho_Stamps, 2, out hv_Timestamp);
            Go2GenTL_Stamp(ho_Stamps, 3, out hv_EncoderPosition);
            Go2GenTL_Stamp(ho_Stamps, 4, out hv_EncoderIndex);
            Go2GenTL_Stamp(ho_Stamps, 5, out hv_Inputs);
            Go2GenTL_Stamp(ho_Stamps, 6, out hv_xOffset);
            Go2GenTL_Stamp(ho_Stamps, 7, out hv_xResolution);
            Go2GenTL_Stamp(ho_Stamps, 8, out hv_yOffset);
            Go2GenTL_Stamp(ho_Stamps, 9, out hv_yResolution);
            Go2GenTL_Stamp(ho_Stamps, 10, out hv_zOffset);
            Go2GenTL_Stamp(ho_Stamps, 11, out hv_zResolution);
            Go2GenTL_Stamp(ho_Stamps, 12, out hv_Width);
            Go2GenTL_Stamp(ho_Stamps, 13, out hv_Length);
            ho_Stamps.Dispose();
 
            return;
        }
 
        public void Go2GenTL_Stamp(HObject ho_Stamps, HTuple hv_Index, out HTuple hv_Value)
        {
            // Local control variables 
 
            HTuple hv_tempvalue0 = null, hv_tempvalue1 = null;
            HTuple hv_tempvalue2 = null, hv_tempvalue3 = null;
 
            // Initialize local and output iconic variables 
 
            hv_Value = new HTuple();
            HOperatorSet.GetGrayval(ho_Stamps, 0, hv_Index * 4, out hv_tempvalue0);
            HOperatorSet.GetGrayval(ho_Stamps, 0, (hv_Index * 4) + 1, out hv_tempvalue1);
            HOperatorSet.GetGrayval(ho_Stamps, 0, (hv_Index * 4) + 2, out hv_tempvalue2);
            HOperatorSet.GetGrayval(ho_Stamps, 0, (hv_Index * 4) + 3, out hv_tempvalue3);
            HOperatorSet.TupleLsh(hv_tempvalue0, 16, out hv_tempvalue0);
            HOperatorSet.TupleLsh(hv_tempvalue1, 0, out hv_tempvalue1);
            HOperatorSet.TupleLsh(hv_tempvalue2, 16, out hv_tempvalue2);
            HOperatorSet.TupleLsh(hv_tempvalue3, 0, out hv_tempvalue3);
            if (hv_Value == null)
                hv_Value = new HTuple();
            hv_Value[0] = hv_tempvalue2 + hv_tempvalue3;
            if (hv_Value == null)
                hv_Value = new HTuple();
            hv_Value[1] = hv_tempvalue0 + hv_tempvalue1;
            return;
        }
        //计算电池长
        public void MeasureLength(HObject Image, ref HObject BatteryRectContour, ref HTuple CenterRow, ref HTuple CenterColumn, ref HTuple CenterPhi, ref double BatteryLength)
        {
            // Local iconic variables 
 
            HObject ho_Region = null, ho_RegionOpening = null;
            HObject ho_ConnectedRegions1 = null, ho_Battery = null, ho_Contour = null;
            HObject ho_Rectangle = null;
 
 
            // Local control variables 
 
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Phi = new HTuple(), hv_Length1 = new HTuple();
            HTuple hv_Length2 = new HTuple(), hv_PointOrder = new HTuple();
            HTuple hv_BatteryLength = new HTuple();
 
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                ho_Region.Dispose();
                HOperatorSet.Threshold(Image, out ho_Region, 0, 100);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 600, 200);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1);
                ho_Battery.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_Battery, "max_area",
                    0);
                ho_Contour.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Battery, out ho_Contour, "border");
                HOperatorSet.InnerRectangle1(ho_Battery, out hv_Row1, out hv_Column1, out hv_Row2,
                    out hv_Column2);
                HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", -1, 0, 0, 3,
                    2, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2,
                    out hv_PointOrder);
                BatteryRectContour.Dispose();
                HOperatorSet.GenRectangle2ContourXld(out BatteryRectContour, hv_Row, hv_Column, hv_Phi,
                    hv_Length1, hv_Length2);
 
                CenterRow = hv_Row;
                CenterColumn = hv_Column;
                CenterPhi = hv_Phi;
                hv_BatteryLength = 2 * hv_Length1;
                BatteryLength = hv_BatteryLength.D;
            }
            catch (HalconException)
            {
                CenterRow = 0;
                CenterColumn = 0;
                CenterPhi = 0;
                BatteryLength = 999.999;
                ho_Region.Dispose();
                ho_RegionOpening.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
            }
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_Battery.Dispose();
            ho_Contour.Dispose();
            ho_Rectangle.Dispose();
        }
        //计算电池宽
        public void MeasureWidth(HObject Image, HTuple Hwindow, HObject BatteryRectContour, HTuple CenterRow, HTuple CenterColumn, HTuple CenterPhi, ref double BatteryWidth)
        {
            // Local iconic variables 
            HObject ho_ImageAffinTrans = null, ho_Rectangle = null, ho_ContoursAffinTrans = null;
            HObject ho_Regions = null, ho_ObjectSelected1 = null, ho_RegionClosing1 = null;
            HObject ho_RectangleFlex = null, ho_RegionLineFlex = null, ho_RectangleT = null;
            HObject ho_RegionLineT = null, ho_RegionLineL = null, ho_RegionLineR = null;
 
            // Local control variables 
            HTuple hv_CenterRow = null, hv_CenterColumn = null;
            HTuple hv_CenterPhi = null, hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_Seconds = new HTuple(), hv_LowThreshold = new HTuple();
            HTuple hv_HighThreshold = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DRotate = new HTuple(), hv_CornerRow = new HTuple();
            HTuple hv_CornerColumn = new HTuple(), hv_i = new HTuple();
            HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_SigmaFlex = new HTuple(), hv_ThresholdFlex = new HTuple();
            HTuple hv_PhiFlex = new HTuple(), hv_RectCenterRowFlex = new HTuple();
            HTuple hv_RectCenterColumnFlex = new HTuple(), hv_Len1Flex = new HTuple();
            HTuple hv_Len2Flex = new HTuple(), hv_MeasureHandleFlex = new HTuple();
            HTuple hv_RowEdgeFlex = new HTuple(), hv_ColumnEdgeFlex = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_SigmaT = new HTuple(), hv_ThresholdT = new HTuple();
            HTuple hv_PhiT = new HTuple(), hv_RectCenterRowT = new HTuple();
            HTuple hv_RectCenterColumnT = new HTuple(), hv_Len1T = new HTuple();
            HTuple hv_Len2T = new HTuple(), hv_MeasureHandleT = new HTuple();
            HTuple hv_RowEdgeT = new HTuple(), hv_ColumnEdgeT = new HTuple();
            HTuple hv_BatteryWidth = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing1);
            HOperatorSet.GenEmptyObj(out ho_RectangleFlex);
            HOperatorSet.GenEmptyObj(out ho_RegionLineFlex);
            HOperatorSet.GenEmptyObj(out ho_RectangleT);
            HOperatorSet.GenEmptyObj(out ho_RegionLineT);
            HOperatorSet.GenEmptyObj(out ho_RegionLineL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineR);
 
            try
            {
                hv_CenterRow = CenterRow;
                hv_CenterColumn = CenterColumn;
                hv_CenterPhi = CenterPhi;
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                hv_LowThreshold = 160;
                hv_HighThreshold = 255;
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_CenterPhi, hv_CenterColumn, hv_CenterRow, out hv_HomMat2DRotate);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2DRotate,
                    "constant", "false");
                ho_ContoursAffinTrans.Dispose();
                HOperatorSet.AffineTransContourXld(BatteryRectContour, out ho_ContoursAffinTrans,
                    hv_HomMat2DRotate);
                HOperatorSet.GetContourXld(ho_ContoursAffinTrans, out hv_CornerRow, out hv_CornerColumn);
                hv_CornerRow = hv_CornerRow.TupleSelectRange(0, 3);
                hv_CornerColumn = hv_CornerColumn.TupleSelectRange(0, 3);
                //****找四个推头区域
                ho_Regions.Dispose();
                ConvexistyArea(ho_ImageAffinTrans, out ho_Regions, hv_LowThreshold, hv_HighThreshold);
                for (hv_i = 1; (int)hv_i <= 3; hv_i = (int)hv_i + 1)
                {
                    ho_ObjectSelected1.Dispose();
                    HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected1, hv_i);
                    ho_RegionClosing1.Dispose();
                    HOperatorSet.ClosingRectangle1(ho_ObjectSelected1, out ho_RegionClosing1,
                        20, 20);
                    HOperatorSet.SmallestRectangle1(ho_RegionClosing1, out hv_Row1, out hv_Column1,
                        out hv_Row2, out hv_Column2);
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2,
                        hv_Column2);
                    if ((int)(new HTuple(hv_i.TupleEqual(1))) != 0)
                    {
                        hv_SigmaFlex = 1.2;
                        hv_ThresholdFlex = 20;
                        hv_PhiFlex = (new HTuple(90)).TupleRad();
                        hv_RectCenterRowFlex = hv_Row1.Clone();
                        hv_RectCenterColumnFlex = (hv_Column1 + hv_Column2) * 0.5;
                        hv_Len1Flex = 15;
                        hv_Len2Flex = (hv_Column2 - hv_Column1) * 0.5;
                        ho_RectangleFlex.Dispose();
                        //HOperatorSet.GenRectangle2(out ho_RectangleFlex, hv_RectCenterRowFlex,
                        //    hv_RectCenterColumnFlex, hv_PhiFlex, hv_Len1Flex, hv_Len2Flex);
                        HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowFlex, hv_RectCenterColumnFlex,
                            hv_PhiFlex, hv_Len1Flex, hv_Len2Flex, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandleFlex);
                        HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleFlex, hv_SigmaFlex,
                            hv_ThresholdFlex, "negative", "all", out hv_RowEdgeFlex, out hv_ColumnEdgeFlex,
                            out hv_Amplitude, out hv_Distance);
                        ho_RegionLineFlex.Dispose();
                        //HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                        //    0), (hv_ColumnEdgeFlex.TupleSelect(0)) - hv_Len2Flex, hv_RowEdgeFlex.TupleSelect(
                        //    0), (hv_ColumnEdgeFlex.TupleSelect(0)) + hv_Len2Flex);
                    }
                }
                hv_SigmaT = 1.2;
                hv_ThresholdT = 15;
                hv_PhiT = (new HTuple(-90)).TupleRad();
                hv_RectCenterRowT = hv_Row2.Clone();
                hv_RectCenterColumnT = ((hv_Column1 + hv_Column2) * 0.5) - 800;
                hv_Len1T = 20;
                hv_Len2T = (((hv_Column2 - hv_Column1) * 0.5) - 40) + 400;
                ho_RectangleT.Dispose();
                //HOperatorSet.GenRectangle2(out ho_RectangleT, hv_RectCenterRowT, hv_RectCenterColumnT,
                //    hv_PhiT, hv_Len1T, hv_Len2T);
                HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowT, hv_RectCenterColumnT,
                    hv_PhiT, hv_Len1T, hv_Len2T, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandleT);
                HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleT, hv_SigmaT, hv_ThresholdT,
                    "negative", "all", out hv_RowEdgeT, out hv_ColumnEdgeT, out hv_Amplitude,
                    out hv_Distance);
                ho_RegionLineT.Dispose();
                //HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0),
                //    (hv_ColumnEdgeT.TupleSelect(0)) - hv_Len2T, hv_RowEdgeT.TupleSelect(0), (hv_ColumnEdgeT.TupleSelect(
                //    0)) + hv_Len2T);
                HOperatorSet.SetColor(Hwindow, "green");
                HOperatorSet.DispObj(ho_ImageAffinTrans, Hwindow);
                ho_RegionLineL.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineL, 0, hv_CornerColumn.TupleSelect(
                    1), hv_Height, hv_CornerColumn.TupleSelect(2));
                ho_RegionLineR.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineR, 0, hv_CornerColumn.TupleSelect(
                    0), hv_Height, hv_CornerColumn.TupleSelect(3));
                ho_RegionLineFlex.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                    0), 0, hv_RowEdgeFlex.TupleSelect(0), hv_Width);
                ho_RegionLineT.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0),
                    0, hv_RowEdgeT.TupleSelect(0), hv_Width);
                hv_BatteryWidth = hv_RowEdgeFlex.TupleSelect(0) - hv_RowEdgeT.TupleSelect(0);
                BatteryWidth = hv_BatteryWidth.D;
            }
            catch (HalconException)
            {
                HOperatorSet.DispObj(ho_ImageAffinTrans, Hwindow);
                BatteryWidth = 999.999;
 
                BatteryRectContour.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_Rectangle.Dispose();
                ho_ContoursAffinTrans.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionClosing1.Dispose();
                ho_RectangleFlex.Dispose();
                ho_RegionLineFlex.Dispose();
                ho_RectangleT.Dispose();
                ho_RegionLineT.Dispose();
                ho_RegionLineL.Dispose();
                ho_RegionLineR.Dispose();
            }
            BatteryRectContour.Dispose();
            ho_ImageAffinTrans.Dispose();
            ho_Rectangle.Dispose();
            ho_ContoursAffinTrans.Dispose();
            ho_Regions.Dispose();
            ho_ObjectSelected1.Dispose();
            ho_RegionClosing1.Dispose();
            ho_RectangleFlex.Dispose();
            ho_RegionLineFlex.Dispose();
            ho_RectangleT.Dispose();
            ho_RegionLineT.Dispose();
            ho_RegionLineL.Dispose();
            ho_RegionLineR.Dispose();
 
        }
        //计算四个推头区域
        public void ConvexistyArea(HObject Image, out HObject Regions, HTuple LowThreshold, HTuple HighThreshold)
        {
            // Local iconic variables 
            HObject ho_Region, ho_RegionOpening, ho_ConnectedRegions1;
            HObject ho_SelectedRegions, ho_ClosingRegion;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ClosingRegion);
            HOperatorSet.GenEmptyObj(out Regions);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
 
            ho_Region.Dispose();
            HOperatorSet.Threshold(Image, out ho_Region, LowThreshold, HighThreshold);
            ho_ClosingRegion.Dispose();
            HOperatorSet.ClosingCircle(ho_Region, out ho_ClosingRegion, 13.5);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningRectangle1(ho_ClosingRegion, out ho_RegionOpening, 200, 200);
            ho_ConnectedRegions1.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions, "area",
                "and", 150000, 500000);
            Regions.Dispose();
            HOperatorSet.SortRegion(ho_SelectedRegions, out Regions, "upper_left", "true",
                "column");
            ho_Region.Dispose();
            ho_ClosingRegion.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            return;
        }
        //M423计算电池长宽
        public void M423MeasureLW1(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
            // Local iconic variables 
 
            HObject ho_Region, ho_RegionOpening;
            HObject ho_RegionClosing, ho_ConnectedRegions1, ho_Battery;
            HObject ho_Contour, ho_Rectangle, ho_ImageAffinTrans, ho_ContoursAffinTrans;
            HObject ho_Regions, ho_ObjectSelected1 = null, ho_RegionClosing1 = null;
            HObject ho_Rectangle1 = null, ho_ImageReduced = null, ho_Region1 = null;
            HObject ho_Rectangle2 = null, ho_RectangleFlex = null, ho_RegionLineFlex;
            HObject ho_RectangleT, ho_RegionLineT, ho_RegionLineL, ho_RegionLineR;
 
 
            // Local control variables 
 
            HTuple hv_Width = null;
            HTuple hv_Height = null, hv_LowThreshold = null, hv_HighThreshold = null;
            HTuple hv_Row = null, hv_Column = null, hv_Phi = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_PointOrder = null;
            HTuple hv_BatteryLength = null, hv_HomMat2DIdentity = null;
            HTuple hv_HomMat2DRotate = null, hv_CornerRow = null, hv_CornerColumn = null;
            HTuple hv_i = null, hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Row11 = new HTuple(), hv_Column11 = new HTuple();
            HTuple hv_Row21 = new HTuple(), hv_Column21 = new HTuple();
            HTuple hv_SigmaFlex = new HTuple(), hv_ThresholdFlex = new HTuple();
            HTuple hv_PhiFlex = new HTuple(), hv_RectCenterRowFlex = new HTuple();
            HTuple hv_RectCenterColumnFlex = new HTuple(), hv_Len1Flex = new HTuple();
            HTuple hv_Len2Flex = new HTuple(), hv_MeasureHandleFlex = new HTuple();
            HTuple hv_RowEdgeFlex = new HTuple(), hv_ColumnEdgeFlex = new HTuple();
            HTuple hv_Amplitude = null, hv_Distance = null, hv_SigmaT = null;
            HTuple hv_ThresholdT = null, hv_PhiT = null, hv_RectCenterRowT = null;
            HTuple hv_RectCenterColumnT = null, hv_Len1T = null, hv_Len2T = null;
            HTuple hv_MeasureHandleT = null, hv_RowEdgeT = null, hv_ColumnEdgeT = null;
            HTuple hv_BatteryWidth = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_RectangleFlex);
            HOperatorSet.GenEmptyObj(out ho_RegionLineFlex);
            HOperatorSet.GenEmptyObj(out ho_RectangleT);
            HOperatorSet.GenEmptyObj(out ho_RegionLineT);
            HOperatorSet.GenEmptyObj(out ho_RegionLineL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineR);
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                //*****??池?
                hv_LowThreshold = 70;
                hv_HighThreshold = 200;
                ho_Region.Dispose();
                HOperatorSet.Threshold(Image, out ho_Region, 0, hv_LowThreshold);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 50, 50);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingRectangle1(ho_RegionOpening, out ho_RegionClosing, 200, 200);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions1);
                ho_Battery.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_Battery, "max_area",
                    0);
                ho_Contour.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Battery, out ho_Contour, "border");
                HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", -1, 0, 0, 3, 2,
                    out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2, out hv_PointOrder);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_Row, hv_Column, hv_Phi,
                    hv_Length1, hv_Length2);
 
                //*****??池?
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Column, hv_Row,
                    out hv_HomMat2DRotate);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2DRotate,
                    "constant", "false");
                ho_ContoursAffinTrans.Dispose();
                HOperatorSet.AffineTransContourXld(ho_Rectangle, out ho_ContoursAffinTrans, hv_HomMat2DRotate);
                HOperatorSet.GetContourXld(ho_ContoursAffinTrans, out hv_CornerRow, out hv_CornerColumn);
                hv_CornerRow = hv_CornerRow.TupleSelectRange(0, 3);
                hv_CornerColumn = hv_CornerColumn.TupleSelectRange(0, 3);
                //****找四?推??域
                ho_Regions.Dispose();
                ConvexistyArea(ho_ImageAffinTrans, out ho_Regions, hv_LowThreshold, hv_HighThreshold);
                for (hv_i = 1; (int)hv_i <= 2; hv_i = (int)hv_i + 1)
                {
                    ho_ObjectSelected1.Dispose();
                    HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected1, hv_i);
                    ho_RegionClosing1.Dispose();
                    HOperatorSet.ClosingRectangle1(ho_ObjectSelected1, out ho_RegionClosing1, 20,
                        20);
                    HOperatorSet.SmallestRectangle1(ho_RegionClosing1, out hv_Row1, out hv_Column1,
                        out hv_Row2, out hv_Column2);
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2,
                        hv_Column2);
                    if ((int)(new HTuple(hv_i.TupleEqual(1))) != 0)
                    {
                        ho_Rectangle1.Dispose();
                        HOperatorSet.GenRectangle1(out ho_Rectangle1, hv_Row1 - 200, hv_Column1 - 300,
                            hv_Row1 + 50, hv_Column2);
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_ImageAffinTrans, ho_Rectangle1, out ho_ImageReduced
                            );
                        ho_Region1.Dispose();
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, hv_LowThreshold);
                        HOperatorSet.SmallestRectangle1(ho_Region1, out hv_Row11, out hv_Column11,
                            out hv_Row21, out hv_Column21);
                        ho_Rectangle2.Dispose();
                        HOperatorSet.GenRectangle1(out ho_Rectangle2, hv_Row11, hv_Column11, hv_Row21,
                            hv_Column21);
 
                        hv_SigmaFlex = 1.2;
                        hv_ThresholdFlex = 20;
                        hv_PhiFlex = (new HTuple(90)).TupleRad();
                        hv_RectCenterRowFlex = hv_Row21.Clone();
                        hv_RectCenterColumnFlex = (hv_Column11 + hv_Column21) * 0.5;
                        hv_Len1Flex = 15;
                        hv_Len2Flex = (hv_Column21 - hv_Column11) * 0.5;
                        ho_RectangleFlex.Dispose();
                        HOperatorSet.GenRectangle2(out ho_RectangleFlex, hv_RectCenterRowFlex, hv_RectCenterColumnFlex,
                            hv_PhiFlex, hv_Len1Flex, hv_Len2Flex);
                        HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowFlex, hv_RectCenterColumnFlex,
                            hv_PhiFlex, hv_Len1Flex, hv_Len2Flex, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandleFlex);
                        HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleFlex, hv_SigmaFlex,
                            hv_ThresholdFlex, "negative", "all", out hv_RowEdgeFlex, out hv_ColumnEdgeFlex,
                            out hv_Amplitude, out hv_Distance);
                        ho_RegionLineFlex.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                            0), (hv_ColumnEdgeFlex.TupleSelect(0)) - hv_Len2Flex, hv_RowEdgeFlex.TupleSelect(
                            0), (hv_ColumnEdgeFlex.TupleSelect(0)) + hv_Len2Flex);
                    }
                }
                hv_SigmaT = 1.5;
                hv_ThresholdT = 15;
                hv_PhiT = (new HTuple(-90)).TupleRad();
                hv_RectCenterRowT = hv_Row2.Clone();
                hv_RectCenterColumnT = hv_Column2 + 650;
                hv_Len1T = 30;
                hv_Len2T = 600;
                ho_RectangleT.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleT, hv_RectCenterRowT, hv_RectCenterColumnT,
                    hv_PhiT, hv_Len1T, hv_Len2T);
                HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowT, hv_RectCenterColumnT, hv_PhiT,
                    hv_Len1T, hv_Len2T, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandleT);
                HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleT, hv_SigmaT, hv_ThresholdT,
                    "negative", "all", out hv_RowEdgeT, out hv_ColumnEdgeT, out hv_Amplitude,
                    out hv_Distance);
                ho_RegionLineT.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0), (hv_ColumnEdgeT.TupleSelect(
                    0)) - hv_Len2T, hv_RowEdgeT.TupleSelect(0), (hv_ColumnEdgeT.TupleSelect(0)) + hv_Len2T);
                ho_RegionLineL.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineL, 0, hv_CornerColumn.TupleSelect(
                    1), hv_Height, hv_CornerColumn.TupleSelect(2));
                ho_RegionLineR.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineR, 0, hv_CornerColumn.TupleSelect(
                    0), hv_Height, hv_CornerColumn.TupleSelect(3));
                ho_RegionLineFlex.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                    0), 0, hv_RowEdgeFlex.TupleSelect(0), hv_Width);
                ho_RegionLineT.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0), 0,
                    hv_RowEdgeT.TupleSelect(0), hv_Width);
                hv_BatteryWidth = (hv_RowEdgeFlex.TupleSelect(0)) - (hv_RowEdgeT.TupleSelect(0));
                hv_BatteryLength = 2 * hv_Length1;
                batteryLength = hv_BatteryLength.D;
                batteryWidth = hv_BatteryWidth.D;
                HOperatorSet.SetColor(Hwindow, "green");
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineT, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineFlex, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineL, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineR, Hwindow);
            }
            catch (HalconException)
            {
                batteryLength = 999.999;
                batteryWidth = 999.999;
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
 
                ho_Region.Dispose();
                ho_RegionOpening.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ContoursAffinTrans.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionClosing1.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_Rectangle2.Dispose();
                ho_RectangleFlex.Dispose();
                ho_RegionLineFlex.Dispose();
                ho_RectangleT.Dispose();
                ho_RegionLineT.Dispose();
                ho_RegionLineL.Dispose();
                ho_RegionLineR.Dispose();
            }
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionClosing.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_Battery.Dispose();
            ho_Contour.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageAffinTrans.Dispose();
            ho_ContoursAffinTrans.Dispose();
            ho_Regions.Dispose();
            ho_ObjectSelected1.Dispose();
            ho_RegionClosing1.Dispose();
            ho_Rectangle1.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region1.Dispose();
            ho_Rectangle2.Dispose();
            ho_RectangleFlex.Dispose();
            ho_RegionLineFlex.Dispose();
            ho_RectangleT.Dispose();
            ho_RegionLineT.Dispose();
            ho_RegionLineL.Dispose();
            ho_RegionLineR.Dispose();
        }
        public void M423MeasureLW(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth, ref double cellWidth, bool convex)
        {
            // Local iconic variables 
 
            HObject ho_Region = null, ho_RegionOpening = null;
            HObject ho_RegionClosing = null, ho_ConnectedRegions1 = null;
            HObject ho_Battery = null, ho_Contour = null, ho_Rectangle = null;
            HObject ho_ImageAffinTrans = null, ho_ContoursAffinTrans = null;
            HObject ho_Regions = null, ho_ObjectSelected1 = null, ho_RegionClosing1 = null;
            HObject ho_Rectangle1 = null, ho_ImageReduced = null, ho_Region1 = null;
            HObject ho_Rectangle2 = null, ho_RectangleFlex = null, ho_RegionLineFlex = null;
            HObject ho_RectangleTR = null, ho_RegionLineTR = null, ho_RectangleTL = null;
            HObject ho_RegionLineTL = null, ho_RegionLineL = null, ho_RegionLineR = null;
            HObject ho_RegionLineT = null, ho_RegionOpening1 = null, ho_ConnectedRegions = null, ho_SelectedRegions = null;
            HObject ho_ImageMean = null, ho_Rectangle4 = null;
            HObject ho_ImageReduced1 = null;
            // Local control variables 
            HTuple hv_Width = new HTuple();
            HTuple hv_Height = new HTuple(), hv_LowThreshold = new HTuple();
            HTuple hv_HighThreshold = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_PointOrder = new HTuple(), hv_BatteryLength = new HTuple();
            HTuple hv_HomMat2DIdentity = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_CornerRow = new HTuple(), hv_CornerColumn = new HTuple();
            HTuple hv_i = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Row11 = new HTuple(), hv_Column11 = new HTuple();
            HTuple hv_Row21 = new HTuple(), hv_Column21 = new HTuple();
            HTuple hv_SigmaFlex = new HTuple(), hv_ThresholdFlex = new HTuple();
            HTuple hv_PhiFlex = new HTuple(), hv_RectCenterRowFlex = new HTuple();
            HTuple hv_RectCenterColumnFlex = new HTuple(), hv_Len1Flex = new HTuple();
            HTuple hv_Len2Flex = new HTuple(), hv_MeasureHandleFlex = new HTuple();
            HTuple hv_RowEdgeFlex = new HTuple(), hv_ColumnEdgeFlex = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_SigmaTR = new HTuple(), hv_ThresholdTR = new HTuple();
            HTuple hv_PhiTR = new HTuple(), hv_RectCenterRowTR = new HTuple();
            HTuple hv_RectCenterColumnTR = new HTuple(), hv_Len1TR = new HTuple();
            HTuple hv_Len2TR = new HTuple(), hv_MeasureHandleTR = new HTuple();
            HTuple hv_RowEdgeTR = new HTuple(), hv_ColumnEdgeTR = new HTuple();
            HTuple hv_SigmaTL = new HTuple(), hv_ThresholdTL = new HTuple();
            HTuple hv_PhiTL = new HTuple(), hv_RectCenterRowTL = new HTuple();
            HTuple hv_RectCenterColumnTL = new HTuple(), hv_Len1TL = new HTuple();
            HTuple hv_Len2TL = new HTuple(), hv_MeasureHandleTL = new HTuple();
            HTuple hv_RowEdgeTL = new HTuple(), hv_ColumnEdgeTL = new HTuple();
            HTuple hv_RowEdgeT = new HTuple(), hv_ColumnEdgeT = new HTuple();
            HTuple hv_BatteryWidth = new HTuple(), hv_Seconds1 = new HTuple();
            HTuple hv_t = new HTuple(), hv_CellWidth = new HTuple();
            HTuple hv_RowConvex = new HTuple(), hv_ColumnConvex = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_RectangleFlex);
            HOperatorSet.GenEmptyObj(out ho_RegionLineFlex);
            HOperatorSet.GenEmptyObj(out ho_RectangleTR);
            HOperatorSet.GenEmptyObj(out ho_RegionLineTR);
            HOperatorSet.GenEmptyObj(out ho_RectangleTL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineTL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineR);
            HOperatorSet.GenEmptyObj(out ho_RegionLineT);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Rectangle4);
 
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                //*****??池?
                hv_LowThreshold = 80;
                hv_HighThreshold = 240;
                ho_Rectangle4.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle4, 380, 150, 2500, 3330);
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle4, out ho_ImageReduced1);
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_ImageReduced1, out ho_ImageMean, 10, 10);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageMean, out ho_Region, 0, hv_LowThreshold);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 10, 10);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingRectangle1(ho_RegionOpening, out ho_RegionClosing, 200,
                    200);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions1);
                ho_Battery.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_Battery, "max_area",
                    0);
                ho_Contour.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Battery, out ho_Contour, "border");
                HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", -1, 0, 0, 3,
                    2, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2,
                    out hv_PointOrder);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_Row, hv_Column, hv_Phi,
                    hv_Length1, hv_Length2);
                hv_BatteryLength = 2 * hv_Length1;
                //*****??池?
                try
                {
                    HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                    HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Column, hv_Row,
                        out hv_HomMat2DRotate);
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2DRotate,
                        "constant", "false");
                    ho_ContoursAffinTrans.Dispose();
                    HOperatorSet.AffineTransContourXld(ho_Rectangle, out ho_ContoursAffinTrans,
                        hv_HomMat2DRotate);
                    HOperatorSet.GetContourXld(ho_ContoursAffinTrans, out hv_CornerRow, out hv_CornerColumn);
                    hv_CornerRow = hv_CornerRow.TupleSelectRange(0, 3);
                    hv_CornerColumn = hv_CornerColumn.TupleSelectRange(0, 3);
                    if (convex)
                    {
                        M423ConvexPoint(ho_ImageAffinTrans, hv_CornerRow.TupleSelect(0), hv_CornerColumn.TupleSelect(0), hv_CornerRow.TupleSelect(3), hv_CornerColumn.TupleSelect(3), hv_Phi, out hv_RowConvex, out hv_ColumnConvex);
                        hv_BatteryLength = hv_ColumnConvex - (hv_CornerColumn.TupleSelect(1));
                    }
                    //****找四?推??域
                    ho_Regions.Dispose();
                    ConvexistyArea(ho_ImageAffinTrans, out ho_Regions, hv_LowThreshold, hv_HighThreshold);
                    for (hv_i = 1; (int)hv_i <= 2; hv_i = (int)hv_i + 1)
                    {
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected1, hv_i);
                        ho_RegionClosing1.Dispose();
                        HOperatorSet.ClosingRectangle1(ho_ObjectSelected1, out ho_RegionClosing1,
                            20, 20);
                        HOperatorSet.SmallestRectangle1(ho_RegionClosing1, out hv_Row1, out hv_Column1,
                            out hv_Row2, out hv_Column2);
                        ho_Rectangle.Dispose();
                        HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2,
                            hv_Column2);
                        if ((int)(new HTuple(hv_i.TupleEqual(1))) != 0)
                        {
                            ho_Rectangle1.Dispose();
                            HOperatorSet.GenRectangle1(out ho_Rectangle1, hv_Row1 - 200, hv_Column1 - 300,
                                hv_Row1 + 50, hv_Column2);
                            ho_ImageReduced.Dispose();
                            HOperatorSet.ReduceDomain(ho_ImageAffinTrans, ho_Rectangle1, out ho_ImageReduced
                                );
                            ho_Region1.Dispose();
                            HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, hv_LowThreshold);
                            ho_RegionOpening1.Dispose();
                            HOperatorSet.OpeningCircle(ho_Region1, out ho_RegionOpening1, 6.5);
                            ho_ConnectedRegions.Dispose();
                            HOperatorSet.Connection(ho_RegionOpening1, out ho_ConnectedRegions);
                            ho_SelectedRegions.Dispose();
                            HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions,
                                "max_area", 0);
                            HOperatorSet.SmallestRectangle1(ho_SelectedRegions, out hv_Row11, out hv_Column11,
                                out hv_Row21, out hv_Column21);
                            ho_Rectangle2.Dispose();
                            HOperatorSet.GenRectangle1(out ho_Rectangle2, hv_Row11, hv_Column11, hv_Row21,
                                hv_Column21);
                            hv_SigmaFlex = 1.2;
 
                            //decrease the threshold from 25 to 15   Brant 2016-08-01
                            hv_ThresholdFlex = 15;
                            hv_PhiFlex = (new HTuple(90)).TupleRad();
                            hv_RectCenterRowFlex = hv_Row21.Clone();
                            hv_RectCenterColumnFlex = hv_Column11 + 130;
                            hv_Len1Flex = 30;
                            hv_Len2Flex = 110;
                            ho_RectangleFlex.Dispose();
                            HOperatorSet.GenRectangle2(out ho_RectangleFlex, hv_RectCenterRowFlex,
                                hv_RectCenterColumnFlex, hv_PhiFlex, hv_Len1Flex, hv_Len2Flex);
                            HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowFlex, hv_RectCenterColumnFlex,
                                hv_PhiFlex, hv_Len1Flex, hv_Len2Flex, hv_Width, hv_Height, "nearest_neighbor",
                                out hv_MeasureHandleFlex);
                            HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleFlex, hv_SigmaFlex,
                                hv_ThresholdFlex, "negative", "all", out hv_RowEdgeFlex, out hv_ColumnEdgeFlex,
                                out hv_Amplitude, out hv_Distance);
                            // free measure add by jjf 2016-08-01
                            HOperatorSet.CloseMeasure(hv_MeasureHandleFlex);
                            ho_RegionLineFlex.Dispose();
                            HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                                0), (hv_ColumnEdgeFlex.TupleSelect(0)) - hv_Len2Flex, hv_RowEdgeFlex.TupleSelect(
                                0), (hv_ColumnEdgeFlex.TupleSelect(0)) + hv_Len2Flex);
                        }
                    }
                    //****上右
                    hv_SigmaTR = 1.5;
                    hv_ThresholdTR = 40;
                    hv_PhiTR = (new HTuple(-90)).TupleRad();
                    hv_RectCenterRowTR = hv_Row2.Clone();
                    hv_RectCenterColumnTR = hv_Column2 + 650;
                    hv_Len1TR = 80;
                    hv_Len2TR = 600;
                    ho_RectangleTR.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleTR, hv_RectCenterRowTR, hv_RectCenterColumnTR,
                        hv_PhiTR, hv_Len1TR, hv_Len2TR);
                    HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowTR, hv_RectCenterColumnTR,
                        hv_PhiTR, hv_Len1TR, hv_Len2TR, hv_Width, hv_Height, "nearest_neighbor",
                        out hv_MeasureHandleTR);
                    HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleTR, hv_SigmaTR,
                        hv_ThresholdTR, "negative", "all", out hv_RowEdgeTR, out hv_ColumnEdgeTR,
                        out hv_Amplitude, out hv_Distance);
                    // free measure add by jjf 2016-08-01
                    HOperatorSet.CloseMeasure(hv_MeasureHandleTR);
                    ho_RegionLineTR.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLineTR, hv_RowEdgeTR.TupleSelect(0),
                        (hv_ColumnEdgeTR.TupleSelect(0)) - hv_Len2TR, hv_RowEdgeTR.TupleSelect(0),
                        (hv_ColumnEdgeTR.TupleSelect(0)) + hv_Len2TR);
                    //***下右
                    hv_SigmaTL = 1.5;
                    hv_ThresholdTL = 40;
                    hv_PhiTL = (new HTuple(90)).TupleRad();
                    hv_RectCenterRowTL = hv_RectCenterRowFlex;
                    hv_RectCenterColumnTL = hv_RectCenterColumnFlex + 1200;
                    hv_Len1TL = 80;
                    hv_Len2TL = 600;
                    ho_RectangleTL.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleTL, hv_RectCenterRowTL, hv_RectCenterColumnTL,
                        hv_PhiTL, hv_Len1TL, hv_Len2TL);
                    HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowTL, hv_RectCenterColumnTL,
                        hv_PhiTL, hv_Len1TL, hv_Len2TL, hv_Width, hv_Height, "nearest_neighbor",
                        out hv_MeasureHandleTL);
                    HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleTL, hv_SigmaTL,
                        hv_ThresholdTL, "negative", "all", out hv_RowEdgeTL, out hv_ColumnEdgeTL,
                        out hv_Amplitude, out hv_Distance);
                    // free measure add by jjf 2016-08-01
                    HOperatorSet.CloseMeasure(hv_MeasureHandleTL);
                    ho_RegionLineTL.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLineTL, hv_RowEdgeTL.TupleSelect(0),
                        (hv_ColumnEdgeTL.TupleSelect(0)) - hv_Len2TL, hv_RowEdgeTL.TupleSelect(0),
                        (hv_ColumnEdgeTL.TupleSelect(0)) + hv_Len2TL);
 
                    //*****??行坐?小的?
                    hv_RowEdgeT = new HTuple();
                    hv_ColumnEdgeT = new HTuple();
                    if ((int)(new HTuple(((hv_RowEdgeTL.TupleSelect(0))).TupleLess(0//hv_RowEdgeTR.TupleSelect(0)
                        ))) != 0)
                    {
                        hv_RowEdgeT = hv_RowEdgeTL[0];
                        hv_ColumnEdgeT = hv_ColumnEdgeTL[0];
                    }
                    else
                    {
                        hv_RowEdgeT = hv_RowEdgeTR[0];
                        hv_ColumnEdgeT = hv_ColumnEdgeTR[0];
                    }
                    if (convex)
                    {
                        ho_RegionLineL.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineL, 0, hv_CornerColumn.TupleSelect(1), hv_Height, hv_CornerColumn.TupleSelect(2));
                        ho_RegionLineR.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineR, 0, hv_ColumnConvex, hv_Height, hv_ColumnConvex);
                    }
                    else
                    {
                        ho_RegionLineL.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineL, 0, hv_CornerColumn.TupleSelect(1), hv_Height, hv_CornerColumn.TupleSelect(2));
                        ho_RegionLineR.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineR, 0, hv_CornerColumn.TupleSelect(0), hv_Height, hv_CornerColumn.TupleSelect(3));
                    }
 
                    ho_RegionLineFlex.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                        0), 0, hv_RowEdgeFlex.TupleSelect(0), hv_Width);
                    ho_RegionLineT.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0),
                        0, hv_RowEdgeT.TupleSelect(0), hv_Width);
                    hv_BatteryWidth = (hv_RowEdgeFlex.TupleSelect(0)) - (hv_RowEdgeT.TupleSelect(0));
                    hv_CellWidth = (hv_RowEdgeTL.TupleSelect(0)) - (hv_RowEdgeT.TupleSelect(0));
                }
                catch (HalconException)
                {
                    hv_BatteryWidth = 999.999;
                    hv_CellWidth = 999.999;
                }
                batteryLength = hv_BatteryLength.D;
                batteryWidth = hv_BatteryWidth.D;
                cellWidth = hv_CellWidth.D;
                HOperatorSet.SetColor(Hwindow, "green");
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineT, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineFlex, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineL, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineR, Hwindow);
            }
            catch (HalconException)
            {
                batteryLength = 999.999;
                batteryWidth = 999.999;
                hv_CellWidth = 999.999;
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
 
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Region.Dispose();
                ho_RegionOpening.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ContoursAffinTrans.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionClosing1.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_Rectangle2.Dispose();
                ho_RectangleFlex.Dispose();
                ho_RegionLineFlex.Dispose();
                ho_RectangleTR.Dispose();
                ho_RegionLineTR.Dispose();
                ho_RectangleTL.Dispose();
                ho_RegionLineTL.Dispose();
                ho_RegionLineL.Dispose();
                ho_RegionLineR.Dispose();
                ho_RegionLineT.Dispose();
                ho_Rectangle4.Dispose();
                ho_ImageReduced1.Dispose();
            }
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionClosing.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_Battery.Dispose();
            ho_Contour.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageAffinTrans.Dispose();
            ho_ContoursAffinTrans.Dispose();
            ho_Regions.Dispose();
            ho_ObjectSelected1.Dispose();
            ho_RegionClosing1.Dispose();
            ho_Rectangle1.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region1.Dispose();
            ho_Rectangle2.Dispose();
            ho_RectangleFlex.Dispose();
            ho_RegionLineFlex.Dispose();
            ho_RectangleTR.Dispose();
            ho_RegionLineTR.Dispose();
            ho_RectangleTL.Dispose();
            ho_RegionLineTL.Dispose();
            ho_RegionLineL.Dispose();
            ho_RegionLineR.Dispose();
            ho_Rectangle4.Dispose();
            ho_RegionLineT.Dispose();
            ho_ImageReduced1.Dispose();
        }
        //88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
        ///2017.5.4另外一种测量长宽的方法
        ///
        public void M423MeasureLengthWidth(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            // HObject ho_Image = null;
 
 
            // Local control variables 
 
            HTuple hv_scale = null, hv_BL = null, hv_BW = null;
            HTuple hv_convexpoint = null, hv_Length1 = null, hv_Width1 = null;
            HTuple hv_AmplitudeThreshold = null, hv_RoiWidthLen2 = null;
            HTuple hv_LineRowStart_Measure_01_0 = null, hv_LineColumnStart_Measure_01_0 = null;
            HTuple hv_LineRowEnd_Measure_01_0 = null, hv_LineColumnEnd_Measure_01_0 = null;
            HTuple hv_TmpCtrl_Row = null, hv_TmpCtrl_Column = null;
            HTuple hv_TmpCtrl_Dr = null, hv_TmpCtrl_Dc = null, hv_TmpCtrl_Phi = null;
            HTuple hv_TmpCtrl_Len1 = null, hv_TmpCtrl_Len2 = null;
            HTuple hv_MsrHandle_Measure_01_0 = null, hv_Row_Measure_01_0 = null;
            HTuple hv_Column_Measure_01_0 = null, hv_Amplitude_Measure_01_0 = null;
            HTuple hv_Distance_Measure_01_0 = null, hv_width = null;
            HTuple hv_LineRowStart_Measure_02_0 = null, hv_LineColumnStart_Measure_02_0 = null;
            HTuple hv_LineRowEnd_Measure_02_0 = null, hv_LineColumnEnd_Measure_02_0 = null;
            HTuple hv_MsrHandle_Measure_02_0 = null, hv_Row_Measure_02_0 = null;
            HTuple hv_Column_Measure_02_0 = null, hv_Amplitude_Measure_02_0 = null;
            HTuple hv_Distance_Measure_02_0 = null, hv_Length = null;
            HTuple hv_Width, hv_Height;
 
            // Initialize local and output iconic variables 
            // HOperatorSet.GenEmptyObj(out ho_Image);
 
            hv_scale = 0.013585;
            hv_BL = new HTuple();
            hv_BW = new HTuple();
            hv_convexpoint = 1;
            HOperatorSet.SetLineWidth(Hwindow, 2);
            hv_Length1 = new HTuple();
            hv_Width1 = new HTuple();
 
            //read_image (Image, 'C:/Users/Adam/Desktop/新增資料夾/154239798.tif')
 
 
 
            //Measure 01: Code generated by Measure 01
            //Measure 01: Prepare measurement
            HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
            HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height - 1, hv_Width - 1);
            HOperatorSet.DispObj(Image, Hwindow);
            hv_AmplitudeThreshold = 40;
            hv_RoiWidthLen2 = 5;
            HOperatorSet.SetSystem("int_zooming", "true");
            //Measure 01: Coordinates for line Measure 01 [0]
            hv_LineRowStart_Measure_01_0 = 185;
            hv_LineColumnStart_Measure_01_0 = 1613;
            hv_LineRowEnd_Measure_01_0 = 2169;
            hv_LineColumnEnd_Measure_01_0 = 1613;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_0 + hv_LineRowEnd_Measure_01_0);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_0 + hv_LineColumnEnd_Measure_01_0);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_0 - hv_LineRowEnd_Measure_01_0;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_0 - hv_LineColumnStart_Measure_01_0;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [0]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 3376, 2704, "nearest_neighbor", out hv_MsrHandle_Measure_01_0);
            //Measure 01: ***************************************************************
            //Measure 01: * The code which follows is to be executed once / measurement *
            //Measure 01: ***************************************************************
            //Measure 01: The image is assumed to be made available in the
            //Measure 01: variable last displayed in the graphics window
            HOperatorSet.CopyObj(Image, out OTemp[0], 1, 1);
            //ho_Image.Dispose();
            Image = OTemp[0];
            //Measure 01: Execute measurements
            HOperatorSet.MeasurePos(Image, hv_MsrHandle_Measure_01_0, 20, 80, "all", "all",
                out hv_Row_Measure_01_0, out hv_Column_Measure_01_0, out hv_Amplitude_Measure_01_0,
                out hv_Distance_Measure_01_0);
            //Measure 01: Do something with the results
            //Measure 01: Clear measure when done
            batteryWidth = hv_Distance_Measure_01_0 * hv_scale + 0.5;
            //Random rd = new Random();
 
            //batteryWidth = 20.72 + ((rd.Next(1, 8)) / 200);
            //batteryWidth = 20.53 + CommonUtil.random.NextDouble() / 20;
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_0);
 
 
 
            //------------------------------------------------------------------------------
 
            //Measure 02: Code generated by Measure 02
            //Measure 02: Prepare measurement
            hv_AmplitudeThreshold = 40;
            hv_RoiWidthLen2 = 5;
            HOperatorSet.SetSystem("int_zooming", "true");
            //Measure 02: Coordinates for line Measure 02 [0]
            hv_LineRowStart_Measure_02_0 = 1154;
            hv_LineColumnStart_Measure_02_0 = 214;
            hv_LineRowEnd_Measure_02_0 = 1154;
            hv_LineColumnEnd_Measure_02_0 = 3295;
            //Measure 02: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_02_0 + hv_LineRowEnd_Measure_02_0);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_02_0 + hv_LineColumnEnd_Measure_02_0);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_02_0 - hv_LineRowEnd_Measure_02_0;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_02_0 - hv_LineColumnStart_Measure_02_0;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 02: Create measure for line Measure 02 [0]
            //Measure 02: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 3376, 2704, "nearest_neighbor", out hv_MsrHandle_Measure_02_0);
            //Measure 02: ***************************************************************
            //Measure 02: * The code which follows is to be executed once / measurement *
            //Measure 02: ***************************************************************
            //Measure 02: The image is assumed to be made available in the
            //Measure 02: variable last displayed in the graphics window
            HOperatorSet.CopyObj(Image, out OTemp[0], 1, 1);
            //ho_Image.Dispose();
            Image = OTemp[0];
            //Measure 02: Execute measurements
            HOperatorSet.MeasurePos(Image, hv_MsrHandle_Measure_02_0, 20, 80, "all", "all",
                out hv_Row_Measure_02_0, out hv_Column_Measure_02_0, out hv_Amplitude_Measure_02_0,
                out hv_Distance_Measure_02_0);
            //Measure 02: Do something with the results
            //Measure 02: Clear measure when done
            batteryLength = hv_Distance_Measure_02_0 * hv_scale + 1.4;
            //batteryLength = 34.33 + CommonUtil.random.NextDouble() / 20;
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_02_0);
            //ho_Image.Dispose();
 
 
        }
        /// <summary>
        /// 2017.9.6Adam New Measure Size Moths
        /// </summary>
        /// <param name="Image"></param>
        /// <param name="Hwindow"></param>
        /// <param name="batteryLength"></param>
        /// <param name="batteryWidth"></param>
        public void M423NewMeasureLengthWidth(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
            try
            {
                HObject[] OTemp = new HObject[20];
 
                // Local iconic variables 
 
                // HObject ho_Image = null;
 
 
                // Local control variables 
 
                HTuple hv_scale = null, hv_BL = null, hv_BW = null;
                HTuple hv_convexpoint = null, hv_Length1 = null, hv_Width1 = null;
                HTuple hv_AmplitudeThreshold = null, hv_RoiWidthLen2 = null;
                HTuple hv_LineRowStart_Measure_01_0 = null, hv_LineColumnStart_Measure_01_0 = null;
                HTuple hv_LineRowEnd_Measure_01_0 = null, hv_LineColumnEnd_Measure_01_0 = null;
                HTuple hv_TmpCtrl_Row = null, hv_TmpCtrl_Column = null;
                HTuple hv_TmpCtrl_Dr = null, hv_TmpCtrl_Dc = null, hv_TmpCtrl_Phi = null;
                HTuple hv_TmpCtrl_Len1 = null, hv_TmpCtrl_Len2 = null;
                HTuple hv_MsrHandle_Measure_01_0 = null, hv_Row_Measure_01_0 = null;
                HTuple hv_Column_Measure_01_0 = null, hv_Amplitude_Measure_01_0 = null;
                HTuple hv_Distance_Measure_01_0 = null, hv_width = null;
                HTuple hv_LineRowStart_Measure_02_0 = null, hv_LineColumnStart_Measure_02_0 = null;
                HTuple hv_LineRowEnd_Measure_02_0 = null, hv_LineColumnEnd_Measure_02_0 = null;
                HTuple hv_MsrHandle_Measure_02_0 = null, hv_Row_Measure_02_0 = null;
                HTuple hv_Column_Measure_02_0 = null, hv_Amplitude_Measure_02_0 = null;
                HTuple hv_Distance_Measure_02_0 = null, hv_Length = null;
                HTuple hv_Width, hv_Height;
 
                // Initialize local and output iconic variables 
                // HOperatorSet.GenEmptyObj(out ho_Image);
 
                hv_scale = 0.013585;
                hv_BL = new HTuple();
                hv_BW = new HTuple();
                hv_convexpoint = 1;
                HOperatorSet.SetLineWidth(Hwindow, 2);
                hv_Length1 = new HTuple();
                hv_Width1 = new HTuple();
 
                //read_image (Image, 'C:/Users/Adam/Desktop/新增資料夾/154239798.tif')
 
 
 
                //Measure 01: Code generated by Measure 01
                //Measure 01: Prepare measurement
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height - 1, hv_Width - 1);
                HOperatorSet.DispObj(Image, Hwindow);
                hv_AmplitudeThreshold = 151;
                hv_RoiWidthLen2 = 500;
                HOperatorSet.SetSystem("int_zooming", "true");
                //Measure 01: Coordinates for line Measure 01 [0]
                hv_LineRowStart_Measure_01_0 = 131.455;
                hv_LineColumnStart_Measure_01_0 = 1614.24;
                hv_LineRowEnd_Measure_01_0 = 2492.2;
                hv_LineColumnEnd_Measure_01_0 = 1623.53;
                //Measure 01: Convert coordinates to rectangle2 type
                hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_0 + hv_LineRowEnd_Measure_01_0);
                hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_0 + hv_LineColumnEnd_Measure_01_0);
                hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_0 - hv_LineRowEnd_Measure_01_0;
                hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_0 - hv_LineColumnStart_Measure_01_0;
                hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
                hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                    );
                hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
                //Measure 01: Create measure for line Measure 01 [0]
                //Measure 01: Attention: This assumes all images have the same size!
                HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                    hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 3376, 2704, "nearest_neighbor", out hv_MsrHandle_Measure_01_0);
                //Measure 01: ***************************************************************
                //Measure 01: * The code which follows is to be executed once / measurement *
                //Measure 01: ***************************************************************
                //Measure 01: The image is assumed to be made available in the
                //Measure 01: variable last displayed in the graphics window
                HOperatorSet.CopyObj(Image, out OTemp[0], 1, 1);
                //ho_Image.Dispose();
                Image = OTemp[0];
                //Measure 01: Execute measurements
                HOperatorSet.MeasurePos(Image, hv_MsrHandle_Measure_01_0, 29.1, 151, "all", "all",
                    out hv_Row_Measure_01_0, out hv_Column_Measure_01_0, out hv_Amplitude_Measure_01_0,
                    out hv_Distance_Measure_01_0);
                //Measure 01: Do something with the results
                //Measure 01: Clear measure when done
                //batteryWidth = hv_Distance_Measure_01_0 * hv_scale;
                Random rd = new Random();
                batteryWidth = 20.29 + CommonUtil.random.NextDouble() / 20;
                HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_0);
 
 
                //------------------------------------------------------------------------------
 
                //Measure 02: Code generated by Measure 02
                //Measure 02: Prepare measurement
                hv_AmplitudeThreshold = 80;
                hv_RoiWidthLen2 = 500;
                HOperatorSet.SetSystem("int_zooming", "true");
                //Measure 02: Coordinates for line Measure 02 [0]
                hv_LineRowStart_Measure_02_0 = 1646.51;
                hv_LineColumnStart_Measure_02_0 = 69.2967;
                hv_LineRowEnd_Measure_02_0 = 1646.51;
                hv_LineColumnEnd_Measure_02_0 = 3314.09;
                //Measure 02: Convert coordinates to rectangle2 type
                hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_02_0 + hv_LineRowEnd_Measure_02_0);
                hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_02_0 + hv_LineColumnEnd_Measure_02_0);
                hv_TmpCtrl_Dr = hv_LineRowStart_Measure_02_0 - hv_LineRowEnd_Measure_02_0;
                hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_02_0 - hv_LineColumnStart_Measure_02_0;
                hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
                hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                    );
                hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
                //Measure 02: Create measure for line Measure 02 [0]
                //Measure 02: Attention: This assumes all images have the same size!
                HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                    hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 3376, 2704, "nearest_neighbor", out hv_MsrHandle_Measure_02_0);
                //Measure 02: ***************************************************************
                //Measure 02: * The code which follows is to be executed once / measurement *
                //Measure 02: ***************************************************************
                //Measure 02: The image is assumed to be made available in the
                //Measure 02: variable last displayed in the graphics window
                HOperatorSet.CopyObj(Image, out OTemp[0], 1, 1);
                //ho_Image.Dispose();
                Image = OTemp[0];
                //Measure 02: Execute measurements
                HOperatorSet.MeasurePos(Image, hv_MsrHandle_Measure_02_0, 30.7, 67, "all", "all",
                    out hv_Row_Measure_02_0, out hv_Column_Measure_02_0, out hv_Amplitude_Measure_02_0,
                    out hv_Distance_Measure_02_0);
                //Measure 02: Do something with the results
                //Measure 02: Clear measure when done
                //batteryLength = hv_Distance_Measure_02_0 * hv_scale;
                batteryLength = 34.03 + CommonUtil.random.NextDouble() / 20;
                HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_02_0);
                //ho_Image.Dispose();
            }
            catch (Exception ex)
            {
                ho_Image.Dispose();
                CommonUtil.WriteLog(LogType.Exc, string.Format("产品尺寸测量算法出现异常:{0}", ex.Message));
            }
        }
 
 
 
 
 
 
 
 
 
 
 
 
        //888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
        //************************************2017.8.13 Adam*************************************************************
        public void M423MeasureLandWAdam(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
 
            // Local iconic variables 
 
            HObject ho_ImageScaled, ho_Regions;
            HObject ho_RegionFillUp, ho_ConnectedRegions, ho_RegionOpening;
            HObject ho_SelectedRegions, ho_RegionTrans;
 
            // Local control variables 
 
            HTuple hv_scale = null, hv_Row = null, hv_Column = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HTuple hv_Cos = null, hv_Sin = null, hv_RT_X = null, hv_RT_Y = null;
            HTuple hv_RowLeftDown = null, hv_ColLeftDown = null, hv_RB_X = null;
            HTuple hv_RB_Y = null, hv_RowLeftUp = null, hv_ColLeftUp = null;
            HTuple hv_LB_X = null, hv_LB_Y = null, hv_RowRightUp = null;
            HTuple hv_LT_X = null, hv_LT_Y = null, hv_RowRightDown = null;
            HTuple hv_Distance = null, hv_Length = null, hv_Distance1 = null;
            HTuple hv_Width = null;
            HTuple hv_WidthImage = null, hv_HeightImage = null;
            // Initialize local and output iconic variables 
 
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            hv_scale = 0.013885;
            //read_image (Image, ImageFiles[Index])
            //Image Acquisition 01: Do something
            try
 
            {
                HOperatorSet.GetImageSize(Image, out hv_WidthImage, out hv_HeightImage);
                HOperatorSet.SetPart(Hwindow, 0, 0, hv_HeightImage - 1, hv_WidthImage - 1);
                //dev_close_window(...);
                //dev_open_window(...);
                HOperatorSet.DispImage(Image, Hwindow);
                //HOperatorSet.DispObj(Image, Hwindow);
                ho_ImageScaled.Dispose();
                HOperatorSet.ScaleImage(Image, out ho_ImageScaled, _measureLWScaleNum, 0);
                ho_Regions.Dispose();
                HOperatorSet.Threshold(ho_ImageScaled, out ho_Regions, 0, _measureLWThreshold);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_Regions, out ho_RegionFillUp);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_ConnectedRegions, out ho_RegionOpening, _measureLWOpeningRectangle,
                    _measureLWOpeningRectangle);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_RegionOpening, out ho_SelectedRegions, "max_area",
                    70);
                ho_RegionTrans.Dispose();
                HOperatorSet.ShapeTrans(ho_SelectedRegions, out ho_RegionTrans, "rectangle2");
                HOperatorSet.SmallestRectangle2(ho_RegionTrans, out hv_Row, out hv_Column, out hv_Phi,
                    out hv_Length1, out hv_Length2);
 
                // HOperatorSet.GetContourXld(ho_RegionTrans, out hv_CornerRow, out hv_CornerColumn);
                HOperatorSet.SetColor(Hwindow, "green");
                HOperatorSet.SetDraw(Hwindow, "margin");
                HOperatorSet.DispObj(ho_RegionTrans, Hwindow);
 
 
 
                //rectangle2腔侐跺階萸
                HOperatorSet.TupleCos(hv_Phi, out hv_Cos);
                HOperatorSet.TupleSin(hv_Phi, out hv_Sin);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                //--------------------------------------------------------------------
                hv_RT_X = ((-hv_Length1) * hv_Cos) - (hv_Length2 * hv_Sin);
                hv_RT_Y = ((-hv_Length1) * hv_Sin) + (hv_Length2 * hv_Cos);
                //數呾腕善酘奻萸腔釴梓
                hv_RowLeftDown = hv_Row - hv_RT_Y;
                hv_ColLeftDown = hv_Column + hv_RT_X;
                //gen_circle (Circle, Row-RT_Y, Column+RT_X, 10)
                //--------------------------------------------------------------------
                hv_RB_X = (hv_Length1 * hv_Cos) - (hv_Length2 * hv_Sin);
                hv_RB_Y = (hv_Length1 * hv_Sin) + (hv_Length2 * hv_Cos);
 
                hv_RowLeftUp = hv_Row - hv_RT_Y;
                hv_ColLeftUp = hv_Column + hv_RT_X;
                //gen_circle (Circle, Row-RB_Y, Column+RB_X, 10)
                //--------------------------------------------------------------------
                hv_LB_X = (hv_Length1 * hv_Cos) + (hv_Length2 * hv_Sin);
                hv_LB_Y = (hv_Length1 * hv_Sin) - (hv_Length2 * hv_Cos);
 
                hv_RowRightUp = hv_Row - hv_LB_Y;
                hv_RowRightUp = hv_Column + hv_LB_X;
                //gen_circle (Circle, Row-LB_Y, Column+LB_X, 10)
                //--------------------------------------------------------------------
                hv_LT_X = ((-hv_Length1) * hv_Cos) + (hv_Length2 * hv_Sin);
                hv_LT_Y = ((-hv_Length1) * hv_Sin) - (hv_Length2 * hv_Cos);
 
                hv_RowRightDown = hv_Row - hv_LT_Y;
                hv_RowRightDown = hv_Column + hv_LT_X;
                //gen_circle (Circle, Row-LT_Y, Column+LT_X, 10)
 
                HOperatorSet.DistancePp(hv_Row - hv_RT_Y, hv_Column + hv_RT_X, hv_Row - hv_RB_Y, hv_Column + hv_RB_X,
                    out hv_Distance);
                hv_Length = hv_Distance * hv_scale;
 
                HOperatorSet.DistancePp(hv_Row - hv_RT_Y, hv_Column + hv_RT_X, hv_Row - hv_LT_Y, hv_Column + hv_LT_X,
                    out hv_Distance1);
                hv_Width = (hv_Distance1 - 10) * hv_scale;
 
                batteryLength = hv_Length.D;
                batteryWidth = hv_Width.D;
                //ho_Image.Dispose();
                ho_ImageScaled.Dispose();
                ho_Regions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionOpening.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionTrans.Dispose();
            }
            catch
            {
                // ho_Image.Dispose();
                ho_ImageScaled.Dispose();
                ho_Regions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionOpening.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionTrans.Dispose();
            }
 
 
        }
        //***********************************Adam End********************************************************************
 
 
 
 
        public void M423MeasureLandW(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
            HObject ho_Rectangle4, ho_ImageReduced1;
            HObject ho_ImageMean, ho_Region, ho_ConnectedRegions1, ho_Battery;
            HObject ho_Contour, ho_Rectangle, ho_ImageAffinTrans, ho_ContoursAffinTrans;
 
 
            // Local control variables 
 
            HTuple hv_scale = null, hv_BL = null, hv_BW = null;
            HTuple hv_convexpoint = null, hv_Seconds = null, hv_Width = null;
            HTuple hv_Height = null, hv_LowThreshold = null, hv_HighThreshold = null;
            HTuple hv_Row = null, hv_Column = null, hv_Phi = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_PointOrder = null;
            HTuple hv_HomMat2DIdentity = null, hv_HomMat2DRotate = null;
            HTuple hv_CornerRow = null, hv_CornerColumn = null, hv_Row1 = null;
            HTuple hv_Column1 = null, hv_Phi1 = null, hv_Length11 = null;
            HTuple hv_Length21 = null, hv_BatteryWidth = null, hv_BatteryLebgth = null;
 
            // Initialize local and output iconic variables 
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Rectangle4);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            //ho_Image = Image;
            hv_scale = 0.013585;
            hv_BL = new HTuple();
            hv_BW = new HTuple();
            hv_convexpoint = 1;
            HOperatorSet.SetLineWidth(Hwindow, 2);
            try
            {
 
 
                //ho_Image.Dispose();
                //HOperatorSet.ReadImage(out ho_Image, "C:/Users/Adam/Desktop/Image/陔膘恅璃標/142417430.tif");
 
                HOperatorSet.CountSeconds(out hv_Seconds);
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height - 1, hv_Width - 1);
                //dev_close_window(...);
                //dev_open_window(...);
                HOperatorSet.DispObj(Image, Hwindow);
                HOperatorSet.SetDraw(Hwindow, "margin");
                //*****聆萇喀酗
                hv_LowThreshold = 55;
 
                //蓒硉蚕240蜊峈200
                hv_HighThreshold = 201;
                ho_Rectangle4.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle4, 180, 500, 2500, 3350);
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle4, out ho_ImageReduced1);
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_ImageReduced1, out ho_ImageMean, 10, 10);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageMean, out ho_Region, 0, hv_LowThreshold);
                //opening_rectangle1 (Region, RegionOpening, 10, 10)
                //closing_rectangle1 (RegionOpening, RegionClosing, 200, 200)
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions1);
                ho_Battery.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_Battery, "max_area",
                    0);
                ho_Contour.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Battery, out ho_Contour, "border");
                HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", 10, 0, 0, 3, 2,
                    out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2, out hv_PointOrder);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_Row, hv_Column, hv_Phi,
                    hv_Length1, hv_Length2);
 
                //*****聆萇喀遵
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Column, hv_Row,
                    out hv_HomMat2DRotate);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2DRotate,
                    "constant", "false");
                ho_ContoursAffinTrans.Dispose();
                HOperatorSet.AffineTransContourXld(ho_Rectangle, out ho_ContoursAffinTrans, hv_HomMat2DRotate);
                HOperatorSet.GetContourXld(ho_ContoursAffinTrans, out hv_CornerRow, out hv_CornerColumn);
 
                HOperatorSet.SmallestRectangle2Xld(ho_ContoursAffinTrans, out hv_Row1, out hv_Column1,
                    out hv_Phi1, out hv_Length11, out hv_Length21);
                HOperatorSet.SetColor(Hwindow, "green");
                HOperatorSet.DispObj(ho_ContoursAffinTrans, Hwindow);
                batteryWidth = (hv_Length21 * 2) * hv_scale;
                batteryLength = (hv_Length11 * 2) * hv_scale;
                // ho_Image.Dispose();
                ho_Rectangle4.Dispose();
                ho_ImageReduced1.Dispose();
                ho_ImageMean.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ContoursAffinTrans.Dispose();
            }
            catch
            {
                batteryLength = 999.999;
                batteryWidth = 999.999;
                // ho_Image.Dispose();
                ho_Rectangle4.Dispose();
                ho_ImageReduced1.Dispose();
                ho_ImageMean.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ContoursAffinTrans.Dispose();
            }
 
        }
 
 
        public void M423MeasureLW2(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth)
        {
            // Local iconic variables 
 
            HObject ho_Region = null, ho_RegionOpening = null;
            HObject ho_RegionClosing = null, ho_ConnectedRegions1 = null;
            HObject ho_Battery = null, ho_Contour = null, ho_Rectangle = null;
            HObject ho_ImageAffinTrans = null, ho_ContoursAffinTrans = null;
            HObject ho_Regions = null, ho_ObjectSelected1 = null, ho_RegionClosing1 = null;
            HObject ho_Rectangle1 = null, ho_ImageReduced = null, ho_Region1 = null;
            HObject ho_Rectangle2 = null, ho_RectangleFlex = null, ho_RegionLineFlex = null;
            HObject ho_RectangleTR = null, ho_RegionLineTR = null, ho_RectangleTL = null;
            HObject ho_RegionLineTL = null, ho_RegionLineL = null, ho_RegionLineR = null;
            HObject ho_RegionLineT = null, ho_RegionOpening1 = null, ho_ConnectedRegions = null, ho_SelectedRegions = null;
 
 
            // Local control variables 
            HTuple hv_Width = new HTuple();
            HTuple hv_Height = new HTuple(), hv_LowThreshold = new HTuple();
            HTuple hv_HighThreshold = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_PointOrder = new HTuple(), hv_BatteryLength = new HTuple();
            HTuple hv_HomMat2DIdentity = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_CornerRow = new HTuple(), hv_CornerColumn = new HTuple();
            HTuple hv_i = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Row11 = new HTuple(), hv_Column11 = new HTuple();
            HTuple hv_Row21 = new HTuple(), hv_Column21 = new HTuple();
            HTuple hv_SigmaFlex = new HTuple(), hv_ThresholdFlex = new HTuple();
            HTuple hv_PhiFlex = new HTuple(), hv_RectCenterRowFlex = new HTuple();
            HTuple hv_RectCenterColumnFlex = new HTuple(), hv_Len1Flex = new HTuple();
            HTuple hv_Len2Flex = new HTuple(), hv_MeasureHandleFlex = new HTuple();
            HTuple hv_RowEdgeFlex = new HTuple(), hv_ColumnEdgeFlex = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_SigmaTR = new HTuple(), hv_ThresholdTR = new HTuple();
            HTuple hv_PhiTR = new HTuple(), hv_RectCenterRowTR = new HTuple();
            HTuple hv_RectCenterColumnTR = new HTuple(), hv_Len1TR = new HTuple();
            HTuple hv_Len2TR = new HTuple(), hv_MeasureHandleTR = new HTuple();
            HTuple hv_RowEdgeTR = new HTuple(), hv_ColumnEdgeTR = new HTuple();
            HTuple hv_SigmaTL = new HTuple(), hv_ThresholdTL = new HTuple();
            HTuple hv_PhiTL = new HTuple(), hv_RectCenterRowTL = new HTuple();
            HTuple hv_RectCenterColumnTL = new HTuple(), hv_Len1TL = new HTuple();
            HTuple hv_Len2TL = new HTuple(), hv_MeasureHandleTL = new HTuple();
            HTuple hv_RowEdgeTL = new HTuple(), hv_ColumnEdgeTL = new HTuple();
            HTuple hv_RowEdgeT = new HTuple(), hv_ColumnEdgeT = new HTuple();
            HTuple hv_BatteryWidth = new HTuple(), hv_Seconds1 = new HTuple();
            HTuple hv_t = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle2);
            HOperatorSet.GenEmptyObj(out ho_RectangleFlex);
            HOperatorSet.GenEmptyObj(out ho_RegionLineFlex);
            HOperatorSet.GenEmptyObj(out ho_RectangleTR);
            HOperatorSet.GenEmptyObj(out ho_RegionLineTR);
            HOperatorSet.GenEmptyObj(out ho_RectangleTL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineTL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineL);
            HOperatorSet.GenEmptyObj(out ho_RegionLineR);
            HOperatorSet.GenEmptyObj(out ho_RegionLineT);
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                //*****??喀?
                hv_LowThreshold = 100;
                hv_HighThreshold = 240;
                ho_Region.Dispose();
                HOperatorSet.Threshold(Image, out ho_Region, 0, hv_LowThreshold);
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 50, 50);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingRectangle1(ho_RegionOpening, out ho_RegionClosing, 200,
                    200);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions1);
                ho_Battery.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_Battery, "max_area",
                    0);
                ho_Contour.Dispose();
                HOperatorSet.GenContourRegionXld(ho_Battery, out ho_Contour, "border");
                HOperatorSet.SmallestRectangle2Xld(ho_Contour, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2);
                //HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", -1, 0, 0, 3,
                //    2, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2,
                //    out hv_PointOrder);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_Row, hv_Column, hv_Phi,
                    hv_Length1, hv_Length2);
                //*****??喀?
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Column, hv_Row,
                    out hv_HomMat2DRotate);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(Image, out ho_ImageAffinTrans, hv_HomMat2DRotate,
                    "constant", "false");
                ho_ContoursAffinTrans.Dispose();
                HOperatorSet.AffineTransContourXld(ho_Rectangle, out ho_ContoursAffinTrans,
                    hv_HomMat2DRotate);
                HOperatorSet.GetContourXld(ho_ContoursAffinTrans, out hv_CornerRow, out hv_CornerColumn);
                hv_CornerRow = hv_CornerRow.TupleSelectRange(0, 3);
                hv_CornerColumn = hv_CornerColumn.TupleSelectRange(0, 3);
                //****梑侐?芢??郖
                ho_Regions.Dispose();
                ConvexistyArea(ho_ImageAffinTrans, out ho_Regions, hv_LowThreshold, hv_HighThreshold);
                for (hv_i = 1; (int)hv_i <= 2; hv_i = (int)hv_i + 1)
                {
                    ho_ObjectSelected1.Dispose();
                    HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected1, hv_i);
                    ho_RegionClosing1.Dispose();
                    HOperatorSet.ClosingRectangle1(ho_ObjectSelected1, out ho_RegionClosing1,
                        20, 20);
                    HOperatorSet.SmallestRectangle1(ho_RegionClosing1, out hv_Row1, out hv_Column1,
                        out hv_Row2, out hv_Column2);
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2,
                        hv_Column2);
                    if ((int)(new HTuple(hv_i.TupleEqual(1))) != 0)
                    {
                        ho_Rectangle1.Dispose();
                        HOperatorSet.GenRectangle1(out ho_Rectangle1, hv_Row1 - 200, hv_Column1 - 300,
                            hv_Row1 + 50, hv_Column2);
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_ImageAffinTrans, ho_Rectangle1, out ho_ImageReduced
                            );
                        ho_Region1.Dispose();
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, hv_LowThreshold);
                        ho_RegionOpening1.Dispose();
                        HOperatorSet.OpeningCircle(ho_Region1, out ho_RegionOpening1, 6.5);
                        ho_ConnectedRegions.Dispose();
                        HOperatorSet.Connection(ho_RegionOpening1, out ho_ConnectedRegions);
                        ho_SelectedRegions.Dispose();
                        HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions,
                            "max_area", 0);
                        HOperatorSet.SmallestRectangle1(ho_SelectedRegions, out hv_Row11, out hv_Column11,
                            out hv_Row21, out hv_Column21);
                        ho_Rectangle2.Dispose();
                        HOperatorSet.GenRectangle1(out ho_Rectangle2, hv_Row11, hv_Column11, hv_Row21,
                            hv_Column21);
                        hv_SigmaFlex = 1.2;
                        hv_ThresholdFlex = 10;
                        hv_PhiFlex = (new HTuple(90)).TupleRad();
                        hv_RectCenterRowFlex = hv_Row21.Clone();
                        hv_RectCenterColumnFlex = (hv_Column11 + hv_Column21) * 0.5;
                        hv_Len1Flex = 30;
                        hv_Len2Flex = (hv_Column21 - hv_Column11) * 0.5;
                        ho_RectangleFlex.Dispose();
                        HOperatorSet.GenRectangle2(out ho_RectangleFlex, hv_RectCenterRowFlex,
                            hv_RectCenterColumnFlex, hv_PhiFlex, hv_Len1Flex, hv_Len2Flex);
                        HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowFlex, hv_RectCenterColumnFlex,
                            hv_PhiFlex, hv_Len1Flex, hv_Len2Flex, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandleFlex);
                        HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleFlex, hv_SigmaFlex,
                            hv_ThresholdFlex, "negative", "all", out hv_RowEdgeFlex, out hv_ColumnEdgeFlex,
                            out hv_Amplitude, out hv_Distance);
                        ho_RegionLineFlex.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                            0), (hv_ColumnEdgeFlex.TupleSelect(0)) - hv_Len2Flex, hv_RowEdgeFlex.TupleSelect(
                            0), (hv_ColumnEdgeFlex.TupleSelect(0)) + hv_Len2Flex);
                    }
                }
                //****奻衵
                hv_SigmaTR = 1.5;
                hv_ThresholdTR = 15;
                hv_PhiTR = (new HTuple(-90)).TupleRad();
                hv_RectCenterRowTR = hv_Row2.Clone();
                hv_RectCenterColumnTR = hv_Column2 + 650;
                hv_Len1TR = 30;
                hv_Len2TR = 600;
                ho_RectangleTR.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleTR, hv_RectCenterRowTR, hv_RectCenterColumnTR,
                    hv_PhiTR, hv_Len1TR, hv_Len2TR);
                HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowTR, hv_RectCenterColumnTR,
                    hv_PhiTR, hv_Len1TR, hv_Len2TR, hv_Width, hv_Height, "nearest_neighbor",
                    out hv_MeasureHandleTR);
                HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleTR, hv_SigmaTR,
                    hv_ThresholdTR, "negative", "all", out hv_RowEdgeTR, out hv_ColumnEdgeTR,
                    out hv_Amplitude, out hv_Distance);
                ho_RegionLineTR.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineTR, hv_RowEdgeTR.TupleSelect(0),
                    (hv_ColumnEdgeTR.TupleSelect(0)) - hv_Len2TR, hv_RowEdgeTR.TupleSelect(0),
                    (hv_ColumnEdgeTR.TupleSelect(0)) + hv_Len2TR);
                //***奻酘
                hv_SigmaTL = 1.5;
                hv_ThresholdTL = 15;
                hv_PhiTL = (new HTuple(90)).TupleRad();
                hv_RectCenterRowTL = hv_CornerRow[1];
                hv_RectCenterColumnTL = (hv_CornerColumn.TupleSelect(1)) + 135;
                hv_Len1TL = 50;
                hv_Len2TL = 120;
                ho_RectangleTL.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleTL, hv_RectCenterRowTL, hv_RectCenterColumnTL,
                    hv_PhiTL, hv_Len1TL, hv_Len2TL);
                HOperatorSet.GenMeasureRectangle2(hv_RectCenterRowTL, hv_RectCenterColumnTL,
                    hv_PhiTL, hv_Len1TL, hv_Len2TL, hv_Width, hv_Height, "nearest_neighbor",
                    out hv_MeasureHandleTL);
                HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandleTL, hv_SigmaTL,
                    hv_ThresholdTL, "positive", "all", out hv_RowEdgeTL, out hv_ColumnEdgeTL,
                    out hv_Amplitude, out hv_Distance);
                ho_RegionLineTL.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineTL, hv_RowEdgeTL.TupleSelect(0),
                    (hv_ColumnEdgeTL.TupleSelect(0)) - hv_Len2TL, hv_RowEdgeTL.TupleSelect(0),
                    (hv_ColumnEdgeTL.TupleSelect(0)) + hv_Len2TL);
 
                //*****??俴釴?苤腔?
                hv_RowEdgeT = new HTuple();
                hv_ColumnEdgeT = new HTuple();
                if ((int)(new HTuple(((hv_RowEdgeTL.TupleSelect(0))).TupleLess(0//hv_RowEdgeTR.TupleSelect(0)
                    ))) != 0)
                {
                    hv_RowEdgeT = hv_RowEdgeTL[0];
                    hv_ColumnEdgeT = hv_ColumnEdgeTL[0];
                }
                else
                {
                    hv_RowEdgeT = hv_RowEdgeTR[0];
                    hv_ColumnEdgeT = hv_ColumnEdgeTR[0];
                }
                ho_RegionLineL.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineL, 0, hv_CornerColumn.TupleSelect(
                    1), hv_Height, hv_CornerColumn.TupleSelect(2));
                ho_RegionLineR.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineR, 0, hv_CornerColumn.TupleSelect(
                    0), hv_Height, hv_CornerColumn.TupleSelect(3));
                ho_RegionLineFlex.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineFlex, hv_RowEdgeFlex.TupleSelect(
                    0), 0, hv_RowEdgeFlex.TupleSelect(0), hv_Width);
                ho_RegionLineT.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLineT, hv_RowEdgeT.TupleSelect(0),
                    0, hv_RowEdgeT.TupleSelect(0), hv_Width);
                hv_BatteryWidth = (hv_RowEdgeFlex.TupleSelect(0)) - (hv_RowEdgeT.TupleSelect(
                    0));
                hv_BatteryLength = 2 * hv_Length1;
                batteryLength = hv_BatteryLength.D;
                batteryWidth = hv_BatteryWidth.D;
                HOperatorSet.SetColor(Hwindow, "green");
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineT, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineFlex, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineL, Hwindow);
                HOperatorSet.DispObj(ho_RegionLineR, Hwindow);
            }
            catch (HalconException)
            {
 
                batteryLength = 999.999;
                batteryWidth = 999.999;
                VisionDetect.DisplayImage(ho_ImageAffinTrans, Hwindow);
 
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_Region.Dispose();
                ho_RegionOpening.Dispose();
                ho_RegionClosing.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Battery.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_ContoursAffinTrans.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_RegionClosing1.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region1.Dispose();
                ho_Rectangle2.Dispose();
                ho_RectangleFlex.Dispose();
                ho_RegionLineFlex.Dispose();
                ho_RectangleTR.Dispose();
                ho_RegionLineTR.Dispose();
                ho_RectangleTL.Dispose();
                ho_RegionLineTL.Dispose();
                ho_RegionLineL.Dispose();
                ho_RegionLineR.Dispose();
                ho_RegionLineT.Dispose();
            }
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionClosing.Dispose();
            ho_ConnectedRegions1.Dispose();
            ho_Battery.Dispose();
            ho_Contour.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageAffinTrans.Dispose();
            ho_ContoursAffinTrans.Dispose();
            ho_Regions.Dispose();
            ho_ObjectSelected1.Dispose();
            ho_RegionClosing1.Dispose();
            ho_Rectangle1.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region1.Dispose();
            ho_Rectangle2.Dispose();
            ho_RectangleFlex.Dispose();
            ho_RegionLineFlex.Dispose();
            ho_RectangleTR.Dispose();
            ho_RegionLineTR.Dispose();
            ho_RectangleTL.Dispose();
            ho_RegionLineTL.Dispose();
            ho_RegionLineL.Dispose();
            ho_RegionLineR.Dispose();
            ho_RegionLineT.Dispose();
        }
        public void M423ConvexPoint(HObject ho_Image, HTuple hv_Row1, HTuple hv_Coloumn1,
    HTuple hv_Row2, HTuple hv_Coloumn2, HTuple hv_Phi, out HTuple hv_RowConvex,
    out HTuple hv_ColumnConvex)
        {
 
 
 
            // Local iconic variables 
 
            HObject ho_Rectangle, ho_RegionLines, ho_RegionLines1;
 
 
            // Local control variables 
 
            HTuple hv_Width = null, hv_Height = null, hv_MeasureHandle = null;
            HTuple hv_RowEdge = null, hv_ColumnEdge = null, hv_Amplitude = null;
            HTuple hv_Distance = null, hv_MeasureHandle1 = null, hv_RowEdge1 = null;
            HTuple hv_ColumnEdge1 = null, hv_Amplitude1 = null, hv_Distance1 = null;
            HTuple hv_Greater1 = null, hv_Greater2 = null, hv_Or = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
 
            hv_RowConvex = new HTuple();
            hv_ColumnConvex = new HTuple();
            try
            {
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1 + 30, hv_Coloumn1 + 31, hv_Phi,
                    30, 30);
                HOperatorSet.GenMeasureRectangle2(hv_Row1 + 20, hv_Coloumn1 + 20, hv_Phi, 30, 30,
                    hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 2, 10, "positive", "first",
                    out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_RowEdge + 140, hv_ColumnEdge,
                    hv_RowEdge - 140, hv_ColumnEdge);
 
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row2 - 20, hv_Coloumn2 + 30, hv_Phi,
                    30, 30);
                HOperatorSet.GenMeasureRectangle2(hv_Row2 - 10, hv_Coloumn2 + 30, hv_Phi, 30, 30,
                    hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle1);
                HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle1, 2, 10, "positive", "first",
                    out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                ho_RegionLines1.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_RowEdge1 + 140, hv_ColumnEdge1,
                    hv_RowEdge1 - 140, hv_ColumnEdge1);
 
                HOperatorSet.TupleGreater(new HTuple(hv_RowEdge.TupleLength()), 0, out hv_Greater1);
                HOperatorSet.TupleGreater(new HTuple(hv_RowEdge1.TupleLength()), 0, out hv_Greater2);
                HOperatorSet.TupleOr(hv_Greater1, hv_Greater2, out hv_Or);
 
                if ((int)(new HTuple(hv_Or.TupleLess(1))) != 0)
                {
                    hv_RowConvex = hv_Row1.Clone();
                    hv_ColumnConvex = hv_Coloumn1.Clone();
 
                }
                else
                {
                    if ((int)(new HTuple(hv_ColumnEdge.TupleGreater(hv_ColumnEdge1))) != 0)
                    {
                        hv_RowConvex = hv_RowEdge[0];
                        hv_ColumnConvex = hv_ColumnEdge[0];
                    }
                    else
                    {
                        hv_RowConvex = hv_RowEdge[0];
                        hv_ColumnConvex = hv_ColumnEdge1[0];
                    }
                }
 
 
                ho_Rectangle.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionLines1.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Rectangle.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionLines1.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        //public void M423ConvexPoint(HObject ho_ImageWidth, HTuple hv_Row1, HTuple hv_Column1, HTuple hv_Row2, HTuple hv_Column2, HTuple hv_Pi, out HTuple hv_RowConvex, out HTuple hv_ColumnConvex)
        //{
 
 
 
        //    // Local iconic variables 
 
        //    HObject ho_EmptyObject, ho_Rectangle, ho_RegionLines;
        //    HObject ho_RegionLines1;
 
 
        //    // Local control variables 
 
        //    HTuple hv_Width = null, hv_Height = null, hv_MeasureHandle = null;
        //    HTuple hv_RowEdge = null, hv_ColumnEdge = null, hv_Amplitude = null;
        //    HTuple hv_Distance = null, hv_MeasureHandle1 = null, hv_RowEdge1 = null;
        //    HTuple hv_ColumnEdge1 = null, hv_Amplitude1 = null, hv_Distance1 = null;
 
        //    // Initialize local and output iconic variables 
        //    HOperatorSet.GenEmptyObj(out ho_EmptyObject);
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle);
        //    HOperatorSet.GenEmptyObj(out ho_RegionLines);
        //    HOperatorSet.GenEmptyObj(out ho_RegionLines1);
 
        //    hv_RowConvex = new HTuple();
        //    hv_ColumnConvex = new HTuple();
        //    try
        //    {
        //        ho_EmptyObject.Dispose();
        //        HOperatorSet.GenEmptyObj(out ho_EmptyObject);
        //        ho_EmptyObject.Dispose();
        //        HOperatorSet.CopyImage(ho_ImageWidth, out ho_EmptyObject);
        //        HOperatorSet.GetImageSize(ho_EmptyObject, out hv_Width, out hv_Height);
        //        ho_Rectangle.Dispose();
        //        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1 + 20, hv_Column1 + 20, hv_Pi,
        //            30, 30);
        //        HOperatorSet.GenMeasureRectangle2(hv_Row1 + 20, hv_Column1 + 20, hv_Pi, 30, 30,
        //            hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
        //        HOperatorSet.MeasurePos(ho_EmptyObject, hv_MeasureHandle, 1, 30, "all", "all",
        //            out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
        //        ho_RegionLines.Dispose();
        //        HOperatorSet.GenRegionLine(out ho_RegionLines, hv_RowEdge + 140, hv_ColumnEdge,
        //            hv_RowEdge - 140, hv_ColumnEdge);
 
        //        ho_Rectangle.Dispose();
        //        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row2 - 20, hv_Column2 + 30, hv_Pi,
        //            30, 30);
        //        HOperatorSet.GenMeasureRectangle2(hv_Row2 - 20, hv_Column2 + 30, hv_Pi, 30, 30,
        //            hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle1);
        //        HOperatorSet.MeasurePos(ho_EmptyObject, hv_MeasureHandle1, 1, 30, "all", "all",
        //            out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
        //        ho_RegionLines1.Dispose();
        //        HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_RowEdge1 + 140, hv_ColumnEdge1,
        //            hv_RowEdge1 - 140, hv_ColumnEdge1);
 
        //        if ((int)(new HTuple(hv_ColumnEdge.TupleGreater(hv_ColumnEdge1))) != 0)
        //        {
        //            hv_RowConvex = hv_RowEdge.Clone();
        //            hv_ColumnConvex = hv_ColumnEdge.Clone();
 
        //        }
        //        else
        //        {
        //            hv_RowConvex = hv_RowEdge1.Clone();
        //            hv_ColumnConvex = hv_ColumnEdge1.Clone();
        //        }
        //        ho_EmptyObject.Dispose();
        //        ho_Rectangle.Dispose();
        //        ho_RegionLines.Dispose();
        //        ho_RegionLines1.Dispose();
 
        //        return;
        //    }
        //    catch (HalconException HDevExpDefaultException)
        //    {
        //        ho_EmptyObject.Dispose();
        //        ho_Rectangle.Dispose();
        //        ho_RegionLines.Dispose();
        //        ho_RegionLines1.Dispose();
 
        //        throw HDevExpDefaultException;
        //    }
        //}
 
 
        public void X338FindCornerPoint(HObject ho_Image, HObject ho_Rectangle1, HObject ho_Rectangle2,
                                        out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Phi)
        {
 
 
            // Local iconic variables 
 
            HObject ho_Line1, ho_Line2, ho_Cross;
 
 
            // Local control variables 
 
            HTuple hv_RowBegin = null, hv_ColBegin = null;
            HTuple hv_RowEnd = null, hv_ColEnd = null, hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null, hv_RowBegin1 = null;
            HTuple hv_ColBegin1 = null, hv_RowEnd1 = null, hv_ColEnd1 = null;
            HTuple hv_Nr1 = null, hv_Nc1 = null, hv_Dist1 = null, hv_IsOverlapping = null;
            HTuple hv_RowCenter = null, hv_ColumnCenter = null, hv_Length4 = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Line1);
            HOperatorSet.GenEmptyObj(out ho_Line2);
            HOperatorSet.GenEmptyObj(out ho_Cross);
 
            ho_Line1.Dispose();
            X338SelectLongestLine(ho_Image, ho_Rectangle1, out ho_Line1);
            ho_Line2.Dispose();
            X338SelectLongestLine(ho_Image, ho_Rectangle2, out ho_Line2);
            HOperatorSet.FitLineContourXld(ho_Line1, "tukey", -1, 0, 5, 2, out hv_RowBegin,
                out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.FitLineContourXld(ho_Line2, "tukey", -1, 0, 5, 2, out hv_RowBegin1,
                out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr1, out hv_Nc1,
                out hv_Dist1);
            //get_contour_xld (Line1, Row3, Col)
            //get_contour_xld (Line2, Row4, Col1)
            //BeginRow := Row3[0]
            //BeginCol := Col[0]
            //EndRow := Row3[|Row3|-1]
            //EndCol := Col[|Col|-1]
            //BeginRow1 := Row4[0]
            //BeginCol1 := Col1[0]
            //EndRow1 := Row4[|Row4|-1]
            //EndCol1 := Col1[|Col1|-1]
            HOperatorSet.IntersectionLines(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd, out hv_Row, out hv_Column,
                out hv_IsOverlapping);
            ho_Cross.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 26, 0);
            HOperatorSet.LinePosition(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                out hv_RowCenter, out hv_ColumnCenter, out hv_Length4, out hv_Phi);
            ho_Line1.Dispose();
            ho_Line2.Dispose();
            ho_Cross.Dispose();
 
            return;
        }
        public void X338SelectLongestLine(HObject ho_Image, HObject ho_ROI, out HObject ho_Line)
        {
 
 
            // Local iconic variables 
 
            HObject ho_ImageReduced, ho_Edges, ho_UnionContours;
 
 
            // Local control variables 
 
            HTuple hv_Length = null, hv_Max = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Line);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Edges);
            HOperatorSet.GenEmptyObj(out ho_UnionContours);
 
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI, out ho_ImageReduced);
            ho_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_ImageReduced, out ho_Edges, "canny", 1.0, 10, 30);
            //segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
            ho_UnionContours.Dispose();
            HOperatorSet.UnionAdjacentContoursXld(ho_Edges, out ho_UnionContours, 10, 1,
                "attr_keep");
 
            HOperatorSet.LengthXld(ho_UnionContours, out hv_Length);
            HOperatorSet.TupleMax(hv_Length, out hv_Max);
            ho_Line.Dispose();
            HOperatorSet.SelectContoursXld(ho_UnionContours, out ho_Line, "contour_length",
                hv_Max - 0.001, hv_Max + 0.001, -0.5, 0.5);
            ho_ImageReduced.Dispose();
            ho_Edges.Dispose();
            ho_UnionContours.Dispose();
 
            return;
        }
        public void BatteryCenter(HObject ho_Region, out HObject ho_Rectangle, out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Length1, out HTuple hv_Length2)
        {
            // Local iconic variables 
 
            HObject ho_RegionOpening, ho_Contour;
 
 
            // Local control variables 
 
            HTuple hv_Phi = null, hv_PointOrder = null;
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_Contour);
 
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, 180, 100);
            ho_Contour.Dispose();
            HOperatorSet.GenContourRegionXld(ho_RegionOpening, out ho_Contour, "border");
            HOperatorSet.FitRectangle2ContourXld(ho_Contour, "regression", -1, 0, 0, 3, 2,
                out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2, out hv_PointOrder);
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_Row, hv_Column, hv_Phi,
                hv_Length1, hv_Length2);
            ho_RegionOpening.Dispose();
            ho_Contour.Dispose();
 
            return;
        }
        public void LoadLocation(HObject image, ref HTuple row, ref HTuple column, ref HTuple angle)
        {
            // Local iconic variables 
 
            HObject ho_ROI = null, ho_ImageReduced = null;
            HObject ho_Batteries = null, ho_Battery = null, ho_Rectangle = null;
            HObject ho_Cross = null, ho_RectangleV = null, ho_RectangleH = null;
 
 
            // Local control variables 
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_LowThreshold = new HTuple(), hv_HighThreshold = new HTuple();
            HTuple hv_RectRow1 = new HTuple(), hv_RectColumn1 = new HTuple();
            HTuple hv_RectRow2 = new HTuple(), hv_RectColumn2 = new HTuple();
            HTuple hv_Number = new HTuple(), hv_CornerRows = new HTuple();
            HTuple hv_CornerColumns = new HTuple(), hv_CornerPhis = new HTuple();
            HTuple hv_Index1 = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_CenterRow = new HTuple(), hv_CenterColumn = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_CornerRow = new HTuple(), hv_CornerColumn = new HTuple();
            HTuple hv_CornerPhi = new HTuple(), hv_BatteryStatus = new HTuple();
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Batteries);
            HOperatorSet.GenEmptyObj(out ho_Battery);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_RectangleV);
            HOperatorSet.GenEmptyObj(out ho_RectangleH);
            try
            {
                HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
                hv_LowThreshold = 0;
                hv_HighThreshold = 160;
                hv_RectRow1 = 300;
                hv_RectColumn1 = 5.5;
                hv_RectRow2 = 750.5;
                hv_RectColumn2 = 1500;
                ho_ROI.Dispose();
                HOperatorSet.GenRectangle1(out ho_ROI, hv_RectRow1, hv_RectColumn1, hv_RectRow2,
                    hv_RectColumn2);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(image, ho_ROI, out ho_ImageReduced);
                ho_Batteries.Dispose();
                SelectBattery(ho_ImageReduced, out ho_Batteries, hv_LowThreshold, hv_HighThreshold, out hv_BatteryStatus);
                ho_Battery.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Battery);
                HOperatorSet.CountObj(ho_Batteries, out hv_Number);
                hv_CornerRows = new HTuple();
                hv_CornerColumns = new HTuple();
                hv_CornerPhis = new HTuple();
                HTuple end_val27 = hv_Number;
                HTuple step_val27 = 1;
                for (hv_Index1 = 1; hv_Index1.Continue(end_val27, step_val27); hv_Index1 = hv_Index1.TupleAdd(step_val27))
                {
                    ho_Battery.Dispose();
                    HOperatorSet.SelectObj(ho_Batteries, out ho_Battery, hv_Index1);
                    HOperatorSet.AreaCenter(ho_Battery, out hv_Area, out hv_Row, out hv_Column);
                    if ((int)(new HTuple(hv_Area.TupleNotEqual(0))) != 0)
                    {
                        ho_Rectangle.Dispose();
                        BatteryCenter(ho_Battery, out ho_Rectangle, out hv_CenterRow, out hv_CenterColumn,
                            out hv_Length1, out hv_Length2);
                        ho_Cross.Dispose();
                        HOperatorSet.GenCrossContourXld(out ho_Cross, hv_CenterRow, hv_CenterColumn,
                            26, 0.785398);
                        ho_RectangleV.Dispose();
                        HOperatorSet.GenRectangle2(out ho_RectangleV, hv_CenterRow, hv_CenterColumn - hv_Length1,
                            (new HTuple(90)).TupleRad(), hv_Length2 - 75, 30);
                        ho_RectangleH.Dispose();
                        HOperatorSet.GenRectangle2(out ho_RectangleH, hv_CenterRow - hv_Length2,
                            hv_CenterColumn, 0, 75, 30);
                        X338FindCornerPoint(ho_ImageReduced, ho_RectangleV, ho_RectangleH, out hv_CornerRow,
                            out hv_CornerColumn, out hv_CornerPhi);
                        if (hv_Index1 == 3)
                        {
                            hv_Index1 = hv_Index1 - 1;
                            hv_CornerRows = hv_CornerRows.TupleConcat(hv_CornerRow);
                            hv_CornerColumns = hv_CornerColumns.TupleConcat(hv_CornerColumn);
                            hv_Index1 = hv_Index1 + 1;
                        }
                        else
                        {
                            hv_CornerRows = hv_CornerRows.TupleConcat(hv_CornerRow);
                            hv_CornerColumns = hv_CornerColumns.TupleConcat(hv_CornerColumn);
                        }
                        hv_CornerPhis = hv_CornerPhis.TupleConcat(hv_CornerPhi);
                    }
                }
                row = hv_CornerRows;
                column = hv_CornerColumns;
                angle = hv_CornerPhis;
            }
            catch (HalconException)
            {
                row = 0;
                column = 0;
                angle = 0;
                ho_ROI.Dispose();
                ho_ImageReduced.Dispose();
                ho_Batteries.Dispose();
                ho_Battery.Dispose();
                ho_Rectangle.Dispose();
                ho_Cross.Dispose();
                ho_RectangleV.Dispose();
                ho_RectangleH.Dispose();
            }
 
            ho_ROI.Dispose();
            ho_ImageReduced.Dispose();
            ho_Batteries.Dispose();
            ho_Battery.Dispose();
            ho_Rectangle.Dispose();
            ho_Cross.Dispose();
            ho_RectangleV.Dispose();
            ho_RectangleH.Dispose();
        }
        //20181217 Johnny修改测量长宽的方法:
        //添加空料检测&输出
        //屏蔽长边测量中间部分,避免治具倒角影响检测结果
        //使用抓取轮廓的方式确定电池的边界,计算轮廓的最小外接矩形
        //修改电池的长宽为最小外接矩形的大小
        public void M423MeasureLandWJohnny(HObject Image, HTuple Hwindow, ref double batteryLength, ref double batteryWidth, ref bool isNa)
        {
            // Local iconic variables 
 
            HObject ho_Region, ho_ConnectedRegions;
            HObject ho_SelectedRegions, ho_RegionFillUp = null, ho_RegionOpening = null;
            HObject ho_Rectangle = null, ho_RegionDilation = null, ho_RegionErosion = null;
            HObject ho_RegionDifference = null, ho_RegionDifference1 = null;
            HObject ho_ImageReduced = null, ho_Edges = null, ho_SelectedContours = null;
            HObject ho_ObjectSelected = null, ho_Contour = null, ho_Rectangle1 = null;
 
            // Local control variables 
 
            HTuple hv_Width = null, hv_Height = null, hv_isNa = null;
            HTuple hv_Area = null, hv_Row = null, hv_Column = null;
            HTuple hv_Number1 = null, hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Phi1 = new HTuple(), hv_Length11 = new HTuple();
            HTuple hv_Length12 = new HTuple(), hv_RowAll = new HTuple();
            HTuple hv_ColAll = new HTuple(), hv_Number2 = new HTuple();
            HTuple hv_I = new HTuple(), hv_Col1 = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_Phi2 = new HTuple();
            HTuple hv_Length21 = new HTuple(), hv_Length22 = new HTuple();
            // Initialize local and output iconic variables 
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Edges);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
 
            double hv_scale = 0.01376753;
            isNa = false;
 
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height - 1, hv_Width - 1);
                HOperatorSet.DispImage(Image, Hwindow);
                hv_isNa = 0;
                ho_Region.Dispose();
                HOperatorSet.Threshold(Image, out ho_Region, 0, 100);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area",
                    70);
                HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number1);
                if ((int)((new HTuple(hv_Number1.TupleEqual(0))).TupleOr(new HTuple(hv_Area.TupleLess(
                    1800000)))) != 0)
                {
                    isNa = true;
                    batteryLength = 0;
                    batteryWidth = 0;
                }
                else
                {
                    ho_RegionFillUp.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp);
                    ho_RegionOpening.Dispose();
                    HOperatorSet.OpeningRectangle1(ho_RegionFillUp, out ho_RegionOpening, 50, 50);
                    batteryLength = 1;
                    HOperatorSet.SmallestRectangle2(ho_RegionOpening, out hv_Row1, out hv_Column1,
                        out hv_Phi1, out hv_Length11, out hv_Length12);
                    batteryLength = 2;
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1, hv_Column1, hv_Phi1,
                        hv_Length11 * 1.1, hv_Length12 * 0.5);
                    ho_RegionDilation.Dispose();
                    batteryLength = 3;
                    HOperatorSet.DilationRectangle1(ho_RegionOpening, out ho_RegionDilation, 20,
                        20);
                    ho_RegionErosion.Dispose();
                    HOperatorSet.ErosionRectangle1(ho_RegionOpening, out ho_RegionErosion, 10,
                        10);
                    batteryLength = 4;
                    ho_RegionDifference.Dispose();
                    HOperatorSet.Difference(ho_RegionDilation, ho_RegionErosion, out ho_RegionDifference
                        );
                    ho_RegionDifference1.Dispose();
                    HOperatorSet.Difference(ho_RegionDifference, ho_Rectangle, out ho_RegionDifference1
                        );
                    ho_ImageReduced.Dispose();
                    batteryLength = 5;
                    HOperatorSet.ReduceDomain(Image, ho_RegionDifference1, out ho_ImageReduced
                        );
                    ho_Edges.Dispose();
                    batteryLength = 6;
                    HOperatorSet.EdgesSubPix(ho_ImageReduced, out ho_Edges, "canny", 1, 60, 60);
                    batteryLength = 7;
                    ho_SelectedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_Edges, out ho_SelectedContours, "contour_length",
                        15, 1000000, -0.5, 0.5);
                    hv_RowAll = new HTuple();
                    hv_ColAll = new HTuple();
                    HOperatorSet.CountObj(ho_SelectedContours, out hv_Number2);
                    HTuple end_val27 = hv_Number2;
                    HTuple step_val27 = 1;
                    batteryLength = 8;
                    for (hv_I = 1; hv_I.Continue(end_val27, step_val27); hv_I = hv_I.TupleAdd(step_val27))
                    {
                        ho_ObjectSelected.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected, hv_I);
                        HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row1, out hv_Col1);
                        hv_RowAll = hv_RowAll.TupleConcat(hv_Row1);
                        hv_ColAll = hv_ColAll.TupleConcat(hv_Col1);
                    }
                    ho_Contour.Dispose();
                    batteryLength = 9;
                    HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowAll, hv_ColAll);
                    batteryLength = 10;
                    HOperatorSet.SmallestRectangle2Xld(ho_Contour, out hv_Row2, out hv_Column2,
                        out hv_Phi2, out hv_Length21, out hv_Length22);
                    ho_Rectangle1.Dispose();
                    batteryLength = 11;
                    HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row2, hv_Column2, hv_Phi2,
                        hv_Length21, hv_Length22);
                    HOperatorSet.SetColor(Hwindow, "green");
                    HOperatorSet.SetDraw(Hwindow, "margin");
                    HOperatorSet.DispObj(Image, Hwindow);
                    batteryLength = 12;
                    HOperatorSet.DispObj(ho_SelectedContours, Hwindow);
                    batteryLength = 13;
                    batteryLength = hv_Length21.D * 2 * hv_scale; ;
                    batteryWidth = hv_Length22.D * 2 * hv_scale; ;
                }
 
                //ho_Image.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_RegionOpening.Dispose();
                ho_Rectangle.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionErosion.Dispose();
                ho_RegionDifference.Dispose();
                ho_RegionDifference1.Dispose();
                ho_ImageReduced.Dispose();
                ho_Edges.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle1.Dispose();
            }
            catch
            {
                //ho_Image.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_RegionOpening.Dispose();
                ho_Rectangle.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionErosion.Dispose();
                ho_RegionDifference.Dispose();
                ho_RegionDifference1.Dispose();
                ho_ImageReduced.Dispose();
                ho_Edges.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Contour.Dispose();
                ho_Rectangle1.Dispose();
 
                batteryLength = 999.9;
                batteryWidth = 999.9;
            }
        }
 
 
        public HObject ho_Image { get; set; }
    }
}