使用YCrCb色彩空间进行颜色检测?

问题描述:

此代码我发现尝试跟踪RGB色彩空间中的红色,

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;
  }

有没有人知道如何使用YCrCb颜色空间而不是RGB来跟踪颜色?
我只是不知道每种颜色的确切范围是什么,以便跟踪它,例如YCrCb中的红色范围。

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.


编辑:我尝试过HSV,它没有给出
比上面的RGB更好的结果为
预期,因此,我认为
使用YCrCb。

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

谢谢。

首先你可以看看这里是为了更好的定义。这种颜色模型(YCrCb)更广泛地用于像mpeg这样的视频格式。大多数视频格式,如果视频使用格式4:2:2,这意味着Y分量(表示亮度)具有与源RGB图像相同的大小,而Yb(蓝色分量)和Yr(红色分量)表示原始图像分辨率的一半。这样做可以减少文件的带宽。

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.

但是,正如kigurai所说,如果你想跟踪颜色,你最好使用HSV(或HSI)格式。在该格式中,H(色调)分量表示颜色本身从0..359(360度)肆虐。因此,为了避免从0..255舍入数字,您可以使用16位矩阵表示内存中的该通道。同样在该颜色空间中,S表示饱和度(在该像素处存在多少H中的颜色成分),S(或I)表示图像亮度。

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.

算法实现它并不难:

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

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

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

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

H = cos ^ -1((((RG)+(RB))/ 2)/(sqrt((RG)^ 2 +(RB)*(GB))))

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