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.
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 ratings0% 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.
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
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;