Simple Histogram

This is a very simple example of how to use the cc3-ilp to get histogram information. The output on a terminal program (115,200 8N1) will look something like this:


hist: 0 1 29 937 2089 1079 1696 2173 2982 1649 415 288 130 71 87 57 33 54 27 36 57 40 60 11354
hist: 0 0 11 491 1658 1361 1295 1868 2413 2378 1349 438 234 108 74 39 72 34 66 35 23 52 41 11304
hist: 0 1 294 1463 2592 1674 1176 1478 1920 1735 636 82 57 57 35 28 46 39 48 40 29 18 52 11844
...

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_histogram.h>
 6
 7void simple_get_histogram (cc3_histogram_pkt_t * h_pkt);
 8
 9int main (void)
10{
11  cc3_histogram_pkt_t my_hist;
12  uint32_t i;
13
14  cc3_uart_init (0,
15                 CC3_UART_RATE_115200,
16                 CC3_UART_MODE_8N1, 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  // When using the virtual-cam, note that this will skip the first image
26  cc3_pixbuf_load ();
27
28  my_hist.channel = CC3_CHANNEL_GREEN;
29  my_hist.bins = 24;
30  my_hist.hist = malloc (my_hist.bins * sizeof (uint32_t));
31
32  while (true) {
33    // Grab an image and take a histogram of it
34    simple_get_histogram (&my_hist);
35
36    // Print the histogram out on the screen
37    printf ("hist: ");
38    for (i = 0; i < my_hist.bins; i++)
39      printf ("%d ", my_hist.hist[i]);
40    printf ("\n");
41  }
42}
43
44void simple_get_histogram (cc3_histogram_pkt_t * h_pkt)
45{
46  cc3_image_t img;
47
48  img.channels = 3;
49  img.width = cc3_g_pixbuf_frame.width;
50  img.height = 1;               // image will hold just 1 row for scanline processing
51  img.pix = cc3_malloc_rows (1);
52  if (img.pix == NULL) {
53    return;
54  }
55
56  cc3_pixbuf_load ();
57  if (cc3_histogram_scanline_start (h_pkt) != 0) {
58    while (cc3_pixbuf_read_rows (img.pix, 1)) {
59      // This does the HSV conversion
60      // cc3_rgb2hsv_row(img.pix,img.width);
61      cc3_histogram_scanline (&img, h_pkt);
62    }
63  }
64  cc3_histogram_scanline_finish (h_pkt);
65
66  free (img.pix);
67  return;
68}
69