We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Color tracking question (Read 1667 times)
Color tracking question
Mar 24th, 2010, 7:11am
 
Hello,

I am a processing newb. I need help in building a system of live video color tracking, which can identify 3 separate colors (Red,Green,Blue). I want to then convert the X/Y values of the positions of the colors in the video into MAX/MSP.

I understand I can use OSC to do this data conversion, alas, I am a conceptual based composer/sound artist and my knowledge of programming is strictly limited. I would be eternally grateful if anybody could point me in the direction of multiple color tracking patches.

Huh

If anyone could help in anyways would be great. I tried to find a solution purely in MAXMSP with jitter/cyclops but I just can't understand how to/find anything similar.
Re: Color tracking question
Reply #1 - Apr 19th, 2010, 2:37pm
 
Hello Friend!
 I am in a similar situation. I am trying to track the yellow paper on the end of my finger.

http://processing.org/discourse/yabb2/num_1271692820.html

I think we need to subtract the "non good" colors, then use a "threshold" value to turn it into a whole lotta black and only a white blob. Finally, OpenCV has libraries to do blob detection which will give the center (centroid actually because it is an irregular object) in an x,y value pair.

The "non good" colors are opposite (I think, I am not fully sure on this part) of the color we are trying to identify.

What is this OSC you refer to
I know processing and java programming very well, but the image filtering/processing is new territory.
Re: Color tracking question
Reply #2 - Apr 19th, 2010, 7:49pm
 
if you're going to use color based tracking, i suggest using chroma values instead of simply taking the RGB values. here's some code to try out:


Code:

/* chromaFilter example
*  left image shows live view
*  right image shows mask of tracked color
*  mouseClick to choose a color in the live video you want to match
* '[' and ']' to increase/decrease threshold value
*  Apr 2010
*  Johnty Wang johntywang@gmail.com
*/

import processing.video.*;

int numPixels;
Capture video;
PImage masked_img;
float U,V;

static final int V_WIDTH = 320;
static final int V_HEIGHT = 240;
int threshold = 25;

void setup()
{
 U = 50;
 V = 50;
 size(V_WIDTH*2,V_HEIGHT, P2D);
 video = new Capture(this, V_WIDTH, V_HEIGHT, 24);
 video.settings();
 masked_img = new PImage(video.width, video.height, RGB);
 numPixels = video.width*video.height;
 loadPixels();

 for (int i=0; i<numPixels; i++)
 {
   masked_img.pixels[i] = color(0);
 }
}

void draw()
{
 if (video.available())
 {
   video.read();
   image(video,0,0);
 
   // chroma filtering
   for (int i=0; i<numPixels; i++)
   {
     color currColor = video.pixels[i];
     float cU = RGBtoU(currColor);
     float cV = RGBtoV(currColor);    
     // Chroma filtering
     //default values green
     masked_img.pixels[i] = 0xFF00FF00;
     if ( abs(cU-U)<threshold && abs(cV-V)<threshold )
     {
       //filtered values red
       masked_img.pixels[i] = 0xFFFF0000;
     }
   }
   //updatePixels();


 }
 image(masked_img, video.width, 0);

}

void mousePressed()
{
 // println(mouseX+" "+mouseY);
 if (mouseX*mouseY <numPixels)
 {
   color currColor = video.pixels[mouseY*video.width+mouseX];
   println(red(currColor)+" "+green(currColor)+" "+blue(currColor));
   U = RGBtoU(currColor);
   V = RGBtoV(currColor);
   println(U+" "+V);
 }
}

void keyPressed()
{
 if (key == '[') threshold-= 5;  if (key == ']') threshold+= 5;
 if (threshold > 255) threshold = 255;
 if (threshold < 0) threshold = 0;
 println("threshold="+threshold);
}

float RGBtoU(color currColor)
{
 return -.147*red(currColor) - 0.289*green(currColor) + 0.436*blue(currColor);
}

float RGBtoV(color currColor)
{
 return 0.615*red(currColor) - 0.515*green(currColor) - 0.1*blue(currColor);
}


what it does is displays the live video view on the left, and a image mask of all the pixels that are close enough to the color you choose (choose by clicking on the pixel in the live view).
Re: Color tracking question
Reply #3 - Apr 21st, 2010, 6:03am
 
Oooo, that looks like fun to play with. I'll give that a shot and see what happens. I don't suppose you have OpenCV code? Just wondering, my first attempt at "translating" to openCV didn't go so well. But I was in a hurry  Roll Eyes

Thanks for the post!
Page Index Toggle Pages: 1