views:

402

answers:

3

This code I have found attempt to track red color in RGB color space,

  // red color detection, turn the detected one into white
  if (((red > (0.85 * (green + blue))) && (red > 105)) 
     && ((red - green > 73)) && (((green < 150) 
     || ((green >= 150) && (blue > 140)))))  {
        // set the pixel to white
        red = 255; green = 255; blue = 255;
  }

Does anyone know how to track color using YCrCb color space instead RGB? I just don't know what exactly was the range for every color in order to track it, e.g. red color range in YCrCb.

Edit: I've tried HSV, It doesn't give better result than RGB above as expected, consequently, I consider to use YCrCb.

Thanks.

A: 

I suggest you instead use HSV colour space which will most likely make this a lot easier.

kigurai
A: 

In YCbCr, the typical range is 16 to 240 for each component. See the following Wikipedia entry that details the various formulas to convert between RGB and YCbCr.

Laurent Etiemble
+1  A: 

First of all you can take a look here for a better definition. This color model (YCrCb) is more widely used in video formats like mpeg. Most video formats, if the video uses the the format 4:2:2, which means that the Y component (representing luminance) has the same size of the source RGB image while Yb (blue component) and Yr (Red component) are represented by half of the original image resolution. Doing that its possible to decrease the bandwidth of the file.

But, as kigurai said, if you want to track colors, you will better do it using HSV (or HSI) format. In that format the H (Hue) component represents the color itself raging from 0..359 (360 degrees). So to avoid rounding the numbers from 0..255 you can use a 16bit matrix to represent that channel in memory. Also in this color space S represents saturation (how much of the color component in H is present at that pixel) and S (or I) represents the image brightness.

To algorithm to implement it is not hard:

I = 1/3 * (R+G+B)

S = 1 - (3/(R+G+B))*(min(R,G,B))

H = cos^-1 ( (((R-G)+(R-B))/2)/ (sqrt((R-G)^2 + (R-B)*(G-B) )))

Andres