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
selective motion tracking with Jmyron (Read 1626 times)
selective motion tracking with Jmyron
Mar 9th, 2009, 10:46am
 
hi all

i am adapting the example 'myron bounding boxes' to make a selective motion tracking system.

i've already adapted it so that on each click of the mouse a new tracking color is adopted and boxes drawn around it.

now i want to create just one instance of tracking box; ie the box that begins immediately around where the mouse clicked.

does anyone know how to do this? pointers would be appreciated

ta
sam


here is the code...



import JMyron.*;

int r = 255;
int g = 255;
int b = 255;

color chooseColor;

JMyron m;//a camera object

void setup(){
 size(320,240);
 m = new JMyron();//make a new instance of the object
 m.start(width,height);//start a capture at 320x240
 
 
 noFill();
}

void draw(){
 m.trackColor(r,g,b,50);//R, G, B, and range of similarity
 m.minDensity(10); //minimum pixels in the glob required to result in a box
 
 m.update();//update the camera view
 drawCamera();//draw the camera to the screen
 int[][] b = m.globBoxes();//get the center points

 //draw the boxes
 stroke(255,0,0);
 for(int i=0;i<b.length;i++){
   rect( b[i][0] , b[i][1] , b[i][2] , b[i][3] );
 }

}

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 mousePressed(){
 int loc = mouseX + mouseY*width;
 chooseColor = pixels[loc];
 r = int(red(chooseColor));
 g = int(green(chooseColor));
 b = int(blue(chooseColor));
}


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();
 }
 
}
Re: selective motion tracking with Jmyron
Reply #2 - Mar 14th, 2009, 10:52am
 
nice conversation i'm having with myself, but anyway, in case anyone is following it:

i've got to the point where i'm calling different instances of jmyron camera object up through an array list on each click of the mouse; the tracking color for that object is defined by the pixel the mouse is clicked on.

theoretically this should work... but it seems that jmyron can only 'start' the camera once - the second mouse click (which triggers the second object 'start') appears to crash everything... i get this message:

myron_ezcam_run() failed at cam->initCamera()
Myron: myron_ezcam_run() failed

am i right in saying that jmyron can only start one feed from the camera? is there a cunning way round this?

Be great to know if anyone knows...

S






code follows






//TAB 1

import JMyron.*;

int p1;
int p2;
 
ArrayList trackings;

void setup() {
 size(640,480);
 trackings = new ArrayList();
 noFill();
 smooth();
 }

void draw() {
 trackDraw();
}

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;
 for (int i = trackings.size()-1; i >= 0; i--) {
   Tracking tracking = (Tracking) trackings.get(i);
   tracking.m.stop();
   }
 trackings.add(new Tracking(p1,p2));
 for (int i = trackings.size()-1; i >= 0; i--) {
   Tracking tracking = (Tracking) trackings.get(i);
   tracking.m = new JMyron();//make a new instance of the object
   tracking.m.start(width,height);
   }}


//TAB 2


class Tracking {
 
 
 JMyron m;//a camera object

 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){
   initialise();
   }
   
 m.update();//update the camera view
 drawCamera();//draw the camera to the screen
 
 if (loops == 0){
   get_color();
   loops++;
   }
   
 m.trackColor(r,g,b,100);//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<100){
       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 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 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();
 }
 
void initialise(){
 
}



}
Re: selective motion tracking with Jmyron
Reply #3 - Mar 16th, 2009, 5:45am
 
I am attempting something similar. I am taking the Windows XP desktop as screen activity. Playing a videogame where the characters are set against a solid colour background (think greenscreen or bluescreen) and as they move I replace the solid colour background with a streaming video or photos.

The tracking comes into place where I want to track a specific pixel or pixel(s).

I saw your post above and I will give the code a try and post with my results.
Page Index Toggle Pages: 1