Ejemplo n.º 1
0
/**************************************************************************//**
 * @author Hannah Aker
 *
 * @par Description:
 * draw_box - draws boxes around transformation spaces
 *
 * @param[in,out] image - original image of target
 * @param[in,out] ts - object containing transformation space
 * @param[in] rows - model height
 * @param[in] cols - model width
 *
 *****************************************************************************/
void draw_box(Image& image, vector<tsObject> &ts, int rows, int cols)
{
    // four points for the corners of our outline box
    point p1, p2, p3, p4;
    for(unsigned int i = 0; i < ts.size(); i++)
    {
        // set the corners to the transformed model
        p1.x = ts[i].transXCenter;
        p1.y = ts[i].transYCenter;
        p2.x = ts[i].transXCenter + (cols * ts[i].scaleXCenter);
        p2.y = ts[i].transYCenter;
        p3.x = ts[i].transXCenter;
        p3.y = ts[i].transYCenter + (rows * ts[i].scaleYCenter);
        p4.x = ts[i].transXCenter + (cols * ts[i].scaleXCenter);
        p4.y = ts[i].transYCenter + (rows * ts[i].scaleYCenter);

        // draw the lines
        image.DrawLine(p1.x, p1.y, p2.x, p2.y, Pixel(0,255,0));
        image.DrawLine(p1.x, p1.y, p3.x, p3.y, Pixel(0,255,0));
        image.DrawLine(p2.x, p2.y, p4.x, p4.y, Pixel(0,255,0));
        image.DrawLine(p4.x, p4.y, p3.x, p3.y, Pixel(0,255,0));
    }
}
Ejemplo n.º 2
0
int main(int argc, char* argv[])
{
  int i, j, k;
  int width, height;
  int numFleshRegions, numHands, xScale, yScale;
  int left, right, top, bottom;
  Image* image;
  Image outlineImage;
  FleshDetector* fleshDetector;
  vector<ConnectedRegion*>* fleshRegionVector;
  vector<Hand*> hands;
  Hand* hand;
  vector<HandCandidate*> handCandidates;
  HandCandidate* candidate;
  unsigned char angledBoxColor[] = {255, 255, 0};
  unsigned char longColor[] = {0, 255, 0};
  unsigned char shortColor[] = {0, 0, 255};
  unsigned char offsetColor[] = {0, 255, 255};
  unsigned char pointColor[] = {255, 0, 0};
  unsigned char farPointColor[] = {255, 0, 255};
  int numLargeRegions;
  string basename;
  DoublePoint centroid, center, nearEdge, farEdge;
  LineSegment shortLine, longLine, offsetLine;
  Rect angledBox;
  double edgeAngle, offsetAngle;
  CompositeClassifier postureDetector;
  string features;
  Matrix input;
  int classIndex;
  SubImage handImage;
  vector<Point> farPoints;
  int numFarPoints;
  string inputFilename, outputFilename;
  VideoDecoder decoder;
  VideoEncoder encoder;
  bool needInit = true;

  if ( argc < 5 )
  {
    printf("Usage: %s <flesh classifier file> <hand classifier file> <input file> <output file>\n", argv[0]);
    return 1;
  }

  // Either loads a real detector or gets a dummy detector if arg is "DUMMY"
  fleshDetector = FleshDetector::Get(argv[1]);
  if ( !fleshDetector )
  {
    fprintf(stderr, "Error loading flesh detector %s\n", argv[1]);
    return 1;
  }

  if ( !postureDetector.Load(argv[2]) )
  {
    fprintf(stderr, "Error loading hand detector %s\n", argv[2]);
    return 1;
  }
  features = postureDetector.GetFeatureString();

  inputFilename = argv[3];
  outputFilename = argv[4];

  decoder.SetFilename(inputFilename);
  if ( !decoder.Load() )
  {
    fprintf(stderr, "Error loading video %s\n", inputFilename.c_str());
    return 1;
  }

  while ( decoder.UpdateFrame() )
  {
    image = decoder.GetFrame();

    if ( needInit )
    {
      needInit = false;
      width = image->GetWidth();
      height = image->GetHeight();

      if ( !encoder.Open(outputFilename.c_str(), width, height, 10) )
      {
        fprintf(stderr, "Failed opening %s\n", outputFilename.c_str());
        return 1;
      }
    }

    hands.clear();
    outlineImage = *image;
    fleshRegionVector = fleshDetector->GetFleshRegions(image, xScale, yScale);
    if ( fleshRegionVector )
    {
      numFleshRegions = fleshRegionVector->size();
      numLargeRegions = 0;
      for (i = 0; i < numFleshRegions; i++)
      {
        if ( !(*fleshRegionVector)[i]->GetBounds(left, right, top, bottom) )
        {
          fprintf(stderr, "Error getting flesh block %d bounds\n", i);
          return 1;
        }
        left *= xScale;
        right = (right + 1) * xScale - 1;
        top *= yScale;
        bottom = (bottom + 1) * yScale - 1;
        if ( (right - left + 1 < FLESH_REGION_MIN_DIMENSION) || (bottom - top + 1 < FLESH_REGION_MIN_DIMENSION) )
          continue;
        numLargeRegions++;

        handImage.CreateFromParent(image, left, right, top, bottom);
        vector<ConnectedRegion*>* fullResRegions;
        fullResRegions = fleshDetector->GetFleshRegions(&handImage);
        int numFullResRegions = 0;
        if ( fullResRegions )
          numFullResRegions = fullResRegions->size();
        if ( !numFullResRegions )
        {
          fprintf(stderr, "Failed getting full resolution hand candidate\n");
          return 1;
        }
        int regionIndex = 0;
        if ( numFullResRegions > 1 )
        {
          for (k = 1; k < numFullResRegions; k++)
            if ( (*fullResRegions)[k]->HasMorePixels( *((*fullResRegions)[regionIndex]) ) )
              regionIndex = k;
        }

        candidate = new HandCandidate( (*fullResRegions)[regionIndex] );
        if ( !candidate->GetScaledFeatures(1, 1, centroid, center, nearEdge, farEdge,
          shortLine, longLine, offsetLine, edgeAngle, offsetAngle) )
        {
          fprintf(stderr, "Error getting hand candidate features for flesh block %d\n", i);
          return 1;
        }
        angledBox = candidate->GetAngledBoundingBox(longLine);
        farPoints.clear();
        if ( !candidate->GetFarPoints(farPoints) )
          fprintf(stderr, "Error getting far points for flesh block %d\n", i);
        numFarPoints = farPoints.size();

        centroid = handImage.GetTopLevelCoords(centroid);
        center = handImage.GetTopLevelCoords(center);
        nearEdge = handImage.GetTopLevelCoords(nearEdge);
        farEdge = handImage.GetTopLevelCoords(farEdge);
        shortLine.Translate(left, top);
        longLine.Translate(left, top);
        offsetLine.Translate(left, top);
        angledBox.Translate(left, top);
        for (k = 0; k < numFarPoints; k++)
          farPoints[k] = handImage.GetTopLevelCoords(farPoints[k]);

        if ( !candidate->GetFeatureVector(features, input) )
        {
          fprintf(stderr, "Error getting hand candidate features for flesh block %d\n", i);
          return 1;
        }

        classIndex = postureDetector.Classify(input);
        if ( classIndex != -1 )
        {
          hand = new Hand;
          hand->SetBounds(left, right, top, bottom);
          hand->SetPostureString(postureDetector.GetClassName(classIndex));
          hands.push_back(hand);
        }

        delete candidate;

        outlineImage.DrawLine(longColor, 1, longLine);
        outlineImage.DrawLine(shortColor, 1, shortLine);
        outlineImage.DrawLine(offsetColor, 1, offsetLine);
        outlineImage.DrawLine(pointColor, 1, centroid, centroid);
        outlineImage.DrawLine(pointColor, 1, center, center);
        outlineImage.DrawLine(pointColor, 1, nearEdge, nearEdge);
        outlineImage.DrawLine(pointColor, 1, farEdge, farEdge);
        outlineImage.DrawRect(angledBoxColor, 1, angledBox);
        for (k = 0; k < numFarPoints; k++)
          outlineImage.DrawLine(farPointColor, 1, centroid, farPoints[k]);
      }
      numHands = hands.size();
      for (j = 0; j < numHands; j++)
      {
        hands[j]->GetBounds(left, right, top, bottom);
        outlineImage.DrawBox(hands[j]->GetPostureColor(0),
                             hands[j]->GetPostureColor(1),
                             hands[j]->GetPostureColor(2),
                             hands[j]->GetPostureColor(3),
                             3, left, top, right, bottom);
        delete hands[j];
      }
      hands.clear();
    }

    if ( !encoder.AddFrame(&outlineImage) )
    {
      fprintf(stderr, "Error inserting video frame\n");
      return 1;
    }
  }
  encoder.Close();

  return 0;
}
Ejemplo n.º 3
0
void TerrainTest::Render()
{
	Image* scr = Screen::Instance();
	if (!grid)
		return;
	
	color c;
	tile* t;
	tile* tt;
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			t = GetTile(x, y);
			c.r = c.g = c.b = t->height * 25;
			
			scr->DrawRect( rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE), c, true);

			if (m_bDisplayTypeMap)
			{
				//Mark tile types
				c = color();
				switch (GetTileType(x, y))
				{
					case TILE_SOUTHEDGE:
						c.r = 255; 
						break;
					case TILE_WESTEDGE:
						c.g = 255;
						break;
					case TILE_EASTEDGE:
						c.b = 255;
						break;
					case TILE_NORTHEDGE:
						c.r = c.g = 255;
						break;
					case TILE_SWEDGE:
						c.r = 128;
						break;
					case TILE_SEEDGE:
						c.g = 128;
						break;
					case TILE_NWEDGE:
						c.b = 128;
						break;
					case TILE_NEEDGE:
						c.r = c.g = 128;
						break;
					case TILE_SWBEND:
						c.g = c.b = 255;
						break;
					case TILE_SEBEND:
						c.g = c.b = 128;
						break;
					case TILE_NEBEND:
						c.r = c.b = 255;
						break;
					case TILE_NWBEND:
						c.r = c.b = 128;
						break;
					default: break;	
				}
			
				if (!isDefaultColor(c))
					scr->DrawRound( x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE/2, c);	
			}
			
			//Render edge lines for all tiles that have too drastic a height difference between them
			tt = GetTile(x, y-1);
			if (tt)
			{
				if (tt->height > t->height+1)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE, x*TILE_SIZE+16, y*TILE_SIZE, color(255), 2);
				else if (tt->height+1 < t->height)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE, x*TILE_SIZE+16, y*TILE_SIZE, color(0,255), 2);
			}
			
			tt = GetTile(x, y+1);
			if (tt)
			{
				if (tt->height > t->height+1)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE+14, x*TILE_SIZE+16, y*TILE_SIZE+14, color(255), 2);
				else if (tt->height+1 < t->height)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE+14, x*TILE_SIZE+16, y*TILE_SIZE+14, color(0,255), 2);
			}
			
			tt = GetTile(x+1, y);
			if (tt)
			{
				if (tt->height > t->height+1)
					scr->DrawLine(x*TILE_SIZE+14, y*TILE_SIZE, x*TILE_SIZE+14, y*TILE_SIZE+14, color(255), 2);
				else if (tt->height+1 < t->height)
					scr->DrawLine(x*TILE_SIZE+14, y*TILE_SIZE, x*TILE_SIZE+14, y*TILE_SIZE+14, color(0,255), 2);
			}
			
			tt = GetTile(x-1, y);
			if (tt)
			{
				if (tt->height > t->height+1)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE, x*TILE_SIZE, y*TILE_SIZE+14, color(255), 2);
				else if (tt->height+1 < t->height)
					scr->DrawLine(x*TILE_SIZE, y*TILE_SIZE, x*TILE_SIZE, y*TILE_SIZE+14, color(0,255), 2);	
			}
			
			
		}
	}
	
	string msg = "m_bDisplayTypeMap: " + its(m_bDisplayTypeMap) + " (T) \\c900 m_iUpperHeight: " + its(m_iUpperHeight) + " (+Q -A). \\c090 m_iLowerHeight: " 
				+  its(m_iLowerHeight) + " (+W -S). \\c009 N to clear all";
	gui->mFont->Render(scr, 5, 5, msg, color(255,255,255));
	
	msg = "\\c090Green \\c999and \\c900red \\c999lines mark gradients too steep (Algorithm failure).";
	gui->mFont->Render(scr, 5, scr->Height() - 40, msg, color());
}