1

I am implementing C++ code to do the color space conversion. The need is to convert YVU to HSV. What I did is to convert YVU to RGB, and then to HSV. When converting back, I converted HSV to RGB and then YVU.

I feel it's not very convenient to convert via RGB. How can I convert YVU to HSV and then convert back in C++?

Below is part of my code to the conversion, which is not very efficient...Thank you!

void yvu2hsv(uint8_t* in_y,  uint8_t* in_vu, uint8_t* unet_in, uint8_t* out_y, uint8_t* out_vu, int size)
{
    
    for(int i = 0; i < size; i++)
    {
        size_t i_2 = i * 2;
        float yuv_y = (float) in_y[i] / 255.f;
        float yuv_v = (float) in_vu[i_2] / 255.f - 0.5;
        float yuv_u = (float) in_vu[i_2 + 1] / 255.f - 0.5;

        rgb rgb_in;
        rgb_in.r = yuv_y + 1.5748 * yuv_v;
        rgb_in.g = yuv_y - 0.1873 * yuv_u - 0.4681 * yuv_v;
        rgb_in.b = yuv_y + 1.8556 * yuv_u;

        float unet_in_x = (float)unet_in[i] / 255.f;


        hsv hsv_out = rgb2hsv(rgb_in);


        // SOME PROCESSING ON HSV COLOR SPACE HERE


        rgb rgb_out = hsv2rgb(hsv_out);



        out_y[i] = (uint8_t)(( std::max(std::min(1.0, 0.2126 * rgb_out.r + 0.7152 * rgb_out.g + 0.0722 * rgb_out.b) ,0.0) ) * 255);
        out_vu[i_2 + 1] =   (uint8_t)(( std::max(std::min(1.0, -0.1146 * rgb_out.r - 0.3854 * rgb_out.g + 0.5 * rgb_out.b + 0.5) ,0.0) ) * 255);
        out_vu[i_2]=   (uint8_t)(( std::max(std::min(1.0, 0.5 * rgb_out.r - 0.4542 * rgb_out.g - 0.0458 * rgb_out.b + 0.5) ,0.0) ) * 255);
    }


}
debug_all_the_time
  • 564
  • 1
  • 5
  • 18
  • See [this related post](https://stackoverflow.com/questions/46460225/convert-yuv-into-hsl-or-hsv-bypassing-the-rgb-step). – G.M. Jul 26 '22 at 17:50
  • The best way to optimize this code is to replace the (horribly slow) `/ 255.f` expressions by the equivalent `* 0.003921569f`. – prapin Jul 26 '22 at 17:56
  • Personally, I would have written this conversion entirely with integers, as typically integers are faster than floating point numbers. That depends on the hardware. But it is more difficult to do it well. – prapin Jul 26 '22 at 18:07
  • This code seems fine to me. Why do you say it is not very efficient? What have you compared it to? Also: did you compile with optimizations? – Cris Luengo Jul 26 '22 at 18:58
  • The only minor thing I see is `i_2 = i * 2`, which could be replaced with `i_2 += 2`. – Cris Luengo Jul 26 '22 at 18:59
  • 1
    In the first place, are you 100% certain that the processing must be made in the HSV color space ? –  Jul 26 '22 at 19:28

0 Answers0