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
// EdgeFind.h: interface for the CEdgeFind class.
//
//////////////////////////////////////////////////////////////////////
 
#if !defined(AFX_EDGEFIND_H__390230EE_3748_47C9_B543_04276FAFACA4__INCLUDED_)
#define AFX_EDGEFIND_H__390230EE_3748_47C9_B543_04276FAFACA4__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "EDGE_DIPM.h"
 
class CSISBuffer;
class CEdgeProc;
 
enum    EDGELINE_DIR{DIR_HORIZONTAL=0,DIR_VERTICAL};
enum    EDGELINE_POLAR{POLAR_WTOB=0,POLAR_BTOW,POLAR_ANY};
enum    enEdgeFindType{EDGE_MAXCONTRAST=0,EDGE_FIRSTEDGE,EDGE_PROJECTION,EDGE_NONE};
enum    EdgeSearchType        { SearchTypeMinMax, SearchTypeOut2In, SearchTypeIn2Out };
enum    EdgeSignalType        { EdgeTypeNegative=0, EdgeTypePositive, EdgeTypeNone };
enum    EdgeResultType        { EdgeResultMid=0, EdgeResultLeft, EdgeResultRight,  };
enum    SubPixelType        { SubPixelLinear=0, SubPixelQuadratic, SubPixelRegression };
enum    PreProcessType        { PreProcNone=0, PreProcSobel };
enum    TriangleResult        {    ImageFailModel=-1, ImageFailSource=-2, ImageFailSub=-3, ImageFailBandModel=-4, ImageFailBandSource=-5,
                                RecipeFailModel=-6, RecipeFailMarker=-7, RecipeFailFormula=-8, 
                                ProcessFailModel=-9, ProcessFailMarker=-10, ProcessFailFormula=-11, 
                                ResultFailModel=-12, ResultFailMarker=-13, ResultFailFormula=-14, 
                                EdgeFailLeft=-15, EdgeFailRight=-16, PreProcessFail=-17,
                                ProcessSuccess=1, ProcessNone=0 };
 
//Projection data
#ifndef _IMPROJ_
#define _IMPROJ_
enum    PROJECTION_TYPE{PROJECTION_VERTICAL=0,PROJECTION_HORIZONTAL};
typedef struct ImProj{
    int        x;
    int        f;
} IMPROJ, *LPIMPROJ;
#endif
 
class CPolynomial
{
public:
    CPolynomial()        { Reset(); }
    CPolynomial(int degree, double coef)
    {
        nDegree = degree;
        dCoef    = coef;
    }
    ~CPolynomial()        { Reset(); }
    void Reset()
    {
        nDegree = 0;
        dCoef    = 0.0;
    }
    int        nDegree;
    double    dCoef;
};
 
typedef std::list<CPolynomial*>                ListPolynomial;
typedef std::list<CPolynomial*>::iterator    ListPolynomialIt;
 
typedef std::vector<double>                    VectorDouble;
typedef std::vector<double>::iterator        VectorDoubleIt;
 
typedef std::vector<int>                    VectorInteger;
typedef std::vector<int>::iterator            VectorIntegerIt;
 
class AFX_EXT_CLASS CEdgeResult
{
public:
    CEdgeResult()        { Reset(); }
 
    CEdgeResult(int indexValue, int signalValue, int positionValue, double edgeValue)
    {
 
        nIndex            = indexValue;
        nSignal            = signalValue;
        nEdgePosition    = positionValue;
        dEdgeValue        = edgeValue;
 
        nEdgeType        = EdgeResultMid;
        dPosition        = 0.0;
        dStrength        = 0.0;
 
        nLeftPosition    = positionValue;
        nRightPosition    = positionValue;
    }
 
    CEdgeResult(int indexValue, int signalValue, double positionValue, double strengthValue)
    {
 
        nIndex        = indexValue;
        nSignal        = signalValue;
        dPosition    = positionValue;
        dStrength    = strengthValue;
 
        nEdgeType        = EdgeResultMid;
        nLeftPosition    = 0;
        nRightPosition    = 0;
    }
 
    ~CEdgeResult()    { Reset(); }
 
    void Reset()
    {
        nEdgeType        = EdgeResultMid;
        nIndex            = -1;
        nSignal            = EdgeTypeNone;
        nEdgePosition    = 0;
        dEdgeValue        = 0.0;
        dPosition        = 0.0;
        dStrength        = 0.0;
 
        nLeftPosition    = 0;
        nRightPosition    = 0;
    }
 
    int        nEdgeType;
    int        nIndex;
    int        nSignal;
    int        nEdgePosition;
    double    dEdgeValue;
 
    int        nLeftPosition;
    int        nRightPosition;
 
    double    dPosition;
    double    dStrength;
};
typedef std::vector<CEdgeResult>            VectorEdgeResult;
typedef std::vector<CEdgeResult>::iterator    VectorEdgeResultIt;
 
class AFX_EXT_CLASS CEdgeFind
{
    CSISBuffer        *m_Buffer;
    LPIMPROJ            m_pHproj;    
    LPIMPROJ            m_pVproj;
    VectorDouble        m_vectorEdgeData;
    VectorInteger        m_vectorEdgeType;
    VectorDouble        m_vectorImageData;    
    VectorEdgeResult    m_vectorEdgeResult;
 
public:
    CEdgeFind();
    virtual ~CEdgeFind();
 
public:
    int Find_LeftEdge(CSISBuffer *pBuffer, double pitch, int threshold, double rDefect, int nContinue);
    int Find_RightEdge(CSISBuffer *pBuffer, double pitch, int threshold, double rDefect, int nContinue);
 
public:
    int    Find_LeftCanny(CSISBuffer *pBuffer,int nThres);
    double FindLine_H(LPBYTE pImg,CSize szImg,CRect &rectIns,int nThres,EDGELINE_POLAR enDir,EdgeSignalType enSignalType,enEdgeFindType enFind);
    double FindLine_V(LPBYTE pImg,CSize szImg,CRect &rectIns,int nThres,EDGELINE_POLAR enDir,EdgeSignalType enSignalType,enEdgeFindType enFind,BOOL bTop);
    double FindGlassHorizontalLine(LPBYTE pImg,CSize szImg,CRect &rectIns,int nThres,BOOL bDir, int nDist = 20);    
    double FindGlassVerticalLine(LPBYTE pImg,CSize szImg,CRect &rectIns,int nThres,BOOL bDir,int nDist = 10);
    double FindLineSubPixel(LPBYTE pImg,CSize szImg,CRect &rectIns,int nPosiThres,int nNegaThres,EDGELINE_DIR enDir,EdgeSearchType enSearchType,EdgeSignalType enSignalType,int nKernelSize=2);
 
protected:
    void AssertOnFrameRect(CRect &rect,CSize szImg=CSize(0,0));
    BOOL FV_ProjGet(LPBYTE src,CSize szImg, CRect &rect, PROJECTION_TYPE type, IMPROJ* res, ULONG* max, ULONG* min);    
    int     FindMaxContrastEx(IMPROJ* src, int nStart,int nEnd, long Width, long Height, EDGELINE_POLAR enDir, long Thresh,BOOL bForward=TRUE);
    int     FindFirstEdge(IMPROJ* src, int nStart,int nEnd, long Width, long Height, EDGELINE_POLAR enDir, long Thresh,BOOL bForward=TRUE);    
    int     FindMaxContrast(CSISBuffer &bufferOrg,CRect rect,BOOL bHor,int &nMaxDiff,BOOL bForward=TRUE);        
    BOOL QuadraticRegression(VectorDouble& vectorX, VectorDouble& vectorY, double& a0, double& a1, double& a2);
    bool parabolicInterpolate(double hm, double h0, double hp, double *c);
    BOOL FindGlssEdgeLine(int &nPos,CRect rect,LPIMPROJ pProj,BOOL bDir,PROJECTION_TYPE nType,int nThres,int nContiValue,int nDist);
 
protected:
    BOOL                CalculateInterpolateLinear(CEdgeResult& edgeResult, double dEdgeThreshold);
    BOOL                CalculateInterpolateQuadratic(CEdgeResult& edgeResult);
    BOOL                CalculateInterpolateRegression(CEdgeResult& edgeResult);
    void                CalculateEdgePosition(int nSubPixelType, double dPosiThres, double dNegaThres);
    BOOL                CalculateImageValue(EDGELINE_DIR nDirection, BYTE *pImage, int nWidth, int nHeight, CRect &rect,int nStep, VectorDouble& vectorImageData);
    BOOL                CalculateEdgeValue(const VectorDouble& vectorImageData, VectorDouble& vectorEdgeData, int nKernelSize);
    BOOL                CalculateEdgeValue(const VectorDouble& vectorImageData, VectorDouble& vectorEdgeData, VectorInteger& vectorEdgeType, double dPositiveThres, double dNegativeThres, int nKernelSize);
    BOOL                CalculateEdgeValue(EDGELINE_DIR nDirection, short *pImage, int nWidth, int nHeight, int nStep, VectorDouble& vectorEdgeData, VectorInteger& vectorEdgeType, double dPositiveThres, double dNegativeThres);
    BOOL                GetEdgeLeftPosition(int nEdgeType, int nSearchType, int nStartPos, double& dPosition);
    BOOL                GetEdgeRightPosition(int nEdgeType, int nSearchType, int nStartPos, double& dPosition);
 
    double                AbsSum(double init, double x);
    double                Sum(double init, double x);
    static double        Square(double init, double x);
    static double        Cubic(double init, double x);
    static double        ForthPower(double init, double x);
    double                Combi(VectorDouble& vectorX, VectorDouble& vectorY, int n, int r);
    void                InsertPolynomial(ListPolynomial &listPolynomial, int degree, double coef);
    void                RemovePolynomial(ListPolynomial& listPolynomial);
    BOOL                CalculateLagrange(VectorDouble& vectorX, VectorDouble& vectorY, ListPolynomial& listPolynomial);
 
    template <class T> 
    inline void imMinMax(const T *map, int count, T& min, T& max)
    {
        min = *map++;
        max = min;
        for (int i = 1; i < count; i++)
        {
            T value = *map++;
 
            if (value > max)
                max = value;
            else if (value < min)
                min = value;
        }
    }
};
 
 
#endif // !defined(AFX_EDGEFIND_H__390230EE_3748_47C9_B543_04276FAFACA4__INCLUDED_)