cmvision.h
1/*=========================================================================
2 CMVision.h
3 -------------------------------------------------------------------------
4 API definition for the CMVision real time Color Machine Vision library
5 -------------------------------------------------------------------------
6 Copyright 1999, 2000 #### ### ### ## ## ## #### ## ### ## ##
7 James R. Bruce ## ####### ## ## ## ## ## ## ## ######
8 School of Computer Science ## ## # ## ## ## ## ### ## ## ## ## ###
9 Carnegie Mellon University #### ## ## ### ## #### ## ### ## ##
10 -------------------------------------------------------------------------
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 =========================================================================*/
25
26#ifndef __CMVISION_H__
27#define __CMVISION_H__
28
29// uncomment if your compiler supports the "restrict" keyword
30// #define restrict __restrict__
31#define restrict
32
33#include <stdlib.h>
34#include <string.h>
35#include <stdio.h>
36
37/*
38Ultra-fast intro to processing steps:
39 - Color segmentation
40 - load / save
41 - set new values
42 - process frame
43 - Connected Regions
44 - RLE
45 - merge
46 - extract blobs
47 - separate blobs
48 - sort blobs
49 - merge blobs
50 - Blob merging (not currently in release)
51 - merge by area occupied
52
53Options File Format: (RGB merge name)
54[Colors]
55(00,00,00) 0.95 'Orange'
56(00,00,00) 0.00 'Pink'
57(00,00,00) 0.00 'Red'
58(00,00,00) 0.00 'DarkBlue'
59(00,00,00) 0.00 'Blue'
60
61[Thresholds]
62(<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
63(<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
64(<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
65*/
66
67#define CMV_COLOR_LEVELS 256
68#define CMV_MAX_COLORS 32
69
70// sets tweaked optimal values for image size
71#define CMV_DEFAULT_WIDTH 320
72#define CMV_DEFAULT_HEIGHT 240
73
74// values may need tweaked, although these seem to work usually
75#define CMV_MAX_RUNS (CMV_DEFAULT_WIDTH * CMV_DEFAULT_HEIGHT) / 4
76#define CMV_MAX_REGIONS CMV_MAX_RUNS / 4
77#define CMV_MIN_AREA 20
78
79#define CMV_NONE ((unsigned)(-1))
80
81#ifndef NULL
82#define NULL (0)
83#endif
84
85struct yuv{
86 unsigned char y,u,v;
87};
88
89struct yuv422{
90 unsigned char y1,u,y2,v;
91};
92
93struct uyvy{
94 unsigned char u,y1,v,y2;
95};
96
97#ifdef USE_METEOR
98 typedef struct uyvy image_pixel;
99#else
100 typedef struct yuv422 image_pixel;
101#endif
102
103#ifndef RGB_STRUCT
104#define RGB_STRUCT
105struct rgb{
106 unsigned char red,green,blue;
107};
108#endif
109
110// Options for level of processing
111// use enable()/disable() to change
112#define CMV_THRESHOLD 0x01
113#define CMV_COLOR_AVERAGES 0x02
114#define CMV_DUAL_THRESHOLD 0x04
115#define CMV_DENSITY_MERGE 0x08
116
117#define CMV_VALID_OPTIONS 0x0F
118
119
121public:
122 struct region{
123 int color; // id of the color
124 int area; // occupied area in pixels
125 int x1,y1,x2,y2; // bounding box (x1,y1) - (x2,y2)
126 float cen_x,cen_y; // centroid
127 yuv average; // average color (if CMV_COLOR_AVERAGES enabled)
128
129 int sum_x,sum_y,sum_z; // temporaries for centroid and avg color
130 // int area_check; // DEBUG ONLY
131
132 region *next; // next region in list
133
134 // int number; // DEBUG ONLY
135 };
136
137 struct rle{
138 unsigned color; // which color(s) this run represents
139 int length; // the length of the run (in pixels)
140 int parent; // run's parent in the connected components tree
141 };
142
144 rgb color; // example color (such as used in test output)
145 char *name; // color's meaninful name (e.g. ball, goal)
146 double merge; // merge density threshold
147 int expected_num; // expected number of regions (used for merge)
148 int y_low,y_high; // Y,U,V component thresholds
149 int u_low,u_high;
150 int v_low,v_high;
151 };
152
153 struct point{
154 double x,y;
155 };
156
157 struct line{
158 point a,b;
159 };
160
161 struct rectangle{
162 int x,y,w,h;
163 };
164
165protected:
166 unsigned y_class[CMV_COLOR_LEVELS];
167 unsigned u_class[CMV_COLOR_LEVELS];
168 unsigned v_class[CMV_COLOR_LEVELS];
169
170 region region_table[CMV_MAX_REGIONS];
171 region *region_list[CMV_MAX_COLORS];
172 int region_count[CMV_MAX_COLORS];
173
174 rle rmap[CMV_MAX_RUNS];
175
176 color_info colors[CMV_MAX_COLORS];
177 int width,height;
178 unsigned *map;
179
180 unsigned options;
181
182 int cmv_min_area;
183 int cmv_max_area;
184
185protected:
186// Private functions
187 void classifyFrame(image_pixel * restrict img,unsigned * restrict map);
188 int encodeRuns(rle * restrict out,unsigned * restrict map);
189 void connectComponents(rle * restrict map,int num);
190 int extractRegions(region * restrict reg,rle * restrict rmap,int num);
191 void calcAverageColors(region * restrict reg,int num_reg,
192 image_pixel * restrict img,
193 rle * restrict rmap,int num_runs);
194 int separateRegions(region * restrict reg,int num);
195 region *sortRegionListByArea(region * restrict list,int passes);
196 void sortRegions(int max_area);
197
198 // density based merging support
199 int mergeRegions(region *p,int num,double density_thresh);
200 int mergeRegions();
201
202 void clear();
203
204public:
205 CMVision() {clear();}
206 ~CMVision() {close();}
207
208 bool initialize(int nwidth,int nheight);
209 bool loadOptions(char *filename);
210 bool saveOptions(char *filename);
211 bool enable(unsigned opt);
212 bool disable(unsigned opt);
213 void close();
214
215 bool testClassify(rgb * restrict out,image_pixel * restrict image);
216 bool getThreshold(int color,
217 int &y_low,int &y_high,
218 int &u_low,int &u_high,
219 int &v_low,int &v_high);
220 bool setThreshold(int color,
221 int y_low,int y_high,
222 int u_low,int u_high,
223 int v_low,int v_high);
224
225 unsigned *getMap()
226 {return(map);}
227
228 char *getColorName(int color)
229 {return(colors[color].name);}
230 rgb getColorVisual(int color)
231 {return(colors[color].color);}
232
233 color_info *getColorInfo(int color)
234 {return(&colors[color]);}
235 void getColorInfo(int color,color_info &info)
236 {info = colors[color];}
237 void setColorInfo(int color,color_info &info)
238 {colors[color] = info;}
239
240 bool processFrame(image_pixel *image);
241 bool processFrame(unsigned *map);
242 int numRegions(int color_id);
243 region *getRegions(int color_id);
244 void set_cmv_min_area(int area) { cmv_min_area = area; }
245 void set_cmv_max_area(int area) { cmv_max_area = area; }
246};
247
248#endif
Definition cmvision.h:120
Definition cmvision.h:143
Definition cmvision.h:157
Definition cmvision.h:153
Definition cmvision.h:161
Definition cmvision.h:122
Definition cmvision.h:137
Definition camera.h:150
Definition cmvision.h:105
Definition cmvision.h:93
Definition cmvision.h:89
Definition cmvision.h:85