Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleBeispiel
collapsetrue
import processing.video.*;
Capture video;

color trackColor;


void setup() {
  size(640, 480);

  // start video capture
  video = new Capture(this, width, height, 15);
  video.start();

  // initialize track color to red
  trackColor = color(255, 0, 0);
  smooth();
}

void draw() {
  // read video frame if available
  if (video.available()) {
    video.read();
  }

  // load pixels
  video.loadPixels();

 image(video, 0, 0);

  float worldRecord = 500; 

  int closestX = 0;
  int closestY = 0;
   // draw video
  image(video, 0, 0);

  // initialize record to number greater than the diagonal of the screen
  float record = width+height;

  // initialize variable to store closest point
  PVector closestPoint = new PVector();
  
  for // get track color as vector
  PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));

  // go through image pixel by pixel
  for (int x=0; x < video.width; x++) {
    for (int y=0; y < video.height; y++) {
      int// get pixel location
      int loc = x + y * video.width;
      
      // get pixel color
      color currentColor = video.pixels[loc];

      // get current color as vector
      PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor)); 
      
PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));  // calculate distance between current color and track color
      float diffdist = currColorVec.dist(trackColorVec);

      // save point if closer than previous
      if (diffdist < worldRecordrecord) {
        worldRecordrecord = diffdist;
        closestPoint.x = x;
        closestPoint.y = y;
      }
    }
  }

  if// (worldRecorddraw <point 10) { if we found a one that is less than 10 apart
  if (record < 10) {
    fill(trackColor);
    strokeWeight(4.0);
    stroke(0);
    ellipse(closestPoint.x, closestPoint.y, 50, 50);
  }
}

void mousePressed() {
  // save color of current pixel under the mouse
  int loc = mouseX + mouseY * video.width;
  trackColor = video.pixels[loc];
}

...

Code Block
titleBeispiel
collapsetrue
import processing.video.*;

Capture video;

BlobDetector// blobDetector;the color blobColor;to track
color voidtrackColor;
setup()
{// a dimensional size(640, 480);

  video = new Capture(this, width, height, 15);
  video.start();
  
  blobDetector = new BlobDetector();
  blobColor = color(255, 0, 0);

  smooth();
}

void drawarray to store marked pixels
boolean marks[][];

// the total marked pixels
int total = 0;

// the most top left pixel
PVector topLeft;

// the most bottom right pixel
PVector bottomRight;

void setup() {
  if (video.available()) {size(640, 480);

  // start video.read();
  }
   capture
  video = new Capture(this, width, height, 15);
  video.loadPixelsstart();
 

  // forset blackinitial andtrack whitecolor imageto red
 // video.filter(THRESHOLD,0.1);
  image(video trackColor = color(255, 0, 0);

  // initialize  blobDetector.findBlob(blobColor, 20);marks array
  marks  blobDetector.drawBlob();
  blobDetector.drawBoundingBox();
  blobDetector.drawCenterOfMass()= new boolean[width][height];
}

void mousePresseddraw() {
  int// locread =video mouseXframe + mouseY*video.width;if available
  blobColorif = (video.pixels[loc];
}available()) {
   class BlobDetector { video.read();
  }
int
blobPoints[][];  // intdraw blobWeightvideo =image
0;   PVector centerOfMassimage(video, 0, 0);

  BlobDetector() {
    blobPoints = new int[width][height];
    centerOfMass = new PVector(0, 0// find track color with treshold
  findBlob(20);

 } // load canvas voidpixels
findBlob(color blobColor, int threshold) { loadPixels();

  //  blobWeight = 0;

 draw blob
  for (int x = 0; x < width; x ++ ) {
 
    for (int y = 0; y < height; y ++ ) {
      // get pixel location
      int loc = x + y*width;

      // colormake currentColorpixel = video.pixels[loc];
red if marked
        PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor));if (marks[x][y]) {
        PVector trackColorVecpixels[loc] = new PVector(red(blobColor), green(blobColor), blue(blobColor)color(255, 0, 0);
      }
 float diff = currColorVec.dist(trackColorVec); }
  }

  // set canvas ifpixels
(diff < thresholdupdatePixels();
{
  // draw bounding box
  stroke(255,  blobPoints[x][y] = 10, 0);
  noFill();
  rect(topLeft.x, topLeft.y, bottomRight.x-topLeft.x, bottomRight.y-topLeft.y);
}

void  blobWeight++;mousePressed() {
  // save current pixel under mouse }as elsetrack {color
  int loc = mouseX + mouseY*video.width;
  blobPoints[x][y]trackColor = 0video.pixels[loc];
}

void findBlob(int threshold) {
  }// reset total
  total = }0;

  // }prepare point trackers
  int lowestX = width;
 text(blobWeight, 20, 20);
  }

  void drawBlob() {
    if (blobWeight > 200) {
      for (int x = 0; x < width; x ++ ) {
      int lowestY = height;
  int highestX = 0;
  int highestY = 0;

  // prepare track color vector
  PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));

  // go through image pixel by pixel
  for (int yx = 0; yx < heightwidth; yx ++ ) {
    for (int y = 0; y < if (blobPoints[x][y] == 1height; y ++ ) {
      // get pixel location
  strokeWeight(5);    int loc = x + y*width;

  stroke(255, 0, 0);  // get color of pixel
      point(x, y);
 color currentColor = video.pixels[loc];

      // }get vector of pixel color
    }  PVector currColorVec = new  }
 PVector(red(currentColor), green(currentColor), blue(currentColor));

    }  // }get distance to track voidcolor
drawBoundingBox() {     PVectorfloat Adist = new PVector(width, heightcurrColorVec.dist(trackColorVec);
    PVector B =
new PVector(0, 0);    // forreset (intmark
x = 0; x < width; marks[x ++ ) {][y] = false;

      for// (intcheck yif =distance 0;is y < height; y ++ ) {
 below threshold
      if (blobPoints[x][y] == 1dist < threshold) {
     
    if (y < A.y)
            A.y = y;
          if (y > B.y)
            B.y = y;
          if (x < A.x)
            A.x = x;
          if (x > B.x)
            B.x = x;
        }
      }
    }

    strokeWeight(1);
    noFill();
    rect(A.x, A.y, B.x-A.x, B.y-A.y);
    text("A "+A.x+","+A.y, A.x, A.y);
    text("B "+B.x+","+B.y, B.x, B.y);
  }

  void drawCenterOfMass() {     centerOfMass.set(0.0, 0.0, 0.0);
 // mark pixel 
   for (int x = 0; marks[x][y] <= widthtrue;
 x ++ ) {    total++;

 for (int y = 0; y < height;// yupdate ++point )trackers {
        if (blobPoints[x][y] == 1) {
 < lowestX) lowestX = x;
        if centerOfMass.x +(x > highestX) highestX = x;
        if (y < lowestY) centerOfMass.ylowestY += y;
        }if (y      }
    }
 > highestY) highestY = y;
      }
centerOfMass.x /= blobWeight;  }
  centerOfMass.y}
/=
blobWeight;  // save locations
 noStroke(); topLeft = new  fill(255PVector(lowestX, lowestY);
  bottomRight = new ellipsePVector(centerOfMass.xhighestX, centerOfMass.y, 10, 10highestY);
 
}
}

Weitere Informationen