Navigation

Version

Drawing blobs

p5.videomotion example
Part of the p5.videomotion examples · Category: Blobs

Draws each blob's convex hull, centre of mass, id and velocity yourself instead of using debug().

Live sketch. Allow camera access when the browser asks. If nothing appears, see Troubleshooting.

Source code

Save as sketch.js next to an index.html that loads p5.js and p5.videomotion.js (see Installation).

// motion.blobs is an array of tracked blobs, in canvas pixels.
let cam, motion;

function setup() {
  createCanvas(640, 480);
  cam = createCapture(VIDEO, { flipped: true });
  cam.hide();
  motion = createMotion(cam, { minArea: 0.005 });
  colorMode(HSB);
}

function draw() {
  image(cam, 0, 0, width, height);

  for (let b of motion.blobs) {
    let c = color((b.id * 47) % 360, 80, 100);

    // outline
    fill(hue(c), 80, 100, 0.3);
    stroke(c);
    strokeWeight(2);
    beginShape();
    for (let v of b.hull()) vertex(v.x, v.y);
    endShape(CLOSE);

    // centre and velocity
    let v = b.velocity.mult(5);
    line(b.cx, b.cy, b.cx + v.x, b.cy + v.y);
    noStroke();
    fill(c);
    circle(b.cx, b.cy, 10);
    textSize(14);
    text('#' + b.id + '  age ' + b.age, b.x, b.y - 6);
  }
}

Used in this example

blobs, createMotion(), hull, velocity

← Direction inside zones · All examples · Blob trails and events →