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
#include "stdafx.h"
#include "InChipDotProcess.h"
#include "VisionBufferPro.h"
#include "Bspline.h"
 
CInChipDotProcess::CInChipDotProcess(){
    /* code */
    m_nSampleNumber = 16;
    m_nLineWidth = 50;
    m_nChipOffset = -32;
    m_nChipRange = 30;
    m_threshold = 20;
    m_nPointType = 1;
    m_nPointMode = 2;
    m_eDir = DIMENSION_NONE;
    CVisionBufferPro::getLineFrameSize(m_eDir, m_nFrameWidth, m_nFrameHeight);
    ClsVision::GenEmptyObject(m_hRoiRegion);
}
 
CInChipDotProcess::CInChipDotProcess(DimensionDir eDir) {
    /* code */
    m_nSampleNumber = 16;
    m_nLineWidth = 100;
    m_nChipOffset = -32;
    m_nChipRange = 30;
    m_threshold = 20;
    m_nPointType = 1;
    m_nPointMode = 2;
    m_eDir = eDir;
    CVisionBufferPro::getLineFrameSize(m_eDir, m_nFrameWidth, m_nFrameHeight);
    ClsVision::GenEmptyObject(m_hRoiRegion);
}
 
CInChipDotProcess::~CInChipDotProcess()
{
}
 
int CInChipDotProcess::Execute(DimensionDir eDir, int index, Point2I ptStart, Point2I ptEnd) {
    /* code */
    if (index < 0) return 0;
 
    return ExeInChip(eDir, index, ptStart, ptEnd);
}
 
int CInChipDotProcess::ExeInChip(DimensionDir eDir, int index, Point2I ptStart, Point2I ptEnd) {
    /* code */
    if (m_nFrameWidth < 1 || m_nFrameHeight < 1) return 0;
 
    int y1 = index * m_nFrameHeight;
    int y2 = index * m_nFrameHeight + m_nFrameHeight - 1;
    int x1 = ptStart.x - m_nLineWidth;
    int x2 = ptEnd.x + m_nLineWidth;
    if (ptStart.x > ptEnd.x) {
        x1 = ptEnd.x - m_nLineWidth;
        x2 = ptStart.x + m_nLineWidth;
    }
    x1 -= 30;
    x2 += 30;
    HalconCpp::HObject hImage;
    if (!CVisionBufferPro::getImageROI(m_eDir, hImage, x1, y1, x2, y2))  return 0;
    int leftPos = x1;
    int topPos = y1;
 
    int width = 0; 
    int height = 0;
    ClsVision::GetImageSize(hImage, width, height);
    //HalconCpp::HWindow hDispWin(0, 0, width, height);
    //hDispWin.SetPart(0, 0, height - 1, width - 1);
    //hDispWin.SetColored(6);
    //hDispWin.DispObj(hImage);
 
    std::vector<Point2D> vEdgePoints;
    int step = (int)(1.0 * (y2 - y1) / (m_nSampleNumber - 1));
    for (int i = 0; i < m_nSampleNumber; i++) {
        int yTarget = y1 + i * step;
        if (0 == i) {
            yTarget = y1 + 15;
        }
        else if (m_nSampleNumber - 1 == i) {
            yTarget = y2 - 15;
        }
 
        Line2D line;
        line.pt0.x = 30;
        line.pt0.y = yTarget - topPos;
        line.pt1.x = x2 - leftPos - 31;
        line.pt1.y = yTarget - topPos;
        Point2D ptStart, ptEnd;
        if (!CVisionBufferPro::GetMeasurePos(hImage, line, m_threshold, m_nPointMode, 10, ptStart, ptEnd)) continue;
        if (0 == i) {
            ptStart.y = 0;
            ptEnd.y = 0;
        }
        else if (m_nSampleNumber - 1 == i) {
            ptStart.y = height - 1;
            ptEnd.y = height - 1;
        }
 
        if (0 == m_nPointType) {
            vEdgePoints.push_back(ptStart);
        }
        else {
            vEdgePoints.push_back(ptEnd);
        }
    }
 
    int num = (int)(vEdgePoints.size());
    if (num < 0.5 * m_nSampleNumber) return 0;
 
    //1. »ñÈ¡µãÕó
    std::vector<vec> vStartCtrlPoint;
    for (int i = 0; i < num; i++) {
        float x0 = (float)vEdgePoints[i].x;
        float y0 = (float)vEdgePoints[i].y;
        vec p0(x0, y0, 0);
        vStartCtrlPoint.push_back(p0);
    }
 
    //2. Ö´ÐÐÑùÌõ²åÖµ
    CBspline bcurve(vStartCtrlPoint);
    bcurve.execute();
 
    int sz = (int)(bcurve.m_vPtResults.size());
    if (sz < 1) return 0;
    HalconCpp::HTuple hvY, hvX;
    for (int i = 0; i < sz; i++) {
        hvX[i] = bcurve.m_vPtResults[i][0] + m_nChipOffset;
        hvY[i] = bcurve.m_vPtResults[i][1];
    }
    int idx = sz;
    hvX[idx] = bcurve.m_vPtResults[sz - 1][0] + m_nChipOffset;
    hvY[idx] = bcurve.m_vPtResults[sz - 1][1];
    idx += 1;
    hvX[idx] = bcurve.m_vPtResults[sz - 1][0] + m_nChipOffset + m_nChipRange;
    hvY[idx] = bcurve.m_vPtResults[sz - 1][1];
    idx += 1;
    for (int i = sz - 1; i >= 0; i--) {
        hvX[idx] = bcurve.m_vPtResults[i][0] + m_nChipOffset + m_nChipRange;
        hvY[idx] = bcurve.m_vPtResults[i][1];
        idx += 1;
    }
 
    HalconCpp::GenRegionPolygonFilled(&m_hRoiRegion, hvY, hvX);
    //hDispWin.DispObj(m_hRoiRegion);
    //hDispWin.Click();
 
    HalconCpp::HObject hProObject, hRegion, hConnectRegion;
    HalconCpp::ReduceDomain(hImage, m_hRoiRegion, &hProObject);
    HalconCpp::Threshold(hProObject, &hRegion, 0, 60);
    HalconCpp::Connection(hRegion, &hConnectRegion);
    //hDispWin.ClearWindow();
    //hDispWin.DispObj(hImage);
    //hDispWin.DispObj(hConnectRegion);
    //hDispWin.Click();
 
    return 0;
}