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
00027
00028
00029 #ifndef INCLUDED_Vision_h
00030 #define INCLUDED_Vision_h
00031
00032 #include <math.h>
00033 #include <ext/hash_map>
00034
00035 #include "cmvision.h"
00036 #include "VisionInterface.h"
00037
00038 using namespace VisionInterface;
00039
00040 #include "Visiondefines.h"
00041 #include "Wireless/Wireless.h"
00042 #include "Events/VisionEvent.h"
00043
00044 typedef uchar pixel;
00045 typedef uchar cmap_t;
00046 typedef CMVision::image<uchar> image;
00047 typedef CMVision::color_class_state color_class_state;
00048 typedef CMVision::run<cmap_t> run;
00049 typedef CMVision::region region;
00050
00051 const int bits_y = 4;
00052 const int bits_u = 6;
00053 const int bits_v = 6;
00054
00055
00056
00057
00058
00059
00060
00061 static const double FocalDist =(176.0/2)/tan(HorzFOV/2);
00062 static const double FocalDistV=(144.0/2)/tan(VertFOV/2);
00063
00064 static const double YPixelSize=(FocalDistV/FocalDist);
00065
00066 static const double RobotArea = 13711;
00067
00068 #define MIN_EXP_REGION_SIZE 16
00069 #define MIN_EXP_RUN_LENGTH 8
00070
00071
00072 struct VisionObjectInfo {
00073 region *reg,*reg2;
00074 };
00075
00076 struct Marker {
00077 region* regs[3];
00078 int cen_x;
00079 int cen_y;
00080 int marker;
00081 };
00082
00083 struct VisionEventSpec {
00084 int listeners;
00085 int filter;
00086 int count;
00087 double confidence;
00088 float cx, cy;
00089 bool present;
00090 };
00091
00092 class VisionSerializer;
00093
00094 using namespace __gnu_cxx;
00095
00096 class Vision{
00097 friend class VisionSerializer;
00098 public:
00099 Vision();
00100 ~Vision() { }
00101
00102 unsigned long frameTimestamp;
00103 int frame_count;
00104
00105 cmap_t *cmap,*tmap;
00106 run *rmap,*rmap2;
00107 region *reg;
00108 int yindex[144];
00109
00110 VisionObjectInfo vobj_info[NUM_VISION_OBJECTS];
00111 VisionEventSpec vevent_spec[NUM_VEVENTS];
00112
00113 color_class_state color[MAX_COLORS];
00114
00115 int width,height;
00116 int max_width,max_height;
00117 int max_runs,max_regions;
00118 int num_colors,num_runs,num_regions;
00119
00120 double body_angle, body_height;
00121 double head_angles[3];
00122
00123 int getWidth() {return width;}
00124 int getHeight() {return height;}
00125 void initialize();
00126 void setCameraParams();
00127 void setResolution();
00128 void setColors();
00129 int getColor(const char* color);
00130 void initializeEventSpecs();
00131
00132 void enableEvents(int vevent);
00133 void enableEvents(int vevent, int noise);
00134 void disableEvents(int vevent);
00135
00136 void setNoiseThreshold(int vevent, int noise);
00137
00138 bool close();
00139
00140 bool processFrame(const uchar *data_y, const uchar *data_u, const uchar *data_v, int width, int height, int skip);
00141 bool saveThresholdImage(char *filename);
00142
00143 Marker markers[3];
00144 int vis_markers;
00145 ObjectInfo *obj_info;
00146 OFbkImageLayer imageLayer;
00147
00148 private:
00149
00150 VisionSerializer* vser;
00151
00152
00153 CMVision::image_yuv<const uchar> img;
00154 hash_map<const char*, int, hash<const char*>, hashcmp_eqstr> color_names;
00155
00156 bool thresholdImage(CMVision::image_idx<rgb> &img);
00157 bool thresholdImage(CMVision::image_yuv<const uchar> &img);
00158
00159 template<class image>
00160 bool runLowLevelVision(image &img);
00161
00162 int getColorUnsafe(int x,int y)
00163 {return(cmap[y*width+x]);}
00164 int getNearColor(int x,int y)
00165 {return(cmap[bound(y,0,height-1)*width+bound(x,0,width-1)]);}
00166
00167 int addToHistHorizStrip(int y, int x1, int x2, int *color_cnt);
00168 int addToHistVertStrip (int x, int y1, int y2, int *color_cnt);
00169 void createEvent(unsigned int tid, unsigned int sid, float cenX, float cenY);
00170
00171
00172 vector3d getPixelDirection(double x, double y);
00173
00174 void findSpan(double &left, double &right, double x1, double x2, double y1, double y2);
00175
00176 int calcEdgeMask(double x1, double x2, double y1, double y2);
00177 int calcEdgeMask(int x1,int x2,int y1,int y2);
00178 int calcEdgeMask(region *tmpreg)
00179 {return(calcEdgeMask(tmpreg->x1,tmpreg->x2,tmpreg->y1,tmpreg->y2));}
00180
00181 int isIn(region *r1, region *r2);
00182 int isAdjacent(region *r1, region *r2);
00183
00184 bool findHand(VObject *hand,VisionObjectInfo *hand_info);
00185 bool findBall(int ball_color, VObject *ball,VisionObjectInfo *ball_info);
00186 bool findThing(VObject *thing,VisionObjectInfo *thing_info);
00187 bool findMarkers();
00188 bool findGesture(VisionObjectInfo *hand_info);
00189 int identifyMarker(int color1, int color2, int color3);
00190 bool generateEvent(int vevent, double conf, int cenX, int cenY);
00191
00192 bool runHighLevelVision(ObjectInfo *obj_info);
00193
00194 Vision(const Vision&);
00195 Vision operator=(const Vision&);
00196 };
00197
00198 extern Vision* vision;
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 #endif