#include"stdafx.h" #include"getboundary.h" using namespace std; Point2D::Point2D() { this->x=0; this->y=0; } List<headtail>* getboundary(char* bmpfilename, int threshold, int leftposition, int rightposition) { int totalx; int totaly; int j; int *red;//,*green,*blue; if(bmp_read(bmpfilename,&totalx,&totaly,&red/*,&green,&blue*/)==1) return NULL; short* intensity=(short*)malloc(sizeof(short)*totalx*totaly); if(intensity==NULL) { write_to_log("Get Boundary: Out of Memory -intensity-!"); return NULL; } for(j=1;j<=totaly;j++) { for(int i=1;i<=totalx;i++) { int x,y,id; x=i-1; y=totaly-(j-1); id=(y-1)*totalx+x; intensity[(j-1)*totalx+i-1]=short(red[id]/*+green[id]+blue[id])/3*/); } } if(red!=NULL) free(red); /* if(green!=NULL) free(green); if(blue!=NULL) free(blue);*/ float* midsecond=(float*)malloc(sizeof(float)*(totaly-2)); if(midsecond==NULL) { write_to_log("Out of Memory -midsecond-!"); return NULL; } List<headtail>* pointlist=new List<headtail>; bool is_reach=false; for(int startxx=leftposition;startxx<totaly-5;startxx++) { for(j=2;j<totaly;j++) { midsecond[j-2]=(float)(-1*intensity[((j-1)-1)*totalx+(startxx-1)-1] +3*intensity[((j-1)-1)*totalx+(startxx-0)-1] -1*intensity[((j-1)-1)*totalx+(startxx+1)-1] +3*intensity[((j-0)-1)*totalx+(startxx-1)-1] -8*intensity[((j-0)-1)*totalx+(startxx-0)-1] +3*intensity[((j-0)-1)*totalx+(startxx+1)-1] -1*intensity[((j+1)-1)*totalx+(startxx-1)-1] +3*intensity[((j+1)-1)*totalx+(startxx-0)-1] -1*intensity[((j+1)-1)*totalx+(startxx+1)-1]); } int startyy=0; for(j=10;j<totaly;j++) { startyy=j; if(midsecond[j-2]==0) continue; else { if(intensity[(j-1)*totalx+startxx-1] > threshold) continue; else { int nozerosize=0; int k=maxnoisesize; while(k>0) { if(midsecond[j-2+maxnoisesize-k]!=0) nozerosize++; k--; } if(nozerosize>maxnoisesize-magicnumber) break; } } } if(!is_reach) { if(abs(startyy-totaly)>maxnoisesize) is_reach=true; continue; } else { if(startxx>rightposition) { if(abs(startyy-totaly)<maxnoisesize) break; } } headtail* newheadtail=new headtail; newheadtail->head.x=startxx; newheadtail->head.y=startyy; for(j=totaly-10;j>=2;j--) { startyy=j; if(midsecond[j-2]==0) continue; else { if(intensity[(j-1)*totalx+startxx-1]>threshold) continue; else { int nozerosize=0; int k=maxnoisesize; while(k>0) { if(midsecond[j-2-(maxnoisesize-k)]!=0) nozerosize++; k--; } if(nozerosize>maxnoisesize-magicnumber) break; } } } newheadtail->tail.x=startxx; newheadtail->tail.y=startyy; pointlist->Add(*newheadtail); } if(midsecond!=NULL) free(midsecond); if(intensity!=NULL) free(intensity); return pointlist; }