RGBtoYCbCr transformation by hand

asked 2016-08-24 06:23:13 -0600

Hello everyone, I need to make transformations between RGB and YCbCr by hand, so I've written the function:

void RGBtoYCbCr2(unsigned char* scrRGB, unsigned char* dstYCbCr,  int w,  int h){

  double R = 0.0;
  double G = 0.0;
  double B = 0.0;

    char flagcr = 0;

    for(int y=0; y < h; y++){
        for(int x=0; x < w; x++){

            R = (unsigned char)*(scrRGB + (x + y*w)*3 + 0);
            G = (unsigned char)*(scrRGB + (x + y*w)*3 + 1);
            B = (unsigned char)*(scrRGB + (x + y*w)*3 + 2);

            *(dstYCbCr + (x + y*w)*2) = (uchar)(0.299*R + 0.587*G  + 0.114*B);
            if( flagcr == 0 ){
                *(dstYCbCr + (x + y*w)*2 + 1) = (uchar)((-0.1687*R - 0.3313*G + 0.5*B) + 128) ;
                flagcr = 1;
            }else{
                *(dstYCbCr + (x + y*w)*2 + 1) = (uchar)((0.5*R - 0.4187*G - 0.0813*B) + 128);
                flagcr = 0;
            }
       }
  }

}

I get source RGB image from VideoCapture cap(0) in BGR(RGB). Then I create Mat object, which must contain result YCrCb image. The problem is to explain OpenCV that I want to save exactly YCrCb in Mat. I use Mat-constructor this way:

Mat YCbCrFrame(h,w,CV_8UC2);
       RGBtoYCbCr2(frame.data, YCbCrFrame.data, w, h);

Does anybody know whether I should use CV_8UC2 or something else?

I'll be pleased any advices.

edit retag flag offensive close merge delete

Comments

it looks different from what opencv does: http://docs.opencv.org/master/de/d25/...

berak gravatar imageberak ( 2016-08-24 06:49:07 -0600 )edit