Search code examples
colorsprocessingwebcamtracking

I want to track 2 colours, but only record the movement of those two colours and hide the video feed


For context: I am going to analyze the breathing movement of parents during kangaroo mother care and I wish to respect their privacy by not recording them, but only the movement of stickers I placed on their chest and stomach.

So far, I'm able to track 2 colours based on webcam input through the code below. However, I would like to record only the tracked colours instead of the webcam feed as to preserve the privacy of the parent.

Does anybody know how to add a background colour, whilst still being able to track colour?

import processing.video.*;


Capture video; 
final int TOLERANCE = 20;

float XRc = 0;// XY coordinate of the center of the first target
float YRc = 0;
float XRh = 0;// XY coordinate of the center of the second target
float YRh = 0;

int ii=0; //Mouse click counter

color trackColor; //The first color is the center of the robot 
color trackColor2; //The second color is the head of the robot

void setup() {

size(640,480);
video = new Capture(this,640,480);
video.start();

trackColor = color(255,0,0);
trackColor2 = color(255,0,0);

smooth();
}

void draw() {
background(0);
if (video.available()) {
    video.read();
}

video.loadPixels();
image(video,0,0);

  float r2 = red(trackColor);
  float g2 = green(trackColor);
  float b2 = blue(trackColor);

  float r3 = red(trackColor2);
  float g3 = green(trackColor2);
  float b3 = blue(trackColor2);




  int somme_x = 0, somme_y = 0; 
  int compteur = 0;

  int somme_x2 = 0, somme_y2 = 0; 
  int compteur2 = 0;



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

      int currentLoc = x + y*video.width;
      color currentColor = video.pixels[currentLoc];

      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);


      if(dist(r1,g1,b1,r2,g2,b2) < TOLERANCE) {
         somme_x += x;
         somme_y += y;
        compteur++;
      }

      else if(compteur > 0) { 
        XRc = somme_x / compteur;
        YRc = somme_y / compteur;
      }


      if(dist(r1,g1,b1,r3,g3,b3) < TOLERANCE) {
         somme_x2 += x;
         somme_y2 += y;
        compteur2++;
      }

      else if(compteur2 > 0) { 
        XRh = somme_x2 / compteur2;
        YRh = somme_y2 / compteur2;
      }



  }
  }




  if(XRc != 0 || YRc != 0) { // Draw a circle at the first target
    fill(trackColor);
    strokeWeight(0.05);
    stroke(0);
    ellipse(XRc,YRc,20,20);
  }


  if(XRh != 0 || YRh != 0) {// Draw a circle at the second target
    fill(trackColor2);
    strokeWeight(0.05);
    stroke(0);
    ellipse(XRh,YRh,20,20);
  }

}
void mousePressed() {
  if (mousePressed && (mouseButton == RIGHT)) { // Save color where the mouse is clicked in trackColor variable  
  if(ii==0){  

    if (mouseY>480){mouseY=0;mouseX=0;}
  int loc = mouseX + mouseY*video.width;

  trackColor = video.pixels[loc];
ii=1;  
}
  else if(ii==1){  
    if (mouseY>480){mouseY=0;mouseX=0;}
  int loc2 = mouseX + mouseY*video.width;
  trackColor2 = video.pixels[loc2];
ii=2;  

}


  }
  }

Solution

  • Try adding the background(0); right before you draw the first circle. It should cover the video and you can draw the circles on top of it.

    Regards Jose