looking for a way to read if a box/rectangle is small or big on the screen

JaiJai
edited April 2016 in Kinect

hello there i have this sketch that tells me if they are faces on the screen and depending on how close or far you are to the cam then the rectangle would change its size, my question is how can i tell if the box is getting bigger or smaller WITHOUT ME actually being able to look at the screen?

what i really want to do is write to the port a string that on the other side i can have a midi sd card read wav/midi files pertaining to the size of the rectangle

import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import processing.serial.*;           

Capture video;
OpenCV opencv;

Serial port;

void setup() {
  size(640, 480);
  port = new Serial(this, Serial.list()[0], 19200);

  video = new Capture(this, 640/2, 480/2, "360HD Hologen");
  opencv = new OpenCV(this, 640/2, 480/2);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  video.start();
}

void draw() {
  scale(2);
  opencv.loadImage(video);

  image(video, 0, 0 );

  noFill();
  stroke(0, 255, 0);
  strokeWeight(1);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
}

void captureEvent(Capture c) {
  c.read();
}

Answers

  • import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    import processing.serial.*;           
    
    Capture video;
    OpenCV opencv;
    
    Serial port;
    
    int oldW = 0;
    int W = 0;
    int oldH = 0;
    int H = 0;
    
    
    void setup() {
      size(640, 480);
      port = new Serial(this, Serial.list()[0], 19200);
    
      video = new Capture(this, 640/2, 480/2, "360HD Hologen");
      opencv = new OpenCV(this, 640/2, 480/2);
      opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
    
      video.start();
    }
    
    void draw() {
      scale(2);
      opencv.loadImage(video);
    
      image(video, 0, 0 );
    
      noFill();
      stroke(0, 255, 0);
      strokeWeight(1);
      Rectangle[] faces = opencv.detect();
      println(faces.length);
    
      for (int i = 0; i < faces.length; i++) {
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
      }
      if(faces.length > 0){
        oldH = H;
        oldW = W;
        W = faces[0].width;
        H = faces[0].height;
      }
    
      if(W+H > oldW+oldH){
        println("face 0 is bigger than previous frame!");
      }
    
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    
  • JaiJai
    edited April 2016

    @erkosone hmmmm thanks for that though i guess what im looking for is a way to account for the Z axis and so i thought that if i use my original logic that will suffice for z

    -Graciaz

  • JaiJai
    edited April 2016

    @erkosone how do we find the height and width of this function?

    void draw() {
      video.loadPixels();
      image(video, 0, 0);
    
      float worldRecord = 100.0;
      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);
    
          float d = dist(r1, g1, b1, r2, g2, b2); 
          if (d < worldRecord) {
            worldRecord = d;
            closestX = x;
            closestY = y;
          }
        }
      }
    
      if (worldRecord < 95.73401) {
        // Draw a circle at the tracked pixel
        fill(trackColor);
        strokeWeight(1);
        stroke(0);
        ellipse(closestX, closestY, 60, 60);//16,16
      }
    }
    
    void mousePressed() {
      int loc = mouseX + mouseY*video.width;
      trackColor = video.pixels[loc];
    
    }
    

    i think i tried just about every combination known in this formula to try to merge just like we did with faces but nothing is giving me or letting me combine other variables to make up theheight and width

    i would like to accomplished the same thing we done with faces

     if(faces.length > 0){
        oldH = H;
        oldW = W;
        W = faces[0].width;
        H = faces[0].height;
      }
    
      if(W+H > oldW+oldH){
        println("face 0 is bigger than previous frame!");
      }
    

    this is what i last just tried so ill share with you guy although it did not work for me "just to show that i really am trying to learn and explore"

     if (worldRecord) { 
          oldX = X;
          oldY = Y;                                            
      X = closestX;
      Y = closestY;
      }
    
Sign In or Register to comment.