0
votes

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...

1

1 Answers

2
votes

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.