RGB2YCbCr

                                           RGB2YCbCr

1、问题起因
        在学习工作中有时候因为色彩空间的不同,对图像处理的结果也会有所不同,因此需要对图像的色彩空间进行转换。对于火焰识别来说,色度对火焰的识别结果产生极大的影响,因此需要将RGB空间的色彩模式转换为YCbCr。

2、RGB和YCbCr色彩模式的不同
(1) RGB简介
RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是目前运用最广的颜色系统之一。

    R:红色通道(Red)

   G:绿色通道(Green)

   B:蓝色通道(Blue)

(2)YCbCr简介
       YCbCr或Y'CbCr有的时候会被写作:YCBCR或是Y'CBCR,是色彩空间的一种,通常会用于影片中的影像连续处理,或是数字摄影系统中。Y'为颜色的亮度(luma)成分、而CB和CR则为蓝色和红色的浓度偏移量成份。Y'和Y是不同的,而Y就是所谓的流明(luminance),表示光的浓度且为非线性,使用伽马修正(gamma correction)编码处理。

Y:明亮度,也就是灰阶值。“亮度”是透过RGB输入信号来建立的,方法是将RGB信号的特定部分叠加到一起。(Luminance或Luma,不要问我Luminance 为什么会简写为Y,我也不懂)

Cb:反映的是RGB输入信号蓝色部分与RGB信号亮度值之间的差异。ChrominanceBlue

Cr:反映了RGB输入信号红色部分与RGB信号亮度值之间的差异。ChrominanceRed

3、相互转换
(1)RGB转换为YCbCr
Y = 0.257*R+0.564*G+0.098*B+16
Cb = -0.148*R-0.291*G+0.439*B+128
Cr = 0.439*R-0.368*G-0.071*B+128
或者

转化为

(2)YCbCr转换为RGB
R = 1.164*(Y-16)+1.596*(Cr-128)
G = 1.164*(Y-16)-0.392*(Cb-128)-0.813*(Cr-128)
B = 1.164*(Y-16)+2.017*(Cb-128)

参考文献


[1]https://en.wikipedia.org/wiki/YCbCr

[2]http://blog.csdn.net/a14730497/article/details/17886127

[3]Celik T, Demirel H. Fire detection in video sequences using a generic color model[J]. Fire Safety Journal, 2009, 44(2): 147-158.

[4]http://baike.baidu.com/link?url=ngq6N5Q0WKqD68gcS8noiR76_b6kcRavZHK8Vus50hkG8SS6s0ngcrIfS19z0hDwOdAAF8ApKjdtckHQwE88xK

[5]http://baike.baidu.com/link?url=iIsaLA_ZDN9vWQfuLZxFiG3yxoe9UOPh46Z_sIxPDrAPkJEqk4EyqWzhs6nCwkPiyo0fF-HSwI--nZv2eu5vWa

Matlab代码:

  clc,clear;
Source=imread('1.jpg');%读入原始RGB图像
 
figure(1);
subplot(1,2,1);
imshow(Source):title('original image');%显示图像
[r c d]=size(Source);%计算图像大小
%------计算红色分量并显示分解图------%
R(:,:,1)=Source(:,:,1);
R(:,:,2)=zeros(r,c);
R(:,:,3)=zeros(r,c);
R=uint8(R);
whos;
figure(2);
subplot(1,3,1);
imshow(R)
title('Red Component');
%-------计算绿色分量并显示分解图-------%
G(:,:,2)=Source(:,:,2);
G(:,:,1)=zeros(r,c);
G(:,:,3)=zeros(r,c);
G=uint8(G);
figure(2);
subplot(1,3,2);
imshow(G)
title('Green Component');
%--------计算蓝色分量并显示分解图-------%
B(:,:,3)=Source(:,:,3);
B(:,:,1)=zeros(r,c);
B(:,:,2)=zeros(r,c);
B=uint8(B);
figure(2);
subplot(1,3,3)
imshow(B)
title('Blue Component');
%------------合成-------------%
Comp(:,:,1)=R(:,:,1);
Comp(:,:,2)=G(:,:,2);
Comp(:,:,3)=B(:,:,3);
figure(5);
subplot(1,2,2);
imshow(Comp):title('composition image');
Y=0.213*R+0.715*G+0.072*B;
Cb=0.511*B-0.117*R-0.394*G+128;
Cr=0.511*R-0.464*G-0.047*B+128;
%red=Y+1.402*(Cr-128);
%green=Y-0.34414*(Cb-128)-0.71414*(Cr-128);
%blue=Y+1.772*(Cb-128);
red = 1.164*(Y-16)+1.793*(Cr-128);
green = 1.164*(Y-16)-0.534*(Cr-128)-0.213*(Cb-128);
blue =1.164*(Y-16)+2.115*(Cb-128);
Comp2(:,:,1)=red(:,:,1);
Comp2(:,:,2)=green(:,:,2);
Comp2(:,:,3)=blue(:,:,3);
Comp5(:,:,1)=Y(:,:,1);
Comp5(:,:,2)=Cb(:,:,2);
Comp5(:,:,3)=Cr(:,:,3);
figure(5);imshow(Comp5);title('显示11YCBcr');
figure(1);subplot(1,2,2);imshow(Comp2);title('RGB转换为YCrCb后又转换为RGB的图像');
RD=R(:,:,1)-red(:,:,1);
GD=G(:,:,2)-green(:,:,2);
BD=B(:,:,3)-blue(:,:,3);
figure(4);subplot(1,3,1);imshow(RD);title('红色分量差异');
subplot(1,3,2);imshow(GD);title('绿色分量差异');
subplot(1,3,3);imshow(BD);title('蓝色分量差异');

实验结果:

 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)

#方法二:利用公式计算
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]
################################################RGB2YCBCR
#获取亮度,即原图的灰度拷贝
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
###############################################YCBCR2RGB
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)




【转载】:https://blog.csdn.net/a1456123a/article/details/50358864

                  https://blog.csdn.net/angelbosj/article/details/38901913

                  https://www.jianshu.com/p/71caefdb1e14

猜你喜欢

转载自blog.csdn.net/u013185349/article/details/84854214