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
//==================================================================================
// DIPM.h : Declaration of DIPM
//==================================================================================
 
#ifndef __EDGE_DIPM__
#define __EDGE_DIPM__
 
//#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "opencv.hpp"
#include "core.hpp"
#include <math.h>
#include <map>
#include <list>
#include <vector>
 
//Define DLL IM/EXPORTs
 
#ifndef DLLEXPORT
#define DLLEXPORT        __declspec(dllexport)
#endif
 
#ifndef DLLIMPORT
#define DLLIMPORT        __declspec(dllimport)
#endif
 
////////////////////////////////////////////////////////////////////////////////////
// MACRO definitions
 
#define align_4byte(in) ((in + 3)/4)*4
 
// Memory allocation MACRO for compatible with windows SDK
#define memalloc(flags, cb)        (GlobalLock(GlobalAlloc((flags),(cb))))
#define memfree(lp)    \
        (GlobalUnlock((HGLOBAL)GlobalHandle(lp)),(BOOL)GlobalFree((HGLOBAL)GlobalHandle(lp)))
 
////////////////////////////////////////////////////////////////////////////////////
// Constant definitions
 
#define ROUND(a)                (a+0.5)
#define FLOOR(a)                (a)
 
// Numerical MACRO's
#define    M_PI                    acos(-1.0)
#define M_RADIAN(x)                (((M_PI)*(x))/180.0)
#define M_DEGREE(x)                (((180.0)*(x))/M_PI)
 
#ifndef RANGEIN
#define RANGEIN(a, r1, r2)        (((a>=r1)&&(a<=r2)) ? (1) : (0))
#endif
 
// Logical MACRO's
#define MINIMUM(a, b)            ((a)<(b) ? (a) : (b))
#define MAXIMUM(a, b)            ((a)>(b) ? (a) : (b))
#define MIDDLE(a, b, c)            (((a>b)&&(a<c)) ? (1) : (0))
 
enum    enGradientMask{GM_Sobel, GM_Prewitt, GM_Roberts};
 
struct sPoint {
    double x, y;
};
 
struct sLine {
    double mx, my;
    double sx, sy;
};
 
typedef struct rtiTEMP_RESULT
{
    float         fRat;
    CvPoint2D32f pt;
}TEMP_RESULT;
 
typedef  struct _rtiTEMP_MATCH_DATA
{
    double dMaxRate;
    double dMinRate;
    double dPt_X;
    double dPt_Y;
 
    _rtiTEMP_MATCH_DATA(void)
    {
        dMaxRate = 0.0;
        dMinRate = 0.0;
        dPt_X = 0.0;
        dPt_Y = 0.0;
    }
}TEMP_MATCH_DATA;
 
enum ChipPair    {CHIPDEFPAIR_NOTDEFINE = 0, CHIPDEFPAIR_PPAIR, CHIPDEFPAIR_MPAIR, CHIPDEFPAIR_SIDE, CHIPDEFPAIR_LARGE, CHIPDEFPAIR_UNPAIR };
enum ChipType    {CHIPDEFTYPE_BLACK = 0, CHIPDEFTYPE_WHITE, CHIPDEFTYPE_MIXED, CHIPDEFTYPE_NODEFECT, CHIPDEFTYPE_DELETE};
enum ChipResionType    {CHIPREGTYPE_LEFT = 0, CHIPREGTYPE_RIGHT, CHIPREGTYPE_RIGHT_WHITE, CHIPREGTYPE_NONE};
enum DefectPosType    {INS_DEFECT_CRACK = 0, INS_DEFECT_BROKEN, INS_DEFECT_CHIP, INS_DEFECT_CHAMFER,INS_DEFECT_BURR,INS_DEFECT_NONE};
enum ChipPosType    {CHIPPOSTYPE_BIN = 0, CHIPPOSTYPE_CEN, CHIPPOSTYPE_SP,CHIPPOSTYPE_NONE};
 
// Directional information
#define IM_NEGATIVE                101
#define IM_POSITIVE                102
#define IM_HORIZONTAL            103
#define IM_VERTICAL                104
#define IM_LEFT                    105
#define IM_RIGHT                106
 
//Gray level
#define IM_BLACK                0
#define IM_WHITE                255
 
//Image
#ifndef _IMAGE_
#define _IMAGE_
typedef struct ImImage {
    unsigned char    *buf;
    int    cu;
    int    cv;
} IMAGE;
#endif
 
//Position in 2D
#ifndef _IMPOS2D_
#define _IMPOS2D_
typedef struct ImPos2D {
    double        u;
    double        v;
} IMPOS2D;
#endif
 
//Region of interest (ROI)
#ifndef _ROI_
#define _ROI_
typedef struct ImRoi {
    int    u1;
    int    v1;
    int    u2;
    int    v2;
} ROI, *LPROI;
#endif
 
class CChipBlob
{
public:    
    ChipResionType    s_RegionType;
    ChipPair    s_DefectPair;
    ChipType    s_DefectType;
    ChipPosType    s_DefectPosType;
    DefectPosType s_DefectJudgeType;
    int            s_nIndex;
    int            s_nDefectArea;
    int            s_nDefectX;
    int            s_nDefectY;
    int            s_xLevelSrcMax;
    int            s_yLevelSrcMax;
    int            s_nDefectRScale;
    CRect        s_DefectRect;
    short        s_sDefectPeak;
 
    short        s_sLevelSrcMin;
    short        s_sLevelSrcMax;
    short        s_sLevelSrcAvg;
    short        s_sLevelRefMin;
    short        s_sLevelRefMax;
    short        s_sLevelRefAvg;
    short        s_sLevelDiffMin;
    short        s_sLevelDiffMax;
    short        s_sLevelDiffAvg;
    int            s_nLevelSrcSum;
    int            s_nLevelRefSum;
    int            s_nLevelDiffSum;
    short        s_sThreshold;    
    BOOL        s_bRemoved;
    double        s_dThick;
    BOOL        s_bCornerChip;
 
    CPoint        s_ptVertex[8];
 
    CChipBlob()
    {
        Reset();
    }
    void Reset()
    {        
        s_RegionType        = CHIPREGTYPE_NONE;
        s_DefectPair        = CHIPDEFPAIR_NOTDEFINE;
        s_DefectType        = CHIPDEFTYPE_NODEFECT;
        s_DefectJudgeType   = INS_DEFECT_NONE;
        s_nIndex            = 0;
        s_nDefectArea        = 0;
        s_nDefectX            = 0;
        s_nDefectY            = 0;
        s_nDefectRScale        = 0;
        // 20080525 ÀÌÁØÈñ
        s_DefectRect.SetRect(INT_MAX, INT_MAX, 0, 0);
        s_sDefectPeak        = 0;
 
        s_sLevelSrcMin        = 2048;
        s_sLevelSrcMax        = 0;
        s_xLevelSrcMax        = 0;
        s_yLevelSrcMax        = 0;
        s_sLevelSrcAvg        = 0;
        s_sLevelRefMin        = 2048;
        s_sLevelRefMax        = 0;
        s_sLevelRefAvg        = 0;
        s_sLevelDiffMin        = 2048;
        s_sLevelDiffMax        = 0;
        s_sLevelDiffAvg        = 0;
        s_nLevelSrcSum        = 0;    
        s_nLevelRefSum        = 0;
        s_nLevelDiffSum        = 0;
 
        s_sThreshold        = 0;
        s_dThick            = 0.;
        s_bCornerChip        = FALSE;
 
        // 20080525 ÀÌÁØÈñ
        s_ptVertex[0]        = MAKELONG(32760, 32760);
        s_ptVertex[1]        = MAKELONG(32760, 0);
        s_ptVertex[2]        = MAKELONG(32760, 32760);
        s_ptVertex[3]        = MAKELONG(0, 32760);
        s_ptVertex[4]        = MAKELONG(0, 32760);
        s_ptVertex[5]        = MAKELONG(0, 0);
        s_ptVertex[6]        = MAKELONG(32760, 0);
        s_ptVertex[7]        = MAKELONG(0, 0);
 
        s_bRemoved            = FALSE;
    }
 
    void SetDefectPair(ChipPair DP)
    {
        if (s_DefectPair < DP)
            s_DefectPair = DP;
    }
 
    // ´ëÀÔ¿¬»êÀÚ ¿À¹ö·Îµù.
    CChipBlob& operator=(const CChipBlob& rhs)
    {
        if(this != &rhs)
        {            
            s_RegionType        = rhs.s_RegionType;
            s_DefectPair        = rhs.s_DefectPair;
            s_DefectType        = rhs.s_DefectType;
            s_DefectJudgeType    = rhs.s_DefectJudgeType;
            s_nIndex            = rhs.s_nIndex;
            s_nDefectArea        = rhs.s_nDefectArea;
            s_nDefectX            = rhs.s_nDefectX;
            s_nDefectY            = rhs.s_nDefectY;
            s_nDefectRScale        = rhs.s_nDefectRScale;
            s_DefectRect        = rhs.s_DefectRect;
            s_sDefectPeak        = rhs.s_sDefectPeak;
 
            s_sLevelSrcMin        = rhs.s_sLevelSrcMin;
            s_sLevelSrcMax        = rhs.s_sLevelSrcMax;
            s_xLevelSrcMax        = rhs.s_xLevelSrcMax;
            s_yLevelSrcMax        = rhs.s_yLevelSrcMax;
            s_sLevelSrcAvg        = rhs.s_sLevelSrcAvg;
            s_sLevelRefMin        = rhs.s_sLevelRefMin;
            s_sLevelRefMax        = rhs.s_sLevelRefMax;
            s_sLevelRefAvg        = rhs.s_sLevelRefAvg;
            s_sLevelDiffMin        = rhs.s_sLevelDiffMin;
            s_sLevelDiffMax        = rhs.s_sLevelDiffMax;
            s_sLevelDiffAvg        = rhs.s_sLevelDiffAvg;
            s_nLevelSrcSum        = rhs.s_nLevelSrcSum;
            s_nLevelRefSum        = rhs.s_nLevelRefSum;
            s_nLevelDiffSum        = rhs.s_nLevelDiffSum;
 
            s_sThreshold        = rhs.s_sThreshold;
            s_dThick            = rhs.s_dThick;
            s_bCornerChip        = rhs.s_bCornerChip;
 
            int i;
            for(i= 0; i< 8; i++)
            {
                s_ptVertex[i]= rhs.s_ptVertex[i];
            }        
            s_bRemoved            = rhs.s_bRemoved;
        }
        return *this;
    }
 
    // + ¿¬»êÀÚ ¿À¹ö·Îµù.
    CChipBlob operator+(const CChipBlob& rhs)
    {
        if (s_DefectPair < rhs.s_DefectPair)
            s_DefectPair = rhs.s_DefectPair;
 
        s_nDefectArea += rhs.s_nDefectArea;
 
        if (s_ptVertex[0].x >= rhs.s_ptVertex[0].x)    // LT
            s_ptVertex[0] = rhs.s_ptVertex[0];
        if (s_ptVertex[1].x >= rhs.s_ptVertex[1].x)    // LB
            s_ptVertex[1] = rhs.s_ptVertex[1];
        if (s_ptVertex[2].y >= rhs.s_ptVertex[2].y)    // TL
            s_ptVertex[2] = rhs.s_ptVertex[2];
        if (s_ptVertex[3].y >= rhs.s_ptVertex[3].y)    // TR
            s_ptVertex[3] = rhs.s_ptVertex[3];
        if (s_ptVertex[4].x <= rhs.s_ptVertex[4].x)    // RT
            s_ptVertex[4] = rhs.s_ptVertex[4];
        if (s_ptVertex[5].x <= rhs.s_ptVertex[5].x)    // RB
            s_ptVertex[5] = rhs.s_ptVertex[5];
        if (s_ptVertex[6].y <= rhs.s_ptVertex[6].y)    // BL
            s_ptVertex[6] = rhs.s_ptVertex[6];
        if (s_ptVertex[7].y <= rhs.s_ptVertex[7].y)    // BR
            s_ptVertex[7] = rhs.s_ptVertex[7];
 
        s_nDefectRScale = (s_nDefectRScale + rhs.s_nDefectRScale);
        if (s_DefectRect.left > rhs.s_DefectRect.left)
            s_DefectRect.left = rhs.s_DefectRect.left;
        if (s_DefectRect.top > rhs.s_DefectRect.top)
            s_DefectRect.top = rhs.s_DefectRect.top;
        if (s_DefectRect.right < rhs.s_DefectRect.right)
            s_DefectRect.right = rhs.s_DefectRect.right;
        if (s_DefectRect.bottom < rhs.s_DefectRect.bottom)
            s_DefectRect.bottom = rhs.s_DefectRect.bottom;
        if (s_sDefectPeak < rhs.s_sDefectPeak)
            s_sDefectPeak        = rhs.s_sDefectPeak;
 
        s_nDefectX = (s_DefectRect.right + s_DefectRect.left) / 2;
        s_nDefectY = (s_DefectRect.bottom + s_DefectRect.top) / 2;
 
        if (s_sLevelSrcMin > rhs.s_sLevelSrcMin)
            s_sLevelSrcMin        = rhs.s_sLevelSrcMin;
        if (s_sLevelSrcMax < rhs.s_sLevelSrcMax)
        {
            s_sLevelSrcMax        = rhs.s_sLevelSrcMax;
            s_xLevelSrcMax        = rhs.s_xLevelSrcMax;
            s_yLevelSrcMax        = rhs.s_yLevelSrcMax;
        }
        s_sLevelSrcAvg            = (s_sLevelSrcAvg + rhs.s_sLevelSrcAvg) / 2;
        if (s_sLevelRefMin > rhs.s_sLevelRefMin)
            s_sLevelRefMin        = rhs.s_sLevelRefMin;
        if (s_sLevelRefMax < rhs.s_sLevelRefMax)
            s_sLevelRefMax        = rhs.s_sLevelRefMax;
        s_sLevelRefAvg            = (s_sLevelRefAvg + rhs.s_sLevelRefAvg) / 2;
        if (s_sLevelDiffMin > rhs.s_sLevelDiffMin)
            s_sLevelDiffMin        = rhs.s_sLevelDiffMin;
        if (s_sLevelDiffMax < rhs.s_sLevelDiffMax)
            s_sLevelDiffMax        = rhs.s_sLevelDiffMax;
        s_sLevelDiffAvg            = (s_sLevelDiffAvg + rhs.s_sLevelDiffAvg) / 2;
        s_nLevelSrcSum        += rhs.s_nLevelSrcSum;
        s_nLevelRefSum        += rhs.s_nLevelRefSum;
        s_nLevelDiffSum        += rhs.s_nLevelDiffSum;
 
        if (s_sThreshold < rhs.s_sThreshold)
            s_sThreshold        = rhs.s_sThreshold;
 
        if(s_dThick < rhs.s_dThick)
            s_dThick = rhs.s_dThick;
 
        s_bCornerChip            |= rhs.s_bCornerChip;
 
        return *this;
    }
 
};
 
class CChipPair
{
public:        
    ChipPair    s_DefectPair;
    ChipType    s_DefectType;
    ChipResionType    s_RegionType;
    DefectPosType    s_DefectPosType;
    float        s_fDefectPeak;
    int            s_nDefectX;
    int            s_nDefectY;
    int            s_nGraySrc;
    int            s_nGrayRef;    
    double        s_dThick;
    BOOL        s_bCornerChip;
 
    CChipPair()
    {
        Reset();
    }
    void Reset()
    {        
        s_DefectPair = CHIPDEFPAIR_NOTDEFINE;
        s_DefectType = CHIPDEFTYPE_NODEFECT;
        s_RegionType = CHIPREGTYPE_NONE;
        s_DefectPosType = INS_DEFECT_NONE;
        s_fDefectPeak = 0.0;
        s_nDefectX = 0;
        s_nDefectY = 0;
        s_nGraySrc = 0;
        s_nGrayRef = 0;
        s_dThick = 0.;
        s_bCornerChip = FALSE;
    }
    void SetPeak(int nPeak, int nKernelThres, int nCalcPixel)
    {
        s_fDefectPeak= static_cast<float>(abs(nPeak));
        s_fDefectPeak -= static_cast<float>(nKernelThres);
        s_fDefectPeak /= static_cast<float>(nCalcPixel);
    }
 
    // º¹»ç »ý¼ºÀÚ.
    CChipPair(const CChipPair& rhs)
    {
        if(this != &rhs)
        {        
            s_DefectPair        = rhs.s_DefectPair;
            s_DefectType        = rhs.s_DefectType;
            s_fDefectPeak        = rhs.s_fDefectPeak;
            s_nDefectX            = rhs.s_nDefectX;
            s_nDefectY            = rhs.s_nDefectY;
            s_nGraySrc            = rhs.s_nGraySrc;
            s_nGrayRef            = rhs.s_nGrayRef;    
            s_RegionType        = rhs.s_RegionType;    
            s_dThick            = rhs.s_dThick;    
            s_bCornerChip        = rhs.s_bCornerChip;
            s_DefectPosType        = rhs.s_DefectPosType;
        }
    }
 
    // ´ëÀÔ¿¬»êÀÚ ¿À¹ö·Îµù.
    CChipPair& operator=(const CChipPair& rhs)
    {
        if(this != &rhs)
        {        
            s_DefectPair        = rhs.s_DefectPair;
            s_DefectType        = rhs.s_DefectType;
            s_fDefectPeak        = rhs.s_fDefectPeak;
            s_nDefectX            = rhs.s_nDefectX;
            s_nDefectY            = rhs.s_nDefectY;
            s_nGraySrc            = rhs.s_nGraySrc;
            s_nGrayRef            = rhs.s_nGrayRef;
            s_RegionType        = rhs.s_RegionType;    
            s_dThick            = rhs.s_dThick;
            s_bCornerChip        = rhs.s_bCornerChip;
        }
        return *this;
    }
};
 
#endif
 
// End of FV_DIPM.h
////////////////////////////////////////////////////////////////////////////////////