$search
00001 /*========================================================================= 00002 CMVision.h 00003 ------------------------------------------------------------------------- 00004 API definition for the CMVision real time Color Machine Vision library 00005 ------------------------------------------------------------------------- 00006 Copyright 1999, 2000 #### ### ### ## ## ## #### ## ### ## ## 00007 James R. Bruce ## ####### ## ## ## ## ## ## ## ###### 00008 School of Computer Science ## ## # ## ## ## ## ### ## ## ## ## ### 00009 Carnegie Mellon University #### ## ## ### ## #### ## ### ## ## 00010 ------------------------------------------------------------------------- 00011 This library is free software; you can redistribute it and/or 00012 modify it under the terms of the GNU Lesser General Public 00013 License as published by the Free Software Foundation; either 00014 version 2.1 of the License, or (at your option) any later version. 00015 00016 This library is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Lesser General Public 00022 License along with this library; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 =========================================================================*/ 00025 00026 #ifndef __CMVISION_H__ 00027 #define __CMVISION_H__ 00028 00029 // uncomment if your compiler supports the "restrict" keyword 00030 // #define restrict __restrict__ 00031 #define restrict 00032 00033 #include <stdlib.h> 00034 #include <string.h> 00035 #include <stdio.h> 00036 00037 /* 00038 Ultra-fast intro to processing steps: 00039 - Color segmentation 00040 - load / save 00041 - set new values 00042 - process frame 00043 - Connected Regions 00044 - RLE 00045 - merge 00046 - extract blobs 00047 - separate blobs 00048 - sort blobs 00049 - merge blobs 00050 - Blob merging (not currently in release) 00051 - merge by area occupied 00052 00053 Options File Format: (RGB merge name) 00054 [Colors] 00055 (00,00,00) 0.95 'Orange' 00056 (00,00,00) 0.00 'Pink' 00057 (00,00,00) 0.00 'Red' 00058 (00,00,00) 0.00 'DarkBlue' 00059 (00,00,00) 0.00 'Blue' 00060 00061 [Thresholds] 00062 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>) 00063 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>) 00064 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>) 00065 */ 00066 00067 #define CMV_COLOR_LEVELS 256 00068 #define CMV_MAX_COLORS 32 00069 00070 // sets tweaked optimal values for image size 00071 #define CMV_DEFAULT_WIDTH 320 00072 #define CMV_DEFAULT_HEIGHT 240 00073 00074 // values may need tweaked, although these seem to work usually 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 // Options for level of processing 00107 // use enable()/disable() to change 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; // id of the color 00120 int area; // occupied area in pixels 00121 int x1,y1,x2,y2; // bounding box (x1,y1) - (x2,y2) 00122 float cen_x,cen_y; // centroid 00123 yuv average; // average color (if CMV_COLOR_AVERAGES enabled) 00124 00125 int sum_x,sum_y,sum_z; // temporaries for centroid and avg color 00126 // int area_check; // DEBUG ONLY 00127 00128 region *next; // next region in list 00129 00130 // int number; // DEBUG ONLY 00131 }; 00132 00133 struct rle{ 00134 unsigned color; // which color(s) this run represents 00135 int length; // the length of the run (in pixels) 00136 int parent; // run's parent in the connected components tree 00137 }; 00138 00139 struct color_info{ 00140 rgb color; // example color (such as used in test output) 00141 char *name; // color's meaninful name (e.g. ball, goal) 00142 double merge; // merge density threshold 00143 int expected_num; // expected number of regions (used for merge) 00144 int y_low,y_high; // Y,U,V component thresholds 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 // Private functions 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 // density based merging support 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