Exemplo n.º 1
0
void TeaBot::engage() {
   if (Broodwar->enemy()->getUnits().empty()) {
      state = ADV;
      adv();
      return;
   }
   map<Unit*,int> attacked;
   set<Unit*> units = Broodwar->self()->getUnits();
   set<Unit*> enemies = Broodwar->enemy()->getUnits();
   double x2, y2;
   findCentre(enemies, x2, y2);
   forUnitSet (enemy, enemies) {
      Unit *target = (*enemy)->getOrderTarget();
      if (target && target->exists())
         if (target->getPlayer() == Broodwar->self())
            if (attacked.find(target) != attacked.end())
               ++attacked[target];
            else
               attacked[target] = 1;
   }
Exemplo n.º 2
0
void perceptualGrouping(CCL_Object positiveSource,CCL_Object negativeSource,IplImage *img)
{
	printf("Performing perceptual grouping...\n");

	IplImage *displayImage = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,3);
	cvCvtColor(img,displayImage,CV_GRAY2BGR);

	int positiveMappings[positiveSource.classCount];
	for(int i = 0;i < positiveSource.classCount;i++){ positiveMappings[i] = -1;}

	int negativeMappings[negativeSource.classCount];
	for(int i = 0;i < negativeSource.classCount;i++){ negativeMappings[i] = -1;}


	//POSITIVE PASS
	//count the number of non-zero classes
	int c = 0;
	for(int i = 0;i < positiveSource.classCount;i++)
	{
		if(positiveSource.classSizes[i] > 0) c++;
	}
	//build the blob data structure
	Blob positiveBlobs[c];
	int p = 0;
	for(int i = 0;i < positiveSource.classCount;i++)
	{
		if(positiveSource.classSizes[i] > 0)
		{
			positiveBlobs[p].size = positiveSource.classSizes[i];
			positiveBlobs[p].top.i = positiveSource.minI[i];
			positiveBlobs[p].top.j = positiveSource.minJ[i];
			positiveBlobs[p].bottom.i = positiveSource.maxI[i];
			positiveBlobs[p].bottom.j = positiveSource.maxJ[i];
			positiveBlobs[p].centre = findCentre(positiveBlobs[p].top,positiveBlobs[p].bottom);
			positiveBlobs[p].linked = -1;
			positiveBlobs[p].group = -1;
			positiveMappings[i] = p;
			p++;
		}
	}

	int groupCount = 0;
	for(int i = 0;i < c;i++)
	{
		float maxPilu = 0;
		int pointer = 0;

		for(int j = 0;j < c;j++)
		{
			if(i != j && positiveBlobs[j].linked != i)
			{
				float pilu = piluOperator(positiveBlobs[i].top,positiveBlobs[i].bottom,positiveBlobs[j].top,positiveBlobs[j].bottom);
				if(pilu > maxPilu)
				{
					maxPilu = pilu;
					pointer = j;
				}
			}
		}

		if(maxPilu >= PILULIMIT)
		{
			positiveBlobs[i].next = positiveBlobs[pointer].centre;
			positiveBlobs[i].linked = pointer;


			//update groups
			if(positiveBlobs[i].group == -1) //i.e new group
			{
				if(positiveBlobs[pointer].group != -1) //ie next is already in a group
				{
					//update group equivalences
					for(int k = 0;k < c;k++)
					{
						if(positiveBlobs[k].group == positiveBlobs[pointer].group) positiveBlobs[k].group = groupCount;
					}
				}
				positiveBlobs[pointer].group = groupCount;
				positiveBlobs[i].group = groupCount;
				groupCount++;
			}
			else
			{
				if(positiveBlobs[pointer].group == -1) positiveBlobs[pointer].group = positiveBlobs[i].group;
				else
				{
					//update group equivalences
					for(int k = 0;k < c;k++)
					{
						if(positiveBlobs[k].group == positiveBlobs[pointer].group) positiveBlobs[k].group = positiveBlobs[i].group;
					}
				}
			}


			//draw line, image, pt1, pt2, colour, thickness, type, offset
			cvLine(displayImage,cvPoint(positiveBlobs[i].centre.j,positiveBlobs[i].centre.i),cvPoint(positiveBlobs[i].next.j,positiveBlobs[i].next.i),cvScalar(155,0,0,0),2,8,0);

		}
	}
	/////////////////////////////////////////////////////////////////////////
	//NEGATIVE PASS
	//count the number of non-zero classes
	int n = 0;
	for(int i = 0;i < negativeSource.classCount;i++)
	{
		if(negativeSource.classSizes[i] > 0) n++;
	}
	//build the blob data structure
	Blob negativeBlobs[n];
	p = 0;
	for(int i = 0;i < negativeSource.classCount;i++)
	{
		if(negativeSource.classSizes[i] > 0)
		{
			negativeBlobs[p].size = negativeSource.classSizes[i];
			negativeBlobs[p].top.i = negativeSource.minI[i];
			negativeBlobs[p].top.j = negativeSource.minJ[i];
			negativeBlobs[p].bottom.i = negativeSource.maxI[i];
			negativeBlobs[p].bottom.j = negativeSource.maxJ[i];
			negativeBlobs[p].centre = findCentre(negativeBlobs[p].top,negativeBlobs[p].bottom);
			negativeBlobs[p].linked = -1;
			negativeBlobs[p].group = -1;
			negativeMappings[i] = p;
			p++;
		}
	}

	for(int i = 0;i < n;i++)
	{
		float maxPilu = 0;
		int pointer = 0;

		for(int j = 0;j < n;j++)
		{
			if(i != j && negativeBlobs[j].linked != i)
			{
				float pilu = piluOperator(negativeBlobs[i].top,negativeBlobs[i].bottom,negativeBlobs[j].top,negativeBlobs[j].bottom);
				if(pilu > maxPilu)
				{
					maxPilu = pilu;
					pointer = j;
				}
			}
		}

		if(maxPilu >= PILULIMIT)
		{
			negativeBlobs[i].next = negativeBlobs[pointer].centre;
			negativeBlobs[i].linked = pointer;


			//update groups
			if(negativeBlobs[i].group == -1) //i.e new group
			{
				if(negativeBlobs[pointer].group != -1) //ie next is already in a group
				{
					//update group equivalences
					for(int k = 0;k < n;k++)
					{
						if(negativeBlobs[k].group == negativeBlobs[pointer].group) negativeBlobs[k].group = groupCount;
					}
				}
				negativeBlobs[pointer].group = groupCount;
				negativeBlobs[i].group = groupCount;
				groupCount++;
			}
			else
			{
				if(negativeBlobs[pointer].group == -1) negativeBlobs[pointer].group = negativeBlobs[i].group;
				else
				{
					//update group equivalences
					for(int k = 0;k < n;k++)
					{
						if(negativeBlobs[k].group == negativeBlobs[pointer].group) negativeBlobs[k].group = negativeBlobs[i].group;
					}
				}
			}


			//draw line, image, pt1, pt2, colour, thickness, type, offset
			cvLine(displayImage,cvPoint(negativeBlobs[i].centre.j,negativeBlobs[i].centre.i),cvPoint(negativeBlobs[i].next.j,negativeBlobs[i].next.i),cvScalar(155,0,0,0),2,8,0);

		}
	}

	/////////////////////////////////////////////////////////////////

	//array of BBoxes
	BBox boxes[groupCount];
	//build BBoxes
	for(int i = 0;i < groupCount;i++)
	{
		//initialise box - inside out
		boxes[i].top.i = 9999;
		boxes[i].top.j = 9999;
		boxes[i].bottom.i = -1;
		boxes[i].bottom.j = -1;
		boxes[i].size = 0;

		//for each positive blob
		for(int j = 0;j < c;j++)
		{
			//if its in this group
			if(positiveBlobs[j].group == i)
			{
				//add it to the bbox
				boxes[i].size++;
				boxes[i] = extendBox(boxes[i],positiveBlobs[j].top,positiveBlobs[j].bottom);
			}
		}
		//for each negative blob
		for(int j = 0;j < n;j++)
		{
			//if its in this group
			if(negativeBlobs[j].group == i)
			{
				//add it to the bbox
				boxes[i].size++;
				boxes[i] = extendBox(boxes[i],negativeBlobs[j].top,negativeBlobs[j].bottom);
			}
		}
	}

	//for each box
	for(int i = 0;i < groupCount;i++)
	{
		//if its got more than one thing in it
		//printf("%i \n",boxes[i].size);
		if(boxes[i].size > 1)
		{
			//draw it
			drawBox(boxes[i],displayImage);
		}
	}


	//show the image
	cvSaveImage("grouping.png",displayImage);
	cvNamedWindow("ImageWindow", CV_WINDOW_AUTOSIZE);
	cvShowImage("ImageWindow", displayImage);
	printf("Image displayed...\n");

	//wait till key is pressed
	printf("PRESS A KEY NOW...\n");
	cvWaitKey(0);

	//update img
	int target[positiveSource.height][positiveSource.width];

	for(int i = 0;i < positiveSource.height;i++)
	{
		for(int j = 0;j < positiveSource.width;j++)
		{
			int pos = positiveSource.labels[(i * positiveSource.width) + j];
			pos = positiveMappings[pos];
			if(pos != -1)
			{
				pos = positiveBlobs[pos].group;
				pos = boxes[pos].size;
			}
			int neg = negativeSource.labels[(i * negativeSource.width) + j];
			//int neg = -1;
			neg = negativeMappings[neg];
			if(pos != -1)
			{
				neg = negativeBlobs[neg].group;
				neg = boxes[neg].size;
			}
			if(pos > 1 || neg > 1)
			{
				target[i][j] =  255;
			}else
			{
				target[i][j] = 0;
			}
		}
	}
	uchar* data = (uchar *)img->imageData;
	//copy new data into image
	for(int i = 0; i < positiveSource.height;i++)
	{
		for(int j = 0; j < positiveSource.width; j++)
		{
			data[(i*img->widthStep) + j ] = target[i][j];
			//printf("%i %i %u \n",i,j,test[i][j]);
		}
	}
	img->imageData = (char*)data;





	printf("Done\n");
}
Exemplo n.º 3
0
void frameblob::calc_searchgrid() {
  // First calculate dimensions of the searchgrid array
  
  unsigned int xfull, yfull, xextra, yextra, xdim, ydim;
  
  xfull  = xpix / grid;       // # of full-sized cells
  yfull  = ypix / grid; 
  xextra = xpix % grid;       // # extra pixels in small edge cells
  yextra = ypix % grid;
  
  xgrid = (xfull + (xextra>0));        // total # grid cells 
  ygrid = (yfull + (yextra>0));
  ngrid = (xgrid+xextra) * ygrid;
  
  // unallocate searchgrid and meangrid before we start in case we have 
  // run this function several times 
  
  delete searchgrid;
  delete meangrid;
  
  searchgrid = new double[ngrid];   // S/N of brightest pixel/grid cell
  meangrid = new double[ngrid];     // array of grid cell means 
  
  // Allocate an array to contain all the GRID*GRID elements within this
  // grid cell of our input map
  
  MAPTYPE *cell;
  cell = new MAPTYPE[grid*grid];
  
  MAPTYPE pix;           // current pixel values
  MAPTYPE max;           // the maximum value pixel in the cell
  int total;             // running total elements in cell (big number)
  
  double meancell ;      // cell mean
  double level;          // max adjusted by centre value
  double sn;             // signal to noise level of brightest pixel
  double x;              // x and y pixel positions for brightest pixels
  double y;              //   for the large input map  
  
  unsigned int i, j, k, l, mapindex, cellindex;
  int startx, endx, starty, endy;
  double thisflux;
  double M[D][D];  // small map centred over blob candidate
  double xcen, ycen, nap;
  int clip_startx, clip_endx, clip_starty, clip_endy;
  int count=0;
  
  clearblobs();    // Make sure we don't have old blobs sitting around
  
  for( i=0; i<xgrid; i++ )            
    for( j=0; j<ygrid; j++ ) {  
      // Sum all the elements within this cell of the input map.
      // Also make a list of the elements in the cell
      
      cellindex = 0;
      total = 0;
      max = 0;
      
      // Pixel dimensions of grid cell. grix*grid unless edge
      if( i==xfull ) xdim = xextra;
      else xdim = grid;
      if( j==yfull ) ydim = yextra;
      else ydim = grid;
      
      
      for( k=0; k<xdim; k++ )     
        for( l=0; l<ydim; l++ ) {
          mapindex = (j*grid+l)*xpix + (i*grid+k);
          pix = map[mapindex];
          
          cell[cellindex] = pix;
          total += pix;
          
          // Check for greatest pixel brightness
          if( pix > max ) {
            x = (double) (i*grid + k);  // store pixel coordinates
            y = (double) (j*grid + l);  // of brightest pixel    
            max = pix; 
          }                                     
          cellindex ++; // end of loop = # pixels in cell
        }
      
      // Get the mean for this cell
      meancell = (double) total / (cellindex);
      
      // Level is the brightness of a pixel - the mean level for the cell
      level = (double) max - meancell; 
      
      // Calculate the sample variance about the central value
      if( mapmean == 0 ) calc_mapstat();   // Calculate the map mean
      
      if( mapmean < readout_offset ) sigma = readout_noise;
      else sigma = (double)sqrt(gain*(mapmean-readout_offset) + 
                                readout_noise*readout_noise); // Poisson
      if( sigma < 1 ) sigma = 1;   // prevent 0 sigmas
      
      sn = level/sigma;
      
      // Store information about this grid cell
      searchgrid[j*xgrid+i] = sn;     // s/n brightest pixel in cell
      meangrid[j*xgrid+i] = meancell; // mean of this cell
      
      // If we got a pixel > threshold sigma, possibly a source
      if( sn >= threshold ) {
        //printf("%i ",count);
        //count ++;
        
        // --------------------------------------------------------------
        // Decide if this is a single-point spike or a real source
        
        startx = (int) x - 1;  // Aperture boundaries
        starty = (int) y - 1;
        endx = (int) x + 1;
        endy = (int) y + 1;
        
        if( startx < 0 ) startx = 0;          // clipping map boundaries
        if( endx >= (int) xpix ) endx = xpix-1;
        if( starty < 0 ) starty = 0;
        if( endy >= (int) ypix ) endy = ypix-1;
        
        thisflux = 0.;
        
        nap = (double) (endx-startx+1)*(endy-starty+1); // pixels in ap.

        // add up flux centred over bright pixel
        for( k=startx; k<=endx; k++ )
          for( l=starty; l<=endy; l++ )
            thisflux += (double) map[l*xpix + k];
        
        // remove flux centre pixel and check remaining flux 
        // exceeds the theoretical flux from pure noise 
        // (check extendedness)
        thisflux -= max;

        // remove the baseline for the remaining pixels
        thisflux -= ((nap-1)*meancell);

        // Extended case
        if( (thisflux/(sqrt(nap-1)*sigma)) >= threshold ) {       
          // ------------------------------------------------------------
          // Re-calculate the baseline over perimeter of a larger box
          
          startx = (int) x - D/2;  // box boundary
          starty = (int) y - D/2;
          endx = startx + D-1;
          endy = starty + D-1;
          
          if( startx < 0 ) startx = 0;    // clipping for boundaries
          if( endx >= (int) xpix ) endx = xpix-1;
          if( starty < 0 ) starty = 0;
          if( endy >= (int) ypix ) endy = ypix-1;
          
          meancell = 0;
          for( k=startx; k<=endx; k++ )
            meancell += (double) map[starty*xpix + k] + 
              (double) map[endy*xpix + k];
          
          for( l=starty+1; l<=endy-1; l++ )
            meancell += (double) map[l*xpix + startx] + 
              (double) map[l*xpix + endx];
          
          meancell /= ( 2*(endx-startx) + 2*(endy-starty) );
          
          // ------------------------------------------------------------
          // Centroid the blob
          
          startx = (int) x - D/2;  
          starty = (int) y - D/2;
          endx = startx + D-1;
          endy = starty + D-1;
          
          if( startx < 0 ) clip_startx = 0;  // clipping for boundaries
          else clip_startx = startx;
         
          if( endx >= (int) xpix ) clip_endx = xpix-1;
          else clip_endx = endx;
          
          if( starty < 0 ) clip_starty = 0;
          else clip_starty = starty;
          
          if( endy >= (int) ypix ) clip_endy = ypix-1;
          else clip_endy = endy;
          
          // boundary case
          if( (startx != clip_startx) || (starty != clip_starty) ||
              (endx != clip_endx) || (endy != clip_endy) ) {
          
            // fill the sub-map with the mean value
            for( k=0; k<D; k++ )
              for( l=0; l<D; l++ )
                M[k][l] = (double) meancell;
            
            // paste in the useful part of the map
            for( k=clip_startx-startx; k<clip_endx-startx; k++ )
              for( l=clip_starty-starty; l<clip_endy-starty; l++ )
                M[k][l] = (double) map[(l+clip_starty)*xpix + 
                                       k+clip_startx];
          }
          
          // normal case
          else {
            for( k=0; k<D; k++ )
              for( l=0; l<D; l++ )
                M[k][l] = (double) map[(l+starty)*xpix + k+startx];
          }
          
          // Find the centroid in this small map
          findCentre( &xcen, &ycen, &thisflux, M );
          
          // Correct for the baseline and final check realness
          thisflux = thisflux - meancell;
          
          //printf("Flux: %lf Sigma: %lf\n",
          //thisflux,sqrt(convweight)*sigma);

          // Add to the list if significant
          if( thisflux/(sqrt(convweight)*sigma) >= threshold ) {
            addblob( (int)thisflux, x+xcen+0.5, y+ycen+0.5 ); 
            thisblob->setmean(meancell);
            thisblob->setsnr( (double)thisflux / 
                              (sqrt(convweight)*sigma) );
          }
        }
        
        // --------------------------------------------------------------
      }
    }
  
  // clean up
  delete cell;
}