-
Notifications
You must be signed in to change notification settings - Fork 4
/
colortrace.c
286 lines (243 loc) · 6.46 KB
/
colortrace.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "stm32f10x.h"
#include "bsp_ili9341_lcd.h"
#include "EasyTracer.h"
#define min3v(v1, v2, v3) ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
#define max3v(v1, v2, v3) ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
typedef struct //RGB格式颜色
{
unsigned char red; // [0,255]
unsigned char green; // [0,255]
unsigned char blue; // [0,255]
}COLOR_RGB;
typedef struct //HSL格式颜色
{
unsigned char hue; // [0,240] 色彩
unsigned char saturation; // [0,240] 饱和度
unsigned char luminance; // [0,240] 亮度
}COLOR_HSL;
typedef struct //区域
{
unsigned int X_Start;
unsigned int X_End;
unsigned int Y_Start;
unsigned int Y_End;
}SEARCH_AREA;
//读取RBG格式颜色,唯一需要移植的函数
//extern unsigned short GUI_ReadBit16Point(unsigned short x,unsigned short y);
#define GUI_ReadBit16Point LCD_GetPoint
//读取坐标点处的颜色
static void ReadColor(unsigned int x,unsigned int y,COLOR_RGB *Rgb)
{
unsigned short C16;
C16 = GUI_ReadBit16Point(x,y); //读点的颜色
Rgb->red = (unsigned char)((C16&0xf800)>>8);
Rgb->green = (unsigned char)((C16&0x07e0)>>3);
Rgb->blue = (unsigned char)((C16&0x001f));
}
//RGB转HSL
static void RGBtoHSL(const COLOR_RGB *Rgb, COLOR_HSL *Hsl)
{
int h,s,l,maxVal,minVal,difVal;
int r = Rgb->red;
int g = Rgb->green;
int b = Rgb->blue;
maxVal = max3v(r, g, b);
minVal = min3v(r, g, b);
difVal = maxVal-minVal;
//计算亮度
l = (maxVal+minVal)*240/255/2;
if(maxVal == minVal)//若r=g=b
{
h = 0;
s = 0;
}
else
{
//计算色调
if(maxVal==r)
{
if(g>=b)
h = 40*(g-b)/(difVal);
else
h = 40*(g-b)/(difVal) + 240;
}
else if(maxVal==g)
h = 40*(b-r)/(difVal) + 80;
else if(maxVal==b)
h = 40*(r-g)/(difVal) + 160;
//计算饱和度
if(l == 0)
s = 0;
else if(l<=120)
s = (difVal)*240/(maxVal+minVal);
else
s = (difVal)*240/(480 - (maxVal+minVal));
}
Hsl->hue = (unsigned char)(((h>240)? 240 : ((h<0)?0:h)));
Hsl->saturation = (unsigned char)(((s>240)? 240 : ((s<0)?0:s)));
Hsl->luminance = (unsigned char)(((l>240)? 240 : ((l<0)?0:l)));
}
//匹配颜色
static int ColorMatch(const COLOR_HSL *Hsl,const TARGET_CONDI *Condition)
{
if(
Hsl->hue > Condition->H_MIN &&
Hsl->hue < Condition->H_MAX &&
Hsl->saturation > Condition->S_MIN &&
Hsl->saturation < Condition->S_MAX &&
Hsl->luminance > Condition->L_MIN &&
Hsl->luminance < Condition->L_MAX
)
return 1;
else
return 0;
}
//搜索腐蚀中心
static int SearchCentre(unsigned int *x, unsigned int *y, const TARGET_CONDI *Condition, const SEARCH_AREA *Area)
{
unsigned int SpaceX,SpaceY,i,j,k,FailCount=0;
COLOR_RGB Rgb;
COLOR_HSL Hsl;
SpaceX = Condition->WIDTH_MIN/3;
SpaceY = Condition->HIGHT_MIN/3;
for(i=Area->Y_Start;i<Area->Y_End;i+=SpaceY)
{
for(j=Area->X_Start;j<Area->X_End;j+=SpaceX)
{
FailCount=0;
for(k=0;k<SpaceX+SpaceY;k++)
{
if(k<SpaceX)
ReadColor(j+k,i+SpaceY/2,&Rgb);
else
ReadColor(j+SpaceX/2,i+(k-SpaceX),&Rgb);
RGBtoHSL(&Rgb,&Hsl);
if(!ColorMatch(&Hsl,Condition))
FailCount++;
if(FailCount>((SpaceX+SpaceY)>>ALLOW_FAIL_PER))
break;
}
if(k == SpaceX+SpaceY)
{
*x = j+SpaceX/2;
*y = i+SpaceY/2;
return 1;
}
}
}
return 0;
}
//从腐蚀中心向外腐蚀,得到新的腐蚀中心
static int Corrode(unsigned int oldx, unsigned int oldy, const TARGET_CONDI *Condition, RESULT *Resu)
{
unsigned int Xmin,Xmax,Ymin,Ymax,i,FailCount=0;
COLOR_RGB Rgb;
COLOR_HSL Hsl;
for(i=oldx;i>IMG_X;i--)
{
ReadColor(i,oldy,&Rgb);
RGBtoHSL(&Rgb,&Hsl);
if(!ColorMatch(&Hsl,Condition))
FailCount++;
if(FailCount>(((Condition->WIDTH_MIN+Condition->WIDTH_MAX)>>2)>>ALLOW_FAIL_PER))
break;
}
Xmin=i;
FailCount=0;
for(i=oldx;i<IMG_X+IMG_W;i++)
{
ReadColor(i,oldy,&Rgb);
RGBtoHSL(&Rgb,&Hsl);
if(!ColorMatch(&Hsl,Condition))
FailCount++;
if(FailCount>(((Condition->WIDTH_MIN+Condition->WIDTH_MAX)>>2)>>ALLOW_FAIL_PER))
break;
}
Xmax=i;
FailCount=0;
for(i=oldy;i>IMG_Y;i--)
{
ReadColor(oldx,i,&Rgb);
RGBtoHSL(&Rgb,&Hsl);
if(!ColorMatch(&Hsl,Condition))
FailCount++;
if(FailCount>(((Condition->HIGHT_MIN+Condition->HIGHT_MAX)>>2)>>ALLOW_FAIL_PER))
break;
}
Ymin=i;
FailCount=0;
for(i=oldy;i<IMG_Y+IMG_H;i++)
{
ReadColor(oldx,i,&Rgb);
RGBtoHSL(&Rgb,&Hsl);
if(!ColorMatch(&Hsl,Condition))
FailCount++;
if(FailCount>(((Condition->HIGHT_MIN+Condition->HIGHT_MAX)>>2)>>ALLOW_FAIL_PER))
break;
}
Ymax=i;
FailCount=0;
Resu->x = (Xmin+Xmax)/2;
Resu->y = (Ymin+Ymax)/2;
Resu->w = Xmax-Xmin;
Resu->h = Ymax-Ymin;
if(((Xmax-Xmin)>(Condition->WIDTH_MIN)) && ((Ymax-Ymin)>(Condition->HIGHT_MIN)) &&\
((Xmax-Xmin)<(Condition->WIDTH_MAX)) && ((Ymax-Ymin)<(Condition->HIGHT_MAX)) )
return 1;
else
return 0;
}
//唯一的API,用户将识别条件写入Condition指向的结构体中,该函数将返回目标的x,y坐标和长宽
//返回1识别成功,返回1识别失败
int Trace(const TARGET_CONDI *Condition, RESULT *Resu)
{
unsigned int i;
static unsigned int x0,y0,flag_=0;
static SEARCH_AREA Area={IMG_X, IMG_X+IMG_W, IMG_Y, IMG_Y+IMG_H}; //识别的有效范围 这里定义为整个屏幕(320*240)
RESULT Result; //保存识别结果
if(flag_ == 0)
{
if(SearchCentre(&x0,&y0,Condition,&Area)) //搜索腐蚀中心
flag_ = 1;
else
{
Area.X_Start= IMG_X ;
Area.X_End = IMG_X+IMG_W ;
Area.Y_Start= IMG_Y ;
Area.Y_End = IMG_Y+IMG_H;
if(SearchCentre(&x0,&y0,Condition,&Area))
{
flag_ =0;
return 0;
}
}
}
Result.x = x0;
Result.y = y0;
for(i=0;i<ITERATE_NUM;i++)
Corrode(Result.x,Result.y,Condition,&Result); //从腐蚀中心向外腐蚀,得到新的腐蚀中心
if(Corrode(Result.x,Result.y,Condition,&Result))
{
x0 = Result.x;
y0 = Result.y;
Resu->x = Result.x;
Resu->y = Result.y;
Resu->w = Result.w;
Resu->h = Result.h;
flag_ = 1;
Area.X_Start= Result.x - ((Result.w)>>1);
Area.X_End = Result.x + ((Result.w)>>1);
Area.Y_Start= Result.y - ((Result.h)>>1);
Area.Y_End = Result.y + ((Result.h)>>1);
return 1;
}
else
{
flag_ = 0;
return 0;
}
}
//使用举例
//RESULT Resu;
//TARGET_CONDI Condition={60,100,20,120,10,160,40,40,320,240};
//Trace(&Condition,&Resu);