RGB转YCbCr

在MATLAB中

ima = imread('abc.bmp') %读取一张名为abc.bmp的图片

ima_ycbcr = rgb2ycbcr(ima) %将rgb格式转为ycbcr格式

在Python中

方法一:利用skimage中的转换函数

from skimage import io as skio 
from skimage import color as skco

ima = skio.imread('abc.bmp')
ima_ycbcr = skimage.color.rgb2ycbcr(ima)

方法二:利用公式计算

读取图片,并获取rgb三个分量

import matplotlib.image as mpimg
import numpy as np

ima = mpimg.imread('abc.bmp')
ima_r = im_l[:, :, 0]
ima_g = im_l[:, :, 1]
ima_b = im_l[:, :, 2]

RGB转换为YCbCr

#获取亮度,即原图的灰度拷贝
ima_y = 0.256789 * ima_r + 0.504129 * ima_g + 0.097906 * ima_b + 16
#获取蓝色分量
ima_cb = -0.148223 * ima_r - 0.290992 * im_l_g + 0.439215 * ima_b + 128
#获取红色分量
ima_cr = 0.439215 * ima_r - 0.367789 * ima_g - 0.071426 * ima_b + 128

# 将三个分量合并在一起
ima_rgb2ycbcr = np.zeros(ima.shape)
ima_rgb2ycbcr[:,:,0] = ima_y
ima_rgb2ycbcr[:,:,1] = ima_cb
ima_rgb2ycbcr[:,:,2] = ima_cr

YCbCr转换为RGB

ima_ycbcr2rgb = np.zeros(ima.shape)

ima_ycbcr2rgb[:,:,0] = 1.164383 * (ima_y-16) + 1.596027 * (ima_cr-128)
ima_ycbcr2rgb[:,:,1] = 1.164383 * (ima_y-16) - 0.391762 * (ima_cb-128)- 0.812969 * (ima_cr-128)
ima_ycbcr2rgb[:,:,2] = 1.164383 * (ima_y-16) + 2.017230 * (ima_cb-128)

文章最后更新时间为:2018 年 10 月 14 日 22:45:14

添加新评论