Está en la página 1de 7

TAREA: CONVERTIR IMAGEN DE HSI A RGB EN OPEN CV A PARTIR DE UN

CÓDIGO EN MATLAB

➢ Código en Matlab

%% CONVERSION DE IMAGEN HSI A RGB


figure,imshow (HSI);title ('Imagen HSI');
%Obtenemos las componente Matiz (H:Hue), Saturacion (S)e Intensidad
(I)
H1=HSI(:,:,1);
S1=HSI(:,:,2);
I1=HSI(:,:,3);
%Multiplicamos Matiz(H) por 360 para representarlo en el rango [0
360]
H1=H1*360;
%Definimos las dimensiones de las componentes RGB
R1=zeros(size(H1));
G1=zeros(size(H1));
B1=zeros(size(H1));
RGB1=zeros([size(H1),3]);
%Sector RG (0<=H<120)
B1(H1<120)=I1(H1<120).*(1-S1(H1<120));
R1(H1<120)=I1(H1<120).*(1+((S1(H1<120).*cosd(H1(H1<120)))./cosd(60-
H1(H1<120))));
G1(H1<120)=3.*I1(H1<120)-(R1(H1<120)+B1(H1<120));
%Sector GB (120<=H<240)
%Restamos 120 del matiz
H2=H1-120;
R1(H1>=120&H1<240)=I1(H1>=120&H1<240).*(1-S1(H1>=120&H1<240));
G1(H1>=120&H1<240)=I1(H1>=120&H1<240).*(1+((S1(H1>=120&H1<240).*cos
d(H2(H1>=120&H1<240)))./cosd(60-H2(H1>=120&H1<240))));
B1(H1>=120&H1<240)=3.*I1(H1>=120&H1<240)-
(R1(H1>=120&H1<240)+G1(H1>=120&H1<240));
%BR Sector(240<=H<=360)
H2=H1-240;
G1(H1>=240&H1<=360)=I1(H1>=240&H1<=360).*(1-S1(H1>=240&H1<=360));
B1(H1>=240&H1<=360)=I1(H1>=240&H1<=360).*(1+((S1(H1>=240&H1<=360).*
cosd(H2(H1>=240&H1<=360)))./cosd(60-H2(H1>=240&H1<=360))));
R1(H1>=240&H1<=360)=3.*I1(H1>=240&H1<=360)-
(G1(H1>=240&H1<=360)+B1(H1>=240&H1<=360));
%formamos la imagen RGB
RGB1(:,:,1)=R1;
RGB1(:,:,2)=G1;
RGB1(:,:,3)=B1;
%representamos la imagen en un rango de [0,255]
RGB1=im2uint8(RGB1);
figure,imshow(RGB1);title('Imagen RGB');
➢ Código en open CV:

✓ Primero: Convertimos la imagen de RGB a HSI


import cv2
import numpy as np

def RGB2HSI(imagen_rgb):
imagen_temp = imagen_rgb/255 #imagen rgb [0,1]

#componentes B[:,:0],G[:,:,1],R[:,:,2]
b,g,r = cv2.split(imagen_temp)
#dimensiones de la imagen
# fil = imagen_rgb.shape[0] #filas
# col = imagen_rgb.shape[1] #columnas

e=1e-6

#calculo de I
suma = r+g+b
I = suma/3.0

#calculo de H
num_h=0.5*((r-g)+(r-b))
den_h=np.sqrt((r-g)**2+(r-b)*(g-b))
H=np.degrees(np.arccos(num_h/(den_h+e))) #[0,180]

H[b>g]=360.0-H[b>g]
H=H/360.0

#calculo de S
#valor_min = np.min(imagen_temp,2)
valor_min = np.minimum(np.minimum(r,g),b)
S=1.0-(3.0/(suma+e))*valor_min

#img_hsi=np.zeros([fil,col,3])
# img_hsi[:,:,0]=I
# img_hsi[:,:,1]=S
# img_hsi[:,:,2]=H
img_hsi = cv2.merge((I,S,H))

return img_hsi

✓ Segundo: Convertimos la imagen de HSI a RGB

def HSI2RGB(imagen_hsi):
I1,S1,H1 = cv2.split(imagen_hsi)
fil = imagen_hsi.shape[0] #filas
col = imagen_hsi.shape[1] #columnas

R1=np.zeros((fil,col))
G1=np.zeros((fil,col))
B1=np.zeros((fil,col))

#normalizamos H1
H1=H1*360.0
HR=np.deg2rad(H1)

#Sector RG (0<=H<120)
B1[HR<120]=I1[HR<120]*(1.0-S1[HR<120]);

R1[HR<120]=I1[HR<120]*(1.0+(S1[HR<120]*np.cos(HR[HR<120]))/(np.cos((
np.pi/3)-HR[HR<120])))
G1[HR<120]=3.0*I1[HR<120]-(R1[HR<120]+B1[HR<120])

#Sector GB (120<=H<240)
#Restamos 120 del matiz
H2=H1-120.0
H2=np.deg2rad(H2)

R1[np.logical_and(HR>=120,HR<240)]=I1[np.logical_and(HR>=120,HR<240)
]*(1.0-S1[np.logical_and(HR>=120,HR<240)])

G1[np.logical_and(HR>=120,HR<240)]=I1[np.logical_and(HR>=120,HR<240)
]*(1.0+((S1[np.logical_and(HR>=120,HR<240)]*np.cos(H2[np.logical_and(HR
>=120,HR<240)]))/np.cos((np.pi/3)-H2[np.logical_and(HR>=120,HR<240)])))

B1[np.logical_and(HR>=120,HR<240)]=3.0*I1[np.logical_and(HR>=120,HR<
240)]-
(R1[np.logical_and(HR>=120,HR<240)]+G1[np.logical_and(HR>=120,HR<24
0)])

#BR Sector(240<=H<=360)
H2=H1-240.0;
H2=np.deg2rad(H2)

G1[np.logical_and(HR>=240,HR<=360)]=I1[np.logical_and(HR>=240,HR<=3
60)]*(1.0-S1[np.logical_and(HR>=240,HR<=360)])

B1[np.logical_and(HR>=240,HR<=360)]=I1[np.logical_and(HR>=240,HR<=36
0)]*(1.0+((S1[np.logical_and(HR>=240,HR<=360)]*np.cos(H2[np.logical_and(
HR>=240,HR<=360)]))/np.cos(np.pi/3-
H2[np.logical_and(HR>=240,HR<=360)])))

R1[np.logical_and(HR>=240,HR<=360)]=3.0*I1[np.logical_and(HR>=240,HR
<=360)]-
(G1[np.logical_and(HR>=240,HR<=360)]+B1[np.logical_and(HR>=240,HR<=
360)])
#formamos la imagen RGB
# img_rgb = np.zeros([fil,col,3])
# img_rgb[:,:,0]=B1
# img_rgb[:,:,1]=G1
# img_rgb[:,:,2]=R1
img_rgb = cv2.merge((B1,G1,R1))
return img_rgb

imagen = cv2.imread('\\Users\\guise\\Pictures\\Saved Pictures\\Tigre.jpeg')


imagen_hsi = RGB2HSI(imagen)
imagen_rgb = HSI2RGB(imagen_hsi)
cv2.imshow('imagen original',imagen)
cv2.imshow('imagen rgb2hsi',imagen_hsi)
cv2.imshow('imagen hsi2rgb',imagen_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
➢ Imagen original e imagen de RGB a HSI

➢ Imagen de RBG a HSI e imagen de HSI a RGB

También podría gustarte