Comparing current angle of tracked pixel to previous, delay?

henhen
edited October 2013 in Questions about Code

Hi,

I am having an issue with comparing points, current and previous. In my current sketch the angles compared are updated so frequently that the difference is minimal.

    if ((closestX>320)&&(closestY<240)) {  //Square 4
      tempX = closestX-320;
      tempY = closestY;
      currentAngle = degrees (atan(tempY/tempX));


      tempAngle = currentAngle - previousAngle;


      println("current angle is :" + currentAngle);
      println("previous angle is:" + previousAngle);
      println("difference is:" + tempAngle);


      previousAngle = (currentAngle);
    }
  }
}

Is there a way to compare angle of half a second ago to the current angle?

Answers

  • full sketch:

    import processing.video.*;
    
    float tempX;  //temporal x value for trigonometry 
    float tempY;  //temporal y value for trigonometry 
    float tempAngle; // //temporal angle value for trigonometry 
    float currentAngle=0;
    float previousAngle=0;
    
    // Variable for capture device
    Capture video;
    
    
    // A variable for the color we are searching for.
    color trackColor; 
    
    void setup() {
      size(640, 480);
      video = new Capture(this, width, height);
      video.start();
      // Start off tracking for red
      trackColor = color(255, 0, 0);
      smooth();
    }
    
    void draw() {
      // Capture and display the video
    
      if (video.available()) {
        video.read();
      }
      video.loadPixels();
      image(video, 0, 0);
      pushMatrix();
      scale(-1, 1);
      image(video.get(), -width, 0);
      popMatrix();
    
      // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
      float worldRecord = 500; 
    
      // XY coordinate of closest color
      int closestX = 0;
      int closestY = 0;
    
      // Begin loop to walk through every pixel
      for (int x = 0; x < video.width; x ++ ) {
        for (int y = 0; y < video.height; y ++ ) {
          int loc = x + y*video.width;
          // What is current color
          color currentColor = video.pixels[loc];
          float r1 = red(currentColor);
          float g1 = green(currentColor);
          float b1 = blue(currentColor);
          float r2 = red(trackColor);
          float g2 = green(trackColor);
          float b2 = blue(trackColor);
    
          // Using euclidean distance to compare colors
          float d = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
    
          // If current color is more similar to tracked color than
          // closest color, save current location and current difference
          if (d < worldRecord) {
            worldRecord = d;
            closestX = x;
            closestY = y;
          }
        }
      }
    
      // We only consider the color found if its color distance is less than 10. 
      // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
    
    
    
    
    
      if (worldRecord < 10) { 
        // Draw a circle at the tracked pixel
        fill(trackColor);
        strokeWeight(4.0);
        stroke(0);
        ellipse(closestX, closestY, 40, 40);
    
    
        if ((closestX<320)&&(closestY<240)) {   // Square 1
          println("SQ1");
        }
        if ((closestX<320)&&(closestY>240)) {  //Square 2
          println("SQ2");
        }
        if ((closestX>320)&&(closestY>240)) {  //Square 3
          println("SQ3");
        }
        if ((closestX>320)&&(closestY<240)) {  //Square 4
          tempX = closestX-320;
          tempY = closestY;
          currentAngle = degrees (atan(tempY/tempX));
    
    
          tempAngle = currentAngle - previousAngle;
    
    
          println("current angle is :" + currentAngle);
          println("previous angle is:" + previousAngle);
          println("difference is:" + tempAngle);
    
    
          previousAngle = (currentAngle);
        }
      }
    }
    
    void mousePressed() {
    
      // Save color where the mouse is clicked in trackColor variable
      int x = video.width - mouseX;
      int y = mouseY;
      int loc = y*video.width+x;
      trackColor = video.pixels[loc];
    }
    
  • If you need to keep track of angles over a period of time, store them in an array.

    Use millis() to keep track of time.

  • Sorry I am really new to processing and I am struggling to understand how to use array, I understand that array will create a list of my previous angles and these can be accessed according to a millis() condition. Could you give me an example?

    Thanks.

  • If you know how many angles you're gonna need, use a float[]:
    http://processing.org/reference/Array.html

    Otherwise, use a FloatList:
    http://processing.org/reference/FloatList.html

  • I know that I want to compare the current angle with the angle that was like 0.5 seconds ago

Sign In or Register to comment.