import cv2
import numpy as np
import math


def RGB_TO_HSI(img):

    #load im with 32 bit
    with np.errstate(divide='ignore', invalid='ignore'):
        bgr = np.float32(img)/255

        blue = bgr[:,:,0]
        green = bgr[:,:,1]
        red = bgr[:,:,2]

    #Calculate Intensity

    def calc_intensity(red,blue,gree):
        return np.divide(blue+green+red,3)

    #Calculate Saturation
    def calc_saturation(red,blue,green):

        minimum = np.minimum(np.minimum(red,green),blue)

        saturation = 1 - (3/(red + green+blue+0.001)*minimum)

        return saturation

    #Calculate Hue

    def calc_hue(red,blue,green):

        hue = np.copy(red)

        for i in range(0,blue.shape[0]):
            for j in range(0,blue.shape[1]):

                hue[i][j] = 0.5* ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / \
                            np.sqrt((red[i][j] - green[i][j])**2 + ((red[i][j] - blue[i][j])*(green[i][j] - blue[i][j])))

                hue[i][j] = math.acos(hue[i][j])


                if blue[i][j] <= green[i][j]:
                    hue[i][j] = hue[i][j]
                else:
                    hue[i][j] = ((360 * math.pi)/ 180.0) - hue[i][j]

        return hue


#merge channels values

    hsi = cv2.merge((calc_hue(red,blue,green),calc_saturation(red,blue,green),calc_intensity(red,blue,green)))

    return hsi
