Having trouble tracking color key

Hi,

I am fairly new to processing, I have been working in it for a little over 2 months for a class that I am taking. To cut straight to the chase, I am trying to track a very bright green color, essentially as a color key. I have set up the parameters to find the distance between each pixel in the array, as captured by the camera, and the green color and eventually select the greenest green and follow it. Here’s my code:

import processing.video.*;

Capture video;

//begin by declaring the main parameters for storing the greenest location
//greenestX and greenestY will continually scan for the greenest pixel while
//colorA will weight the color information of that pixel against an ideal green

int greenestX;
int greenestY;
int pixelTrack;
color colorA;
color pixelColor;
float colorDistance;


//setup will grab the camera, initiate the footage, and declare the base values
//for our coordinates and the our greenest value

//color a will be the green that we define as "true green"
//every other color (color b) will be weighed by how close it is to that color
void setup(){
  
  size(640,480);
  
  String[] cameras = Capture.list();
  
  if(cameras == null) {
    println("Failed to retrieve the list of available cameras, will try the default...");
    video = new Capture(this, 640, 480);
  } 
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    printArray(cameras);


    video = new Capture(this, cameras[4]);
    video.start();
  }
    noStroke();
    smooth();

    greenestX = 0;
    greenestY = 0;
    pixelTrack = 0;
    pixelColor = color(0,0,0);
    colorA = color(58, 255, 0);
  }
  
  
  void draw(){
    
    //if the camera is available, display it on the screen
    
    if (video.available()){
      video.read();
      
      image(video,0,0,width,height);
      
      //generate an array of pixels based off of each frame of the video
      video.loadPixels();
      
      //index will browse through the array of video pixels while x and y
      //store the coordinates of the pixel being currently browsed
      int index = 0;
      for(int y = 0; y<video.height; y++){
        for(int x = 0; x<video.width; x++){
          
          pixelTrack = video.pixels[index];
          pixelColor = color(pixelTrack);
          //pixelColor = pixels[y*width+x];
          
          //println(red(pixelColor), green(pixelColor), blue(pixelColor));
          
          //here's where it gets tricky. Each frame will be analyzed for the pixel color values
          //we need to generate a formula to determine which pixel is nearest to colorA
          //which is r58,g255,b0
         
         final int rA = 58;
         final int gA = 255;
         final int bA = 0;
         
         final float colorThreshold = 100;
         
         float rPixel = red(pixelColor);
         float gPixel = green(pixelColor);
         float bPixel = blue(pixelColor);
         
         float bigDif = abs(dist(rA,gA,bA,rPixel,gPixel,bPixel));
         
         println(bigDif);
         
         if (bigDif < colorThreshold){
           println("the colors are pretty close");
         }
            index++;
          
        }
      }
    }
  }

I have bigger plans for this sketch eventually but for now all I want to do is follow that damn green. I have the println giving me a unit that describes the distance between the analyzed color and the green that it’s searching for, but I cant get it to give me less than 150 units apart (or an average of 50 values across each r, g, and b).

Can you provide an example screenshot of what your webcam sees? Is this a scene that you are painting with a green laser pointer? Where did you get your magic value (58, 255, 0) – is that based on camera input, and does it come from a setup with a constant surface and controlled lighting?

For anything beyond trivial, simple setups, searching for an ideal color can fail easily do to changes in lighting, underlying surface coloration, reflectivity, et cetera et cetera. If you can’t tune it right, better to either detect the marker with background subtraction (if the rest of the scene is static) or perhaps use the facilities of a computer vision library like OpenCV or BoofCV.