Trying to understand Arrays and Classes in Flob detection
in
Contributed Library Questions
•
3 years ago
I am trying to make the Flob_collide example work for the specific ballcount array I made, but I don't know how to do it.
you will need the Flob library...
here is the sketch , any help would be greatly appreciated, basically I am trying to make the array of rectangles to bounce off , the detection of the blob detection (much like the flob collide example but with the rect array)
import processing.opengl.*;
import processing.video.*;
import s373.flob.*;
Capture video;
Flob flob;
int videores=128;
int fps = 60;
Bola bolas[];
boolean showcamera=true;
boolean om=true,omset=false;
float velmult = 10000.0f;
int vtex=0;
int ballCount=1400;
int ballSize =8;
int ballSpeed=3;
int cokl=350;
float[]xspeed= new float[ballCount];
float[]yspeed= new float[ballCount];
float[]xpos= new float [ballCount];
float[]ypos= new float[ballCount];
float[]wdth= new float[ballCount];
float[]ht= new float[ballCount];
float[]col= new float[cokl];
void setup(){
try {
quicktime.QTSession.open();
}
catch (quicktime.QTException qte) {
qte.printStackTrace();
}
size(640,480,OPENGL);
frameRate(fps);
String[] devices = Capture.list();
println(devices);
video = new Capture(this, videores, videores, devices[6], fps);
flob = new Flob(this, video, width, height);
flob.setMirror(true,false);
flob.setThresh(12);//25);//16);
flob.setFade(25);//2);//10);
flob.setMinNumPixels(10);
flob.setImage( vtex );
bolas = new Bola[10];
for(int i=0;i<bolas.length;i++){
bolas[i] = new Bola();
}
background(0);
smooth();
//initialize values for all the bals
for(int i =0; i<ballCount; i++){
// set varied ball speed
xspeed[i] = random(1,ballSpeed);
yspeed[i] = random(-ballSpeed,ballSpeed);
//set varied ball size
wdth[i] = random(1, ballSize);
ht[i] = wdth[i];
xpos[i] = width/2+random(-width/3,-width/3);
ypos[i] = height/2+random(-height/3,-height/3);
for (int j=0; j<cokl;j++){
col[j]= color(255);
}
}
noStroke();
frameRate(30);
}
void draw(){
if(video.available()) {
if(!omset){
if(om)
flob.setOm(flob.CONTINUOUS_DIFFERENCE);
else
flob.setOm(flob.STATIC_DIFFERENCE);
omset=true;
}
video.read();
flob.calcsimple( flob.binarize(video) );
}
image(flob.getSrcImage(), 0, 0, width, height);
//fill(255,152,255);
rect(0,0,flob.getPresencef()*width,10);
//fill(255,100);
//stroke(255,200);
//get and use the data
// int numblobs = flob.getNumBlobs();
int numtrackedblobs = flob.getNumTrackedBlobs();
fill(255,10);
rectMode(CENTER);
stroke(127,200);
trackedBlob tb;
for(int i = 0; i < numtrackedblobs; i++) {
tb = flob.getTrackedBlob(i);
rect(tb.cx, tb.cy, tb.dimx, tb.dimy );
line(tb.cx, tb.cy, tb.cx + tb.velx * velmult ,tb.cy + tb.vely * velmult );
String txt = ""+tb.id+" "+tb.cx+" "+tb.cy;
}
float cdata[] = new float[5];
for(int i=0;i<bolas.length;i++){
float x = bolas[i].x / (float) width;
float y = bolas[i].y / (float) height;
cdata = flob.imageblobs.postcollidetrackedblobs(x,y,bolas[i].rad/(float)width);
if(cdata[0] > 0) {
bolas[i].toca=true;
bolas[i].vx +=cdata[1]*width*0.015;
bolas[i].vy +=cdata[2]*height*0.015;
}
else {
bolas[i].toca=false;
}
bolas[i].run();
}
if(showcamera){
tint(255,150);
image(flob.videoimg,width-videores,height-videores);
image(flob.videotexbin,width-2*videores,height-videores);
image(flob.videotexmotion,width-3*videores,height-videores);
}
noFill();
noStroke();
smooth();
for(int i=0;i<ballCount; i++){
// fill (i*255/ballCount);
//draw balls
rect(xpos[i],ypos[i],wdth[i]-random(200),ht[i]);
// rect(xpos[i],ypos[i],wdth[i],ht[i]-random(200));
//upgrade possition values
xpos[i]+= xspeed[i];
ypos[i]+=yspeed[i];
//conditionals (detect balls colllision)
if(xpos[i]+wdth[i]/2 >=width|| xpos[i]<=wdth[i]/2){
xspeed[i]*=-1;
fill(i*255/ballCount,0,cokl,100);
color(255);
}
if(xpos[i]+wdth[i]/2>=100){
//noFill();
//xpos[]=cokl;
fill(100,0,i*255/cokl,40);
}
if(ypos[i]+ht[i]/2 >= height || ypos[i]<= ht[i]/2){
yspeed[i]*=-1;
if(xpos[i]+wdth[i]/8>=180){
//noFill();
//xpos[]=cokl;
}
fill(255,0,i*255/cokl,20);
}
}
}
void keyPressed(){
if(key==' ')
flob.setBackground(video);
if(key=='o'){
om^=true;
omset=false;
}
if(key=='i')
showcamera^=true;
if(key=='v'){
vtex = (vtex + 1) % 4;
flob.setVideoTex( vtex );
}
}
class Bola
{
float x,y,vx,vy;
float g = 0.025, rad= random(5,25);
boolean toca=false;
Bola(){
init();
}
void init(){
}
void update(){
// vy+=g;
// vx+=g;
x+=vx;
y+=vy;
if(abs(vx)>3)
vx*=0.9;
if(abs(vy)>3)
vy*=0.9;
if(x<-rad){
x=-rad;
vx = -vx;
}
if(x>width+rad){
x=width+rad;
vx = -vx;
}
if(y<-100){
y=-100;
vy = -vy;
}
if(y>height-rad){
y=height-rad;
vy = -vy;
}
}
void draw(){
if(!toca)
fill(0,255,0);
else
fill(255,0,0);
ellipse(x,y,rad*2,rad*2);
}
void run(){
update();
draw();
}
}
2