SDC C-Project CF Review 프로그램
LYW
2021-08-09 b354c153b0074e5d54371bc05b12edbe8e613a19
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
#pragma once
#include "CHImageControls/CHImageData.h"
#include "RecipeTriangle.h"
 
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, CircleCoefficientFail=-18,
                        ProcessSuccess=1, ProcessNone=0 };
 
typedef std::vector<double>                        VectorDouble;
typedef std::vector<double>::iterator            VectorDoubleIt;
 
typedef std::vector<int>                        VectorInteger;
typedef std::vector<int>::iterator                VectorIntegerIt;
 
typedef std::vector<CPoint>                        VectorPoint;
typedef std::vector<CPoint>::iterator            VectorPointIt;
 
class CPixelRegion
{
public:
    int            nLeft;
    int            nTop;
    int            nRight;
    int            nBottom;
    VectorPoint    vectorPoint;
 
    CPixelRegion()    { Reset(); }
    virtual ~CPixelRegion() { Reset(); }
 
    CPixelRegion(int left, int top, int right, int bottom)
    {
        nLeft    = left;
        nTop    = top;
        nRight    = right;
        nBottom = bottom;
 
        vectorPoint.clear();
    }
 
    void Reset()
    {
        nLeft    = INT_MAX;
        nTop    = INT_MAX;
        nRight    = INT_MIN;
        nBottom = INT_MIN;
 
        vectorPoint.clear();
    }
 
    int        GetSquareSize()    const    { return (nRight-nLeft+1) * (nBottom-nTop+1); }
    int        GetWidth() const        { return (nRight-nLeft+1); }
    int        GetHeight() const        { return (nBottom-nTop+1); }
    int        GetCenterX() const        { return nLeft + (nRight - nLeft + 1) / 2; }
    int        GetCenterY() const        { return nTop + (nBottom - nTop + 1) / 2; }
    int        GetMaxSize() const        { return (GetWidth() < GetHeight()) ? GetHeight(): GetWidth(); }
    int        GetMinSize() const        { return (GetWidth() > GetHeight()) ? GetHeight(): GetWidth(); }
    size_t    GetPixelCount()    const    { return vectorPoint.size(); }
};
typedef std::vector<CPixelRegion>                VectorPixelRegion;
typedef std::vector<CPixelRegion>::iterator        VectorPixelRegionIt;
 
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        = EdgeResult_Mid;
        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        = EdgeResult_Mid;
        nLeftPosition    = 0;
        nRightPosition    = 0;
    }
 
    ~CEdgeResult()    { Reset(); }
 
    void Reset()
    {
        nEdgeType        = EdgeResult_Mid;
        nIndex            = -1;
        nSignal            = EdgeSignal_None;
        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 CEdgeTriangle
{
public:
    CEdgeTriangle(void);
    ~CEdgeTriangle(void);
    void Reset()
    {
        m_imageSource.ReleaseImage();
        m_imageModel.ReleaseImage();
        m_recipeTriangle.Reset();
        m_resultTriangle.Reset();
    }
 
 
    VectorDouble        GetImageData()            { return m_vectorImageData; }
    VectorDouble        GetEdgeData()            { return m_vectorEdgeData; }
    VectorInteger        GetEdgeType()            { return m_vectorEdgeType; }
    VectorEdgeResult    GetEdgeResult()            { return m_vectorEdgeResult; }
 
    // getter
    CRecipeTriangle*    GetRecipeTriangle()        { return &m_recipeTriangle; }
    CResultTriangle*    GetResultTriangle()        { return &m_resultTriangle; }
 
    // setter
    BOOL    SetImageSource(CCHImageData* imageSource);
    BOOL    SetImageModel(CCHImageData* imageModel);
    BOOL    SetRecipeTriangle(const CRecipeTriangle& recipeTriangle);
 
    // reset
    void    ResetImageSource()            { m_imageSource.ReleaseImage(); }
    void    ResetImageModel()            { m_imageModel.ReleaseImage(); }
    void    ResetRecipeTriangle()        { m_recipeTriangle.Reset(); }
    void    ResetResultTriangle()        { m_resultTriangle.Reset(); }
 
    // process function
    int        ProcessModel();
    int        ProcessModel(CCHImageData* imageSource, const CRecipeModel& recipeModel);
    int        ProcessModel(CCHImageData* imageSource, const VectorRecipeModel& vectorRecipeModel);
    int        ProcessModel(CCHImageData* imageModel, CCHImageData* imageSource, const CRecipeModel& recipeModel);
    int        ProcessModel(CCHImageData* imageModel, CCHImageData* imageSource, const VectorRecipeModel& vectorRecipeModel);
 
    int        ProcessMarker();
    int        ProcessMarker(CCHImageData* imageSource, const CRecipeMarker& recipeMarker);
    int        ProcessMarker(CCHImageData* imageSource, const VectorRecipeMarker& vectorRecipeMarker);
 
    int        ProcessModelMarker();
    int        ProcessModelMarker(CCHImageData* imageSource, const CRecipeModel& recipeModel, const CRecipeMarker& recipeMarker);
    int        ProcessModelMarker(CCHImageData* imageSource, const CRecipeModel& recipeModel, const VectorRecipeMarker& vectorRecipeMarker);
    int        ProcessModelMarker(CCHImageData* imageSource, const VectorRecipeModel& vectorRecipeModel, const VectorRecipeMarker& vectorRecipeMarker);
    int        ProcessModelMarker(CCHImageData* imageModel, CCHImageData* imageSource, const CRecipeModel& recipeModel, const CRecipeMarker& recipeMarker);
    int        ProcessModelMarker(CCHImageData* imageModel, CCHImageData* imageSource, const CRecipeModel& recipeModel, const VectorRecipeMarker& vectorRecipeMarker);
    int        ProcessModelMarker(CCHImageData* imageModel, CCHImageData* imageSource, const VectorRecipeModel& vectorRecipeModel, const VectorRecipeMarker& vectorRecipeMarker);
 
    int        ProcessMarkerFormula();
    int        ProcessMarkerFormula(CCHImageData* imageSource, const CRecipeMarker& recipeMarker, const CRecipeFormula& recipeFormula);
    int        ProcessMarkerFormula(CCHImageData* imageSource, const VectorRecipeMarker& vectorRecipeMarker, const VectorRecipeFormula& vectorRecipeFormula);
    
    int        ProcessTriangle();
    int        ProcessTriangle(CCHImageData* imageSource);
    int        ProcessTriangle(CCHImageData* imageSource, const CRecipeTriangle& recipeTriangle);
    int        ProcessTriangle(CCHImageData* imageModel, CCHImageData* imageSource, const CRecipeTriangle& recipeTriangle);
 
    // static func
    static    int        ImageResizeHalf(CCHImageData* pSource, CCHImageData* pResult);
    static    int        ImageResizeQuarter(CCHImageData* pSource, CCHImageData* pResult);
    static    int        ImageResizeOneEighth(CCHImageData* pSource, CCHImageData* pResult);
    static    BOOL    ImageSobelEdge(CCHImageData* pSource, CCHImageData* pResult);
    
    static    BOOL    GetImageData(CCHImageData* pSubImage, VectorDouble& vectorImageData);
    static    BOOL    GetEdgeData(CCHImageData* pSubImage, VectorDouble& vectorEdgeData, int nKernelSize, double dKernelSigma);
    static    BOOL    GetEdgeData(int& nBandType, CCHImageData* pSubImage, VectorDouble& vectorEdgeData, int nKernelSize, double dKernelSigma);
 
    static double    AbsSum(double init, double x);
    static 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);
 
    static    int        ImageSobelEdge(BYTE* pSrcImage, BYTE *pDstImage, int nWidth, int nHeight);
    static    int        ImageAverage(BYTE* pSrcImage, BYTE *pDstImage, int nWidth, int nHeight);
    static    int        ImageThresholding(BYTE* pImage, int nWidth, int nHeight, int nThresValue);
    static    int        ImageThinning(BYTE* pImage, int nWidth, int nHeight);
    static    int        ImageBlobAnalysis(BYTE *pImage, int nWidth, int nHeight, VectorPixelRegion& vectorRegion, int nBlobMargin=-1);
    
    static    int        ImageErode(BYTE* pSrcImage, BYTE *pDstImage, int nWidth, int nHeight);
    static    int        ImageDilate(BYTE* pSrcImage, BYTE *pDstImage, int nWidth, int nHeight);
    static    int        ImageExclusiveOR(BYTE* pSrcImage1, BYTE *pSrcImage2, BYTE* pDstImage, int nWidth, int nHeight);
    static    int        ImageAND(BYTE* pSrcImage1, BYTE *pSrcImage2, BYTE* pDstImage, int nWidth, int nHeight);
    static    int        ImageOR(BYTE* pSrcImage1, BYTE *pSrcImage2, BYTE* pDstImage, int nWidth, int nHeight);
 
    // check func
    static BOOL        CheckFormulationString(const CString& strFormulation, CString& strResultFormulation, CRecipeTriangle* pRecipeTriangle=NULL);
    
private:
    // internal process
    int        FormulaProcess();
    int        MarkerProcess(CCHImageData *imageSource);
    int        MarkerFormulaProcess(CCHImageData *imageSource);
    int        ModelProcess(CCHImageData *imageModel, CCHImageData *imageSource);
    int        ModelMarkerProcess(CCHImageData *imageModel, CCHImageData *imageSource);
    int        TriangleProcess(CCHImageData *imageModel, CCHImageData *imageSource);
 
    // model func
    BOOL    MatchImage(CCHImageData *pSource, CCHImageData *pModel, CRect* pRect, double dScore, int& nPosX, int& nPosY, double& dResult);
    BOOL    MatchImage(CCHImageData *pSource, CCHImageData *pModel, CRect* pRect, double dScore, double& dPosX, double& dPosY, double& dResult);
    int        MatchFull(CCHImageData *pSource, CCHImageData *pModel, const CRecipeModel *pRecipeModel, CResultModel& resultModel);
    int        MatchLevel(CCHImageData *pSource, CCHImageData *pModel, const CRecipeModel *pRecipeModel, CResultModel& resultModel);
    static    void    GetMinLocation(float *pImage, int nWidth, int nHeight, int nStep, int& nPosX, int& nPosY, double& dValue);
    static    void    GetMaxLocation(float *pImage, int nWidth, int nHeight, int nStep, int& nPosX, int& nPosY, double& dValue);
 
    // Marker func
    int        MeasureImage(CCHImageData *imageSource, const CRecipeMarker* pRecipeMarker, CResultMarker& resultMarker);
    int        Process_MeasureLine(CCHImageData& imageData, const CRecipeMarker* pRecipeMarker, CResultMarker& resultMarker);
    int        Process_MeasureCircle(CCHImageData& imageData, const CRecipeMarker* pRecipeMarker, CResultMarker& resultMarker);
    int        Process_MeasureEllipse(CCHImageData& imageData, const CRecipeMarker* pRecipeMarker, CResultMarker& resultMarker);
    void    CalculateEdgePosition(int nSubPixelType, double dPosiThres, double dNegaThres, int nIteration=0);
 
    BOOL    CalculateInterpolateLinear(CEdgeResult& edgeResult, double dEdgeThreshold);
    BOOL    CalculateInterpolateQuadratic(CEdgeResult& edgeResult);
    BOOL    CalculateInterpolateGaussian(CEdgeResult& edgeResult, int nEdgeType);
    BOOL    CalculateInterpolateQuadraticRegression(CEdgeResult& edgeResult);
    BOOL    CalculateInterpolateGaussianRegression(CEdgeResult& edgeResult, int nEdgeType);
    BOOL    CalculateInterpolateGaussianRegressionReweight(CEdgeResult& edgeResult, int nEdgeType, int nIteartion);
 
 
    BOOL    GetEdgeLeftPosition(int nEdgeType, int nSearchType, int nStartPos, int nEndPos, double& dPosition);
    BOOL    GetEdgeRightPosition(int nEdgeType, int nSearchType, int nStartPos, int nEndPos, double& dPosition);
    BOOL    GetResultFormulation(const CString& strFormulation, CString& strResultFormulation);
 
    static    BOOL    CalculateImageValue(int nDirection, BYTE *pImage, int nWidth, int nHeight, int nStep, VectorDouble& vectorImageData);
    static    BOOL    CalculateEdgeValue(const VectorDouble& vectorImageData, VectorDouble& vectorEdgeData, int nKernelSize, double dKernelSigma);
    static    BOOL    CalculateEdgeValue(const VectorDouble& vectorImageData, VectorDouble& vectorEdgeData, VectorInteger& vectorEdgeType, double dPositiveThres, double dNegativeThres, int nKernelSize, double dKernelSigma);
    static    BOOL    CalculateEdgeValue(int nDirection, short *pImage, int nWidth, int nHeight, int nStep, VectorDouble& vectorEdgeData, VectorInteger& vectorEdgeType, double dPositiveThres, double dNegativeThres);
 
 
    CCHImageData        m_imageSource;
    CCHImageData        m_imageModel;
 
    CRecipeTriangle        m_recipeTriangle;
    CResultTriangle        m_resultTriangle;
 
    VectorDouble        m_vectorImageData;
    VectorDouble        m_vectorEdgeData;
    VectorInteger        m_vectorEdgeType;
    VectorEdgeResult    m_vectorEdgeResult;
 
};