problem full screen with color tracking and webcam

edited February 2017 in Library Questions

Hi! I have a big question, I'm trying to go to full screen with a simple colortracking game but my webcam doesn't support the resolution of my monitor. (I'm playing with Shiffman code).

How can I do? I have tried with image to resize the output but after how Can i "take" (I'm not english native speaker) the right pixel of the output to use for the color traking (and not the pixel of the low-res camera)?

thx

Answers

  • What code are you using? Please post.

  • edited February 2017

    I have take the code of Shiffman

    import processing.video.*;
    
    // Variable for capture device
    Capture video;
    PImage img;
    PImage img1;
    
    // A variable for the color we are searching for.
    color trackColor; 
    
    // A Snake variable
    Snake snake;
    
    void setup() {
      size(1280,720);
      video = new Capture(this,width,height);
      video.start();
      // Start off tracking for red
      trackColor = color(172,107,72);
      img =loadImage("gatto.jpg");
        img1 =loadImage("cane.jpg");
    
      // Initialize the snake
      snake = new Snake(50);
      
    }
    `
    
    `
    // New frame available from camera
    void captureEvent(Capture video) {
      video.read();
    }
    
    void draw() {
      video.loadPixels();
      image(video,0,0);
    
      // 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 = 800; 
    
      // 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) { 
        // Update the snake's location
        snake.update(closestX,closestY);
      }
      
      snake.display();
    
    }
    
    
    
    void mousePressed() {
      // Save color where the mouse is clicked in trackColor variable
      int loc = mouseX + mouseY*video.width;
      trackColor = video.pixels[loc];
      print(red(trackColor),"\n");
        print(green(trackColor),"\n");
    
      print(blue(trackColor),"\n");
    
    }
    class Snake {
      // x and y positions
      int[] xpos;
      int[] ypos;
    
      // The constructor determines the length of the snake
      Snake(int n) {
        xpos = new int[n];
        ypos = new int[n];
      }
    
      void update(int newX, int newY) {
        // Shift all elements down one spot. 
        // xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
        for (int i = 0; i < xpos.length-1; i ++ ) {
          xpos[i] = xpos[i+1]; 
          ypos[i] = ypos[i+1];
        }
    
        // Update the last spot in the array with the mouse location.
        xpos[xpos.length-1] = newX; 
        ypos[ypos.length-1] = newY;
      }
    
      void display() {
        // Draw everything
        for (int i = 0; i < xpos.length; i ++ ) {
          // Draw an ellipse for each element in the arrays. 
          // Color and size are tied to the loop's counter: i.
          stroke(0);
          fill(255-i*5);
          ellipse(xpos[i],ypos[i],i,i); 
        }
    
      }
    
    }
    

    `

  • I think you missed some lines here. But I get what code you're talking about. It would still be better if you post your try at making it work.

  • edited February 2017

    yep. it missed the snake class:

    class Snake {
      // x and y positions
      int[] xpos;
      int[] ypos;
    
      // The constructor determines the length of the snake
      Snake(int n) {
        xpos = new int[n];
        ypos = new int[n];
      }
    
      void update(int newX, int newY) {
        // Shift all elements down one spot. 
        // xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
        for (int i = 0; i < xpos.length-1; i ++ ) {
          xpos[i] = xpos[i+1]; 
          ypos[i] = ypos[i+1];
        }
    
        // Update the last spot in the array with the mouse location.
        xpos[xpos.length-1] = newX; 
        ypos[ypos.length-1] = newY;
      }
    
      void display() {
        // Draw everything
        for (int i = 0; i < xpos.length; i ++ ) {
          // Draw an ellipse for each element in the arrays. 
          // Color and size are tied to the loop's counter: i.
          stroke(0);
          fill(#098156);
          ellipse(xpos[i],ypos[i],i,i); 
        }
    
      }
    
    }
    
  • I also meant the last line (#86). It is clearly incomplete.

  • ohh some strange problem. I have removed some lines now `

  • (i have updated the code)

Sign In or Register to comment.