How to deal with RGB to YUV conversion

后端 未结 3 1064
萌比男神i
萌比男神i 2020-12-08 16:01

The formula says:

Y = 0.299 * R + 0.587 * G + 0.114 * B;

U = -0.14713 * R - 0.28886 * G + 0.436 * B;

V = 0.615 * R - 0.51499 * G - 0.10001 * B;


        
相关标签:
3条回答
  • 2020-12-08 16:47

    You can convert RGB<->YUV in OpenCV with cvtColor using the code CV_YCrCb2RGB for YUV->RGB and CV_RGBYCrCb for RGB->YUV.

    void cvCvtColor(const CvArr* src, CvArr* dst, int code)
    

    Converts an image from one color space to another.

    0 讨论(0)
  • 2020-12-08 16:47

    Y, U and V are all allowed to be negative when represented by decimals, according to the YUV color plane.

    YUV Color space

    0 讨论(0)
  • 2020-12-08 17:05

    for planar formats OpenCV is not the right tool for the job. Instead you are better off using ffmpeg. for example

    static void rgbToYuv(byte* src, byte* dst, int width,int height)
    {
    
        byte* src_planes[3] = {src,src + width*height, src+ (width*height*3/2)};
        int src_stride[3] = {width, width / 2, width / 2};
        byte* dest_planes[3] = {dst,NULL,NULL};
        int dest_stride[3] = {width*4,0,0};
        struct SwsContext *img_convert_ctx = sws_getContext(
            width,height,
            PIX_FMT_YUV420P,width,height,PIX_FMT_RGB32,SWS_POINT,NULL,NULL,NULL);
            sws_scale(img_convert_ctx, src_planes,src_stride,0,height,dest_planes,dest_stride); 
        sws_freeContext(img_convert_ctx);
    }
    

    will convert a YUV420 image to RGB32

    0 讨论(0)
提交回复
热议问题