图像在处理过程中,有时需要去除一些无用的背景,限制处理范围,以提高图像下一步处理的速度。
下面这个函数主要实现了对二值化后的8位DIB查找范围的功能。其中参数c为背景的颜色.
1
void FindBlankBound(LPBYTE src, RECT & bound,INT c,INT limit)
2

{
3
int w = DIBWidth(src);
4
int h = DIBHeight(src);
5
int bitCount=DIBBitCount(src);
6
int rowstride = DIBCalBytesPerRow(w,bitCount);
7
int startY = 0, endY = 0;
8
int startX = 0, endX = 0;
9
int fillColor= 255-c;
10
11
BYTE* srcData0 = (BYTE*)DIBGetPixelPtr(src,0,0);
12
BYTE * srcData = srcData0;
13
14
int i, j;
15
LONG x0,y0,x1,y1;
16
x0=bound.left;
17
y0=bound.top;
18
x1=bound.right;
19
y1=bound.bottom;
20
21
CLAMP(x0,0,w);
22
CLAMP(x1,0,w);
23
CLAMP(y0,0,h);
24
CLAMP(y1,0,h);
25
26
BOOL bFind=FALSE;
27
28
for (i = x0; i < x1; ++i)
29
{
30
startY = y0;
31
endY = y0;
32
33
for (j = y0; j < y1; ++j)
34
{
35
srcData = srcData0 + j * rowstride;
36
endY = j;
37
if(srcData[i]==c) /**////FIND COLOR
38
///if (IsBlock(srcData0,w,h,i,j,c))
39
break;
40
}
41
42
x0 = i; /**////
43
if (abs((endY - startY) - (y1 - y0)) > limit)
44
break;
45
46
bFind=TRUE;
47
for (j = 0; j < h; ++j)
48
{
49
*( srcData0 + j * rowstride + i) = fillColor;
50
}
51
52
}
53
54
55
56
for (i = (x1 - 1); i >= x0; --i)
57
{
58
startY = y0;
59
endY = y0;
60
for (j = y0; j < y1; ++j)
61
{
62
srcData = srcData0 + j * rowstride;
63
endY = j;
64
65
if(srcData[i]==c)
66
/**////if (IsBlock(srcData0,w,h,i,j,c))
67
break;
68
69
}
70
71
x1 = i + 1;
72
if (abs((endY - startY) - (y1 - y0)) > limit)
73
break;
74
75
for (j = 0; j < h; ++j)
76
{
77
*( srcData0 + j * rowstride + i) = fillColor;
78
}
79
80
}
81
82
if ((x1-x0) <= 1)
83
{
84
y0 = y1;
85
}
86
87
for (j = y0; j < y1; ++j)
88
{
89
90
startX = x0;
91
endX = x0;
92
srcData = srcData0 + j * rowstride;
93
94
for (i = x0; i < x1; ++i)
95
{
96
endX = i;
97
if(srcData[i]==c)
98
/**////if (IsBlock(srcData0,w,h,i,j,c))
99
break;
100
}
101
102
y0 = j;
103
if (abs(endX - startX - (x1 - x0)) > limit)
104
break;
105
106
for (i = 0; i < w; ++i)
107
{
108
srcData[i] = fillColor;
109
}
110
}
111
112
for (j = (y1 - 1); j >= y0; --j)
113
{
114
115
startX = x0;
116
endX = x0;
117
srcData = srcData0 + j * rowstride;
118
for (i = x0; i < x1; ++i)
119
{
120
endX = i;
121
if(srcData[i]==c)
122
/**////if (IsBlock(srcData0,w,h,i,j,c))
123
break;
124
125
}
126
127
128
y1 = j+1;
129
if (abs(endX - startX - (x1 - x0)) > limit)
130
break;
131
132
for (i = 0; i < w; ++i)
133
{
134
srcData[i] = fillColor;
135
}
136
}
137
138
bound.left=x0;
139
bound.top=y0;
140
bound.right=x1;
141
bound.bottom=y1;
142
143
}