wakeupsilver
YaBB Newbies
Offline
Posts: 1
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; } }