Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Really confused on how to go about this (color tracking again)

$
0
0

So, for anyone that saw my last post, im using the same code with a bit more added on. I took out anything that had to do with the xbox kinect because it is getting in the way of my finishing this atm but i do plan on using it with the same code in the future. My program now uses a webcam. So, Following Daniel Shiffman's tutorials I got the color tracking and blob detection working (image). What I am trying to do now is to make my program so that a line is drawn following the blobs path. So, a line is drawn on the screen, and every time the blob moves, more of that line is drawn. I was thinking of using some sort of code similar to the code in a processing online book

void draw(){
line(pmouseX,pmouseY,mouseX,mouseY)
}

to draw the line however i dont know how i would get the blobs previous position. So far under the blob class in its show() function I am putting line(maxx,maxy) to start the line off in the corner of the blob which works (if you are confused I am following the tutorial in the link above) However I do no know what to do with the last coordinates or if I should try to do this in a different way. Any tips?

import processing.video.*;


Capture video;

color trackColor;
float threshold = 25;
float distThresh = 150;
ArrayList<Blob> blobs = new ArrayList<Blob>();

void setup() {
  size(640, 400);
  video = new Capture (this,640,480,30);
  video.start();
  trackColor = color(0,0,255);
}

 void captureEvent(Capture video){
   video.read();
 }


void draw(){
  video.loadPixels();
  image(video, 0, 0);

blobs.clear();


 for (int x=0; x< video.width; x++){
   for (int y =0; y < video.height; y++){
     int loc = x+y * video.width;

     color currentColor = video.pixels[loc];
     float r1 = red(currentColor);
     float g1 = blue(currentColor);
     float b1 = green(currentColor);
     float r2 = red(trackColor);
     float g2 = blue(trackColor);
     float b2 = green(trackColor);

     float d = dist(r1,b1,g1,r2,b2,g2);

     if (d < threshold){

       boolean found = false;
       for (Blob b : blobs){
         if(b.isNear(x,y)){
           b.add (x,y);
           found = true;
           break;

           }
         }

       if (!found){
        Blob b = new Blob(x,y);
        blobs.add(b);


       }
     }
   }
 }
 int i = 0;
 for (Blob b :blobs){
 if (b.size() > 500){
   b.show(i);
   i++;
     }
   }
 }


 float distSq( float x1, float y1, float x2, float y2) {
 float d = (x2-x1)*(x2-y1) + (y2-y1)*(y2-y1);
 return d;
}



void mousePressed(){
 int loc = mouseX + mouseY*video.width;
 trackColor = video.pixels[loc];
}

class Blob {

  float minx;
  float miny;
  float maxx;
  float maxy;

Blob(float x , float y) {

minx = x;
miny = y;
maxx = x;
miny = y;
}

void show(int num){
  stroke(0);
  fill(255,255,255,150);
  strokeWeight(2);
  rectMode(CORNERS);
  rect (minx,miny, maxx,maxy);

  textAlign (CENTER);
  textSize(64);
  fill(255);
  text(num, minx + (maxx-minx)*0.5, miny + (maxy-miny)*0.5);

}

void add(float x, float y){
  minx = min(minx,x);
  miny = min(miny,y);
  maxx = max(maxx,x);
  maxy = max(maxy,y);

}

 float size() {
    return (maxx-minx)*(maxy-miny);
  }

boolean isNear(float x, float y){

  float cx =(minx+maxx)/2;
  float cy = (miny+maxy)/2;


  float d = distSq (cx,cy,x,y);
  if (d< distThresh*distThresh){
   return true;
  } else{
   return false;
  }
}

}

Viewing all articles
Browse latest Browse all 2896

Trending Articles