00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __CMVISION_H__
00027 #define __CMVISION_H__
00028
00029
00030
00031 #define restrict
00032
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define CMV_COLOR_LEVELS 256
00068 #define CMV_MAX_COLORS 32
00069
00070
00071 #define CMV_DEFAULT_WIDTH 320
00072 #define CMV_DEFAULT_HEIGHT 240
00073
00074
00075 #define CMV_MAX_RUNS (CMV_DEFAULT_WIDTH * CMV_DEFAULT_HEIGHT) / 4
00076 #define CMV_MAX_REGIONS CMV_MAX_RUNS / 4
00077 #define CMV_MIN_AREA 20
00078
00079 #define CMV_NONE ((unsigned)(-1))
00080
00081 #ifndef NULL
00082 #define NULL (0)
00083 #endif
00084
00085 struct yuv{
00086 unsigned char y,u,v;
00087 };
00088
00089 struct yuv422{
00090 unsigned char y1,u,y2,v;
00091 };
00092
00093 struct uyvy{
00094 unsigned char u,y1,v,y2;
00095 };
00096
00097 typedef struct uyvy image_pixel;
00098
00099 #ifndef RGB_STRUCT
00100 #define RGB_STRUCT
00101 struct rgb{
00102 unsigned char red,green,blue;
00103 };
00104 #endif
00105
00106
00107
00108 #define CMV_THRESHOLD 0x01
00109 #define CMV_COLOR_AVERAGES 0x02
00110 #define CMV_DUAL_THRESHOLD 0x04
00111 #define CMV_DENSITY_MERGE 0x08
00112
00113 #define CMV_VALID_OPTIONS 0x0F
00114
00115
00116 class CMVision{
00117 public:
00118 struct region{
00119 int color;
00120 int area;
00121 int x1,y1,x2,y2;
00122 float cen_x,cen_y;
00123 yuv average;
00124
00125 int sum_x,sum_y,sum_z;
00126
00127
00128 region *next;
00129
00130
00131 };
00132
00133 struct rle{
00134 unsigned color;
00135 int length;
00136 int parent;
00137 };
00138
00139 struct color_info{
00140 rgb color;
00141 char *name;
00142 double merge;
00143 int expected_num;
00144 int y_low,y_high;
00145 int u_low,u_high;
00146 int v_low,v_high;
00147 };
00148
00149 struct point{
00150 double x,y;
00151 };
00152
00153 struct line{
00154 point a,b;
00155 };
00156
00157 struct rectangle{
00158 int x,y,w,h;
00159 };
00160
00161 protected:
00162 unsigned y_class[CMV_COLOR_LEVELS];
00163 unsigned u_class[CMV_COLOR_LEVELS];
00164 unsigned v_class[CMV_COLOR_LEVELS];
00165
00166 region region_table[CMV_MAX_REGIONS];
00167 region *region_list[CMV_MAX_COLORS];
00168 int region_count[CMV_MAX_COLORS];
00169
00170 rle rmap[CMV_MAX_RUNS];
00171
00172 color_info colors[CMV_MAX_COLORS];
00173 int width,height;
00174 unsigned *map;
00175
00176 unsigned options;
00177
00178 protected:
00179
00180 void classifyFrame(image_pixel * restrict img,unsigned * restrict map);
00181 int encodeRuns(rle * restrict out,unsigned * restrict map);
00182 void connectComponents(rle * restrict map,int num);
00183 int extractRegions(region * restrict reg,rle * restrict rmap,int num);
00184 void calcAverageColors(region * restrict reg,int num_reg,
00185 image_pixel * restrict img,
00186 rle * restrict rmap,int num_runs);
00187 int separateRegions(region * restrict reg,int num);
00188 region *sortRegionListByArea(region * restrict list,int passes);
00189 void sortRegions(int max_area);
00190
00191
00192 int mergeRegions(region *p,int num,double density_thresh);
00193 int mergeRegions();
00194
00195 void clear();
00196
00197 public:
00198 CMVision() {clear();}
00199 ~CMVision() {close();}
00200
00201 bool initialize(int nwidth,int nheight);
00202 bool loadOptions(const char *filename);
00203 bool saveOptions(char *filename);
00204 bool enable(unsigned opt);
00205 bool disable(unsigned opt);
00206 void close();
00207
00208 bool testClassify(rgb * restrict out,image_pixel * restrict image);
00209 bool getThreshold(int color,
00210 int &y_low,int &y_high,
00211 int &u_low,int &u_high,
00212 int &v_low,int &v_high);
00213 bool setThreshold(int color,
00214 int y_low,int y_high,
00215 int u_low,int u_high,
00216 int v_low,int v_high);
00217
00218 unsigned *getMap()
00219 {return(map);}
00220
00221 char *getColorName(int color)
00222 {return(colors[color].name);}
00223 rgb getColorVisual(int color)
00224 {return(colors[color].color);}
00225
00226 color_info *getColorInfo(int color)
00227 {return(&colors[color]);}
00228 void getColorInfo(int color,color_info &info)
00229 {info = colors[color];}
00230 void setColorInfo(int color,color_info &info)
00231 {colors[color] = info;}
00232
00233 bool processFrame(image_pixel *image);
00234 bool processFrame(unsigned *map);
00235 int numRegions(int color_id);
00236 region *getRegions(int color_id);
00237 };
00238
00239 #endif