[IGSTK-Users] Conversion YUV2RGB

Patrick Cheng cheng at isis.georgetown.edu
Tue Jun 1 12:59:01 EDT 2010


Hi Kuba,

First, you can check if your device allow both RGB output mode, if so 
you can try change the output to RGB mode.

If your device doesn't support RGB output, then you need to some hacking 
to get it to work. Here are some directions:

1. Open the igstkWebcamWinVideoImager.cxx class, around line 278:
       memcpy(frame->GetImagePtr(),
              (unsigned char*)m_Cvframe->imageData,
              frameDims[0]*frameDims[1]*frameDims[2]);
     This where, we import the frame data from OpenCV to IGSTK

2. You can try use CvtColor to convert the color space in OpenCV 
(detailed documentation is pasted below)
      or you can try iterate through that memory space and perform the 
conversion, which might be less efficient.
      You can find the conversion function here on wiki:
http://en.wikipedia.org/wiki/YUV#Converting_between_Y.27UV_and_RGB

Let me know how it goes. The VideoGrabber code is still in the 
experimental stage. You are welcome to submit your hacks back into the 
repository.

Patrick


*************************************************************************************************************************************************************************************
 From OpenCV manual page:
http://www.seas.upenn.edu/~bensapp/opencvdocs/ref/opencvref_cv.htm 
<http://www.seas.upenn.edu/%7Ebensapp/opencvdocs/ref/opencvref_cv.htm>
*************************************************************************************************************************************************************************************


      CvtColor

Converts image from one color space to another

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

src
    The source 8-bit (8u), 16-bit (16u) or single-precision
    floating-point (32f) image.
dst
    The destination image of the same data type as the source one. The
    number of channels may be different.
code
    Color conversion operation that can be specifed using
    CV_<src_color_space>2<dst_color_space> constants (see below).

The function |cvCvtColor| converts input image from one color space to 
another. The function ignores|colorModel| and |channelSeq| fields of 
|IplImage| header, so the source image color space should be specified 
correctly (including order of the channels in case of RGB space, e.g. 
BGR means 24-bit format with B_0 G_0 R_0 B_1 G_1 R_1 ... layout, whereas 
RGB means 24-bit format with R_0 G_0 B_0 R_1 G_1 B_1 ... layout).

The conventional range for R,G,B channel values is:

    * 0..255 for 8-bit images
    * 0..65535 for 16-bit images and
    * 0..1 for floating-point images.

Of course, in case of linear transformations the range can be arbitrary, 
but in order to get correct results in case of non-linear 
transformations, the input image should be scaled if necessary.

The function can do the following transformations:

    * Transformations within RGB space like adding/removing alpha
      channel, reversing the channel order, conversion to/from 16-bit
      RGB color (R5:G6:B5 or R5:G5:B5) color, as well as conversion
      to/from grayscale using:

      RGB[A]->Gray: Y<-0.299*R + 0.587*G + 0.114*B
      Gray->RGB[A]: R<-Y G<-Y B<-Y A<-0
           

    * RGB<=>CIE XYZ.Rec 709 with D65 white point (|CV_BGR2XYZ,
      CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB|):

      |X|    |0.412453  0.357580  0.180423| |R|
      |Y|<- |0.212671  0.715160  0.072169|*|G|
      |Z|    |0.019334  0.119193  0.950227| |B|

      |R|    | 3.240479  -1.53715  -0.498535| |X|
      |G|<- |-0.969256   1.875991  0.041556|*|Y|
      |B|    | 0.055648  -0.204043  1.057311| |Z|

      X, Y and Z cover the whole value range (in case of floating-point images Z may exceed 1).
           

    * RGB<=>YCrCb JPEG (a.k.a. YCC) (|CV_BGR2YCrCb, CV_RGB2YCrCb,
      CV_YCrCb2BGR, CV_YCrCb2RGB|)

      Y<- 0.299*R + 0.587*G + 0.114*B
      Cr<- (R-Y)*0.713 + delta
      Cb<- (B-Y)*0.564 + delta

      R<- Y + 1.403*(Cr - delta)
      G<- Y - 0.344*(Cr - delta) - 0.714*(Cb - delta)
      B<- Y + 1.773*(Cb - delta),

                     { 128 for 8-bit images,
      where delta = { 32768 for 16-bit images
                     { 0.5 for floating-point images

      Y, Cr and Cb cover the whole value range.
           

    * RGB<=>HSV (|CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB|)

      // In case of 8-bit and 16-bit images
      // R, G and B are converted to floating-point format and scaled to fit 0..1 range

      V<- max(R,G,B)
      S<- (V-min(R,G,B))/V   if V?0, 0 otherwise

                (G - B)*60/S,  if V=R
      H<- 180+(B - R)*60/S,  if V=G
            240+(R - G)*60/S,  if V=B

      if H<0 then H<-H+360

      On output 0?V?1, 0?S?1, 0?H?360.
      The values are then converted to the destination data type:
           8-bit images:
               V<- V*255, S<- S*255, H<- H/2 (to fit to 0..255)
           16-bit images (currently not supported):
               V<- V*65535, S<- S*65535, H<- H
           32-bit images:
               H, S, V are left as is
           

    * RGB<=>HLS (|CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB|)

      // In case of 8-bit and 16-bit images
      // R, G and B are converted to floating-point format and scaled to fit 0..1 range

      V_max    <- max(R,G,B)
      V_min    <- min(R,G,B)

      L<- (V_max    + V_min  )/2

      S<- (V_max    - V_min  )/(V_max    + V_min  )  if L<  0.5
            (V_max    - V_min  )/(2 - (V_max    + V_min  ))  if L ? 0.5

                (G - B)*60/S,  if V_max  =R
      H<- 180+(B - R)*60/S,  if V_max  =G
            240+(R - G)*60/S,  if V_max  =B

      if H<0 then H<-H+360

      On output 0?L?1, 0?S?1, 0?H?360.
      The values are then converted to the destination data type:
           8-bit images:
               L<- L*255, S<- S*255, H<- H/2
           16-bit images (currently not supported):
               L<- L*65535, S<- S*65535, H<- H
           32-bit images:
               H, L, S are left as is
           

    * RGB<=>CIE L*a*b* (|CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR, CV_Lab2RGB|)

      // In case of 8-bit and 16-bit images
      // R, G and B are converted to floating-point format and scaled to fit 0..1 range

      // convert R,G,B to CIE XYZ
      |X|    |0.412453  0.357580  0.180423| |R|
      |Y|<- |0.212671  0.715160  0.072169|*|G|
      |Z|    |0.019334  0.119193  0.950227| |B|

      X<- X/Xn, where Xn = 0.950456
      Z<- Z/Zn, where Zn = 1.088754

      L<- 116*Y^1/3         for Y>0.008856
      L<- 903.3*Y      for Y<=0.008856

      a<- 500*(f(X)-f(Y)) + delta
      b<- 200*(f(Y)-f(Z)) + delta
      where f(t)=t^1/3                 for t>0.008856
             f(t)=7.787*t+16/116   for t<=0.008856


      where delta = 128 for 8-bit images,
                     0 for floating-point images

      On output 0?L?100, -127?a?127, -127?b?127
      The values are then converted to the destination data type:
           8-bit images:
               L<- L*255/100, a<- a + 128, b<- b + 128
           16-bit images are currently not supported
           32-bit images:
               L, a, b are left as is
           

    * RGB<=>CIE L*u*v* (|CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB|)

      // In case of 8-bit and 16-bit images
      // R, G and B are converted to floating-point format and scaled to fit 0..1 range

      // convert R,G,B to CIE XYZ
      |X|    |0.412453  0.357580  0.180423| |R|
      |Y|<- |0.212671  0.715160  0.072169|*|G|
      |Z|    |0.019334  0.119193  0.950227| |B|

      L<- 116*Y^1/3  -16   for Y>0.008856
      L<- 903.3*Y      for Y<=0.008856

      u'<- 4*X/(X + 15*Y + 3*Z)
      v'<- 9*Y/(X + 15*Y + 3*Z)

      u<- 13*L*(u' - u_n  ), where u_n  =0.19793943
      v<- 13*L*(v' - v_n  ), where v_n  =0.46831096

      On output 0?L?100, -134?u?220, -140?v?122
      The values are then converted to the destination data type:
           8-bit images:
               L<- L*255/100, u<- (u + 134)*255/354, v<- (v + 140)*255/256
           16-bit images are currently not supported
           32-bit images:

    *

               L, u, v are left as is
           

      The above formulae for converting RGB to/from various color spaces
      have been taken from multiple sources on Web, primarily from Color
      Space Conversions (*[Ford98]*) <#paper_ford98> document at Charles
      Poynton site.
    * Bayer=>RGB (|CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR,
      CV_BayerGR2BGR,
      CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB|)

      Bayer pattern is widely used in CCD and CMOS cameras. It allows to
      get color picture out of a single plane where R,G and B pixels
      (sensors of a particular component) are interleaved like this:

      R

      	

      G

      	

      R

      	

      G

      	

      R

      G

      	

      B

      	

      G

      	

      B

      	

      G

      R

      	

      G

      	

      R

      	

      G

      	

      R

      G

      	

      B

      	

      G

      	

      B

      	

      G

      R

      	

      G

      	

      R

      	

      G

      	

      R

      G

      	

      B

      	

      G

      	

      B

      	

      G

      The output RGB components of a pixel are interpolated from 1, 2 or
      4 neighbors of the pixel having the same color. There are several
      modifications of the above pattern that can be achieved by
      shifting the pattern one pixel left and/or one pixel up. The two
      letters C_1 and C_2 in the conversion constants CV_BayerC_1 C_2
      2{BGR|RGB} indicate the particular pattern type - these are
      components from the second row, second and third columns,
      respectively. For example, the above pattern has very popular "BG"
      type.



On 5/30/2010 10:47 AM, KubaP wrote:
> Hi,
> I would like to capture video from the camera in real time.
> I work with an example VideoFrameGrabberAndViewerWebcamWin (sandbox, WIndows),
> but I have a problem with the format of pixels:
> Program works with RGB , but the device provides e.g. YUV. How convert format YUV to RGB?
>
> Thanks in advance for any help.
>
> Kuba.P
>
>
> ----------------------------------------------------------------------
> Kup wlasne mieszkanie za 72 tys. zl.
> Sprawdz najlepsze oferty>>>  http://linkint.pl/f26c8
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/cgi-bin/mailman/listinfo/igstk-users
>
>
>    
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/igstk-users/attachments/20100601/57a0df95/attachment.htm>


More information about the IGSTK-Users mailing list