BallTrackingHead_Image.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include "BallTrackingHead.h"
00015 
00016 void
00017 BallTrackingHead::NotifyImage(const ONotifyEvent& event)
00018 {
00019     static int found = 0;
00020     static int lost = 0;
00021 
00022     int    xc, yc;              // centroid
00023     double d_pan, d_tilt;       // delta angle
00024 
00025     if (ballTrackingHeadState == BTHS_IDLE) return; // do nothing
00026 
00027     OFbkImageVectorData* imageVec = (OFbkImageVectorData*)event.Data(0);
00028 
00029     OFbkImageInfo* info = imageVec->GetInfo(ofbkimageLAYER_C);
00030     byte*          data = imageVec->GetData(ofbkimageLAYER_C);
00031     OFbkImage cdtImage(info, data, ofbkimageBAND_CDT);
00032 
00033     if (ballTrackingHeadState == BTHS_SEARCHING_BALL) {
00034 
00035         if (cdtImage.ColorFrequency(BALL_CDT_CHAN) >= BALL_THRESHOLD) {
00036             found++;
00037         } else {
00038             found = 0;
00039             SearchBall();
00040         }
00041         
00042         if (found == FOUND_THRESHOLD) {
00043             OSYSPRINT(("### BALL FOUND ### \n"));
00044             PlaySound(BTHCMD_PLAY_FOUND_SOUND);
00045             found = 0;
00046             CentroidAndDeltaAngle(cdtImage, &xc, &yc, &d_pan, &d_tilt);
00047             TrackBall(info->frameNumber, d_pan, d_tilt);
00048         }
00049 
00050     } else if (ballTrackingHeadState == BTHS_TRACKING_BALL) {
00051 
00052         if (cdtImage.ColorFrequency(BALL_CDT_CHAN) >= BALL_THRESHOLD) {
00053             lost = 0; 
00054             CentroidAndDeltaAngle(cdtImage, &xc, &yc, &d_pan, &d_tilt);
00055             TrackBall(info->frameNumber, d_pan, d_tilt);
00056         } else {
00057             lost++;
00058         }
00059 
00060         if (lost == LOST_THRESHOLD) {
00061             OSYSPRINT(("### BALL LOST ### \n"));
00062             PlaySound(BTHCMD_PLAY_LOST_SOUND);
00063             SearchBall();
00064             lost = 0;
00065         }
00066     }
00067     
00068     observer[event.ObsIndex()]->AssertReady();
00069 }
00070 
00071 void
00072 BallTrackingHead::SetCdtVectorDataOfPinkBall()
00073 {
00074     OSYSDEBUG(("BallTrackingHead::SetCdtVectorDataOfPinkBall()\n"));
00075 
00076     OStatus result;
00077     MemoryRegionID  cdtVecID;
00078     OCdtVectorData* cdtVec;
00079     OCdtInfo*       cdt;
00080 
00081     result = OPENR::NewCdtVectorData(&cdtVecID, &cdtVec);
00082     if (result != oSUCCESS) {
00083         OSYSLOG1((osyslogERROR, "%s : %s %d",
00084                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00085                   "OPENR::NewCdtVectorData() FAILED", result));
00086         return;
00087     }
00088 
00089     cdtVec->SetNumData(1);
00090 
00091     cdt = cdtVec->GetInfo(0);
00092     cdt->Init(fbkID, BALL_CDT_CHAN);
00093 
00094     //
00095     // cdt->Set(Y_segment, Cr_max,  Cr_min, Cb_max, Cb_min)
00096     //
00097     cdt->Set( 0, 230, 150, 190, 120);
00098     cdt->Set( 1, 230, 150, 190, 120);
00099     cdt->Set( 2, 230, 150, 190, 120);
00100     cdt->Set( 3, 230, 150, 190, 120);
00101     cdt->Set( 4, 230, 150, 190, 120);
00102     cdt->Set( 5, 230, 150, 190, 120);
00103     cdt->Set( 6, 230, 150, 190, 120);
00104     cdt->Set( 7, 230, 150, 190, 120);
00105     cdt->Set( 8, 230, 150, 190, 120);
00106     cdt->Set( 9, 230, 150, 190, 120);
00107     cdt->Set(10, 230, 150, 190, 120);
00108     cdt->Set(11, 230, 150, 190, 120);
00109     cdt->Set(12, 230, 150, 190, 120);
00110     cdt->Set(13, 230, 150, 190, 120);
00111     cdt->Set(14, 230, 150, 190, 120);
00112     cdt->Set(15, 230, 150, 190, 120);
00113     cdt->Set(16, 230, 150, 190, 120);
00114     cdt->Set(17, 230, 150, 190, 120);
00115     cdt->Set(18, 230, 150, 190, 120);
00116     cdt->Set(19, 230, 150, 190, 120);
00117     cdt->Set(20, 230, 160, 190, 120);
00118     cdt->Set(21, 230, 160, 190, 120);
00119     cdt->Set(22, 230, 160, 190, 120);
00120     cdt->Set(23, 230, 160, 190, 120);
00121     cdt->Set(24, 230, 160, 190, 120);
00122     cdt->Set(25, 230, 160, 190, 120);
00123     cdt->Set(26, 230, 160, 190, 120);
00124     cdt->Set(27, 230, 160, 190, 120);
00125     cdt->Set(28, 230, 160, 190, 120);
00126     cdt->Set(29, 230, 160, 190, 120);
00127     cdt->Set(30, 230, 160, 190, 120);
00128     cdt->Set(31, 230, 160, 190, 120);
00129 
00130     result = OPENR::SetCdtVectorData(cdtVecID);
00131     if (result != oSUCCESS) {
00132         OSYSLOG1((osyslogERROR, "%s : %s %d",
00133                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00134                   "OPENR::SetCdtVectorData() FAILED", result));
00135     }
00136 
00137     result = OPENR::DeleteCdtVectorData(cdtVecID);
00138     if (result != oSUCCESS) {
00139         OSYSLOG1((osyslogERROR, "%s : %s %d",
00140                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00141                   "OPENR::DeleteCdtVectorData() FAILED", result));
00142     }
00143 }
00144 
00145 bool
00146 BallTrackingHead::CentroidAndDeltaAngle(const OFbkImage& cdtImage,
00147                                         int* xcentroid, int* ycentroid,
00148                                         double* delta_pan, double* delta_tilt)
00149 {
00150     int w     = cdtImage.Width();
00151     int h     = cdtImage.Height();
00152     byte* ptr = cdtImage.Pointer();
00153 
00154     int xsum   = 0;
00155     int ysum    = 0;
00156     int npixels = 0;
00157     
00158     // Last line is not used because of TagInfo.
00159     for (int y = 0; y < h-1; y++) {
00160         for (int x = 0; x < w; x++) {
00161             if (*ptr++ & BALL_CDT_CHAN) {
00162                 xsum += x;
00163                 ysum += y;
00164                 npixels++;
00165             }
00166         }
00167     }
00168 
00169     if (npixels != 0) {
00170         *xcentroid  = xsum / npixels;
00171         *ycentroid  = ysum / npixels;
00172         *delta_pan  = -1.0 * FIELD_VIEW_H * (*xcentroid - w/2.0) / w;
00173         *delta_tilt = -1.0 * FIELD_VIEW_V * (*ycentroid - h/2.0) / h;
00174         return true;
00175     } else {
00176         *xcentroid  = 0;
00177         *ycentroid  = 0;
00178         *delta_pan  = 0.0;
00179         *delta_tilt = 0.0;
00180         return false;
00181     }
00182 }

Generated on Sun Dec 2 23:04:27 2007 for openSDK by  doxygen 1.3.9.1