patrick.xu
2022-07-28 45a1c9f2540754e7cea2b58cd736bc677b2d4fdc
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
 
namespace HalconTools
{
    public class CalibImage
    {
        /// <summary>
        /// Reference to the controller class that performs all 
        /// calibration operations and interacts with the GUI.
        /// </summary>
        private CalibrationAssistant mAssistant;
        /// <summary>Calibration image</summary>
        private HImage mImage;
        /// <summary>
        /// Width of calibration image
        /// </summary>
        public int mWidth;
        /// <summary>
        /// Height of calibration image
        /// </summary>
        public int mHeight;
 
 
        /// <summary>
        /// Tuple with row coordinates of the detected marks
        /// </summary>
        private HTuple mMarkCenterRows;
        /// <summary>
        /// Tuple with column coordinates of the detected marks
        /// </summary>
        private HTuple mMarkCenterCols;
 
        private HTuple mWorldX;
        private HTuple mWorldY;
 
        /// <summary>
        /// Estimation for the external camera parameters (position and
        /// orientation)
        /// </summary>
        private HPose mEstimatedPose;
 
 
        /// <summary>
        /// HALCON error message that occurs when calculating the 
        /// basic information for the calibration image 
        /// (plate region, marks and pose).
        /// </summary>
        public string mErrorMessage;
        /// <summary>
        /// Flag that describes the degree of success or failure 
        /// after an update of the basic information.
        /// </summary>
        public string mPlateStatus;
        /// <summary>
        /// Flag that permits or forbids this calibration image
        /// to be part of the calibration process
        /// </summary>
        public int mCanCalib; // true  = 0  ||  false = 1
 
 
        /// <summary>
        /// Region of the plane calibration plate in the calibration image
        /// </summary>
        private HRegion mCaltabRegion;
        /// <summary>
        /// XLD contour points of the marks detected in 
        /// the calibration image, generated from the row and 
        /// column values <c>mMarkCenterRows</c> and 
        /// <c>mMarkCenterCols</c> 
        /// </summary>
        private HXLDCont mMarkCenter;
        /// <summary>
        /// Estimated world coordinate system (pose of the calibration plate
        /// in camera coordinates), based on the
        /// <c>mEstimatedPose</c> and the camera parameters 
        /// for this calibration image
        /// </summary>
        private HObject mEstimatedWCS;
 
        private double mEstimatedPlateSize;
 
        // for quality measurement 
        private ArrayList mQualityIssuesList;
 
        /// <summary>
        /// Initializes all status flags and objects to set up 
        /// this image for the calibration process
        /// </summary>
        /// <param name="img">Calibration image</param>
        /// <param name="assist">Reference to the Calibration Assistant</param>
        public CalibImage(HImage img, CalibrationAssistant assist)
        {
            string tmp;
            mImage = img;
 
            mAssistant = assist;
            mCanCalib = 1;  //labeled as 'for not having been evaluated'
            mPlateStatus = CalibrationAssistant.PS_NOT_FOUND;// "No Plate found" yet
            mImage.GetImagePointer1(out tmp, out mWidth, out mHeight);
            mEstimatedPlateSize = 0;
            mErrorMessage = "";
 
            // initialize all instances
            mCaltabRegion = new HRegion();
            mMarkCenter = new HXLDCont();
            mEstimatedWCS = new HObject();
            mQualityIssuesList = new ArrayList(15);
 
            mMarkCenterRows = new HTuple();
            mMarkCenterCols = new HTuple();
            mEstimatedPose = new HPose();
        }
 
 
        /************** getter-methods  *************/
        /********************************************/
        public HImage getImage()
        {
            return mImage;
        }
 
        public HTuple getMarkCenterRows()
        {
            return mMarkCenterRows;
        }
        public HTuple getMarkCenterColumns()
        {
            return mMarkCenterCols;
        }
        public HTuple getWorldX()
        {
            return mWorldX;
        }
        public HTuple getWorldY()
        {
            return mWorldY;
        }
        public HXLDCont getMarkCenters()
        {
            return mMarkCenter;
        }
        public HTuple getEstimatedPose()
        {
            return mEstimatedPose;
        }
        public HObject getEstimatedWCS()
        {
            return mEstimatedWCS;
        }
        public double getEstimatedPlateSize()
        {
            return mEstimatedPlateSize;
        }
        public HObject getCaltabRegion()
        {
            return mCaltabRegion;
        }
        public ArrayList getQualityIssueList()
        {
            return mQualityIssuesList;
        }
        public string getPlateStatus()
        {
            return mPlateStatus;
        }
 
 
 
        /// <summary>
        /// Determine s(or updates) the basic information for this 
        /// calibration image, which are the values for the region 
        /// plate, the center marks, and the estimated pose. 
        /// The flag <c>mPlateStatus</c> describes the evaluation 
        /// of the computation process.
        /// If desired the quality assessment can be recalculated 
        /// as well.
        /// </summary>
        /// <param name="updateQuality">
        /// Triggers the recalculation of the quality assessment for
        /// this calibration image 
        /// </param>
        public void UpdateCaltab(bool updateQuality)
        {
            HTuple unit = new HTuple("mm");
 
            bool failed = false;
            QualityProcedures proc = new QualityProcedures();
            string descrFile;
            HTuple startCamp;
            mErrorMessage = "";
 
 
            mCaltabRegion.Dispose();
            mMarkCenter.Dispose();
            mEstimatedWCS.Dispose();
 
            //reset this variable
            mMarkCenterRows = new HTuple();
 
            mPlateStatus = CalibrationAssistant.PS_NOT_FOUND;
 
            descrFile = mAssistant.getDesrcFile();
 
            try
            {
                mCaltabRegion = mImage.FindCaltab(descrFile,
                                                 (int)mAssistant.mFilterSize,
                                                 (int)mAssistant.mMarkThresh,
                                                 (int)mAssistant.mMinMarkDiam);
 
                mPlateStatus = CalibrationAssistant.PS_MARKS_FAILED;
 
                //-- Quality issue measurements --
                if (updateQuality)
                {
                    mQualityIssuesList.Clear();
                    failed = mAssistant.testQualityIssues(this);
                }
 
                startCamp = mAssistant.getCameraParams(this);
                mMarkCenterRows = mImage.FindMarksAndPose(mCaltabRegion,
                                                          descrFile,
                                                          startCamp,
                                                          (int)mAssistant.mInitThresh,
                                                          (int)mAssistant.mThreshDecr,
                                                          (int)mAssistant.mMinThresh,
                                                          mAssistant.mSmoothing,
                                                          mAssistant.mMinContLength,
                                                          mAssistant.mMaxMarkDiam,
                                                          out mMarkCenterCols,
                                                          out mEstimatedPose);
 
 
                mMarkCenter.GenCrossContourXld(mMarkCenterRows,
                                               mMarkCenterCols,
                                               new HTuple(6.0),
                                               0.785398);
 
                if (failed)
                    mAssistant.addQualityIssue(this, CalibrationAssistant.QUALITY_ISSUE_FAILURE, 0.0);
 
 
                HOperatorSet.ImagePointsToWorldPlane(startCamp, mEstimatedPose,
                                                     mMarkCenterRows, mMarkCenterCols,
                                                     unit, out mWorldX, out mWorldY);
                mEstimatedPlateSize = HMisc.DistancePp(mWorldY[0].D, mWorldX[0].D,
                                                       mWorldY[1].D, mWorldX[1].D);
                mEstimatedPlateSize *= 10.0;
                proc.get_3d_coord_system(mImage, out mEstimatedWCS,
                                         startCamp, mEstimatedPose,
                                         new HTuple(mEstimatedPlateSize / 2.0));
 
                mPlateStatus = mQualityIssuesList.Count > 0 ? CalibrationAssistant.PS_QUALITY_ISSUES : CalibrationAssistant.PS_OK; // "Quality Issues found": "OK";
                mCanCalib = 0;
            }
            catch (HOperatorException e)
            {
                this.mErrorMessage = e.Message;
                mCanCalib = 1;
 
                /* if exception was raised due to lack of memory, 
                 * forward the error to the calling method */
                if (e.Message.IndexOf("not enough") != -1)
                    throw (e);
            }
        }
 
 
        /// <summary>
        /// Releases the memory for all iconic HALCON objects contained in
        /// this instance.
        /// </summary>
        public void Clear()
        {
            mImage.Dispose();
            mCaltabRegion.Dispose();
        }
       
    }
}