Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB to YCbCr Conversion problems

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?

like image 387
Runnko Avatar asked Oct 18 '13 22:10

Runnko


1 Answers

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

like image 126
embert Avatar answered Oct 07 '22 04:10

embert