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.
Page Index Toggle Pages: 1
counting rects on current screen (Read 821 times)
counting rects on current screen
Jun 12th, 2005, 7:13pm
 
i search after a way to count the by motion produced
rects, to write the number of the current ones in an array.
has someone an idea?

here the sketch:

/////motion_tracker///////////////////////////////////////

import processing.opengl.*;

import processing.video.*;

Capture myCapture;


float sensitivity; // sensitivity is a percentage of pixels changed
boolean newFrame;
// switch var for determining when a new frame is received
color[] prevFrame; // vars for holding the current and previous frames

void setup(){
 size(640, 480, OPENGL);

 String s = "IIDC FireWire Video";
 myCapture = new Capture(this, s, width, height, 30);

 sensitivity=0.24;
 newFrame=false;
 stroke(140);
 noFill();

 prevFrame=new color[640*480];
 myCapture.read();


}

public void captureEvent(){
 newFrame=true;
}

void captureEvent(Capture myCapture) {
 myCapture.read();
}



void draw(){
 image(myCapture, 0, 0);

 if(newFrame){
   newFrame=false;
 }

 for(int y=0; y<30-1; y++){ // loop through video in 40x30 grid
   for(int x=0; x<40-1; x++){

     int cxLoc=x*(640/40); // x position on grid
     int cyLoc=y*(480/30); // y position on grid

     if(motionTest(cxLoc,cyLoc,width/40,width/30)==true){
       rect(cxLoc+4,cyLoc+4,(width/40)-2,(width/30)-2); //indicate motion
       stroke(255,0,0);

println(cxLoc);




     }
   }
 }
}



boolean motionTest(int srcX,int srcY,int tw, int th){
 int dc=0; // counter to track number of differences
 color newPixel;  
 for(int y=0;y<th;y++){
   for(int x=0;x<tw;x++){
     int srcPos=((srcY+y)*width)+(srcX+x);
     newPixel = myCapture.pixels[srcPos];
     if(abs(red(newPixel)-red(prevFrame[srcPos]))>25) dc++;
     prevFrame[srcPos]=newPixel; // update prevFrame w/ current pixel clr



   }
 }
 // test to see if number of difference is 'significant'
 if(dc>(sensitivity*(tw*th))){  
   return true;
 }

 else{
   return false;
 }

}


Re: counting rects on current screen
Reply #1 - Aug 11th, 2005, 5:22pm
 
Hi sg_FHwue,

I have added to your code so that (i think) the number of rectangles in each frame is saved into lastTen[9]

lastTen[] keeps track of the total number of rectangles for each of the last 10 frames with number 9 being the most recent frame and number 0 being the oldest. I have commented out your println and added a new println that displays the total number of rectangles in the last 10 frames. So you may need to change that back depending on what you want to do with it.

I hope this helps.

Jay Silver

______________________
// code starts here


import processing.opengl.*;

import processing.video.*;

Capture myCapture;  
int motionCounter; // motion counted within a single frame
int totalMotion; // motion counted within the last 10 frames
int totalAveMotionThreshold; // amount of total allowable motion within last 10 frames
int[] lastTen=new int[10]; // array to hold motion of each of last 10 frames

 
float sensitivity; // sensitivity is a percentage of pixels changed  
boolean newFrame;  
// switch var for determining when a new frame is received  
color[] prevFrame; // vars for holding the current and previous frames  

void setup(){  
 size(640, 480, OPENGL);  
 totalAveMotionThreshold = 30;
 String s = "Logitech QuickCam Pro 4000-WDM";  
 myCapture = new Capture(this, s, width, height, 30);  

 sensitivity=0.24;  
 newFrame=false;  
 stroke(140);  
 noFill();

 prevFrame=new color[640*480];  
 myCapture.read();  
 for(int i=0;i<10;i++)
{
lastTen[i]=lastTen[0];
}

}  

public void captureEvent(){  
 newFrame=true;  
}  

void captureEvent(Capture myCapture) {  
 myCapture.read();  
}  



void draw(){  
 motionCounter=0;
 image(myCapture, 0, 0);  

for(int i=1;i<10;i++)
{
lastTen[i-1]=lastTen[i];
}
 

 if(newFrame){  
   newFrame=false;
   
 }

 for(int y=0; y<30-1; y++){ // loop through video in 40x30 grid  
   for(int x=0; x<40-1; x++){  

int cxLoc=x*(640/40); // x position on grid  
int cyLoc=y*(480/30); // y position on grid  

if(motionTest(cxLoc,cyLoc,width/40,width/30)==true){  
  rect(cxLoc+4,cyLoc+4,(width/40)-2,(width/30)-2); //indicate motion  
  stroke(255,0,0);  
  motionCounter++;

//println(cxLoc);




}  
   }  
 }  
 lastTen[9]=motionCounter;
 totalMotion=0;
  for(int i=0;i<10;i++)
{
totalMotion=totalMotion+lastTen[i];
}
 println(Integer.toString(totalMotion));
 if (totalMotion > totalAveMotionThreshold) // was there more motion in the last 10 frames than the threshold?
 {
    println("totalAveMotionThresholdExceeded **************************************");
 }
}  



boolean motionTest(int srcX,int srcY,int tw, int th){  
 int dc=0; // counter to track number of differences  
 color newPixel;  
 for(int y=0;y<th;y++){  
   for(int x=0;x<tw;x++){  
int srcPos=((srcY+y)*width)+(srcX+x);  
newPixel = myCapture.pixels[srcPos];  
if(abs(red(newPixel)-red(prevFrame[srcPos]))>25) dc++;  
prevFrame[srcPos]=newPixel; // update prevFrame w/ current pixel clr  



   }  
 }  
 // test to see if number of difference is 'significant'  
 if(dc>(sensitivity*(tw*th))){  
   return true;  
 }

 else{  
   return false;  
 }  

}
Page Index Toggle Pages: 1