模式“RGB”转换为“YCbCr”的公式如下:
Y= 0.257R+0.504G+0.098B+16
Cb = -0.148R-0.291G+0.439B+128
Cr = 0.439R-0.368G-0.071*B+128
python 中的PIL Image 本身是带有rgb2ycbcr()函数的,但是其MATLAB实现上有差异。
python 自带的函数实现:
from PIL import Image
image =Image.open("test.jpg")
image_ycbcr =image.convert("YCbCr")
python 实现MATLAB中rgb2ycbcr()函数:
mat = np.array(
[[65.481, 128.553, 24.966],
[-37.797, -74.203, 112.0],
[112.0, -93.786, -18.214]])
mat_inv = np.linalg.inv(mat)
offset = np.array([16, 128, 128])
def rgb2ycbcr(rgb_img):
ycbcr_img = np.zeros(rgb_img.shape)
for x in range(rgb_img.shape[0]):
for y in range(rgb_img.shape[1]):
ycbcr_img[x, y, :] = np.round(np.dot(mat, rgb_img[x, y, :] * 1.0 / 255) + offset)
return ycbcr_img