mrDarker
2025-08-06 c2da3f2d26079c9efba3961ef091a325d21d1c86
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
#pragma once
 
enum CORNERCUR_DIR{CO_LT=0,CO_LB,CO_RT,CO_RB};
enum ERRCODE_CORNERCUT{ERR_CO_IMAGENULL=0,ERR_CO_INSRECTNULL,ERR_CO_FINDVERT,ERR_CO_FINDHOR,ERR_CO_SUCCESS};
 
struct sCoPoint {
    double x, y;
};
 
struct sCoLine {
    double mx, my;
    double sx, sy;
};
 
using namespace std;
 
class AFX_EXT_CLASS CCornerCut
{
public:
    CCornerCut(void);
    ~CCornerCut(void);
 
public:
    ERRCODE_CORNERCUT    Inspection(LPBYTE pImg,CSize szImg,CRect &rectIns,int iFrame,int nLowThres,int nHighThres,CORNERCUR_DIR nDir, CString strHPanelID,BOOL bSaveImage
                                    ,COwnerBuffer    &pOrg,double &dResWidth,double &dResHeight);
    void        GetData(sCoPoint *pPoint){CopyMemory(pPoint,m_nCornerPoint,sizeof(sCoPoint)*3);}
 
protected:
    BOOL        FindOuterLine(LPBYTE pImg,CRect rectIns,CORNERCUR_DIR nDir);
    BOOL        FindOuterHori(LPBYTE pImg,CRect rectIns,double nYPos,double nStartX,CORNERCUR_DIR nDir);
    BOOL        FindOuterVert(LPBYTE pImg,CRect rectIns,double nYPos,double nStartX,CORNERCUR_DIR nDir);
    void        SaveMarkImage(LPBYTE pBuffer,CRect &rectIns);
 
    void        AssertOnFrameRect(CRect &rect,CSize szImg=CSize(0,0));
    float        AdaptiveThreshold(BYTE* pImageData, int nWidth, int nHeight, int nStep, int nThresValue);
    float        GetThresholdValue(BYTE* pImageData, int nWidth, int nHeight, int nStep, int nThresValue);
    BOOL        CopyEdgeImg(LPBYTE pImg,CSize szImg,CRect &rectIns);
    void        ThresholdProcessing(LPBYTE pImg,CSize szImg,int nThres,int nDir);
    void        ThresholdProcLowHigh(CSISBuffer &pImg,int nLowThres,int nHighThres);
 
    BOOL        FindVerticalLine(LPBYTE pImg,CRect rectIns,int &nVertLine,CORNERCUR_DIR nDir);
    BOOL        FindHorizontalLine(LPBYTE pImg,CRect rectIns,int &nHorLine,CORNERCUR_DIR nDir);
    BOOL        FindCornerPoint(LPBYTE pImg,CRect rectIns,sCoPoint point,CORNERCUR_DIR nDir);
    int            DetectLine(LPBYTE pImg,CRect rectIns,sCoPoint point,BOOL bDir,BOOL bReverse,CORNERCUR_DIR nDir,sCoPoint &pointResult);
    void        GetRegionInfo(sCoPoint point,CORNERCUR_DIR nDir,BOOL bDir,int &nStart,int &nEnd,BOOL &bReverse,int nWidth,int nHeight);
    int            GetHorCount(LPBYTE pImg,int v,sCoPoint point,BOOL bDir,int nWidth,int nSetCount);
    int            GetVertCount(LPBYTE pImg,int u,sCoPoint point,BOOL bDir,int nWidth,int nHeight,int nSetCount);
    void        CalculateLenght(double &dResWidth,double &dResHeight);
 
    template<typename T>
    inline void swapN(T a,T b)
    {
        T    c;    
        c = a;    
        a = b;    
        b = c;
    }
 
    inline void swapLong(LONG &a,LONG &b)
    {
        LONG    c;    
        c = a;    
        a = b;    
        b = c;
    }
 
protected:
    BOOL        FindFirstPoint(CRect &rectIns);
    double        ransac_line_fitting(sCoPoint *data, int no_data, sCoLine &model, double distance_threshold);
    bool        find_in_samples (sCoPoint *samples, int no_samples, sCoPoint *data);
    void        get_samples (sCoPoint *samples, int no_samples, sCoPoint *data, int no_data);
    int            compute_model_parameter(sCoPoint samples[], int no_samples, sCoLine &model);
    double        compute_distance(sCoLine &line, sCoPoint &x);
    double        model_verification (sCoPoint *inliers, int *no_inliers, sCoLine &estimated_model, sCoPoint *data, int no_data, double distance_threshold);
    BOOL        LineInterSection(CPoint p1,CPoint p2,CPoint p3,CPoint p4,double &dInterX,double &dInterY);
    void        bresenham_Line(COwnerBuffer *pRes,CPoint posStart,CPoint posEnd,BYTE color);
    
protected:    
    LPBYTE        m_pInsImg;
 
    sCoPoint        m_nCornerPoint[3];
    sCoPoint        *m_pOuterLineH,*m_pOuterLineV;
    int            m_nOuterCountH,m_nOuterCountV;
};