领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.UI.hdisplay.ViewROI
{
    public class ROIPolygon : ROI
    {
        private List<double> rows;
 
        private List<double> cols;
 
        private double midR, midC;   // midpoint 
 
        HRegion region;
 
        private static ROIPolygon instance;
 
        protected override int NumHandles { get => rows.Count + 1; }
        
        public static ROIPolygon GetCurrentInstance()
        {
            if (instance == null)
                instance = new ROIPolygon();
 
            return instance;
        }
 
        private ROIPolygon()
        {
            midR = 0;
            midC = 0;
 
            rows = new List<double>();
            cols = new List<double>();
        }
 
        /// <summary>Creates a new ROI instance at the mouse position</summary>
        /// <param name="midX">
        /// x (=column) coordinate for interactive ROI
        /// </param>
        /// <param name="midY">
        /// y (=row) coordinate for interactive ROI
        /// </param>
        public override void createROI(double x, double y)
        {
            region = new HRegion();
 
            rows.Add(y);
            cols.Add(x);
        }
 
        public void CreateROIEnd()
        {
            HOperatorSet.GenRegionPolygon(out HObject hObject, new HTuple(rows.ToArray()), new HTuple(cols.ToArray()));
            HOperatorSet.AreaCenter(hObject, out HTuple area, out HTuple row, out HTuple col);
 
            midR = row;
            midC = col;
 
        }
 
        public void DisposeCurrentInstance()
        {
            instance = null;
        }
 
        /// <summary>Paints the ROI into the supplied window</summary>
        /// <param name="window">HALCON window</param>
        public override void draw(HalconDotNet.HWindow window)
        {
            if (rows.Count > 1)
            {
                region.GenRegionPolygonFilled(new HTuple(rows.ToArray()), new HTuple(cols.ToArray()));
                window.DispRegion(region);
            }
                
            for (int i = 0; i < rows.Count; i++)
            {
                window.DispRectangle2(rows[i], cols[i], 0, 5, 5);
            }
 
            if (midR != 0 || midC != 0)
                window.DispRectangle2(midR, midC, 0, 5, 5);
        }
 
        /// <summary> 
        /// Returns the distance of the ROI handle being
        /// closest to the image point(x,y)
        /// </summary>
        /// <param name="x">x (=column) coordinate</param>
        /// <param name="y">y (=row) coordinate</param>
        /// <returns> 
        /// Distance of the closest ROI handle.
        /// </returns>
        public override double distToClosestHandle(double x, double y)
        {
 
            double max = 10000;
            double[] val = new double[NumHandles];
 
 
            for (int i = 0; i < rows.Count; i++)
            {
                val[i] = HMisc.DistancePp(y, x, rows[i], cols[i]);
            }
 
            val[NumHandles - 1] = HMisc.DistancePp(y, x, midR, midC); // midpoint 
 
            for (int i = 0; i < NumHandles; i++)
            {
                if (val[i] < max)
                {
                    max = val[i];
                    activeHandleIdx = i;
                }
            }// end of for 
 
            return val[activeHandleIdx];
        }
 
        /// <summary> 
        /// Paints the active handle of the ROI object into the supplied window
        /// </summary>
        /// <param name="window">HALCON window</param>
        public override void displayActive(HalconDotNet.HWindow window)
        {
            if (activeHandleIdx == rows.Count)
                window.DispRectangle2(midR, midC, 0, 5, 5);
            else
                window.DispRectangle2(rows[activeHandleIdx], cols[activeHandleIdx], 0, 5, 5);
 
        }
 
        /// <summary>
        /// Gets the HALCON region described by the ROI
        /// </summary>
        public override HRegion getRegion()
        {
            region.GenRegionPolygonFilled(new HTuple(rows.ToArray()), new HTuple(cols.ToArray()));
            return region;
        }
 
        /// <summary>
        /// Gets the model information described by 
        /// the interactive ROI
        /// </summary> 
        public override HTuple getModelData()
        {
            HTuple val = new HTuple();
 
            val.Append(new HTuple(rows.ToArray()));
            val.Append(new HTuple(cols.ToArray()));
 
            return val;
        }
 
 
        /// <summary> 
        /// Recalculates the shape of the ROI instance. Translation is 
        /// performed at the active handle of the ROI object 
        /// for the image coordinate (x,y)
        /// </summary>
        /// <param name="newX">x mouse coordinate</param>
        /// <param name="newY">y mouse coordinate</param>
        public override void moveByHandle(double newX, double newY)
        {
            var deltaRow = new List<double>();
            var deltaCol = new List<double>();
 
            for (int i = 0; i < rows.Count; i++)
            {
                deltaRow.Add(rows[i] - midR);
                deltaCol.Add(cols[i] - midC);
            }
 
            if (activeHandleIdx == rows.Count)
            {
                midR = newY;
                midC = newX;
 
                for (int i = 0; i < rows.Count; i++)
                {
                    rows[i] = midR + deltaRow[i];
                    cols[i] = midC + deltaCol[i];
                }
            }
            else
            {
                rows[activeHandleIdx] = newY;
                cols[activeHandleIdx] = newX;
            }
 
 
        }//end of method
 
    }
}