ndmccormack
YaBB Newbies
Offline
Posts: 13
Slow sketch needs tune up!
Nov 8th , 2005, 1:36pm
I'm creating a sketch to analyze realtime video and see if there has been any movement. The sketch then saves any frames which are significantly different from the frame before it. The sketch waits for 50 frames while the camera initialises. It then duplicates the two frames it needs to compare, and starts comparing & duplicating from there on in. To compare the sketch creates a pixel grid (using the gridVertStep/gridHorzStep variables) and compares colour difference, on these pixel, between the current and previous frames. This all works well, but runs relatively slowly, I'm on a Dual 1.42Ghz G4 mac, not the fastest machine I'd admit, but even running this at 320x240 the average framerate is about 8fps. not exactly inspiring. Any ideas on how I can speed this up? Cheers Niall //INIT// // procesing libraries import processing.video.*; import processing.opengl.*; // variables + setup Capture myCapture; int myFrameRate = 25; boolean initialPlay = true; PImage frameA; PImage frameB; // grid setup int gridVertStep = 32; int gridHorzStep = 24; int gridVert = 1; int gridHorz = 1; int gridLookUp = gridVert + gridHorz; //difference detect float gridCount = 0; float gridVariance = 30; float varianceCount = 0; float percentage; //saving bits int thisFrameCount = 0; String name; //SETUP// void setup () { size(320,240); background(128); this.framerate(myFrameRate); String s = "IIDC FireWire Video"; myCapture = new Capture(this, s, width, height, 25); } //LOOP// void draw () { if (frameCount == 50) { myCapture.read(); image (myCapture, 0, 0); initDuplicate(); } else if (frameCount > 50) { detectChange1(); percentage = (100 * (varianceCount/gridCount)); println("percentage: " + percentage); if (percentage > 25) { saveImage(); } varianceCount = 0; myCapture.read(); image (myCapture, 0, 0); duplicate(); } else { myCapture.read(); image (myCapture, 0, 0); } println("framerate: " + framerate); } //FUNCTIONS// void initDuplicate() { frameA = get(); frameB = get(); } void detectChange1() { // dotGrid //create grid gridCount = 0; for (gridVert = 0; gridVert < myCapture.height; gridVert += gridVertStep) { for (gridHorz = 0; gridHorz < myCapture.width; gridHorz += gridHorzStep) { gridCount ++; // get colour of grid then check to see if its different gridLookUp = gridHorz+gridVert; color c = frameA.pixels[gridLookUp]; color d = frameB.pixels[gridLookUp]; if ((red(c)+green(c)+green(c)) - (red(d)+green(d)+green(d)) > gridVariance || (red(c)+green(c)+green(c)) - (red(d)+green(d)+green(d)) < -1 * gridVariance) { varianceCount ++; } } } } void duplicate() { frameA = frameB.get(); frameB = get(); } void saveImage() { if (thisFrameCount < 10) { name = "image-000" + thisFrameCount + ".tga"; } else if (thisFrameCount < 100) { name = "image-00" + thisFrameCount + ".tga"; } else if (thisFrameCount < 1000) { name = "image-0" + thisFrameCount + ".tga"; } save(name); thisFrameCount += 1; }