0% found this document useful (0 votes)
22 views

Modul 7

The document describes 10 functions for converting between different color models: 1. RGB to CMY and back 2. RGB to YIQ and back 3. RGB to YCbCr and back 4. RGB to HSV and back 5. RGB to HSI and back The functions take color values as input, perform the necessary calculations based on the specific color model conversion, and output the converted color values. Standard formulas are used for the conversions between the different color representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Modul 7

The document describes 10 functions for converting between different color models: 1. RGB to CMY and back 2. RGB to YIQ and back 3. RGB to YCbCr and back 4. RGB to HSV and back 5. RGB to HSI and back The functions take color values as input, perform the necessary calculations based on the specific color model conversion, and output the converted color values. Standard formulas are used for the conversions between the different color representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Modul 7

1. Konversi RGB ke CMY

function [C, M, Y, K] = RGBkeCMY(R,G,B);


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 ||...
max(max(B)) > 1.0
R = double(R)/255;
G = double(G)/255;
B = double(B)/255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(R);
for m = 1: tinggi
for n = 1: lebar
b = min([(1-R(m,n)) (1-G(m,n)) (1-B(m,n))]);
if b == 1
C(m,n) = 0;
M(m,n) = 0;
Y(m,n) = 0;
else
C(m,n) = (1.0 - R(m,n) - u * b);
M(m,n) = (1.0 - G(m,n) - u * b);
Y(m,n) = (1.0 - B(m,n) - u * b);
K(m,n) = b * b;
end
end
end
C = uint8(C * 255);
M = uint8(M * 255);
Y = uint8(Y * 255);
K = uint8(K * 255);

2. Konversi CMYke RGB

function [R, G, B] = CMYkeRGB(C, M, Y, K);


C = double(C);
M = double(M);
Y = double(Y);
K = double(K);
if max(max(C)) > 1.0 || max(max(M)) > 1.0 ||...
max(max(Y)) > 1.0 || max(max(K)) > 1.0
C = double(C)/255;
M = double(M)/255;
Y = double(Y)/255;
K = double(K)/255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(C);
for m = 1: tinggi
for n = 1: lebar
Kb = K(m,n) / b;
if Kb == 1
R(m,n) = 0;
G(m,n) = 0;
B(m,n) = 0;
else
R(m,n) = (1.0 - C(m,n) - u * Kb);
G(m,n) = (1.0 - M(m,n) - u * Kb);
B(m,n) = (1.0 - Y(m,n) - u * Kb);
end
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

3. Konversi RGB ke YIQ

function [Y, I, Q] = RGBkeYIQ(R,G,B);


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 ||...
max(max(B)) > 1.0
R = double(R)/255;
G = double(G)/255;
B = double(B)/255;
end
[tinggi, lebar] = size(R);
for m = 1: tinggi
for n = 1: lebar
Y(m,n) = 0.299*R(m,n) + 0.587*G(m,n) + 0.144*B(m,n);
I(m,n) = 0.596*R(m,n) - 0.274*G(m,n) - 0.322*B(m,n);
Q(m,n) = 0.211*R(m,n) + 0.523*G(m,n) + 0.312*B(m,n);
end
end
Y = uint8(Y * 255);
I = uint8(I * 255);
Q = uint8(Q * 255);

4. Konversi YIQ ke RGB

function [R, G, B] = YIQkeRGB(Y, I, Q);


Y = double(Y);
I = double(I);
Q = double(Q);
if max(max(Y)) > 1.0 || max(max(I)) > 1.0 ||...
max(max(Q)) > 1.0
Y = double(Y)/255;
I = double(I)/255;
Q = double(Q)/255;
end
[tinggi, lebar] = size(Y);
for m = 1: tinggi
for n = 1: lebar
R(m,n) = Y(m,n) + 0.956*I(m,n) + 0.621*Q(m,n);
G(m,n) = Y(m,n) - 0.272*I(m,n) - 0.647*Q(m,n);
B(m,n) = Y(m,n) - 1.106*I(m,n) + 1.703*Q(m,n);
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

5. Konversi RGB ke YCB

function [Y, Cb, Cr] = RGBkeYCB(R,G,B);


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 ||...
max(max(B)) > 1.0
R = double(R)/255;
G = double(G)/255;
B = double(B)/255;
end
[tinggi, lebar] = size(R);
for m = 1: tinggi
for n = 1: lebar
Y(m,n) = 0.299*R(m,n) + 0.587*G(m,n) + 0.144*B(m,n);
Cb(m,n) = -0.1687*R(m,n) - 0.33126*G(m,n) - 0.5*B(m,n);
Cr(m,n) = 0.5*R(m,n) - 0.41869*G(m,n) + 0.08131*B(m,n);
end
end
Y = uint8(Y * 255);
Cb = uint8(Cb * 255);
Cr = uint8(Cr * 255);

6. Konversi YCB ke RGB

function [R, G, B] = YCBkeRGB(Y, Cb, Cr);


Y = double(Y);
Cb = double(Cb);
Cr = double(Cr);
if max(max(Y)) > 1.0 || max(max(Cb)) > 1.0 ||...
max(max(Cr)) > 1.0
Y = double(Y)/255;
Cb = double(Cb)/255;
Cr = double(Cr)/255;
end
[tinggi, lebar] = size(Y);
for m = 1: tinggi
for n = 1: lebar
R(m,n) = Y(m,n) + 1.402*Cr(m,n);
G(m,n) = Y(m,n) - 0.34414*Cb(m,n) - 0.71414*Cr(m,n);
B(m,n) = Y(m,n) + 1.7720*Cb(m,n);
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

7. Konversi RGB ke HSV

function [H, S, V] = RGBkeHSV(R,G,B);


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 ||...
max(max(B)) > 1.0
R = double(R)/255;
G = double(G)/255;
B = double(B)/255;
end
[tinggi, lebar] = size(R);
for m = 1: tinggi
for n = 1: lebar
minrgb = min([R(m,n) G(m,n) B(m,n)]);
maxrgb = max([R(m,n) G(m,n) B(m,n)]);
V(m,n) = maxrgb
delta = maxrgb - minrgb;
if maxrgb == 0
S(m,n) = 0;
else
S(m,n) = 1 - minrgb / maxrgb;
end
if S(m,n) == 0
H(m,n) = 0;
else
SV = S(m,n) * V(m,n);
if R(m,n) == maxrgb
H(m,n) = (G(m,n) - B(m,n))/ SV;
elseif G(m,n) == maxrgb
H(m,n) = 2 + (B(m,n) - R(m,n))/ SV;
else
H(m,n) = 4 + (R(m,n) - G(m,n))/ SV;
end
H(m,n) = H(m,n) * 60;
if H(m,n) < 0
H(m,n) = H(m,n) + 360;
end
end
end
end
H = uint8(H * 255);
S = uint8(S * 255);
V = uint8(V * 255);

8. Konversi HSV ke RGB

function [R, G, B] = HSVkeRGB(H, S, V);


H = double(H);
S = double(S);
V = double(V);
if max(max(H)) > 1.0 || max(max(S)) > 1.0 ||...
max(max(V)) > 1.0
H = double(H)/255*360;
S = double(S)/255;
V = double(V)/255;
end
[tinggi, lebar] = size(H);
for m = 1: tinggi
for n = 1: lebar
if S(m,n) == 0
R(m,n) = V(m,n);
G(m,n) = V(m,n);
B(m,n) = V(m,n);
else
H(m,n) = H(m,n) / 60;
sektor = floor(H(m,n));
faktor = H(m,n) - sektor;
p = V(m,n) * (1 - S(m,n));
q = V(m,n) * (1 - S(m,n) * faktor);
t = V(m,n) * (1 - S(m,n) * (1-faktor));
switch sektor
case 0
R(m,n) = V(m,n);
G(m,n) = t;
B(m,n) = p;
case 1
R(m,n) = q;
G(m,n) = V(m,n);
B(m,n) = p;
case 2
R(m,n) = p;
G(m,n) = V(m,n);
B(m,n) = t;
case 3
R(m,n) = p;
G(m,n) = q;
B(m,n) = V(m,n);
case 4
R(m,n) = t;
G(m,n) = p;
B(m,n) = V(m,n);
otherwise
R(m,n) = V(m,n);
G(m,n) = p;
B(m,n) = q;
end
end
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

9. Konversi RGB ke HSI

function [H,S,I] = RGBkeHSI(R,G,B)


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || ...
max(max(B)) > 1.0
R = double(R)/ 255;
G = double(G)/ 255;
B = double(B)/ 255;
end
[tinggi, lebar] = size(R);
for m=1 : tinggi
for n=1 : lebar
minrgb = min([R(m,n) G(m,n) B(m,n)]);
I(m,n) = (R(m,n) + G(m,n) + B(m,n)) / 3.0;
if R(m,n) == G(m,n) && G(m,n) == B(m,n)
S(m,n) = 0;
H(m,n) = 0;
else
S(m,n) =1 - 3 * minrgb / ...
(R(m,n)+G(m,n)+B(m,n));
y = (R(m,n)-G(m,n)+R(m,n)-B(m,n))/2;
x = (R(m,n)-G(m,n))*(R(m,n)-G(m,n)) + ...
(R(m,n)-B(m,n)) * (G(m,n)-B(m,n));
x = sqrt(x);
sudut = acos(y/x) * 180/pi;
if B(m,n) > G(m,n)
H(m,n) = 360 - sudut;
else
H(m,n) = sudut;
end
end
end
end
H = uint8(H * 255/360);
S = uint8(S * 255);
I = uint8(I * 255);
10. Konversi HSI ke RGB

function [R,G,B] = HSIkeRGB(H,S,I)


H = double(H);
S = double(S);
I = double(I);
if max(max(H)) > 1.0 || max(max(S)) > 1.0 || ...
max(max(I)) > 1.0
H = double(H)/ 255 * 360;
S = double(S)/ 255;
I = double(I)/ 255;
end
[tinggi, lebar] = size(H);
for m=1 : tinggi
for n=1 : lebar
if I(m,n) == 0
R(m,n) = 0;
G(m,n) = 0;
B(m,n) = 0;
elseif S(m,n) == 0
R(m,n) = I(m,n);
G(m,n) = I(m,n);
B(m,n) = I(m,n);
else
if H(m,n) < 0
H(m,n) = H(m,n) + 360.0;
end
skala = 3 * I(m,n);
if H(m,n) <= 120
sudut1 = H(m,n) * 0.017453292;
sudut2 = (60 - H(m,n)) * 0.017453292;
B(m,n) = (1 - S(m,n)) / 3;
R(m,n) = (1 + (S(m,n) * cos(sudut1)/...
cos(sudut2))) /3;
G(m,n) = 1 - R(m,n) - B(m,n);
B(m,n) = B(m,n) * skala;
R(m,n) = R(m,n) * skala;
G(m,n) = G(m,n) * skala;
elseif H(m,n) <= 240
H(m,n) = H(m,n) - 120;
sudut1 = H(m,n) * 0.017453292;
sudut2 = (60 - H(m,n)) * 0.017453292;
R(m,n) = (1 - S(m,n)) / 3;
G(m,n) = (1 + (S(m,n) * cos(sudut1)/...
cos(sudut2))) /3;
B(m,n) = 1 - R(m,n) - G(m,n);
R(m,n) = R(m,n) * skala;
G(m,n) = G(m,n) * skala;
B(m,n) = B(m,n) * skala;
else
H(m,n) = H(m,n) - 240;
sudut1 = H(m,n) * 0.017453292;
sudut2 = (60 - H(m,n)) * 0.017453292;
G(m,n) = (1 - S(m,n)) / 3;
B(m,n) = (1 + (S(m,n) * cos(sudut1)/...
cos(sudut2))) /3;
R(m,n) = 1 - G(m,n) - B(m,n);
G(m,n) = G(m,n) * skala;
B(m,n) = B(m,n) * skala;
R(m,n) = R(m,n) * skala;
end
end
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

11. Konversi RGB ke HSL

function [H,S,L] = RGBkeHSL(R,G,B)


R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || ...
max(max(B)) > 1.0
R = double(R)/ 255;
G = double(G)/ 255;
B = double(B)/ 255;
end
[tinggi, lebar] = size(R);
for m=1 : tinggi
for n=1 : lebar
minrgb = min([R(m,n) G(m,n) B(m,n)]);
maxrgb = max([R(m,n) G(m,n) B(m,n)]);
if maxrgb == minrgb
S(m,n) = 0;
H(m,n) = 0;
else
L(m,n) = (minrgb + maxrgb) / 2;
d = (maxrgb - minrgb);
if L(m,n) <= 0.5
S(m,n) = d / (maxrgb + minrgb);
else
S(m,n) = d / (2 - minrgb - maxrgb);
end
if R(m,n) == maxrgb
H(m,n) = (G(m,n)-B(m,n))/d;
elseif G(m,n) == maxrgb
H(m,n) = 2+(B(m,n)-R(m,n))/d;
else
H(m,n) = 4+(R(m,n)-G(m,n))/d;
end
H(m,n) = H(m,n) * 60;
if H(m,n) < 0
H(m,n) = H(m,n) + 360;
end
end
end
end
H = uint8(H * 255/360);
S = uint8(S * 255);
L = uint8(L * 255);

12. Konversi HSL ke RGB

function [R,G,B] = HSLkeRGB(H,S,L)


H = double(H);
S = double(S);
L = double(L);
if max(max(H)) > 1.0 || max(max(S)) > 1.0 || ...
max(max(L)) > 1.0
H = double(H)/ 255 * 360;
S = double(S)/ 255;
L = double(L)/ 255;
end
[tinggi, lebar] = size(H);
for m=1 : tinggi
for n=1 : lebar
if L(m,n) <= 0.5
v = L(m,n) * (1 + S(m,n));
else
v = L(m,n) + S(m,n) - L(m,n) * S(m,n);
end
if v == 0
R(m,n) = 0;
G(m,n) = 0;
B(m,n) = 0;
else
terkecil = 2 * L(m,n) - v;
sv = (v - terkecil) / v;
if H(m,n) == 360;
H(m,n) = 0;
else
H(m,n) = H(m,n) / 60;
end
sektor = floor(H(m,n));
frak = H(m,n) - sektor;
vsf = v * sv * frak;
mid1 = terkecil + vsf;
mid2 = v - vsf;
switch sektor
case 0
R(m,n) = v;
G(m,n) = mid1;
B(m,n) = terkecil;
case 1
R(m,n) = mid2;
G(m,n) = v;
B(m,n) = terkecil;
case 2
R(m,n) = terkecil;
G(m,n) = v;
B(m,n) = mid1;
case 3
R(m,n) = terkecil;
G(m,n) = mid2;
B(m,n) = v;
case 4
R(m,n) = mid1;
G(m,n) = terkecil;
B(m,n) = v;
case 5
R(m,n) = v;
G(m,n) = terkecil;
B(m,n) = mid2;
end
end
end
end
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);

13. Memperoleh Statistika Warna

function [stat] = statwarna(berkas)


RGB = double(imread(berkas));
[m,n,d] = size(RGB);
if (d ~= 3)
disp('Citra harus berupa citra berwarna');
return;
end
jum_r=0;
jum_g=0;
jum_b=0;
jum_piksel = m * n;
for baris = 1:m
for kolom = 1:n
jum_r = jum_r + RGB(baris, kolom, 1);
jum_g = jum_g + RGB(baris, kolom, 2);
jum_b = jum_b + RGB(baris, kolom, 3);
end
end
mean_r = jum_r / jum_piksel;
mean_g = jum_g / jum_piksel;
mean_b = jum_b / jum_piksel;
jum_dev_r = 0;
jum_dev_g = 0;
jum_dev_b = 0;
jum_skew_r = 0;
jum_skew_g = 0;
jum_skew_b = 0;
jum_cur_r = 0;
jum_cur_g = 0;
jum_cur_b = 0;
for baris = 1:m
for kolom = 1:n
jum_dev_r = jum_dev_r + ...
(RGB(baris, kolom, 1) - mean_r)^2;
jum_dev_g = jum_dev_g + ...
(RGB(baris, kolom, 2) - mean_g)^2;
jum_dev_b = jum_dev_b + ...
(RGB(baris, kolom, 3) - mean_b)^2;
jum_skew_r = jum_skew_r + ...
(RGB(baris, kolom, 1) - mean_r)^3;
jum_skew_g = jum_skew_g + ...
(RGB(baris, kolom, 2) - mean_g)^3;
jum_skew_b = jum_skew_b + ...
(RGB(baris, kolom, 3) - mean_b)^3;
jum_cur_r = jum_cur_r + ...
(RGB(baris, kolom, 1) - mean_r)^4;
jum_cur_g = jum_cur_g + ...
(RGB(baris, kolom, 2) - mean_g)^4;
jum_cur_b = jum_cur_b + ...
(RGB(baris, kolom, 3) - mean_b)^4;
end
end
dev_r = sqrt(jum_dev_r/jum_piksel);
dev_g = sqrt(jum_dev_g/jum_piksel);
dev_b = sqrt(jum_dev_b/jum_piksel);
skew_r = jum_skew_r/ (jum_piksel * (dev_r^3));
skew_g = jum_skew_g/ (jum_piksel * (dev_g^3));
skew_b = jum_skew_b/ (jum_piksel * (dev_b^3));
cur_r = jum_cur_r / (jum_piksel * (dev_r^4)) - 3;
cur_g = jum_cur_g / (jum_piksel * (dev_g^4)) - 3;
cur_b = jum_cur_b / (jum_piksel * (dev_b^4)) - 3;
stat.mean_r = mean_r;
stat.mean_g = mean_g;
stat.mean_b = mean_b;
stat.dev_r = dev_r;
stat.dev_g = dev_g;
stat.dev_b = dev_b;
stat.skew_r = skew_r;
stat.skew_g = skew_g;
stat.skew_b = skew_b;
stat.cur_r = cur_r;
stat.cur_g = cur_g;
stat.cur_b = cur_b;

14. Menghitung Jumlah Warna

function [jumlah] = jumwarna(berkas)


RGB = double(imread(berkas));
[m,n,d] = size(RGB);
if (d ~= 3)
disp('Citra harus berupa citra berwarna');
return;
end
RGB = double(RGB);
Data = zeros(1, m * n);
jum = 0;
for i=1:m
for j=1:n
jum = jum + 1;
r = RGB(i,j,1);
g = RGB(i,j,2);
b = RGB(i,j,3);
Data(jum) = bitshift(r,16) + bitshift(g,8) + b;
end
end
Data = sort(Data);
jwarna = 1;
for i = 1 : jum - 1
if Data(i) ~= Data(i+1)
jwarna = jwarna + 1;
end
end
jumlah = jwarna;

You might also like