-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplecamera.c
127 lines (61 loc) · 2.48 KB
/
samplecamera.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
/* This sample program is a demo for using the graphics API to display the camera image along as many as 5 bounding boxes for blobs on color channel 0. If a bounding box is touched, the program reports its number.
*/
void show_cam_image();
int main()
{
int n, b, tf, row, col, rgb[3]={255,0,0}; // color for box
rectangle bbx[5]; // array of rectangles to hold up to 5 bounding boxes
camera_open(); // open camera
graphics_open(160, 120); // camera window in middle of the screen
display_clear(); // get rid of any messages from camera_open()
while(!side_button()){ // physical button used for exit
camera_update(); // new camera data
show_cam_image(); // display camera image on Link
n=get_object_count(0);
n=n<5 ? n : 5; // no more than 5 blobs processed
if (n > 0) { // if there is a blob, locate it and show its bbox
for(b=0;b<n;b++){
bbx[b] = get_object_bbox(0,b);
if (bbx[b].width < 6 || bbx[b].height < 6) {
n=b;
break; // rest of blobs too small to worry about
}
graphics_rectangle(bbx[b].ulx,bbx[b].uly,(bbx[b].ulx+bbx[b].width),
(bbx[b].uly+bbx[b].height), rgb[0], rgb[1], rgb[2]);
}
}
graphics_update(); // update Link screen
if (get_mouse_left_button()) { // has user tapped the screen?
get_mouse_position(&col, &row);
tf=0;
for(b=0; b<n; b++) {
if ((col>=bbx[b].ulx) && (col<=(bbx[b].ulx+bbx[b].width)) &&
(row>=bbx[b].uly) && (row<=(bbx[b].uly+bbx[b].height))) {
display_printf(1,0,"blob %d ",b);
tf=1; // tap flag
}
}
if (tf==0) {
display_printf(1,0,"not a blob ",b);
}
}
}
// clean things up and exit
camera_close();
graphics_close();
return 0;
}
// display the (default) camera 160x120 pixel image in a graphics window
void show_cam_image(){
const unsigned char *img=get_camera_frame();
// Frame data is in BGR 888 pixel format: 3 bytes per pixel; each
// character is the 8-bit (unsigned) integer value for each BGR color
int row, col, rgb[3]={2,1,0}; // array used to convert BGR to RGB
for(row=0;row<120;row++){
for(col=0;col<160; col++){
graphics_pixel(col,row,
img[3*(160*row+col)+rgb[0]],img[3*(160*row+col)+rgb[1]],
img[3*(160*row+col)+rgb[2]]);
}
}
}