緑が強い部分が白くなる
function ycbcr_test
close all; clear;
im = imread('lena.jpg');
[X,Y,c] = size(im);
im2 = zeros(X,Y,c);
for y=1:Y
for x=1:X
R = im(y,x,1);
G = im(y,x,1);
B = im(y,x,1);
[Y,Cb,Cr] = RGB2YCbCR(R,G,B);
im2(y,x,1) = Y;
im2(y,x,2) = Cb;
im2(y,x,3) = Cr;
end
end
subplot(221);
imshow(im);
title('original');
subplot(222);
imshow(im2);
title('YCbCr converted');
im = imread('bird.jpg');
[X,Y,c] = size(im);
im2 = zeros(X,Y,c);
for y=1:Y
for x=1:X
R = im(y,x,1);
G = im(y,x,1);
B = im(y,x,1);
[Y,Cb,Cr] = RGB2YCbCR(R,G,B);
im2(y,x,1) = Y;
im2(y,x,2) = Cb;
im2(y,x,3) = Cr;
end
end
subplot(223);
imshow(im);
title('original');
subplot(224);
imshow(im2);
title('YCbCr converted');
function [Y,Cb,Cr] = RGB2YCbCR(R,G,B)
Y = 0.299 * R + 0.587 * G + 0.114 * B - 128;
Cb = - 0.1687 * R - 0.3313 * G + 0.5 * B + 128;
Cr = 0.5 * R - 0.4187 * G - 0.0813 * B + 128;
end
end
function ycbcr_test
close all; clear;
%% lena
im = imread('lena.jpg');
[X,Y,c] = size(im);
Ys= zeros(X,Y);
Cbs= zeros(X,Y);
Crs = zeros(X,Y);
for y=1:Y
for x=1:X
R = im(y,x,1);
G = im(y,x,1);
B = im(y,x,1);
[Y,Cb,Cr] = RGB2YCbCR(R,G,B);
Ys(y,x) = Y;
Cbs(y,x) = Cb;
Crs(y,x) = Cr;
end
end
subplot(241);
imshow(im);
title('original');
subplot(242);
imshow(Ys);
title('Luminance');
subplot(243);
imshow(Cbs);
title('Color diff(Blue)');
subplot(244);
imshow(Crs);
title('Color diff(Red)');
%% bird
im = imread('bird.jpg');
[X,Y,c] = size(im);
Ys= zeros(X,Y);
Cbs= zeros(X,Y);
Crs = zeros(X,Y);
for y=1:Y
for x=1:X
R = im(y,x,1);
G = im(y,x,1);
B = im(y,x,1);
[Y,Cb,Cr] = RGB2YCbCR(R,G,B);
Ys(y,x) = Y;
Cbs(y,x) = Cb;
Crs(y,x) = Cr;
end
end
subplot(245);
imshow(im);
title('original');
subplot(246);
imshow(Ys);
title('Luminance');
subplot(247);
imshow(Cbs);
title('Color diff(Blue)');
subplot(248);
imshow(Crs);
title('Color diff(Red)');
function [Y,Cb,Cr] = RGB2YCbCR(R,G,B)
Y = 0.299 * R + 0.587 * G + 0.114 * B - 128;
Cb = - 0.1687 * R - 0.3313 * G + 0.5 * B + 128;
Cr = 0.5 * R - 0.4187 * G - 0.0813 * B + 128;
end
end