Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to make the ellipse stay on captured colour - video cam

$
0
0

I want achive that with everz click on colour, there will appear and staz ellipse, capturing the colour/not to disappear. Is there some easier way than to create separate drawing loops for different ellipses? Just to say something like capture and stay, even if I click again somewhere else?

// * Brightness Tracking by Golan Levin and Daniel Shiffman
// http://www.learningprocessing.com

import processing.video.*;

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 black
  trackColor = color(0);
}

void captureEvent(Capture video) {
  // Read image from the camera
  video.read();
}

void draw() {
  video.loadPixels();
  image(video, 0, 0);

//more higher more arger chance to find the colour
  float worldRecord = 500;

// seek for the closest color
  int closestX = 0;
  int closestY = 0;

// search every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
// define and showWhat 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;
      }
    }
  }

  // trackcolour not more far than 20
    if (worldRecord < 20) {
    // Draw a circle at the tracked pixel
    fill(trackColor);
    strokeWeight(4.0);
    noStroke();
    ellipse(closestX, closestY, 60, 60);
  }
}

void mousePressed() {
  // store the colour from video and keep it tracked
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles