Motion Tracking

Hello,

I have a project that includes motion tracking, after running the code you can see a blue circle following the moving object. The problem here is that the blue circle is not stable and keeps on moving around the moving object and not just going to its center and keep tracking the moving object's center and this effects the servos that suppose to follow the moving object. here is the code I hope you can help me. _PS: The camera in this project is not going to be mounted on the servos _

import processing.video.*;

Capture video;

PImage prevFrame;

float threshold = 150;
int Mx = 0;
int My = 0;
int ave = 0;

int ballX = width/8;
int ballY = height/8;
int rsp = 5;

void setup() {
  size(320, 240);
  video = new Capture(this, width, height, 30);
  video.start();
  prevFrame = createImage(video.width, video.height, RGB);
}

void draw() {


  if (video.available()) {

    prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
    prevFrame.updatePixels();
    video.read();
  }

  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();

  Mx = 0;
  My = 0;
  ave = 0;


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

      int loc = x + y*video.width;            
      color current = video.pixels[loc];      
      color previous = prevFrame.pixels[loc]; 


      float r1 = red(current); 
      float g1 = green(current); 
      float b1 = blue(current);
      float r2 = red(previous); 
      float g2 = green(previous); 
      float b2 = blue(previous);
      float diff = dist(r1, g1, b1, r2, g2, b2);


      if (diff > threshold) { 
        pixels[loc] = video.pixels[loc];
        Mx += x;
        My += y;
        ave++;
      } 
      else {

        pixels[loc] = video.pixels[loc];
      }
    }
  }
  fill(255);
  rect(0, 0, width, height);
  if (ave != 0) { 
    Mx = Mx/ave;
    My = My/ave;
  }
  if (Mx > ballX + rsp/2 && Mx > 50) {
    ballX+= rsp;
  }
  else if (Mx < ballX - rsp/2 && Mx > 50) {
    ballX-= rsp;
  }
  if (My > ballY + rsp/2 && My > 50) {
    ballY+= rsp;
  }
  else if (My < ballY - rsp/2 && My > 50) {
    ballY-= rsp;
  }

  updatePixels();
  noStroke();
  fill(0, 0, 255);
  ellipse(ballX, ballY, 20, 20);
}

Maybe the video should be in black and white or gray-scale to avoid the too many color brightness moving around?

Answers

  • Answer ✓

    The blue circle is not stable because when u track motion , the problem is processing cannot distinguish between the motion u are concerned with and that which happens randomly. For example if i want my computer to respond to the motion of my finger , the problem with the code is that it also detects the motion of my arm,wrist etc which is irrelevant , so if it were me , i would use some kind of an led to tell the computer to track the motion of either an absolute red or blue or green pixel.

    if ( r2>250 && g2<10 && b2<10 )

    {

    pixels[loc] = video.pixels[loc];

    Mx += x;

    My += y;

    ave++;

    }

    else

    {

    pixels[loc] = video.pixels[loc];

      }
    
  • You are right! it is the other moving stuff that makes it unstable..

    But **making it follow an absolute color will get out of the main idea :( as I am making a **sentry gun that tracks any moving object

    what about making a square around the moving parts? or a circle? and make the blue tracking circle follow/track the center of that square.

    I have no idea how do it though..

    I mean I need to calculate the max x and y or the moving object and make a square of that then make the blue circle track the center of the square. then it will be stable as it will only follow a dot(center of square)

    I am new to processing so I do not know much about it ^^

    any idea's?

  • edited April 2014

    use blobscanner library to find blobs in the video, once ur done with that, u can choose the weight of the moving parts to single out which ones are relevant,and then using drawBlobcontour() method u can precisely draw the outline of that particular target

    import blobscanner.*;

    import processing.video.*;

    detector b;

    Capture v;

    void setup()

    {

    size(640,480);

    v=new Capture(this,320,240);

    v.start();

    }

    void draw()

    {

    if(v.available())

    {

    v.read();

    }

    int u=2;

    v.filter(THRESHOLD);

    b=new detector(this,255);

    b.findblobs(v);

    b.loadblobFeatures();

    b.drawcontours(color(0,0,255),2);

    }

  • edited April 2014

    hmmm I got what you mean and I have looked in to this library, it seems to be what I am looking for, so I am looking into examples right now which I could not get good examples that could explain to me this library.

    PS: your code says "The function findblolbs(capture) does not exist" although it is defined as "capture v;"

    many examples about this library are online but no one is explaining or posting the code.

    and the library website is not so helpful

    maybe you could give me another example please? or teach me whats wrong with the code you posted.

    Thank you

  • sorry should have been, b.findBlobs(v.pixels,v.width,v.height);

  • the code still not working! :( if I get it to work I might figure how this thing work and it would help me a lot in my project..

    "The function loadblobFeatures()does not exist" This is what comes up I did include the library so..

  • it should have been loadBlobsFeatures(), sorry

  • Also the last one should have been drawContours() ^_^

    All I got is this :(

    Untitled

  • edited April 2014

    wrong post

  • Answer ✓

    the code was just an example to detect the blobs , u'll have to yet assign some function once the blobs have been detected

  • oh =D I think I need to learn more about blobscanner

    But thank you any way ^^

Sign In or Register to comment.