Homepage Demos Overview Downloads Tutorials Reference
Credits

Vision.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 /*=========================================================================
00003     CMPack'02 Source Code Release for OPEN-R SDK v1.0
00004     Copyright (C) 2002 Multirobot Lab [Project Head: Manuela Veloso]
00005     School of Computer Science, Carnegie Mellon University
00006   -------------------------------------------------------------------------
00007     This software is distributed under the GNU General Public License,
00008     version 2.  If you do not have a copy of this licence, visit
00009     www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
00010     Suite 330 Boston, MA 02111-1307 USA.  This program is distributed
00011     in the hope that it will be useful, but WITHOUT ANY WARRANTY,
00012     including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00013   -------------------------------------------------------------------------
00014     Additionally licensed to Sony Corporation under the following terms:
00015 
00016     This software is provided by the copyright holders AS IS and any
00017     express or implied warranties, including, but not limited to, the
00018     implied warranties of merchantability and fitness for a particular
00019     purpose are disclaimed.  In no event shall authors be liable for
00020     any direct, indirect, incidental, special, exemplary, or consequential
00021     damages (including, but not limited to, procurement of substitute
00022     goods or services; loss of use, data, or profits; or business
00023     interruption) however caused and on any theory of liability, whether
00024     in contract, strict liability, or tort (including negligence or
00025     otherwise) arising in any way out of the use of this software, even if
00026     advised of the possibility of such damage.
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 /* focal dist of camera:
00056    fh = (w/2) / tan(h/2) = (176/2) / (100 / (2*109)) = 88 / (100/109)
00057       = 95.92
00058    fv = (h/2) / tan(v/2) = (144/2) / (100 / (2*150)) = 60 / (100/150)
00059       = 90
00060  */
00061 static const double FocalDist =(176.0/2)/tan(HorzFOV/2); // 158.76
00062 static const double FocalDistV=(144.0/2)/tan(VertFOV/2); // 161.71
00063 // size of vertical pixel in terms of size of horizontal pixel
00064 static const double YPixelSize=(FocalDistV/FocalDist);
00065 
00066 static const double RobotArea = 13711; // mm^2 (empirically guessed, side view)
00067 
00068 #define MIN_EXP_REGION_SIZE 16
00069 #define MIN_EXP_RUN_LENGTH   8
00070 
00071 // additional information needed by vision about objects not exported to other objects
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; // number of 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   // other stuff
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   // adds the colors found to the color_cnt histogram
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   // finds the direction of the ray through this pixel in robot coordinates
00172   vector3d getPixelDirection(double x, double y);
00173   // finds the leftest and rightmost ego angles on ground for a bounding box
00174   void findSpan(double &left, double &right, double x1, double x2, double y1, double y2);
00175   // calculates the edge mask for a bounding box
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&); //!< don't copy
00195   Vision operator=(const Vision&); //!< don't assign
00196 };
00197 
00198 extern Vision* vision;
00199 
00200 /*! @file
00201  * @brief Does majority of vision processing
00202  * @author CMU RoboSoccer 2001-2002 (Creator)
00203  * @author alokl (ported)
00204  * 
00205  * @verbinclude CMPack_license.txt
00206  *
00207  * $Author: ejt $
00208  * $Name: tekkotsu-1_5 $
00209  * $Revision: 1.10 $
00210  * $State: Rel $
00211  * $Date: 2003/10/10 05:38:25 $
00212  */
00213 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4