Simple Track Color

This is a very simple example of how to use the cc3-ilp to track colors. The color in this example is hard-coded to a bright red object. The output on a terminal program (115,200 8N1) will look something like this:

centroid = 0,0 bounding box = 0,0,0,0 num pix= 0 density = 0
centroid = 144,91 bounding box = 124,85,175,106 num pix= 462 density = 431
centroid = 118,103 bounding box = 70,92,171,112 num pix= 1169 density = 578
centroid = 96,110 bounding box = 40,97,141,122 num pix= 1393 density = 551

The code looks like this:

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <cc3.h>
 4#include <cc3_ilp.h>
 5#include <cc3_color_track.h>
 6
 7void simple_track_color(cc3_track_pkt_t* t_pkt);
 8
 9int main(void) {
10  cc3_track_pkt_t t_pkt;
11
12  cc3_uart_init (0, 
13         CC3_UART_RATE_115200,
14         CC3_UART_MODE_8N1,
15         CC3_UART_BINMODE_TEXT);
16
17  cc3_camera_init ();
18
19  //cc3_camera_set_colorspace(CC3_COLORSPACE_YCRCB);
20  cc3_camera_set_resolution(CC3_CAMERA_RESOLUTION_LOW);
21  //cc3_pixbuf_frame_set_subsample(CC3_SUBSAMPLE_NEAREST, 2, 2);
22
23  // init pixbuf with width and height
24  cc3_pixbuf_load();
25
26  // Load in your tracking parameters
27  t_pkt.lower_bound.channel[CC3_CHANNEL_RED] = 150;
28  t_pkt.upper_bound.channel[CC3_CHANNEL_RED] = 255;
29  t_pkt.lower_bound.channel[CC3_CHANNEL_GREEN] = 0;
30  t_pkt.upper_bound.channel[CC3_CHANNEL_GREEN] = 50;
31  t_pkt.lower_bound.channel[CC3_CHANNEL_BLUE] = 0;
32  t_pkt.upper_bound.channel[CC3_CHANNEL_BLUE] = 50; 
33  t_pkt.noise_filter = 2; 
34
35  while(true) {
36    simple_track_color(&t_pkt);
37    printf( "centroid = %d,%d bounding box = %d,%d,%d,%d num pix= %d density = %d\n",
38            t_pkt.centroid_x, t_pkt.centroid_y,
39            t_pkt.x0,t_pkt.y0,t_pkt.x1,t_pkt.y1,
40            t_pkt.num_pixels, t_pkt.int_density );
41
42   }
43
44}
45
46void simple_track_color(cc3_track_pkt_t * t_pkt)
47{
48  cc3_image_t img;
49
50  img.channels = 3;
51  img.width = cc3_g_pixbuf_frame.width;
52  img.height = 1;               // image will hold just 1 row for scanline processing
53  img.pix = cc3_malloc_rows (1);
54  if (img.pix == NULL) {
55    return;
56  }
57
58    cc3_pixbuf_load ();
59    if (cc3_track_color_scanline_start (t_pkt) != 0) {
60      while (cc3_pixbuf_read_rows (img.pix, 1)) {
61       // This does the HSV conversion 
62       // cc3_rgb2hsv_row(img.pix,img.width);
63           cc3_track_color_scanline (&img, t_pkt);
64          }
65        }
66    cc3_track_color_scanline_finish (t_pkt);
67
68  free (img.pix);
69  return;
70}
71

Tracking Multiple Colors

The following example tracks bright red and green within the same frame without rewinding the FIFO. Loading the scanline into memory is a relatively slow operation. Once the scanline resides in memory computation over it is fast.

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <cc3.h>
 4#include <cc3_ilp.h>
 5#include <cc3_color_track.h>
 6
 7void simple_track_color_2(cc3_track_pkt_t* t_pkt,cc3_track_pkt_t* t_pkt2);
 8
 9int main(void) {
10  cc3_track_pkt_t t_pkt1;
11  cc3_track_pkt_t t_pkt2;
12
13  cc3_uart_init (0, 
14         CC3_UART_RATE_115200,
15         CC3_UART_MODE_8N1,
16         CC3_UART_BINMODE_TEXT);
17
18  cc3_camera_init ();
19
20  //cc3_camera_set_colorspace(CC3_COLORSPACE_YCRCB);
21  cc3_camera_set_resolution(CC3_CAMERA_RESOLUTION_LOW);
22  //cc3_pixbuf_frame_set_subsample(CC3_SUBSAMPLE_NEAREST, 2, 2);
23
24  // init pixbuf with width and height
25  cc3_pixbuf_load();
26
27  // Load in your tracking parameters
28  // Track Red
29  t_pkt1.lower_bound.channel[CC3_CHANNEL_RED] = 150;
30  t_pkt1.upper_bound.channel[CC3_CHANNEL_RED] = 255;
31  t_pkt1.lower_bound.channel[CC3_CHANNEL_GREEN] = 0;
32  t_pkt1.upper_bound.channel[CC3_CHANNEL_GREEN] = 50;
33  t_pkt1.lower_bound.channel[CC3_CHANNEL_BLUE] = 0;
34  t_pkt1.upper_bound.channel[CC3_CHANNEL_BLUE] = 50; 
35  t_pkt1.noise_filter = 2; 
36
37  // Track Green
38  t_pkt2.lower_bound.channel[CC3_CHANNEL_RED] = 0;
39  t_pkt2.upper_bound.channel[CC3_CHANNEL_RED] = 50;
40  t_pkt2.lower_bound.channel[CC3_CHANNEL_GREEN] = 150;
41  t_pkt2.upper_bound.channel[CC3_CHANNEL_GREEN] = 255;
42  t_pkt2.lower_bound.channel[CC3_CHANNEL_BLUE] = 0;
43  t_pkt2.upper_bound.channel[CC3_CHANNEL_BLUE] = 50; 
44  t_pkt2.noise_filter = 2; 
45
46  while(true) {
47    simple_track_color_2(&t_pkt1,&t_pkt2);
48    printf( "red centroid = %d,%d bounding box = %d,%d,%d,%d num pix= %d density = %d\n",
49            t_pkt1.centroid_x, t_pkt1.centroid_y,
50            t_pkt1.x0,t_pkt1.y0,t_pkt1.x1,t_pkt1.y1,
51            t_pkt1.num_pixels, t_pkt1.int_density );
52    printf( "green centroid = %d,%d bounding box = %d,%d,%d,%d num pix= %d density = %d\n",
53            t_pkt2.centroid_x, t_pkt2.centroid_y,
54            t_pkt2.x0,t_pkt2.y0,t_pkt2.x1,t_pkt2.y1,
55            t_pkt2.num_pixels, t_pkt2.int_density );
56
57   }
58
59}
60
61void simple_track_color_2(cc3_track_pkt_t * t_pkt,cc3_track_pkt_t * t_pkt2)
62{
63  cc3_image_t img;
64
65  img.channels = 3;
66  img.width = cc3_g_pixbuf_frame.width;
67  img.height = 1;               
68  // image will hold just 1 row for scanline processing
69  img.pix = cc3_malloc_rows (1);
70  if (img.pix == NULL) {
71    return;
72  }
73
74    cc3_pixbuf_load ();
75    if (cc3_track_color_scanline_start (t_pkt) != 0 && cc3_track_color_scanline_start (t_pkt2) != 0) {
76      while (cc3_pixbuf_read_rows (img.pix, 1)) {
77       // This does the HSV conversion 
78       // cc3_rgb2hsv_row(img.pix,img.width);
79           // Use the same scanline twice since it is already in memory and hence quick to operate upon
80           cc3_track_color_scanline (&img, t_pkt);
81           cc3_track_color_scanline (&img, t_pkt2);
82          }
83        }
84    cc3_track_color_scanline_finish (t_pkt);
85    cc3_track_color_scanline_finish (t_pkt2);
86
87  free (img.pix);
88  return;
89}
90