简体   繁体   English

颜色处理

[英]Color Manipulation

I have this task to interchange colors in an image. 我有这个任务来交换图像中的颜色。

Change Red to Blue Change Blue to Green and Change Green to Red 将红色更改为蓝色将蓝色更改为绿色并将绿色更改为红色

User will input an image, and the output will show an image w/ interchanged colors. 用户将输入图像,输出将显示具有互换颜色的图像。

I was given a hint to convert RGB to HSI. 我得到了一个提示,将RGB转换为HSI。 But still... I don't get to how to do it. 但仍然......我不知道怎么做。 What steps should I do to make this task possible? 我应该采取哪些措施来完成这项任务? Below is the formula for converting RGB to HSI Thanks. 下面是将RGB转换为HSI的公式谢谢。 :) :)

Equations to Convert RGB Values to HSI Values Suppose R, G, and B are the red, green, and blue values of a color. 将RGB值转换为HSI值的公式假设R,G和B是颜色的红色,绿色和蓝色值。 The HSI intensity is given by the equation HSI强度由等式给出

I = (R + G + B)/3.

Now let m be the minimum value among R, G, and B. The HSI saturation value of a color is given by the equation 现在让m为R,G和B中的最小值。颜色的HSI饱和度值由等式给出

S = 1 - m/I    if I > 0, or
S = 0            if I = 0.

To convert a color's overall hue, H, to an angle measure, use the following equations: 要将颜色的整体色调H转换为角度测量,请使用以下等式:

H = cos-1[ (R - ½G - ½B)/√R² + G² + B² - RG - RB - GB ]            if G ≥ B, or
H = 360 - cos-1[ (R - ½G - ½B)/√R² + G² + B² - RG - RB - GB ]    if B > G,

where the inverse cosine output is in degrees. 其中反余弦输出以度为单位。

由于颜色以度为单位且为圆形,因此应在色调中添加120度。

Add 1/3 of the maximum Hue value to the H channel of each pixel. 将最大Hue值的1/3加到每个像素的H通道。 If you are using an 8U depth, add 255/3, if you are using 32F depth, add 0.333 (but subtract 1 if it overflows past 1) 如果使用的是8U深度,请添加255/3,如果使用的是32F深度,请添加0.333(如果超过1,则减去1)

you could try the hsv color rappresentation : 你可以尝试hsv颜色rappresentation:

http://en.wikipedia.org/wiki/HSL_and_HSV http://en.wikipedia.org/wiki/HSL_and_HSV

you can find under "Hue and chroma" how to transform 你可以在“Hue and chroma”下找到如何变换

there maximal G means value is between 1 and 3 maximal blue value is between 3 and 5 and maximal R value is between 5 and 6 or 0 and 1 ! 最大G表示值在1到3之间,最大蓝色值在3到5之间,最大R值在5到6或0和1之间! at the end you multiply with 60 and you get a degree like value 最后你乘以60,你得到一个像价值的程度

The way you've worded it makes me think you are supposed to do the underlying calculations, but since you have mentioned opencv as a tag, I'll say that you can transform an image to HSV (same as HSI I believe) with one line using opencv: 你说它的方式让我觉得你应该做基础计算,但是既然你已经提到opencv作为标签,我会说你可以用一个将图像转换为HSV(我认为与HSI相同)使用opencv的行:

cvCvtColor( src, hsvDestination, CV_BGR2HSV );

You can find more information here: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html 您可以在此处找到更多信息: http//docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html

Here are the functions I wrote (and some comments describing a couple) that are used to map "light" (hard to see on white paper) colors to black. 以下是我写的函数(以及描述一对的一些评论),用于将“光”(白纸上难以看到)颜色映射为黑色。 I call CheckSwapWhite for each color to be printed, but it could be called for each pixel. 我为每种要打印的颜色调用CheckSwapWhite ,但可以为每个像素调用它。 I don't have a formula for creating something in gray-scale, but I bet you could find that with a creative Google search. 我没有使用灰度创建内容的公式,但我敢打赌,你可以通过创造性的谷歌搜索找到它。

//----------------------------------------------------------------------------
/*
   Colour Brightness Formula

   The following is the formula suggested by the World Wide Web
   Consortium (W3C) to determine the brightness of a colour.

   ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000

   The difference between the background brightness, and the
   foreground brightness should be greater than 125.
 */
//----------------------------------------------------------------------------

int ColorBrightness( COLORREF cr )
{
   return ((GetRValue(cr) * 299) +
           (GetGValue(cr) * 587) +
           (GetBValue(cr) * 114)) / 1000;
}

//----------------------------------------------------------------------------
/*
   Colour Difference Formula

   The following is the formula suggested by the W3C to determine
   the difference between two colours.

     (maximum (Red   value 1, Red   value 2) - minimum (Red   value 1, Red   value 2))
   + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2))
   + (maximum (Blue  value 1, Blue  value 2) - minimum (Blue  value 1, Blue  value 2))

   The difference between the background colour and the foreground
   colour should be greater than 500.
 */
//----------------------------------------------------------------------------

int ColorDifference( COLORREF c1, COLORREF c2 )
{
   return (max(GetRValue(c1), GetRValue(c2)) - min(GetRValue(c1), GetRValue(c2)))
        + (max(GetGValue(c1), GetGValue(c2)) - min(GetGValue(c1), GetGValue(c2)))
        + (max(GetBValue(c1), GetBValue(c2)) - min(GetBValue(c1), GetBValue(c2)));
}

//----------------------------------------------------------------------------

COLOREF CheckSwapWhite( COLORREF cr )
{
   int      cdiff;
   int      bdiff;

   bdiff = 255 - ColorBrightness( cr ); // 255 = ColorBrightness(WHITE)
   cdiff = ColorDifference( cr, RGB(0xFF,0xFF,0xFF) );

   if( (cdiff < gnDiffColorThreshold) ||  // (500 by default)
       (bdiff < gnDiffBrightThreshold) )  // (125 by default)
   {
      return RGB(0x00,0x00,0x00); // black
   }
   return cr;
}

The two threshold variables are configurable settings with the given defaults. 两个阈值变量是具有给定默认值的可配置设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM