robot. In Figure 17.9 the environment window with a colored ball and the console window with displayed image and histogram can be seen.
Program 17.9: Color search main program
1 |
#define |
X |
40 |
// ball coordinates for teaching |
1 |
#define |
Y |
40 |
|
2 |
|
|
|
|
3int main()
4{ colimage c;
5int hue, pos, val;
7LCDPrintf("Teach Color\n");
8LCDMenu("TEA","","","");
9CAMInit(NORMAL);
10while (KEYRead() != KEY1)
11{ CAMGetColFrame(&c,0);
12LCDPutColorGraphic(&c);
13hue = RGBtoHue(c[Y][X][0], c[Y][X][1], c[Y][X][2]);
14LCDSetPos(1,0);
15LCDPrintf("R%3d G%3d B%3d\n",
16 |
c[Y][X][0], c[Y][X][1], c[Y][X][2]); |
17 |
LCDPrintf("hue %3d\n", hue); |
18 |
OSWait(100); |
19 |
} |
20 |
|
21LCDClear();
22LCDPrintf("Detect Color\n");
23LCDMenu("","","","END");
24while (KEYRead() != KEY4)
25{ CAMGetColFrame(&c,0);
26LCDPutColorGraphic(&c);
27ColSearch(c, hue, 10, &pos, &val); /* search image */
28LCDSetPos(1,0);
29LCDPrintf("h%3d p%2d v%2d\n", hue, pos, val);
30 LCDLine (pos, 0, pos, 53, 2); /* vertical line */
31}
32return 0;
33}
The main program for the color search is shown in Program 17.9. In its first phase, the camera image is constantly displayed together with the RGB value and hue value of the middle position. The user can record the hue value of an object to be searched. In the second phase, the color search routine is called with every image displayed. This will display the color detection histogram and also locate the object’s x-position.
This algorithm only determines the x-position of a colored object. It could easily be extended to do the same histogram analysis over all lines (instead of over all columns) as well and thereby produce the full [x, y] coordinates of an object. To make object detection more robust, we could further extend this