Versions Compared

Key

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

...

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 draw() {
  if (video.available()) {
    video.read();
  }
  
  video.loadPixels();
  
  // for black and white image
  // video.filter(THRESHOLD,0.1);
  image(videoarray 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() {
  size(640, 480);

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

  // set initial track color to red
  trackColor = color(255, 0, 0);

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

void mousePresseddraw() {
  int// locread =video mouseXframe + mouseY*video.width;if available
  blobColor =if (video.available()) {
    video.pixels[loc]read();
}  class}
BlobDetector
{  // int blobPoints[][];
draw video image
 int blobWeight =image(video, 0, 0);

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

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

  //  blobWeight = 0;

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

       color currentColor = video.pixels[loc];
// make pixel red if marked
      if (marks[x][y]) {
        PVector currColorVecpixels[loc] = new PVector(red(currentColor), green(currentColor), blue(currentColor))color(255, 0, 0);
      }
 PVector trackColorVec = new PVector(red(blobColor), green(blobColor), blue(blobColor));}
  }

  // set canvas  pixels
float diff = currColorVec.distupdatePixels(trackColorVec);

  // draw bounding box
  if stroke(diff255, <0, threshold0);
{  noFill();
  rect(topLeft.x, topLeft.y,     blobPoints[x][y] = 1;
          blobWeight++;
        } else {
          blobPoints[x][y] = 0;
        }
      }
    }
    
    text(blobWeight, 20, 20);
  }

  void drawBlob() {
    if (blobWeight > 200) {
      for (int x = 0; x < width; x ++ ) {
     bottomRight.x-topLeft.x, bottomRight.y-topLeft.y);
}

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

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

  // prepare point trackers
  int lowestX = width;
  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      stroke(255, 0, 0)+ y*width;

      // 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));

   void drawBoundingBox() { // get distance to PVectortrack Acolor
= new PVector(width, height);    float PVector Bdist = new PVector(0, 0currColorVec.dist(trackColorVec);
    for (int x
= 0; x < width; x ++// )reset {mark
      for (int ymarks[x][y] = 0false;
y
< height; y ++ ) {        // check if (blobPoints[x][y] == 1) {
   distance is below threshold
      if (ydist < A.ythreshold)             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;
// save locations
  topLeft centerOfMass.y /= blobWeight;

    noStroke(new PVector(lowestX, lowestY);
  bottomRight = fill(255);
    ellipse(centerOfMass.x, centerOfMass.y, 10, 10);
 new PVector(highestX, highestY);
}
}

Weitere Informationen