6

i need convert RGB image to YCbCr colour space, but have some colour shift problems, i used all formulas and got the same result.

Formula in python

    cbcr[0] =  int(0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]) #Y
    cbcr[1] =  int(-0.1687*rgb[0] - 0.3313*rgb[1] + 0.5*rgb[2] + 128) #Cb
    cbcr[2] =  int( 0.5*rgb[0] - 0.4187*rgb[1] - 0.0813*rgb[2] + 128) #Cr

I know that i should get the same image with different way to record data, but i got wrong colour result.

http://i.imgur.com/zHuv8yq.png Original

http://i.imgur.com/Ek2WEA1.png Result

So how i can get normal image or convert RGB PNG into YCbCr 4:2:2?

nathanchere
  • 8,008
  • 15
  • 65
  • 86
Runnko
  • 61
  • 1
  • 3
  • Ok , i have new question. How can i save image with YUV palette? I mean - i got YUV colour values, but when i try save or merge image come colour shift, because of colours come from RGB palette not YCbCr and we got different colours. – Runnko Oct 19 '13 at 22:50

1 Answers1

6

This should work (approximately)

def _ycc(r, g, b): # in (0,255) range
    y = .299*r + .587*g + .114*b
    cb = 128 -.168736*r -.331364*g + .5*b
    cr = 128 +.5*r - .418688*g - .081312*b
    return y, cb, cr

def _rgb(y, cb, cr):
    r = y + 1.402 * (cr-128)
    g = y - .34414 * (cb-128) -  .71414 * (cr-128)
    b = y + 1.772 * (cb-128)
    return r, g, b

>>> c = _ycc(10, 20, 30)
>>> _rgb(*c)
(10.000005760000002, 20.000681726399996, 29.996457920000005)

See also Wikipedia

mahemoff
  • 44,526
  • 36
  • 160
  • 222
embert
  • 7,336
  • 10
  • 49
  • 78