Separating YCbCr components of an image

11 visualizzazioni (ultimi 30 giorni)
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 Commento
Raksha Gopal Kulkarni
Raksha Gopal Kulkarni il 21 Set 2017
Modificato: Walter Roberson il 24 Dic 2021
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);

Accedi per commentare.

Risposta accettata

Alexander De-Ville
Alexander De-Ville il 6 Apr 2015
answered my own question. the 'a' matrix should not be 0s but 128s (50%)
  2 Commenti
Sanik
Sanik il 11 Ago 2016
what does this mean ??
Aspira Tripathy
Aspira Tripathy il 14 Giu 2018
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.

Accedi per commentare.

Più risposte (2)

Dominique Sutter
Dominique Sutter il 22 Giu 2017
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 il 22 Nov 2022
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.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by