Increasing the Y value in a YCbCr image in Python (pillow) results desaturated image

182 views Asked by At

I'm using the code below to convert from/to RGB/YCbCr images, which works properly when I don't do any adjustment over Y values.

def rgb2ycbcr(im):
    xform = np.array([[.299, .587, .114], [-.1687, -.3313, .5], [.5, -.4187, -.0813]])
    ycbcr = im.dot(xform.T)
    ycbcr[:,:,[1,2]] += 128
    return np.uint8(ycbcr)
def ycbcr2rgb(im):
    xform = np.array([[1, 0, 1.402], [1, -0.34414, -.71414], [1, 1.772, 0]])
    rgb = im.astype(np.float32)
    rgb[:,:,[1,2]] -= 128
    rgb = rgb.dot(xform.T)
    np.putmask(rgb, rgb > 255, 255)
    np.putmask(rgb, rgb < 0, 0)
    return np.uint8(rgb)
    fn = 'parrot.jpg'
    im = Image.open(fn)
    plt.imshow(im)

Here I want to increase the Y value by some number.

    yuv_img_arr = rgb2ycbcr(np.array(im))
    print(yuv_img_arr.shape)
    print(yuv_img_arr[:,:, 0])

    yuv_img_arr[:,:, 0] += 100

    print(yuv_img_arr[:,:, 0])

    rgb_img = Image.fromarray(ycbcr2rgb(yuv_img_arr))
    rgb_img.save(fn+'converted.jpg')
    plt.imshow(rgb_img)

Original Image Processed Image

How can I solve this problem? I just want to increase the brightness of this image by using Y value.

The following code works in MathLab without any problems. What am I doing wrong here?

RGB=imread('parrot.jpg');
YCBCR=rgb2ycbcr(RGB);
Y=YCBCR(:,:,1);
Y=Y+100;
YCBCR(:,:,1)=Y;
RGB2 = ycbcr2rgb(YCBCR);

Matlab Processed Image I was expecting the image to be brighter but there are color changes.

0

There are 0 answers