sam_mcelhinney
YaBB Newbies
Offline
Posts: 34
Re: selective motion tracking with Jmyron
Reply #1 - Mar 9th , 2009, 1:52pm
all, i've moved on a bit and pretty much achieved the above; now i have a new (but related) problem... a color is chosen to be tracked using the mouse; the bounding boxes drawn are limited to 1 main one around that color. i want to track more than one color at a time, so i have made the tracking code into a class in an array list; new items are called to this on each click of the mouse... this should allow me to pick and track individual colors one by one BUT the jmyron box list still refers back to the m. object created at the upper level, so it will only track one color at a time that's about as clear as mud; but the code is below (it is two tabs...) Free beer for anyone who can help out... S ////TAB 1 import JMyron.*; int p1; int p2; JMyron m;//a camera object ArrayList trackings; void setup() { size(640,480); m = new JMyron();//make a new instance of the object m.start(width,height);//start a capture trackings = new ArrayList(); noFill(); smooth(); } void draw() { m.update();//update the camera view drawCamera();//draw the camera to the screen trackDraw(); } void drawCamera(){ int[] img = m.image(); //get the normal image of the camera loadPixels(); for(int i=0;i<width*height;i++){ //loop through all the pixels pixels[i] = img[i]; //draw each pixel to the screen } updatePixels(); } void trackDraw(){ stroke(200); strokeWeight(1); for (int i = trackings.size()-1; i >= 0; i--) { Tracking tracking = (Tracking) trackings.get(i); tracking.draw(); }} void mousePressed() { //create new tracking with mouse X,Y, position p1 = mouseX; p2 = mouseY; trackings.add(new Tracking(p1,p2)); } ////TAB 2 class Tracking { color chooseColor; int r = 255; int g = 255; int b = 255; int s1; int s2; int or1; int or2; float prox; int loops; int num = 2000; int[] xpos = new int[num]; int[] ypos = new int[num]; Tracking(int a, int b){ or1= a; or2 = b; s1 = a; s2 = b; } void draw() { ellipse(or1,or2,50,50); if (loops == 0){ get_color(); loops++; } m.trackColor(r,g,b,80);//R, G, B, and range of similarity m.minDensity(50); //minimum pixels in the glob required to result in a box int[][] b = m.globBoxes();//get the box points int[][] c = m.globCenters();//get the centre points //draw the boxes stroke(255,0,0); for(int i=0;i<b.length;i++){ prox = abs(dist(s1,s2, c[i][0], c[i][1])); if (prox<50){ rect( b[0][0] , b[0][1] , b[0][2] , b[0][3] ); update( c[0][0] , c[0][1]); s1=c[0][0]; s2=c[0][1]; }} //trail(); } void get_color(){ int loc = s1 + s2*width; chooseColor = pixels[loc]; r = int(red(chooseColor)); g = int(green(chooseColor)); b = int(blue(chooseColor)); } void update(int newX, int newY) { for(int i = 0; i < xpos.length; i++){ if (xpos[i] == 0 && ypos[i] == 0){ xpos[i]=s1; ypos[i]=s2;}} for(int i = 1; i < xpos.length; i++){ xpos[i-1] = xpos[i]; ypos[i-1] = ypos[i]; } xpos[xpos.length-1] = newX; ypos[ypos.length-1] = newY; } void trail() { strokeWeight(2.5); beginShape(); noFill(); for (int i = 0; i < xpos.length; i ++ ) { stroke(255,220); if(i%2 == 0){ curveVertex(xpos[i],ypos[i]); //line(xpos[i],ypos[i],xpos[i+1],ypos[i+1]); }} endShape(); } }