Hello,
I am a new user of Processing and I am trying to create a sketch whereby a series of agents follow a point that is the average location of motion (as per Daniel Shiffman Learning Processing Example 16-7).
I am attempting to combine this logic with Genware Tutorial 16, where a Vec3D follows the mouse, by replacing the mouseLoc with the average location of motion.
Is this possible? Or obvious? or a better way to do this. I think I am missing something behind the logic here...
Any help appreciated.
Sketch below.
Thanks!
////////////////////////////////////////// Follow Location of Motion.
import toxi.geom.*;
import processing.video.*;
Capture video;
PImage prevFrame;
float threshold = 30;
Agent a;
void setup() {
size(600, 600);
smooth();
Vec3D startLoc = new Vec3D (width/2, height/2, 0);
a = new Agent(startLoc);
// Using the default capture device
video = new Capture(this, width, height, 15);
// Create an empty image the same size as the video
prevFrame = createImage(video.width, video.height, RGB);
}
void draw() {
background(0);
a.run();
// image(video, 0, 0); // VIDEO IMAGE DOES NOT NEED TO BE DISPLAYED
// Capture video
if (video.available()) {
// Save previous frame for motion detection!!
prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
// These are the variables we'll need to find the average X and Y
float sumX = 0;
float sumY = 0;
int motionCount = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
// What is the current color
color current = video.pixels[x+y*video.width];
// What is the previous color
color previous = prevFrame.pixels[x+y*video.width];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// Motion for an individual pixel is the difference between the previous color and current color.
float diff = dist(r1, g1, b1, r2, g2, b2);
// If it's a motion pixel add up the x's and the y's
if (diff > threshold) {
sumX += x;
sumY += y;
motionCount++;
}
}
}
// average location is total location divided by the number of motion pixels.
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
// Draw a circle based on average motion
//smooth();
//noStroke();
//fill(#101010);
ellipse(avgX, avgY, 5, 5);
}
//////////// end
/////////////////// Agent
float sumX = 0;
float sumY = 0;
int motionCount = 0;
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
class Agent {
Vec3D loc;
Vec3D vel = new Vec3D(0, 0, 0);
Vec3D acc = new Vec3D(0, 0, 0);
Vec3D target = new Vec3D (0, 0, 0);
Agent(Vec3D _loc) {
loc = _loc;
}
void run() {
display();
followMotion();
update();
}
void display() {
stroke(0);
fill(255);
ellipse(loc.x, loc.y, 20, 20);
}
void followMotion() {
Vec3D target = new Vec3D(avgX, avgY, 0);
Vec3D dif = target.sub(loc);
float distance = dif.magnitude();
dif.normalize();
dif.scaleSelf(distance*100);
acc.addSelf(dif);
}
void update() {
vel.addSelf(acc);
//vel.limit(300);
loc.addSelf(vel);
acc = new Vec3D(0, 0, 0);
}
}
////////////end
I am a new user of Processing and I am trying to create a sketch whereby a series of agents follow a point that is the average location of motion (as per Daniel Shiffman Learning Processing Example 16-7).
I am attempting to combine this logic with Genware Tutorial 16, where a Vec3D follows the mouse, by replacing the mouseLoc with the average location of motion.
Is this possible? Or obvious? or a better way to do this. I think I am missing something behind the logic here...
Any help appreciated.
Sketch below.
Thanks!
////////////////////////////////////////// Follow Location of Motion.
import toxi.geom.*;
import processing.video.*;
Capture video;
PImage prevFrame;
float threshold = 30;
Agent a;
void setup() {
size(600, 600);
smooth();
Vec3D startLoc = new Vec3D (width/2, height/2, 0);
a = new Agent(startLoc);
// Using the default capture device
video = new Capture(this, width, height, 15);
// Create an empty image the same size as the video
prevFrame = createImage(video.width, video.height, RGB);
}
void draw() {
background(0);
a.run();
// image(video, 0, 0); // VIDEO IMAGE DOES NOT NEED TO BE DISPLAYED
// Capture video
if (video.available()) {
// Save previous frame for motion detection!!
prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
prevFrame.updatePixels();
video.read();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
// These are the variables we'll need to find the average X and Y
float sumX = 0;
float sumY = 0;
int motionCount = 0;
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
// What is the current color
color current = video.pixels[x+y*video.width];
// What is the previous color
color previous = prevFrame.pixels[x+y*video.width];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// Motion for an individual pixel is the difference between the previous color and current color.
float diff = dist(r1, g1, b1, r2, g2, b2);
// If it's a motion pixel add up the x's and the y's
if (diff > threshold) {
sumX += x;
sumY += y;
motionCount++;
}
}
}
// average location is total location divided by the number of motion pixels.
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
// Draw a circle based on average motion
//smooth();
//noStroke();
//fill(#101010);
ellipse(avgX, avgY, 5, 5);
}
//////////// end
/////////////////// Agent
float sumX = 0;
float sumY = 0;
int motionCount = 0;
float avgX = sumX / motionCount;
float avgY = sumY / motionCount;
class Agent {
Vec3D loc;
Vec3D vel = new Vec3D(0, 0, 0);
Vec3D acc = new Vec3D(0, 0, 0);
Vec3D target = new Vec3D (0, 0, 0);
Agent(Vec3D _loc) {
loc = _loc;
}
void run() {
display();
followMotion();
update();
}
void display() {
stroke(0);
fill(255);
ellipse(loc.x, loc.y, 20, 20);
}
void followMotion() {
Vec3D target = new Vec3D(avgX, avgY, 0);
Vec3D dif = target.sub(loc);
float distance = dif.magnitude();
dif.normalize();
dif.scaleSelf(distance*100);
acc.addSelf(dif);
}
void update() {
vel.addSelf(acc);
//vel.limit(300);
loc.addSelf(vel);
acc = new Vec3D(0, 0, 0);
}
}
////////////end
1