Separating YCbCr components of an image

조회 수: 36 (최근 30일)
Alexander De-Ville
Alexander De-Ville 2015년 4월 6일
답변: DGM 2022년 11월 22일
I am trying to seperate out the Y, Cb, and Cr components of an image and display them. I wanted the results to look like: Wiki Article Figure
I haven't quite got it right and have been trying to fix it for ages, I'm sure it's something really stupid.
%Clear command window.
clc;
%Clear workspace.
clear;
%Load image 'Airplane'.
RGB = imread('Airplane.bmp');
%Convert RGB image to YCbCr Components.
YCbCr = rgb2ycbcr(RGB);
%Create a matrix of 0s, 512x512.
a = zeros(512,512);
%Isolate Y.
Y = YCbCr(:,:,1);
%Isolate Cb.
Cb = YCbCr(:,:,2);
%Isolate Cr.
Cr= YCbCr(:,:,3);
%Create a YCbCr image with only the Y component.
just_Y = cat(3, Y, a, a);
%Create a YCbCr image with only the Cb component.
just_Cb = cat(3, a, Cb, a);
%Create a YCbCr image with only the Cr component.
just_Cr = cat(3, a, a, Cr);
%turn back to rgb
YY = ycbcr2rgb(just_Y);
CbCb = ycbcr2rgb(just_Cb);
CrCr = ycbcr2rgb(just_Cr);
%Display the Original Image.
figure, imshow(RGB), title('Original Image')
%Display the Y(black and white) Component.
figure, imshow(YY), title('Y Component')
%Display the Cb Component.
figure, imshow(CbCb), title('Cb Component')
%Display the Cr Component.
figure, imshow(CrCr), title('Cr Component')
  댓글 수: 1
Raksha Gopal Kulkarni
Raksha Gopal Kulkarni 2017년 9월 21일
편집: Walter Roberson 2021년 12월 24일
Hey!!! I have a easy Mat lab code to display YCbCr components separately!!, it is consistent with the code to get RGB components from a colored image. Code:
if ~isunix()
filename = 'path to your image';
else
filename = 'flamingos.jpg';
end
image = imread(filename);
imshow(image);
ycbcr = rgb2ycbcr(image);
y = ycbcr(:,:,1); % y channel
cb = ycbcr(:,:,2); % cb channel
cr = ycbcr(:,:,3); % cr channel
figure();
imshow(y);
figure();
imshow(cb);
figure();
imshow(cr);

댓글을 달려면 로그인하십시오.

채택된 답변

Alexander De-Ville
Alexander De-Ville 2015년 4월 6일
answered my own question. the 'a' matrix should not be 0s but 128s (50%)
  댓글 수: 2
Sanik
Sanik 2016년 8월 11일
what does this mean ??
Aspira Tripathy
Aspira Tripathy 2018년 6월 14일
This means that the values concatenated along with the Y-component should have intensity of 50% and therefore, the values shouldn't be zero, but 128. This can be done by adding 128 to the zeros' matrix i.e. a = 128+zeros(512,512). Hope this helps.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Dominique Sutter
Dominique Sutter 2017년 6월 22일
Hello, i just work on the same problem. I try the code above but it doesn't lead to good results. I've done the following : Title, and comments are in french, sorry.
clear variables
close all
clc
% Fichier image à traiter
fic='Capture.bmp';
% Lecture et Affichage de l'image
img=imread(fic);
ti=size(img);
figure(1)
imshow(img);
title(['Image originale : ',fic])
xlabel(['Résolution de l''image : ',num2str(ti(2)),'x',num2str(ti(1))])
R=img(:,:,1);
V=img(:,:,2);
B=img(:,:,3);
zer=zeros(ti,'uint8');
% Calcul
img_R=zer;
img_V=zer;
img_B=zer;
img_Y=zer;
img_Cb=zer;
img_Cr=zer;
% Séparation et affichage des canaux
img_R(:,:,1)=R;
img_V(:,:,2)=V;
img_B(:,:,3)=B;
% Calcul des Composantes
% Y (luminance - Image en niveaux de gris)
% Cb (chrominance bleue)
% Cr (chrominance rouge)
Y = 0.299*R+0.587*V+0.114*B;
Cb= -0.1687*R-0.3313*V+0.5*B+128;
Cr = 0.5*R-0.4187*V-0.0813*B+128;
figure(2)
subplot(131)
imshow(img_R)
title('Composante R')
subplot(132)
imshow(img_V)
title('Composante V')
subplot(133)
imshow(img_B)
title('Composante B')
img_Y(:,:,1)=Y;
img_Y(:,:,2)=Y;
img_Y(:,:,3)=Y;
figure(3)
imshow(img_Y);
title('Image en niveaux de gris (Luminance Y)')
img_Cb(:,:,1)=Y;
img_Cb(:,:,2)=Y;
img_Cb(:,:,3)=Y;
% Y, Cr NULL
img_Cb(:,:,1)=1.402*(-128); %R
img_Cb(:,:,2)=-0.34414*(Cb-128)-0.71414*(-128); %V
img_Cb(:,:,3)=1.772*(Cb-128); %B
figure(4)
imshow(img_Cb);
title('Chrominance bleue (Cb)')
% Y, Cb NULL
img_Cr(:,:,1)=1.402*(Cr-128); %R
img_Cr(:,:,2)=-0.34414*(-128)-0.71414*(Cr-128); %V
img_Cr(:,:,3)=1.772*(-128); %B
figure(5)
imshow(img_Cr);
title('Chrominance rouge (Cr)')

DGM
DGM 2022년 11월 22일
See also:
This answer explains the limits of the given method of representation, and it includes links to similar examples, including ones recreating the Wiki barn photoset.

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by