Need help finding the location of where to insert a line of code!
in
Contributed Library Questions
•
5 months ago
Hey guys,
I'm trying to write a piece of code that uses the flob library to detect the movement of a person though a webcam. The motion detection captured interacts with random shapes that appear on the screen. When the shapes are "touched" they disappear and re-spawn off-screen as one of 8 random shapes through my changeShape function. I know where it's currently placed the shapes will continuously changeShapes. Can anyone tell me if I'm doing something wrong? Or guide me in the write direction to fixing this code?
Thanks!
I'm trying to write a piece of code that uses the flob library to detect the movement of a person though a webcam. The motion detection captured interacts with random shapes that appear on the screen. When the shapes are "touched" they disappear and re-spawn off-screen as one of 8 random shapes through my changeShape function. I know where it's currently placed the shapes will continuously changeShapes. Can anyone tell me if I'm doing something wrong? Or guide me in the write direction to fixing this code?
Thanks!
- import processing.opengl.*;
import processing.video.*;
import s373.flob.*;
Capture video;
Flob flob;
PImage videoinput;
int videoResolution=256;
int fps = 30;
Object objects[];
boolean om=true, omset=false;
float velmult = 10000.0f;
void setup() {
size(640, 480, OPENGL);
frameRate(fps);
video = new Capture(this, 320, 240);
video.start();
// create one image with the dimensions you want flob to run at
videoinput = createImage(videoResolution, videoResolution, RGB);
// construct flob
flob = new Flob(this, videoResolution, videoResolution, width, height);
//Not exactly sure what this does, but it's needed
flob.setThresh(10);
//Sensitivity - Default 45
flob.setFade(80);
flob.setMinNumPixels(10);
objects = new Object[100];
for (int i=0; i < objects.length; i++) {
objects[i] = new Object();
}
}
void draw() {
background(0);
if (video.available()) {
if (!omset) {
if (om)
flob.setOm(flob.CONTINUOUS_DIFFERENCE);
else
flob.setOm(flob.STATIC_DIFFERENCE);
omset=true;
}
video.read();
//downscale video image to videoinput pimage
videoinput.copy(video, 0, 0, 320, 240, 0, 0, videoResolution, videoResolution);
// Here is defined the method of calculation: calc, calcsimple, ou tracksimple
// Tracksimple is more accurate, but is much more data intensive than calcsimple
// flob.tracksimple( flob.binarize(video) );
flob.calcsimple( flob.binarize(videoinput) );
}
//Displays the actual webcam stream
image(flob.getSrcImage(), 0, 0, width, height);
// collision
float collisionData[] = new float[5];
for (int i=0;i<objects.length;i++) {
float x = objects[i].x / (float) width;
float y = objects[i].y / (float) height;
collisionData = flob.imageblobs.postcollidetrackedblobs(x, y, objects[i].radius/(float)width);
if (collisionData[0] > 0) {
objects[i].collision=true;
objects[i].x = random(width);
objects[i].y = random(-200, -100);
objects[i].xSpeed += collisionData[1] * width * 0.015;
objects[i].ySpeed += collisionData[2] * height * 0.015;
}
else {
objects[i].collision=false;
}
objects[i].run();
}
}
class Object {
float x, y, xSpeed, ySpeed, radius= random(15, 25);
boolean collision=false;
Object() {
init();
}
void init() {
xSpeed = random(-2, 2);
ySpeed = random(5);
x = random(width);
y = random(-100, -50);
}
void update() {
x+=xSpeed;
y+=ySpeed;
if (abs(xSpeed)>3)
xSpeed*=0.9;
if (abs(ySpeed)>3)
ySpeed*=0.9;
if (x<-radius) {
x=-radius;
xSpeed = -xSpeed;
}
if (x>width+radius) {
x=width+radius;
xSpeed = -xSpeed;
}
if (y<-100) {
y=-100;
ySpeed = -ySpeed;
}
if (y>height-radius) {
y=height-radius;
ySpeed = -ySpeed;
}
}
void display() {
if (!collision) {
changeShape(x, y, radius);
//fill(0, 255, 0, 128);
//fill(random(255), random(255), random(255), 128);
}
else {
//fill(255, 0, 0);
}
//ellipse(x, y, radius*2, radius*2);
//quad(x, y-radius, x+radius, y, x, y+radius, x-radius, y);
//stroke(random(255), 255, 255);
//strokeWeight(random(.1, 2));
//noStroke();
//fill(random(255), 0, random(255), 200);
//rect(x-(radius/2), y-(radius/2), radius, radius);
}
void run() {
update();
display();
}
void changeShape(float x, float y, float radius) {
int Shape = int(random(1, 9));
if (Shape == 1) {
fill(255, 0, 0, 128);
rect(x-(radius), y-(radius), radius*2, radius*2);
}
if (Shape == 2) {
fill(random(128, 255), 0, 0, 128);
rect(x-(radius), y-(radius), radius*2, radius*2);
}
if (Shape == 3) {
fill(0, random(128, 255), 0, 128);
triangle(x-(radius-2), y+(radius/2-2), x, y-radius, x+(radius-2), y+(radius/2-2));
}
if (Shape == 4) {
fill(0, 255, 0, 128);
triangle(x-(radius-2), y+(radius/2-2), x, y-radius, x+(radius-2), y+(radius/2-2));
}
if (Shape == 5) {
fill(0, 0, random(128, 255), 128);
ellipse(x, y, radius*2, radius*2);
}
if (Shape == 6) {
fill(0, 0, 255, 128);
ellipse(x, y, radius*2, radius*2);
}
if (Shape == 7) {
fill(255, 255, 0, random(128));
quad(x, y-radius, x+radius, y, x, y+radius, x-radius, y);
}
if (Shape == 8) {
fill(255, 255, 0, random(128));
quad(x, y-radius, x+radius, y, x, y+radius, x-radius, y);
}
}
}
1