SDC C-Project CF Review 프로그램
LYW
2021-11-09 9ad2aa59da822e9d30c5e0cd677025fe6e12df95
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
#pragma once
 
namespace CHImageControls
{
    enum ImageConvertType {
        ConvertTypeGray2RGB = 0, ConvertTypeRGB2Gray, ConvertTypeRGBA2RGB};
    enum ImageFlipType        { FlipTypeBoth=-1, FlipTypeVert=0, FlipTypeHoriz=1 };
    enum ImageRotateType    { RotateTypeRight=0, RotateTypeLeft };
    enum ProcessResultType    { ResultSuccess=1, ResultImageNULL=0, ResultImageNoExist=-1, ResultFail=-2 };
}
 
#include <list>
#include <vector>
#include <algorithm>
#include "CHImageData.h"
 
struct _IplConvKernel;
 
 
class AFX_EXT_CLASS CLineData
{
public:
    CLineData()
    {
        dPoint1X = 0.;
        dPoint1Y = 0.;
        dPoint2X = 0.;
        dPoint2Y = 0.;
        dRatio = 0.;
        dPeak = 0.;
        nCount = 0;
    }
 
    CLineData(double dFx, double dFy, double dSx, double dSy, double dCount=0)
    {
        dPoint1X = dFx;
        dPoint1Y = dFy;
        dPoint2X = dSx;
        dPoint2Y = dSy;
        dLength = sqrt((dPoint1X - dPoint2X) * (dPoint1X - dPoint2X) + (dPoint1Y - dPoint2Y) * (dPoint1Y - dPoint2Y));
        dRatio = 0.;
        dPeak = 0.;
        nCount = 0;
    }
 
    double dPoint1X;
    double dPoint1Y;
    double dPoint2X;
    double dPoint2Y;
    double dLength;
    double    dRatio;
    double    dPeak;
    int        nCount;
 
};
typedef std::vector<CLineData>                        VectorLineData;
typedef std::vector<CLineData>::iterator            VectorLineDataIt;
 
class AFX_EXT_CLASS CCircleData
{
public:
    CCircleData()
    {
        dCenterX    = 0.;
        dCenterY    = 0.;
        dRadius        = 0.;
    }
 
    double dCenterX;
    double dCenterY;
    double dRadius;
};
typedef std::vector<CCircleData>                    VectorCircleData;
typedef std::vector<CCircleData>::iterator            VectorCircleDataIt;
 
struct SDefectBlob
{
    SDefectBlob()    
    { 
        Reset(); 
    }
 
    SDefectBlob(int left, int top, int right, int bottom)
    {
        nLeft    = left;
        nTop    = top;
        nRight    = right;
        nBottom = bottom;
        nPeak    = 0;
        nCount    = 0;
        //    vectorPoint.clear();
    }
 
    void Reset()
    {
        nLeft    = INT_MAX;
        nTop    = INT_MAX;
        nRight    = INT_MIN;
        nBottom = INT_MIN;
        nPeak    = 0;
        nCount    = 0;
        //    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 nCount; }
    int        GetAvgPeak() const        { return (nCount==0) ? 0 : nPeak / nCount; }
 
    int                    nLeft;
    int                    nTop;
    int                    nRight;
    int                    nBottom;
    int                    nPeak;
    int                    nCount;
 
    //std::vector<CPoint> vectorPoint;
};
typedef std::vector<SDefectBlob>            VectorDefectBlob;
typedef std::vector<SDefectBlob>::iterator    VectorDefectBlobIt;
 
 
class AFX_EXT_CLASS CCHImageProcess
{
public:
    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;
 
    static BOOL    CalculateLagrange(VectorDouble& vectorX, VectorDouble& vectorY, ListPolynomial& listPolynomial);
 
public:
    CCHImageProcess(void);
    virtual ~CCHImageProcess(void);
 
    static int ImageConvert(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nConvertCode);
    static int ImageAverage(CCHImageData* pSourceImage, double& dValue);
    static int ImageMinMax(CCHImageData* pSourceImage, double& dMinValue, double& dMaxValue);
 
    static int ImageResize(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nNewWidth, int nNewHeight, int nInterpolation=1);
    static int ImageResize(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dScale, int nInterpolation=1);
    static int ImageFlip(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nFlipMode);
 
    static int ImageErode(CCHImageData* pSourceImage, CCHImageData* pResultImage, _IplConvKernel* pElement=NULL, int nIterations=1);
    static int ImageDilate(CCHImageData* pSourceImage, CCHImageData* pResultImage, _IplConvKernel* pElement=NULL, int nIterations=1);
    static int ImageOpening(CCHImageData* pSourceImage, CCHImageData* pResultImage, _IplConvKernel* pElement=NULL, int nIterations=1);
    static int ImageClosing(CCHImageData* pSourceImage, CCHImageData* pResultImage, _IplConvKernel* pElement=NULL, int nIterations=1);
 
    static int ImageRotate(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dAngle, int nFlag=9, UCHAR nValue=0);
    static int ImageRotate(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nRotateType);
 
    static int ImageSmooth(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nSmoothType=2, int nSize1=3, int nSize2=0, double dSigma1=1.0, double dSigma2=1.0);
    static int ImageNormalize(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dMin, double dMax, int nType=32);
 
    static int ImageAdd(CCHImageData* pAImage, CCHImageData* pBImage, CCHImageData* pCImage);
    static int ImageAnd(CCHImageData* pSourceImage, CCHImageData* pMaskImage, CCHImageData* pResultImage);
    static int ImageEqualizeHist(CCHImageData* pSourceImage, CCHImageData* pResultImage);
    static int ImagePreCornerDetect(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nApertureSize=3);
    static int ImageSobel(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nXOrder, int nYOrder, int nApertureSize=3);
    static int ImageSobel(CCHImageData* pSourceImage, CCHImageData* pXImage, CCHImageData* pYImage, int nApertureSize=0);
    static int ImageCanny(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dThreshold1, double dThreshold2, int nApertureSize=3);
    static int ImageAdaptiveThreshold(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dMaxValue, int nMethodType=0, int nThresholdType=0, int nBlockSize=3, double dParam1=5);
    static int ImageThreshold(CCHImageData* pSourceImage, CCHImageData* pResultImage, double dThresholdValue, double dMaxValue, int nThresholdType);
 
    static int ImageFFT(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nFlags, CCHImageData* pNormalImage=NULL);
    static int ImageInverseFFT(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nWidth, int nHeight);
 
    static int ImageLowPassFilter(CCHImageData* pSourceImage, CCHImageData* pResultImage, int D0, int N, int nFlags=1, CCHImageData *pNormalImage=NULL);
    static int ImageHighPassFilter(CCHImageData* pSourceImage, CCHImageData* pResultImage, int D0, int N, int nFlags=1, CCHImageData *pNormalImage=NULL);
 
    static int ImageHoughLines2(CCHImageData* pSourceImage, int nMethod, double dRho, double dTheta, int nThreshold, double dParam1, double dParam2, VectorLineData& vectorLineData);
    static int ImageHoughCircles(CCHImageData* pSourceImage, int nMethod, double dDp, double dMinDist, double dParam1, double dParam2, int nMinRadius, int nMaxRadius, VectorCircleData& vectorCircleData);
 
    static int MatrixSolve(double *A, double* B, double* X, int nSize, int nMethod=0);
 
    static int ImageDCT(CCHImageData* pSourceImage, CCHImageData* pResultImage);
    static int ImageInvertDCT(CCHImageData* pSourceImage, CCHImageData* pResultImage);
    static void    RemovePolynomial(ListPolynomial& listPolynomial);
 
    static int ImageFindLineInfo(CCHImageData *pSource, float rho, float theta, int threshold, int lineLength, int lineGap, VectorLineData& vectorLine, int linesMax=INT_MAX);
    //static int FastMatchTemplate(const IplImage& source, const IplImage&  target, std::vector<CvPoint>* foundPointsList, std::vector<double>* confidencesList, int matchPercentage=70, BOOL findMultipleTargets=TRUE, int numMaxima=10, int numDownPyrs=2, int searchExpansion=15);
 
    static    BOOL    ImageSmooth_HybridMedianFilter(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nFilterSize);
    static    BOOL    ImageSmooth_HybridMedianFilter2(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nFilterSize);
    static    int        GetHybridMedianFilterValue(BYTE* pKernelValue, int nFilterSize);
    static    void    Swap(int* pA, int* pB);
    static    void    QuickSort(int* nArr, int nStart, int nEnd);
    static    void    ImageDiffusion(BYTE *pBuffer, int w, int h, int iter, float lambda=0.25f, float k=4.0f);
 
    // pixel
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CCHImageData* pResultImage, int nMethod=5);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CRect& rtROIRegion,  CCHImageData* pResultImage, int nMethod=5);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, int& nResultX, int& nResultY, double& dResultValue, int nMethod=5);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, int& nResultX, int& nResultY, double& dResultValue, CCHImageData* pResultImage, int nMethod=5);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CRect& rtROIRegion, int& nResultX, int& nResultY, double& dResultValue, int nMethod=5);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CRect& rtROIRegion, int& nResultX, int& nResultY, double& dResultValue, CCHImageData* pResultImage, int nMethod=5);
 
    // sub pixel
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, double& dResultX, double& dResultY, double& dResultValue, int nMethod=5, int nSubPixel=0);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, double& dResultX, double& dResultY, double& dResultValue, CCHImageData* pResultImage, int nMethod=5, int nSubPixel=0);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CRect& rtROIRegion, double& dResultX, double& dResultY, double& dResultValue, int nMethod=5, int nSubPixel=0);
    static int ImageMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, CRect& rtROIRegion, double& dResultX, double& dResultY, double& dResultValue, CCHImageData* pResultImage, int nMethod=5, int nSubPixel=0);
 
    // pixel
    static int ImageFastMatching(CCHImageData* pSourceImage, CCHImageData* pTemplateImage, int& nResultX, int& nResultY, double& dResultValue);
    static int ImageConvolution(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nLeft, int nTop, int nRight, int nBottom, int nKernelX, int nKernelY, int nPitch, int nThres);
    static int ImageLocalConvolution(CCHImageData* pSourceImage, CCHImageData* pVertImage, CCHImageData* pHoriImage, int nPitchX, int nPitchY, int& nPosX, int& nPosY);
    static int ImageFindDefect(CCHImageData *pImageData, int nPitchX, int nPitchY, int nThreshold, int nBlobMargin, POINT& ptResult, int nLocalCount=2, int nKernelX=1, int nKernelY=1);
    static int ImageBlobAnalysis4FindDefect(BYTE *pImage, int nWidth, int nHeight, int nThreshold, int nBlobMargin, int nStartX, int nStartY, VectorDefectBlob& vectorBlob);
 
    static int ImageIntegral(CCHImageData* pSourceImage, CCHImageData* pResultImage);
    static int ImageKernelMean(CCHImageData* pSourceImage, CCHImageData* pResultImage, int nKernelSizeW, int nKernelSizeH);
protected:
    
 
    static void    InsertPolynomial(ListPolynomial &listPolynomial, int degree, double coef);
    static double    Combi(VectorDouble& vectorX, VectorDouble& vectorY, int n, int r);
 
};