Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

motion tracking Opencv Game

März 5, 2012

import hypermedia.video.*;          

OpenCV opencv;                      
PImage movementImg, castle, dude1, dude2;                 
int poppedBubbles;                  
ArrayList bubbles;  
int c1 =0;
int c2 =0;
int h1 = 395;
int h2 = 395;
PImage [] PNG = new PImage[3];
PFont font;  
int zufall;

void setup()
{
  smooth();
  size ( 640, 480 );                      
  opencv = new OpenCV( this );            
  opencv.capture( 640, 480 );             
  movementImg = new PImage( 640, 480 );   
  poppedBubbles = 0;                     

  bubbles = new ArrayList();
  castle= loadImage("castel.png");
  dude1 = loadImage("dude1.png");
  dude2 = loadImage("dude2.png");


  PNG[0]= loadImage("mario.png");
  PNG[1]= loadImage("luigi.png");
  PNG[2]= loadImage("yoshi.png");  
  font = loadFont("Serif-48.vlw");        
  textFont(font, 20);
}

void draw()
{
  int m = int(random(PNG.length));
  zufall = int(random(20, 70));
  bubbles.add(new Bubble(PNG[m], (int)random( 0, width-40), -PNG[m].height, zufall, zufall));  
  //bubbles.add(new Bubble( "bubble2.png",(int)random( 0, width - 40), -PNG2.height, PNG2.width, PNG2.height));   
  opencv.read();                                 
  opencv.flip(OpenCV.FLIP_HORIZONTAL);        
  image( opencv.image(), 0, 0 );              
  opencv.absDiff();                           

  opencv.convert(OpenCV.GRAY);                
  opencv.blur(OpenCV.BLUR, 3);                
  opencv.threshold(20);                       
  movementImg = opencv.image();              

  for ( int i = 0; i < bubbles.size(); i++ ) {

    Bubble _bubble = (Bubble) bubbles.get(i);    
    if (_bubble.update() == 1) {                  
      bubbles.remove(i);                        
      _bubble = null;                           
      i--;
    }
    else {      
      bubbles.set(i, _bubble);                  
      _bubble = null;
    }
  }

  opencv.remember(OpenCV.SOURCE, OpenCV.FLIP_HORIZONTAL);   


  if (c1 < 630) {
    c1+=3;
  }
  else {
    c1 = 0;
    h1 = 395;
  }
  if (c2 > 0) {
    c2-=3;
  }
  else {
    c2 = 660;
    h2 = 395;
  }
  //drawing the clouds on the screen with moving variables
  image(dude2, c1, h1);
  image(dude1, c2, h2);
  image(castle, 0, 455, width, 33);
  text( poppedBubbles, 15, height-5);
}

Class: Bubble
class Bubble
{

  int bubbleX, bubbleY, bubbleWidth, bubbleHeight;    //  Some variables to hold information about the bubble
  boolean richtung = false;
  int counter = 0;
  PImage PNGB;

  Bubble (PImage bpgn, int bX, int bY, int bW, int bH )           //  The class constructor- sets the values when a new bubble object is made
  {
    PNGB = bpgn;
    bubbleX = bX;
    bubbleY = bY;
    bubbleWidth = bW;
    bubbleHeight = bH;
  }

  int update()      //   The Bubble update function
  {
    int movementAmount;          
    movementAmount = 0;

    for ( int y = bubbleY; y < (bubbleY + (bubbleHeight-1)); y++ ) {    
      for ( int x = bubbleX; x < (bubbleX + (bubbleWidth-1)); x++ ) {

        if ( x < width && x > 0 && y < height && y > 0 ) {             
          if (brightness(movementImg.pixels[x + (y * width)]) > 127) 
          {
            movementAmount++;
          }
        }
      }
    }

    if (movementAmount > 15)               
    {
      poppedBubbles++;                   
      return 1;
    }
                    
      bubbleY += random(3, 12);
      if (bubbleY > height)              
      {  
        return 1;
      }                      
      image(PNGB, bubbleX, bubbleY,bubbleWidth,bubbleHeight);   
      return 0;
    }
  }