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
Object Tracking help needed! (Read 618 times)
Object Tracking help needed!
May 11th, 2007, 8:44pm
 
First of all: i´m new on processing and it´s great!

The Problem:
I have two days left to get my project working, otherwise ..
u do not really want to know..(;))

the plan is to take a live picture from a sony dv camera via firewire. on this live picture u can see a black/or if better a completely white table.
on this table should be some onecolored objects.

the trick:
if the cam recognises motion of these objects on the table, a trail should be seen in the same color as the object.

this tails from the objects i want to beam back again on the table.

i tried many times and still cant get through.
for now, i can track objects with a special color,
in my code red, green, blue.
i also tried blob detection but cant figure out how to use it to get the answer for my problem.

My question: 1)could anybody tell me why the offset isnt really working and if a color isnt in picture it shows rects with these color all over the picture.
2)how can i get a tail for these object positions to see where they have been moved, for example like water effect or
metaballs effect.

i would be very pleased if someone could figure it out.

heres my code:

import processing.video.*;
import processing.opengl.*;

//essential
Capture video;
color trackColorRed, trackColorGreen, trackColorBlue;
PImage img;
int factor;
boolean newBegin = false;
//
float rCurrent = 0.0f;
float gCurrent = 0.0f;
float bCurrent = 0.0f;
float rTracked = 0.0f;
float gTracked = 0.0f;
float bTracked = 0.0f;
float minRed = 300.0f;
float minGreen = 300.0f;
float minBlue = 300.0f;
tracePixel redPix[] = new tracePixel[80*60];
tracePixel greenPix[] = new tracePixel[80*60];
tracePixel bluePix[] = new tracePixel[80*60];

void setup()
{
 size(1024, 768, P3D);
 colorMode(RGB,255,255,255);
 video = new Capture(this,1024, 768, 60);
 factor = 128;
 //using a little image is much faster
 img = new  
PImage(video.width/factor*10,video.height/factor*10);
 trackColorRed = color(255,0,0); // tracking red
 trackColorGreen = color(0,255,0); // tracking green
 trackColorBlue = color(0,0,255); // tracking blue
 noFill();
}
void captureEvent(Capture capture) {
 if(!newBegin){
     capture.read();
     newBegin = true;
 }
}
void draw()
{
 loadPixels();
 if (newBegin)
 {        


image(video,0,0,width,height);


img.copy(video, 0, 0, video.width, video.height, 0, 0, img.width, img.height);
 }
 int rIndex = 0;
 int gIndex = 0;
 int bIndex = 0;
 
 for ( int x = 0; x < img.width; x++) {
   for ( int y = 0; y < img.height; y++) {
       int loc = x + y*img.width;
       //current pixel color
       color curr = img.pixels[loc];
       //get the distance to the origin color
       float distanceRed = getDistance(curr, trackColorRed);
       float distanceGreen = getDistance(curr, trackColorGreen);
       float distanceBlue = getDistance(curr, trackColorBlue);
       //get the minimum distance in the pictures
       if(distanceRed < minRed) minRed = distanceRed;
       if(distanceGreen < minGreen) minGreen = distanceGreen;
       if(distanceBlue < minBlue) minBlue = distanceBlue;
       //create array of same colored pixels in range of an offsetvalue
       //maybe theres the problem
        if(distanceRed <  (minRed+50)){
          redPix[rIndex] = new tracePixel(x*factor/10, y*factor/10, distanceRed);  
          rIndex++;
       }
       if(distanceGreen <  (minGreen+50)){
          greenPix[gIndex] = new tracePixel(x*factor/10, y*factor/10, distanceGreen);  
          gIndex++;
       }
       if(distanceBlue <  (minBlue+50)){
          bluePix[bIndex] = new tracePixel(x*factor/10, y*factor/10, distanceBlue);  
          bIndex++;
       }
     }//end for2
   }//end for1
   //colored objects are now stored in an pix array, now do something with them
   stroke(255,0,0);
   strokeWeight(2);
   //show rects on the stored pixels in pixAr´s
   for(int i = 0; i < rIndex; i++){ if(redPix[i] != null)rect(redPix[i].getX(), redPix[i].getY(), 8,8);}
   stroke(0,255,0);
   for(int i = 0; i < gIndex; i++){ if(greenPix[i] != null)rect(greenPix[i].getX(), greenPix[i].getY(), 8,8);}
   stroke(0,0,255);
   for(int i = 0; i < bIndex; i++){ if(bluePix[i] != null)rect(bluePix[i].getX(), bluePix[i].getY(), 8,8);}
   //redraw...
   newBegin = false;
}

//first i took dist() but i think abs is better
float getDistance(color current, color tracked){
   rCurrent = current >> 16 & 0xFF;
   gCurrent = current >> 8 & 0xFF;
   bCurrent = current & 0xFF; //current
   rTracked = red(tracked);
   gTracked = green(tracked);
   bTracked = blue(tracked); //wanted
   return (abs(rTracked-rCurrent) + abs(gTracked -gCurrent) + abs(bTracked - bCurrent));  
}

//my class for objects with all i want to get stored
class tracePixel{
 
 private int x,y;
 private float diff;
 
 public tracePixel(int x, int y, float distance){
   setX(x);
   setY(y);
   diff = distance;
 }
 void setX(int x){
    this.x = x;
 }
 int getX(){
     return x;
 }
 void setY(int y){
     this.y = y;
 }
 int getY(){
       return y;  
 }
 void setDiff(float d){
      this.diff = d;
 }
 float getDiff(){
      return diff;
 }
}
Page Index Toggle Pages: 1