We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › help with blob detection openCV please
Page Index Toggle Pages: 1
help with blob detection openCV please (Read 3163 times)
help with blob detection openCV please
May 5th, 2009, 4:55am
 
hi all, basically i want to link between particle system with human movement, and i think this is possible by using open cv, i tried to put open cv in my code but doesnt wrok, can anyone help me figure out please? btw this is the code for particle system

Code:

class Particle {
 PVector or;  //declaring PVector object
 PVector loc;
 PVector vel;
 PVector acc;
 float ms;
 float distance;

 Particle(PVector a, PVector v, PVector l, PVector o, float ms_) {
   acc = a;  //defining the pvector object, assigning
   vel = v;
   loc = l;
   ms = ms_;
   or = o;
 }

 void run() {
   update();  //what run the software, update function and render function
   render();  // they are made up function below
 }

 void update() {  // i guess behind the scene code
   vel.add(acc);  //what update does, add acceleration to velocity
   loc.add(vel);  // get the new location when velocity added
   acc = new PVector();  // defining the acceleration itself (mouse click)
 }

 void render() {
   stroke(1, 100, 0); // the result of void update, draw the stroke with
   point(loc.x,loc.y);//  1 px thickness in location x & y
 }

void add_force(PVector force) {
   force.div(ms); //defining what force is, it's under div class not sure about
   vel.add(force); // adding velocity a force
 }  

float getMass() {
   return ms; //looping a program
 }  
 
 PVector getLocation() {
   return loc; //samething here looping in getting new location
 }
 PVector getOrigin() {
   return or; //another loop to identify the origin position
 }
 PVector getVelocity() {
   return vel; // another one to detect the velocity
 }
 void setLocation(PVector l){
   loc = l;    //defining setLocation function to loc = l
 }

 void setAcceleration(PVector a){
   acc = a; //setting the acceleration
 }
}

   

Re: help with blob detection openCV please
Reply #1 - May 5th, 2009, 4:57am
 
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);
             
           }
         }

}



Re: help with blob detection openCV please
Reply #2 - May 8th, 2009, 6:59am
 
can someone help me please? im panick here,since this is my project half done and the due date is getting closer and closer....
Re: help with blob detection openCV please
Reply #3 - May 8th, 2009, 7:53am
 
That's a lot of code not everybody can even run (need to have the library and probably some video device...).
And at a glance, I can't say want is your problem ("doesnt wrok" isn't very explicit...).

BTW, I wonder why you ask that in the Syntax Questions section instead of the Video one... (where perhaps more knowledgeable people might look).
Page Index Toggle Pages: 1