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

edited December 2016 in Library Questions

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 (). 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; 
  }
}

}

Answers

  • i dont know how i would get the blobs previous position

    Your blobs currently track their location with variables at the top of the blob class, like this:

    float minx;
    float miny;
    float maxx;
    float maxy
    

    Perhaps you should add variables like pMouseX to each blob -- pblobx, pbloby?

    Then update your previous values at the end of Blob.show() -- that saves them for next time. Assuming, of course, that you are saving the same blob from frame to frame and updating its location...!

  • oh ok thank you! That doesnt sound like a bad idea, Im just gonna be a bit confused on how I go about adding variables like pblobx and ploby. Like how i would tell the program that those are the bobs previous positions.

  • ok, two things.

    stop starting new threads for continuation questions. people won't know which question to answer.

    and please give your threads descriptive names.

    "Really confused on how to go about this" tells us nothing about the problem

    "Idk what is wrong" again, adds nothing

    "Having trouble with some code", well, yes, hence the question

    "So stumped..." stumped by what?

    "Help with code error"

    "Having trouble understanding code."

    ALL useless

  • Sorry, should I have posted this in the last thread as well?

  • "So, for anyone that saw my last post, im using the same code with a bit more added on."

    this suggests yes. it's a continuation of the previous problem, you say so yourself.

    but instead you start a new one and i have to go and close the old one and link it here so that people don't waste their time replying there.

  • Really sorry for the hassle. Ill stay more organized.

  • So, im still trying to figure out how to implement variables like pblobx and pbloby. It is just hard to think about how I would use them to get the previous positions of the blobs. Is there a way that you guys usually do this stuff when you have to get the previous position of an object?

  • edited December 2016

    I don't actually have particular experience Kinect tracking. However, you should be able to think through general approaches and then try implementing them.

    Each frame your sketch finds some blobs. How can you know if one is "the same" blob or a "different" blob? For example if I'm holding two red balls in my hands, and checking 1 frame per second, if I switch balls, would the blob detector know? No, it wouldn't know. It isn't "recognizing" separate objects.

    This implies that the answer to "what is the new location / old location of this blob" is actually: What is the previous blob that is closest to my new position?

    So, for example, you could keep a list of previous blob locations (x,y). Then, when you draw new blobs, you pair them with old blobs by closest distance, and draw a line to the old position.

    (Be careful to remove each blob from the old list, or you will sometimes pair both new blobs to the same old blob, which doesn't make sense.)

Sign In or Register to comment.