二值图像跟踪曲线程序
稍后提交一个完整的测试程序
1
2
BOOL WINAPI TraceDIB(LPSTR lpDIBBits,LONG lWidth,LONG lHeight,CArray<CPoint,CPoint>* pPoint,CArray<int,int>* pSlope)
3
{
4
// 指向源图像的指针
5
BYTE*lpSrc;
6
7
// 指向缓存图像的指针
8
BYTE*lpDst;
9
10
// 指向缓存DIB图像的指针
11
BYTE*lpNewDIBBits;
12
HLOCALhNewDIBBits;
13
14
// 图像每行的字节数
15
LONG lLineBytes=WIDTHBYTES(lWidth * 8);
16
17
long i,j;
18
DWORD ii=0;
19
20
BYTE pixel;
21
bool bFindStartPoint;
22
23
//是否扫描到一个边界点
24
bool bFindPoint;
25
26
27
//起始边界点与当前边界点
28
CPoint StartPoint,CurrentPoint;
29
//顺时针方向
30
int Direction[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
31
int BeginDirect;
32
33
hNewDIBBits = LocalAlloc(LHND, lLineBytes * lHeight);
34
if (hNewDIBBits == NULL)
35
{
36
// 分配内存失败
37
return FALSE;
38
}
39
lpNewDIBBits = (BYTE* )LocalLock(hNewDIBBits);
40
// 初始化新分配的内存,设定初始值为255
41
lpDst = (BYTE*)lpNewDIBBits;
42
memset(lpDst, (BYTE)255, lLineBytes * lHeight);
43
44
45
//先找到最左下方的边界点
46
bFindStartPoint = false;
47
for (j = 0;j < lHeight && !bFindStartPoint;j++)
48
{
49
for(i = 0;i < lWidth && !bFindStartPoint;i++)
50
{
51
// 指向源图像倒数第j行,第i个象素的指针
52
lpSrc = (BYTE *)lpDIBBits + lLineBytes * j + i;
53
54
pixel = (BYTE)*lpSrc;
55
56
if(pixel == 0)
57
{
58
bFindStartPoint = true;
59
60
StartPoint.y = j;
61
StartPoint.x = i;
62
63
// 指向目标图像倒数第j行,第i个象素的指针
64
lpDst = (BYTE *)lpNewDIBBits + lLineBytes * j + i;
65
*lpDst = (BYTE)0;
66
}
67
}
68
}
69
70
//由于起始点是在左下方,故起始扫描沿左上方向
71
BeginDirect = 5;
72
//跟踪边界
73
bFindStartPoint = false;
74
//从初始点开始扫描
75
CurrentPoint.y = StartPoint.y;
76
CurrentPoint.x = StartPoint.x;
77
pPoint->Add(CurrentPoint);
78
// 第一个点的斜率为2
79
pSlope->Add(int(2));
80
ii++;
81
82
while(!bFindStartPoint)
83
{
84
bFindPoint = false;
85
while(!bFindPoint)
86
{
87
//沿扫描方向查看一个像素
88
long offset = lLineBytes * ( CurrentPoint.y + Direction[BeginDirect][1])
89
+ (CurrentPoint.x + Direction[BeginDirect][0]);
90
lpSrc = (unsigned char *)lpDIBBits + offset;
91
lpDst = (unsigned char *)lpNewDIBBits + offset;
92
pixel = (unsigned char)*lpSrc;
93
94
if(pixel == 0&&(unsigned char)*lpDst==255)
95
{
96
bFindPoint = true;
97
CurrentPoint.y = CurrentPoint.y + Direction[BeginDirect][1];
98
CurrentPoint.x = CurrentPoint.x + Direction[BeginDirect][0];
99
pPoint->Add(CurrentPoint);
100
101
// s = BeginDirect%4;
102
//if(s==3)
103
//s = -1;
104
//左上方代表方向的5,以后按照
105
pSlope->Add(BeginDirect);//s;
106
ii++;
107
*lpDst = (unsigned char)0;
108
//扫描的方向逆时针旋转两格
109
BeginDirect--;
110
if(BeginDirect == -1)
111
BeginDirect = 7;
112
BeginDirect--;
113
if(BeginDirect == -1)
114
BeginDirect = 7;
115
}
116
else
117
{
118
if(pixel!=0)
119
{
120
//扫描方向顺时针旋转一格
121
BeginDirect++;
122
if(BeginDirect == 8)
123
BeginDirect = 0;
124
}
125
else{
126
// 扫描到以扫过的点
127
bFindPoint = true;
128
bFindStartPoint = true;
129
}
130
}
131
132
}
133
}
134
135
ASSERT(pPoint->GetSize()==ii);
136
137
// 复制腐蚀后的图像
138
memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);
139
140
// 释放内存
141
LocalUnlock(hNewDIBBits);
142
LocalFree(hNewDIBBits);
143
144
// 返回
145
return TRUE;
146
}
147
148
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
浙公网安备 33010602011771号