0

I need to perform hue adjustment functionality on the image. Since it involves Slider functionality in UI the algorithm needs to be faster.

  1. I had already implemented RGB->HSV->H adjust->RGB with fixed point optimization etc., but the speed of the algorithm is still an issue.

  2. I tried the approach mentioned in the below link Shift hue of an RGB Color

--

Color TransformH(
    const Color &in,  // color to transform
    float H
)
{
  float U = cos(H*M_PI/180);
  float W = sin(H*M_PI/180);

  Color ret;
  ret.r = (.701*U+.168*W)*in.r
    + (-.587*U+.330*W)*in.g
    + (-.114*U-.497*W)*in.b;
  ret.g = (-.299*U-.328*W)*in.r
    + (.413*U+.035*W)*in.g
    + (-.114*U+.292*W)*in.b;
  ret.b = (-.3*U+1.25*W)*in.r
    + (-.588*U-1.05*W)*in.g
    + (.886*U-.203*W)*in.b;
  return ret;
}

skipped the conversion of RGB<->HSV. The speed has improved but the problem is along with hue, saturation and value is also changing.

I want to maintain same saturation and value of the current pixel but modify only the hue component, how do I achieve it... your help is most appreciated...

Community
  • 1
  • 1
JK_S
  • 145
  • 1
  • 5

1 Answers1

2

I recommend using the HSI color space:

// input RGB values
R = ...;
G = ...;
B = ...;

// Transform to HSI color space
I = (R + G + B) / 3.0;
alpha = 1.5 * (R - I);
beta = sqrt(3.0) * 0.5 * (G - B);
H = atan2 (beta, alpha);
C = sqrt (alpha * alpha + beta * beta);


// modify Hue
H = ...;

// Transform back to RGB
beta = sin (H) * C;
alpha = cos (H) * C;

// update RGB values
R = I + (2.0 / 3.0) * alpha;
G = I - alpha / 3.0 + beta / sqrt(3.0);
B = I - alpha / 3.0 - beta / sqrt(3.0);

Test code:

Input:

Output:

Obviously a more perceptually correct color space would give better results but be slower.

I tested the formula you gave in your question and it doesn't work.

  • Hi Antoine, I used your equation for R=100, G=200, B=50 (Actual Hue=100,S=60,I=49) but I get alpha = -25; beta = 130; H2 = 1.76; C2 = 132; I = 116; am I going wrong some where? – JK_S Apr 14 '14 at 06:35
  • I would like to add more to your answer. As it involves slider functionality, sqrt and atan2 for every pixel is very costly. But Output quality seems to be good. I tried http://beesbuzz.biz/code/hsv_color_transforms.php approach. Hue changes are observable and faster but the output image becomes slightly darker. Anyways thanks again Antonie.. – JK_S Apr 22 '14 at 08:24
  • I you want to set all pixels to the same hue, you don't need to calculate the original one, so you only need to compute sqrt and that can be optimized. If you want to shift the hue of all pixels by the same amount you only need to rotate the (alpha, beta) vector using the same rotation matrix for each pixel. You don't need to calculate the original hue and chroma and therefore need to compute neither atan2 nor sqrt. In the later case my proposal should be faster. –  Apr 22 '14 at 10:04