/* video tracking based on rob hodgin's code (http://www.proce55ing.net/software/tracking/) new features: - colour tracking instead of luminance only - rewritten as class, so multiple instances can be used to track more than one object/colour - optimized performance see comments and example below for intended usage comments & improvements please send to: info@toxi.co.uk, robert@flight404.com */ VTracker[] vTrackers; TrackCursor[] cursors; boolean colour_pick=false; int currTracker=0; void setup() { size(320, 240); noBackground(); ellipseMode(CENTER_DIAMETER); noStroke(); beginVideo(width,height,15); // new vTracker objects to track several instances vTrackers = new VTracker[2]; // first tracker to track white areas with a tolerance of 15 vTrackers[0] = new VTracker(2,0xffffff,15,0.75); // another tracker to track bright red areas with a tolerance of 10 vTrackers[1] = new VTracker(2,0xff0000,10,0.3); // attach independent cursors to the trackers cursors = new TrackCursor[2]; cursors[0] = new TrackCursor(vTrackers[0],10); cursors[1] = new TrackCursor(vTrackers[1],5); } void loop() { vTrackers[0].analyse(true); // only display one copy of the video vTrackers[1].analyse(false); cursors[0].update(); cursors[1].update(); // if mouse has been pressed, use new colour under mouse position for the next tracker if (colour_pick) { vTrackers[currTracker++].trackingNewColour(pixels[mouseY*width+mouseX]); currTracker%=vTrackers.length; colour_pick=false; } } void mousePressed() { colour_pick=true; } void videoEvent(){ // send new frame event to all trackers for(int i=0; i>16&0xff; trackG=c>>8&0xff; trackB=c&0xff; println("tracking new colour: "+trackR+","+trackG+","+trackB); } // parse the current video image and try to find hotspot // needs to be called every frame, but will only fully execute if there's a new video frame present // boolean display is used to disable drawing the video image (important when using several instances) // see above example // note: quality of tracking will improve once the new imaging functions are implemented in P5 // at the moment only single pixels instead of averaged pixelblocks are used for the analysis void analyse(boolean display) { if (newFrame) { int yoffset=0; int col,lum; int xx=0; int yy=0; int r,g,b; int deltaCol; // parse the video pixel buffer in the set (scaled down) resolution for(int y=0; y>16&0xff; g = col>>8&0xff;; b = col&0xff; // compute difference to tracked colour deltaCol=(abs(trackR-r)+abs(trackG-g)+abs(trackB-b))/3 - tolerance; // if within tolerance, add value to find X and Y based sums if (deltaCol<=0){ similaritiesX[x] -= deltaCol; similaritiesY[y] -= deltaCol; } // only display if we were told to... if (display) { fill(r,g,b); rect(xx, yy, res, res); } xx+=res; } yoffset+=rowHeight; } // now on to finding the peaks in horizontal and vertical direction // if no peak is found (or in other words, the tracked colour is not // present in the video image) the peak coodinates will be negative // (your code could check for that, for instance...) int peakXPos=-1,peakYPos=-1; for(int x=0; x