Tuesday, December 27, 2011

merubah RGB ke HSI dengan matlab

Contoh perbahan model warna lainnya yaitu dari RGB ke HSI
function rgbtohsi(x)
F=imread('baboon24.bmp');
F=im2double(F);
%F=double(x);
r=F(:,:,1);
g=F(:,:,2);
b=F(:,:,3);
th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps));
H=th;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
S=1-3.*(min(min(r,g),b))./(r+g+b+eps);
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
HE=H*2*pi;
HE=histeq(HE);
HE=HE/(2*pi);
SE=histeq(S);
IE=histeq(I);
choice=input('1:RGB to HSI\n2:Display Hue, Saturation and Intensity Images\n3:HSI to RGB\n4:Hue-Equalization\n5:Saturation-Equalization\n6:Intensity-Equalization\n7:HSI-Equalization\n Enter your choice :');
switch choice
case 1
figure,imshow(F),title('RGB Image');
figure, imshow(hsi),title('HSI Image');
case 2
figure,imshow(F),title('RGB Image');
figure, imshow(H),title('Hue Image');
figure, imshow(S),title('Saturation Image');
figure, imshow(I),title('Intensity Image');
case 3
C=hsitorgb(hsi);
figure,imshow(hsi),title('HSI Image');
figure, imshow(C),title('RGB Image');
case 4
RV=cat(3,HE,S,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI Image');
figure,imshow(F),title('RGB Image');
figure, imshow(C),title('RGB Image-Hue Equalized');
case 5
RV=cat(3,H,SE,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI Image');
figure,imshow(F),title('RGB Image');
figure, imshow(C),title('RGB Image-Saturation Equalized');
case 6
RV=cat(3,H,S,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI Image');
figure,imshow(F),title('RGB Image');
figure, imshow(C),title('RGB Image-Intensity Equalized');
case 7
RV=cat(3,HE,SE,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI Image');
figure,imshow(F),title('RGB Image');
figure, imshow(C),title('RGB Image-HSI Equalized');
otherwise
display('Wrong choice');
end
end
function C=hsitorgb(hsi)
HV=hsi(:,:,1)*2*pi;
SV=hsi(:,:,2);
IV=hsi(:,:,3);
R=zeros(size(HV));
G=zeros(size(HV));
B=zeros(size(HV));
%RG Sector
id=find((0<=HV)& (HV<2*pi/3)); B(id)=IV(id).*(1-SV(id)); R(id)=IV(id).*(1+SV(id).*cos(HV(id))./cos(pi/3-HV(id))); G(id)=3*IV(id)-(R(id)+B(id)); %BG Sector id=find((2*pi/3<=HV)& (HV<4*pi/3)); R(id)=IV(id).*(1-SV(id)); G(id)=IV(id).*(1+SV(id).*cos(HV(id)-2*pi/3)./cos(pi-HV(id))); B(id)=3*IV(id)-(R(id)+G(id)); %BR Sector id=find((4*pi/3<=HV)& (HV<2*pi)); G(id)=IV(id).*(1-SV(id)); B(id)=IV(id).*(1+SV(id).*cos(HV(id)-4*pi/3)./cos(5*pi/3-HV(id))); R(id)=3*IV(id)-(G(id)+B(id)); C=cat(3,R,G,B); C=max(min(C,1),0); end

1 comment:

  1. Easy "water hack" burns 2 lbs OVERNIGHT

    At least 160 thousand men and women are trying a simple and secret "water hack" to burn 1-2 lbs each night in their sleep.

    It is scientific and it works all the time.

    Here's how you can do it yourself:

    1) Grab a drinking glass and fill it with water half full

    2) And now learn this weight losing HACK

    and be 1-2 lbs thinner as soon as tomorrow!

    ReplyDelete