00001 #include "RegionGenerator.h"
00002 #include "Events/EventRouter.h"
00003 #include "Wireless/Wireless.h"
00004 #include "Shared/Profiler.h"
00005 #include "SegmentedColorGenerator.h"
00006
00007 #include "Vision/RLEGenerator.h"
00008
00009 #include "Shared/debuget.h"
00010
00011 RegionGenerator::RegionGenerator(unsigned int mysid, RLEGenerator* rleg, EventBase::EventTypeID_t tid)
00012 : FilterBankGenerator("RegionGenerator",EventBase::visRegionEGID,mysid,rleg,tid), srcNumColors(0), srcColors(NULL), regions(NULL)
00013 {
00014 if(rleg!=NULL) {
00015 numLayers=numChannels=0;
00016 setNumImages(rleg->getNumLayers(),rleg->getNumChannels());
00017 if(const SegmentedColorGenerator* seg=dynamic_cast<const SegmentedColorGenerator*>(rleg->getSourceGenerator())) {
00018 srcNumColors=seg->getNumColors();
00019 srcColors=seg->getColors();
00020 } else {
00021 serr->printf("WARNING: Region generator expects it's source's source to be a SegmentedColorGenerator so that it can access color table information.\n");
00022 }
00023 }
00024 }
00025
00026 void
00027 RegionGenerator::freeCaches() {
00028 FilterBankGenerator::freeCaches();
00029 for(unsigned int l=0; l<numLayers; l++)
00030 for(unsigned int c=0; c<numChannels; c++) {
00031 delete [] regions[l][c];
00032 regions[l][c]=NULL;
00033 }
00034 }
00035
00036 void
00037 RegionGenerator::doEvent() {
00038 if(event->getGeneratorID()==getListenGeneratorID() && event->getSourceID()==getListenSourceID()) {
00039 const RLEGenerator * rle=dynamic_cast<const RLEGenerator*>(src);
00040 if(NULL==rle) {
00041 serr->printf("RegionGenerator's event %s is not from RLEGenerator\n",event->getName().c_str());
00042 return;
00043 }
00044 const SegmentedColorFilterBankEvent * segev=dynamic_cast<const SegmentedColorFilterBankEvent*>(event);
00045 if(NULL==segev) {
00046 serr->printf("RegionGenerator's event %s is not SegmentedColorFilterBankEvent\n",event->getName().c_str());
00047 return;
00048 }
00049 if(srcNumColors!=segev->getNumColors())
00050 freeCaches();
00051 srcNumColors=segev->getNumColors();
00052 srcColors=segev->getColors();
00053 SegmentedColorFilterBankEvent fbkev(this,getGeneratorID(),getSourceID(),EventBase::activateETID,*segev);
00054 erouter->postEvent(fbkev);
00055 fbkev.setTypeID(EventBase::statusETID);
00056 erouter->postEvent(fbkev);
00057 fbkev.setTypeID(EventBase::deactivateETID);
00058 erouter->postEvent(fbkev);
00059 }
00060 }
00061
00062 unsigned int
00063 RegionGenerator::getBinSize() const {
00064 unsigned int used=FilterBankGenerator::getBinSize();
00065 used+=strlen("RegionImage")+LoadSave::stringpad;
00066 used+=sizeof(unsigned int);
00067 used+=sizeof(unsigned int)*srcNumColors;
00068 unsigned int xmit_bytes_per_run=5*sizeof(int)+2*sizeof(float)+2*sizeof(int);
00069 if(imageValids[selectedSaveLayer][selectedSaveChannel]) {
00070 unsigned char* img=images[selectedSaveLayer][selectedSaveChannel];
00071 region_stats * stats=reinterpret_cast<region_stats*>(img);
00072 if(stats==NULL)
00073 return 0;
00074 for(unsigned int i=0; i<srcNumColors; i++)
00075 used+=xmit_bytes_per_run*stats[i].num;
00076 } else {
00077 used+=xmit_bytes_per_run*MAX_REGIONS;
00078 }
00079 used+=sizeof(unsigned int)*srcNumColors;
00080 used+=sizeof(unsigned int)*srcNumColors;
00081 used+=sizeof(float)*srcNumColors;
00082 return used;
00083 }
00084
00085 unsigned int
00086 RegionGenerator::loadBuffer(const char buf[], unsigned int len, const char* filename) {
00087 unsigned int origlen=len;
00088 if(!checkInc(FilterBankGenerator::loadBuffer(buf,len,filename),buf,len)) return 0;
00089 std::string tmp;
00090 if(!decodeInc(tmp,buf,len)) return 0;
00091 if(tmp!="RegionImage") {
00092 serr->printf("Unhandled image type for RegionGenerator: %s",tmp.c_str());
00093 return 0;
00094 } else {
00095 unsigned int tmpNumClr=0;
00096 if(!decodeInc(tmpNumClr,buf,len)) return 0;
00097 if(tmpNumClr!=srcNumColors)
00098 freeCaches();
00099 srcNumColors=tmpNumClr;
00100 if(images[selectedSaveLayer][selectedSaveChannel]==NULL)
00101 images[selectedSaveLayer][selectedSaveChannel]=createImageCache(selectedSaveLayer,selectedSaveChannel);
00102 region_stats * stats=reinterpret_cast<region_stats*>(images[selectedSaveLayer][selectedSaveChannel]);
00103 if(stats==NULL)
00104 return 0;
00105 for(unsigned int i=0; i<srcNumColors; i++) {
00106 unsigned int tmpNumReg=0;
00107 if(!decodeInc(tmpNumReg,buf,len)) return 0;
00108 if(MAX_REGIONS<=tmpNumReg)
00109 return 0;
00110 for(unsigned int j=0; j<tmpNumReg; j++) {
00111 region * curreg=®ions[selectedSaveLayer][selectedSaveChannel][j];
00112 if(!decodeInc(curreg->color,buf,len)) return 0;
00113 if(!decodeInc(curreg->x1,buf,len)) return 0;
00114 if(!decodeInc(curreg->y1,buf,len)) return 0;
00115 if(!decodeInc(curreg->x2,buf,len)) return 0;
00116 if(!decodeInc(curreg->y2,buf,len)) return 0;
00117 if(!decodeInc(curreg->cen_x,buf,len)) return 0;
00118 if(!decodeInc(curreg->cen_y,buf,len)) return 0;
00119 if(!decodeInc(curreg->area,buf,len)) return 0;
00120 if(!decodeInc(curreg->run_start,buf,len)) return 0;
00121 if(j==tmpNumReg-1)
00122 curreg->next=NULL;
00123 else
00124 curreg->next=®ions[selectedSaveLayer][selectedSaveChannel][j+1];
00125 }
00126
00127 if(!decodeInc(stats[i].min_area,buf,len)) return 0;
00128 if(!decodeInc(stats[i].total_area,buf,len)) return 0;
00129 if(!decodeInc(stats[i].merge_threshold,buf,len)) return 0;
00130 stats[i].list=regions[selectedSaveLayer][selectedSaveChannel];
00131 stats[i].num=tmpNumReg;
00132 }
00133 imageValids[selectedSaveLayer][selectedSaveChannel]=true;
00134 return origlen-len;
00135 }
00136 }
00137
00138 unsigned int
00139 RegionGenerator::saveBuffer(char buf[], unsigned int len) const {
00140 unsigned int origlen=len;
00141 if(!checkInc(FilterBankGenerator::saveBuffer(buf,len),buf,len)) return 0;
00142 if(!encodeInc("RegionImage",buf,len)) return 0;
00143
00144 if(images[selectedSaveLayer][selectedSaveChannel]==NULL) {
00145 serr->printf("RegionGenerator::saveBuffer() failed because selected image is NULL -- call selectSaveImage first to make sure it's up to date\n");
00146 return 0;
00147 }
00148 if(!imageValids[selectedSaveLayer][selectedSaveChannel]) {
00149 serr->printf("RegionGenerator::saveBuffer() failed because selected image is invalid -- call selectSaveImage first to make sure it's up to date\n");
00150 return 0;
00151 }
00152 unsigned char* img=images[selectedSaveLayer][selectedSaveChannel];
00153 region_stats * stats=reinterpret_cast<region_stats*>(img);
00154 if(stats==NULL)
00155 return 0;
00156 if(!encodeInc(srcNumColors,buf,len)) return 0;
00157 for(unsigned int i=0; i<srcNumColors; i++) {
00158 if(!encodeInc(stats[i].num,buf,len)) return 0;
00159 region * curreg=stats[i].list;
00160 for(int j=0; j<stats[i].num; j++) {
00161 if(!encodeInc(curreg->color,buf,len)) return 0;
00162 if(!encodeInc(curreg->x1,buf,len)) return 0;
00163 if(!encodeInc(curreg->y1,buf,len)) return 0;
00164 if(!encodeInc(curreg->x2,buf,len)) return 0;
00165 if(!encodeInc(curreg->y2,buf,len)) return 0;
00166 if(!encodeInc(curreg->cen_x,buf,len)) return 0;
00167 if(!encodeInc(curreg->cen_y,buf,len)) return 0;
00168 if(!encodeInc(curreg->area,buf,len)) return 0;
00169 if(!encodeInc(curreg->run_start,buf,len)) return 0;
00170 curreg=curreg->next;
00171 }
00172
00173 if(!encodeInc(stats[i].min_area,buf,len)) return 0;
00174 if(!encodeInc(stats[i].total_area,buf,len)) return 0;
00175 if(!encodeInc(stats[i].merge_threshold,buf,len)) return 0;
00176 }
00177 return origlen-len;
00178 }
00179
00180 void
00181 RegionGenerator::setNumImages(unsigned int nLayers, unsigned int nChannels) {
00182 if(nLayers==numLayers && nChannels==numChannels)
00183 return;
00184 FilterBankGenerator::setNumImages(nLayers,nChannels);
00185 regions = new region**[numLayers];
00186 for(unsigned int i=0; i<numLayers; i++) {
00187 regions[i] = new region*[numChannels];
00188 for(unsigned int j=0; j<numChannels; j++)
00189 regions[i][j]=NULL;
00190 }
00191 }
00192
00193 unsigned char *
00194 RegionGenerator::createImageCache(unsigned int layer, unsigned int chan) const {
00195
00196 regions[layer][chan]=new region[MAX_REGIONS];
00197
00198
00199
00200 region_stats * stats=new region_stats[srcNumColors];
00201
00202
00203 memcpy(stats,srcColors,srcNumColors*sizeof(region_stats));
00204
00205 return reinterpret_cast<unsigned char*>(stats);
00206 }
00207
00208 void
00209 RegionGenerator::calcImage(unsigned int layer, unsigned int chan) {
00210 PROFSECTION("RegionGenerator::calcImage(...)",*mainProfiler);
00211
00212
00213 RLEGenerator& srcRLE=dynamic_cast<RLEGenerator&>(*src);
00214 RLEGenerator::run * rmap=reinterpret_cast<RLEGenerator::run*>(srcRLE.getImage(layer,chan));
00215 region_stats * stats=reinterpret_cast<region_stats*>(images[layer][chan]);
00216
00217
00218 CMVision::ConnectComponents(rmap,srcRLE.getNumRuns(layer,chan));
00219 unsigned int numReg = CMVision::ExtractRegions(regions[layer][chan],MAX_REGIONS,rmap,srcRLE.getNumRuns(layer,chan));
00220 unsigned int maxArea = CMVision::SeparateRegions(stats,srcNumColors,regions[layer][chan],numReg);
00221 CMVision::SortRegions(stats,srcNumColors,maxArea);
00222 CMVision::MergeRegions(stats,(int)srcNumColors,rmap);
00223 CMVision::CalcTotalArea(stats,srcNumColors);
00224
00225
00226 imageValids[layer][chan]=true;
00227 }
00228
00229 void
00230 RegionGenerator::destruct() {
00231 FilterBankGenerator::destruct();
00232 for(unsigned int i=0; i<numLayers; i++)
00233 delete [] regions[i];
00234 delete [] regions;
00235 regions=NULL;
00236 }
00237
00238
00239
00240
00241
00242
00243