root/trunk/lib/cc3-ilp/cc3_color_track.c

Revision 529, 6.5 kB (checked in by anthony_rowe, 1 year ago)

cc3 color track patch applied from camscripter team

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright 2006  Anthony Rowe and Adam Goode
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18
19 #include "cc3.h"
20 #include "cc3_ilp.h"
21 #include "cc3_color_track.h"
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 /**
27  * cc3_track_color()
28  *
29  * This function takes parameters set in the  cc3_track_pkt_t structure
30  * and fills in the missing parameters with tracked color data.
31  * This operates on the image directly from the FIFO.
32  *
33  * Returns: 0 if the bounds fail, 1 upon sucess
34  *
35  * Check if num_pixels is greater than 1 to see if an object was detected.
36  *
37  */
38 uint8_t cc3_track_color (cc3_track_pkt_t * pkt)
39 {
40   uint16_t y, x;
41
42   uint8_t *row, *pixel;
43
44   x = pkt->scratch_x;
45   y = pkt->scratch_y;
46
47   if ((pkt->lower_bound.channel[0] > pkt->upper_bound.channel[0]) ||
48       (pkt->lower_bound.channel[1] > pkt->upper_bound.channel[1]) ||
49       (pkt->lower_bound.channel[2] > pkt->upper_bound.channel[2]))
50     return 0;
51
52   pkt->num_pixels = 0;
53   pkt->x0 = UINT16_MAX;
54   pkt->y0 = UINT16_MAX;
55   pkt->x1 = 0;
56   pkt->y1 = 0;
57   pkt->centroid_x = 0;
58   pkt->centroid_y = 0;
59
60   pixel = row = cc3_malloc_rows(1);
61  
62   for (y = 0; y < cc3_g_pixbuf_frame.height; y++) {
63     cc3_pixbuf_read_rows(row, 1);
64
65     for (x = 0; x < cc3_g_pixbuf_frame.width; x++) {
66       bool pixel_good = 0;
67
68       if (cc3_g_pixbuf_frame.coi == CC3_CHANNEL_ALL) {
69           if (pixel[0] >= pkt->lower_bound.channel[0]
70             && pixel[0] <= pkt->upper_bound.channel[0]
71             && pixel[1] >= pkt->lower_bound.channel[1]
72             && pixel[1] <= pkt->upper_bound.channel[1]
73             && pixel[2] >= pkt->lower_bound.channel[2]
74             && pixel[2] <= pkt->upper_bound.channel[2])
75           pixel_good = 1;
76      
77           pixel += 3;
78       }
79       else {
80         if (*pixel >=
81             pkt->lower_bound.channel[cc3_g_pixbuf_frame.coi]
82             && pixel [cc3_g_pixbuf_frame.coi] <=
83             pkt->upper_bound.channel[cc3_g_pixbuf_frame.coi])
84           pixel_good = 1;
85          
86           pixel++;
87       }
88
89       if(pkt->track_invert==1) pixel_good=!pixel_good;
90       if (pixel_good) {
91         pkt->num_pixels++;
92         if (pkt->x0 > x)
93           pkt->x0 = x;
94         if (pkt->y0 > y)
95           pkt->y0 = y;
96         if (pkt->x1 < x)
97           pkt->x1 = x;
98         if (pkt->y1 < y)
99           pkt->y1 = y;
100         pkt->centroid_x += x;
101         pkt->centroid_y += y;
102       }
103     }
104     pixel=row;
105   }
106
107   if (pkt->num_pixels > 0) {
108     // FIXME:  Density hack to keep it an integer
109     pkt->int_density =
110       (pkt->num_pixels * 1000) / ((pkt->x1 - pkt->x0) * (pkt->y1 - pkt->y0));
111     pkt->centroid_x = pkt->centroid_x / pkt->num_pixels;
112     pkt->centroid_y = pkt->centroid_y / pkt->num_pixels;
113
114   }
115   else {
116     pkt->int_density = 0;
117     pkt->x0 = 0;
118     pkt->y0 = 0;
119     pkt->x1 = 0;
120     pkt->y1 = 0;
121     pkt->centroid_x = 0;
122     pkt->centroid_y = 0;
123
124   }
125
126   free(row);
127
128   return 1;
129 }
130
131 uint8_t cc3_track_color_img (cc3_image_t * img, cc3_track_pkt_t * pkt)
132 {
133
134
135 }
136
137
138 uint8_t cc3_track_color_scanline_start (cc3_track_pkt_t * pkt)
139 {
140
141   if ((pkt->lower_bound.channel[0] > pkt->upper_bound.channel[0]) ||
142       (pkt->lower_bound.channel[1] > pkt->upper_bound.channel[1]) ||
143       (pkt->lower_bound.channel[2] > pkt->upper_bound.channel[2]))
144     return 0;
145   pkt->num_pixels = 0;
146   pkt->x0 = UINT16_MAX;
147   pkt->y0 = UINT16_MAX;
148   pkt->x1 = 0;
149   pkt->y1 = 0;
150   pkt->centroid_x = 0;
151   pkt->centroid_y = 0;
152   pkt->scratch_x = 0;
153   pkt->scratch_y = 0;
154   return 1;
155 }
156
157 uint8_t cc3_track_color_scanline (cc3_image_t * img, cc3_track_pkt_t * pkt)
158 {
159   uint32_t x, y;
160   uint8_t seq_count;
161
162   for (x = 0; x < MAX_BINARY_WIDTH; x++)
163     pkt->binary_scanline[x] = 0;
164
165   for (y = pkt->scratch_y; y < (pkt->scratch_y + img->height); y++)
166   {
167     seq_count=0;
168     for (x = 0; x < img->width; x++) {
169       bool pixel_good = 0;
170       cc3_pixel_t cp;
171       //cc3_pixbuf_read();
172       cc3_get_pixel (img, x, 0, &cp);
173       if (cc3_g_pixbuf_frame.coi == CC3_CHANNEL_ALL) {
174         if (cp.channel[0] >= pkt->lower_bound.channel[0] &&
175             cp.channel[0] <= pkt->upper_bound.channel[0] &&
176             cp.channel[1] >= pkt->lower_bound.channel[1] &&
177             cp.channel[1] <= pkt->upper_bound.channel[1] &&
178             cp.channel[2] >= pkt->lower_bound.channel[2] &&
179             cp.channel[2] <= pkt->upper_bound.channel[2])
180           pixel_good = 1;
181       }
182       else {
183         if (cp.channel[cc3_g_pixbuf_frame.coi] >=
184             pkt->lower_bound.channel[cc3_g_pixbuf_frame.coi]
185             && cp.channel[cc3_g_pixbuf_frame.coi] <=
186             pkt->upper_bound.channel[cc3_g_pixbuf_frame.coi])
187           pixel_good = 1;
188       }
189
190       if(pkt->track_invert==1) pixel_good=!pixel_good;
191       /*      pkt->binary_scanline[0]=0x01020304;
192          pkt->binary_scanline[1]=0x05060708;
193          pkt->binary_scanline[2]=0x090A0B0C;
194          pkt->binary_scanline[3]=0x0D0E0F10;
195          pkt->binary_scanline[4]=0x11121314;
196        */
197        if(pixel_good)
198        {
199              if(  seq_count<255) seq_count++;
200        } else seq_count=0;
201      
202        if (pixel_good && seq_count>pkt->noise_filter) {
203         uint8_t block, offset;
204         block = x / 8;
205         offset = x % 8;
206         offset = 7 - offset;
207         pkt->binary_scanline[block] |= (1 << offset);
208         pkt->num_pixels++;
209         if (pkt->x0 > x)
210           pkt->x0 = x;
211         if (pkt->y0 > y)
212           pkt->y0 = y;
213         if (pkt->x1 < x)
214           pkt->x1 = x;
215         if (pkt->y1 < y)
216           pkt->y1 = y;
217         pkt->centroid_x += x;
218         pkt->centroid_y += y;
219       }
220     }
221   }
222   pkt->scratch_y = y;
223
224   return 1;
225 }
226
227
228
229 uint8_t cc3_track_color_scanline_finish (cc3_track_pkt_t * pkt)
230 {
231   if (pkt->num_pixels > 0) {
232     // FIXME:  Density hack to keep it an integer
233     pkt->int_density =
234       (pkt->num_pixels * 1000) / ((pkt->x1 - pkt->x0) * (pkt->y1 - pkt->y0));
235     pkt->centroid_x = pkt->centroid_x / pkt->num_pixels;
236     pkt->centroid_y = pkt->centroid_y / pkt->num_pixels;
237
238   }
239   else {
240     pkt->int_density = 0;
241     pkt->x0 = 0;
242     pkt->y0 = 0;
243     pkt->x1 = 0;
244     pkt->y1 = 0;
245     pkt->centroid_x = 0;
246     pkt->centroid_y = 0;
247
248   }
249
250
251 }
Note: See TracBrowser for help on using the browser.