and this is the code itself that i'm trying to figure that out
Code:
import hypermedia.video.*;
OpenCV opencv;
Capture cameraFrame;
PImage previousFrame;
PImage currentFrame;
PImage differenceFrame;
PImage displayFrame;
MovieMaker mm;
color[][] clre;
PVector[][] posizioni;
PVector origin;
ArrayList particles = new ArrayList();
int numParticles = 10000;
int z = 0;
void setup()
{
size(640, 480);
colorMode(HSB, 360, 100, 100, 100);
PFont font = createFont("sansSerif", 12);
textFont(font, 24);
clre = new color[width][height/2];
posizioni = new PVector[width][height/2];
//mm = new MovieMaker(this, width, height, "drawing.mov", 30, MovieMaker.VIDEO, MovieMaker.LOSSLESS);
cameraFrame = new Capture(this, width, height,30);
previousFrame = new PImage(width, height);
currentFrame = new PImage(width, height);
differenceFrame = new PImage(width, height);
displayFrame = new PImage(width, height);
}
void draw()
{
opencv.read(); // grab frame from camera
opencv.absDiff(); // make the difference between the current image and the image in memory
opencv.remember(); //remember the current image into memory
opencv.convert(OpenCV.GRAY); //convert the image to grayscale
opencv.threshold(80); //apply a threshold filers
image( opencv.image(), 0, 0 ); // display the result
//get all the blobs and store them in the array. Value are blobs(minArea, maxArea, maxNumBlobs, findHoles);
Blob[] blobs = opencv.blobs( 50, width*height/3, 20, false );
//count throught the array
for( int i=0; i<blobs.length; i++ ) {
//get the rectange shape and the centroids (centres)
Rectangle bounding_rect = blobs[i].rectangle;
Point centroid = blobs[i].centroid;
// display rectangles
noFill();
// A shortcut for writing an if and else structure.
//If the condition evaluates to true, the first value is
//evaluated and returned. If the condition evaluates to
//false, the second is evaluated and returned.
stroke( blobs[i].isHole ? 128 : 64 );
rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
// display centroids (centres)
stroke(0,0,255);
line( centroid.x-5, centroid.y, centroid.x+5, centroid.y );
line( centroid.x, centroid.y-5, centroid.x, centroid.y+5 );
}
//this code below is the original code
// background(1, 0, 100,255); // set the background to white
textAlign(CENTER);
fill(1, 100, 100, 1); // color of the particle, particle fill maybe
text("Human,"+'\n'+"We are just no more than dust." , width/2, 50);
for(int i=0; i<width; i++) {
for(int j = 0; j<height/2; j++) {
clre[i][j] = get(i,j);
if(clre[i][j] != -1){
if(z < numParticles){
posizioni[i][j] = new PVector(i,j,0);
origin = new PVector(posizioni[i][j].x, 130+posizioni[i][j].y, 0);
PVector a = new PVector();
PVector v = new PVector();
PVector l = new PVector(random(width), random(height) , 0);
particles.add(new Particle(a,v,l, origin, random(0.05f, 2.0f)));
z++;
}
}
}
}
for (int h = 0; h<particles.size()-1; h++){
Particle prt = (Particle) particles.get(h);
prt.run();
PVector actualVel = prt.getVelocity();
PVector attrito = PVector.mult(actualVel,-0.05);
prt.add_force(attrito);
//need to change code below to display the text without pressing a, as default
// if(keyPressed) {
// if (key == 'a' || key == 'A')
// {
PVector origLoc = prt.getOrigin();
PVector diff = PVector.sub(origLoc,prt.getLocation());
diff.normalize();
float factor = 0.5f;
diff.mult(factor);
prt.setAcceleration(diff);
}
/*
if (key == ' ' || key == ' ')
{
mm.finish();
}
*/
}
//need to change code below to map into camera , movement detection
// if(mousePressed) {
//instead of mousePressed
//check to see if on black or white
//prt.getLocation (work out how to get the x and y point
//get the colour of the pixel at that location
//if on white don't move
//if on black move
//x is 20 and y is 20
//int prtx = the location of the particle int()
//int prty = the location of the particle
PVector prtPos = prt.getLocation();
color prtColor = displayFrame.get(opencv.absDiff);
//red(), green(), blue
//if (prtRed >=253 && prtGreen >=253 && prtBlue > 253
//pixel must be white! so don't move
//else
//pixel is black
//
PVector mouseLoc = new PVector(mouseX, mouseY, 0);
PVector diff = opencv.absDiff;
diff.normalize();
float factor = 1.0f;
diff.mult(factor);
prt.setAcceleration(diff);
}
}
}