Eines de l'usuari

Eines del lloc


tutorials:processing:25_video

Processing-Color-Based-Object-Tracking/ObjectTrackingAndDrawing.pde

objColorDetect.pde
import processing.video.*;
 
// Variable for capture device
Capture video;
 
// A variable for the color we are searching for.
color trackColor; 
 
int closestX = 0;
int closestY = 0;
float pclosestX = 0;
float pclosestY = 0;
 
void setup() {
  size(1280, 480);
  background(255);
  video = new Capture(this, 640, 480);
  video.start();
  // Start off tracking for red
  trackColor = color(255, 0, 0);
}
 
void captureEvent(Capture video) {
  // Read image from the camera
  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 = 500; 
 
  // XY coordinate of closest color
 
  float sumX = 0;
  float sumY = 0;
  int SameColorCount=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 < 20) {
        worldRecord = d;
        sumX = x+sumX;
        sumY = y+sumY;
        SameColorCount++;
      }
    }
  }
  closestX = int(sumX / SameColorCount); 
  closestY = int(sumY / SameColorCount);
 
  // 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 < 20) { 
    // Draw a circle at the tracked pixel
    fill(trackColor);
    strokeWeight(3.0);
    stroke(0);
    ellipse(closestX, closestY, 16, 16);
    if(abs(float(closestY)-pclosestY)<40 && abs(float(closestX)-pclosestX)<40)
    {
    line(float(closestX)+640, float(closestY),pclosestX+640, pclosestY);
    }
  }
 
  pclosestX = closestX;
  pclosestY = closestY;
}
 
void mousePressed() {
  // Save color where the mouse is clicked in trackColor variable
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
  background(255);
}

OpenCV-Tracking-Processing-UI

Stop Motion Movie Maker

import processing.video.*;
      int beginh = hour();
      int beginm = minute();
	  int begins = second();
      int vorigeh = beginh;
      int vorigem = beginm;
	  int voriges = begins;
	  int beginpunt = beginh*3600+beginm*60+begins;
	  int timediff = 0;
      boolean gemaakt = false;
      String minuut;
      String seconde;
      String filenaam = "";
Capture cam;

void setup() {
  size(640, 480);
  String[] cameras = Capture.list();
      //int[] rood;
      
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(i + ": " + cameras[i]);
    }
    
    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[0]);
    cam.start();     
  }      
}

void draw() {
  if (cam.available() == true) {
    cam.read();//Get the image
  }
  //get the time
  int h  = hour();
  int m = minute();
  int s = second();
  //if we have moved a minute further than the image taken
  //we set this boolean 'gemaakt' to false, so we know it's
  //allowed to take a new picture
  if (vorigeh != h || vorigem !=m || voriges !=s){
   gemaakt = false; 
  }
  int step = 5;//time step size in seconds
  timediff = (h*3600 + m*60 + s) - beginpunt;
  image(cam, 0, 0);
  cam.loadPixels();
  
  cam.updatePixels(); 
 //println(filenaam);
 //% is the modulo operator
  if (!boolean(timediff % step) && gemaakt == false){
    //define the filename
    int som = 10000*h + 100*m + s;
    filenaam = ("images/" + som + ".jpg");
    println("time is: "+ h + ":" + m + ":" + s);
    saveFrame(filenaam);
    println(filenaam);
    gemaakt = true;
    vorigeh = h;
    vorigem = m;
    voriges = s;
  }
  
  
  
}

Motion Tracking in Processing

tutorials/processing/25_video.txt · Darrera modificació: 2018/11/20 00:36 per crevert