import cv2
import numpy as np

def rgbtohsi(rgb_lwpImg):
    BadPoint = 0
    BrightPoint = 0
    rows = int(rgb_lwpImg.shape[0])
    cols = int(rgb_lwpImg.shape[1])
    b, g, r = cv2.split(rgb_lwpImg)
    # 归一化到[0,1]
    b = b / 255.0
    g = g / 255.0
    r = r / 255.0
    hsi_lwpImg = rgb_lwpImg.copy()
    H, S, I = cv2.split(hsi_lwpImg)
    for i in range(rows):
        for j in range(cols):
            num = 0.5 * ((r[i, j]-g[i, j])+(r[i, j]-b[i, j]))
            den = np.sqrt((r[i, j]-g[i, j])**2+(r[i, j]-b[i, j])*(g[i, j]-b[i, j]))
            theta = float(np.arccos(num/den))

            if den == 0:
                    H = 0
            elif b[i, j] <= g[i, j]:
                H = theta
            else:
                H = 2*3.14169265 - theta

            min_RGB = min(min(b[i, j], g[i, j]), r[i, j])
            sum = b[i, j]+g[i, j]+r[i, j]
            if sum == 0:
                S = 0
            else:
                S = 1 - 3*min_RGB/sum

            H = H/(2*3.14159265)
            I = sum/3.0
            # 输出HSI图像，扩充到255以方便显示，一般H分量在[0,2pi]之间，S和I在[0,1]之间
            hsi_lwpImg[i, j, 0] = H*255
            hsi_lwpImg[i, j, 1] = S*255
            hsi_lwpImg[i, j, 2] = I*255

            if I > 60:
                BrightPoint+=1
            elif I < 2:
                BadPoint+=1
    return hsi_lwpImg,BadPoint,BrightPoint
if __name__ == '__main__':
    rgb_lwpImg = cv2.imread("badpoing2")
    BadPoint = 0
    BrightPoint = 0
    hsi_lwpImg,BadPoint,BrightPoint = rgbtohsi(rgb_lwpImg)
    print(BadPoint,BrightPoint)
    cv2.imshow('rgb_lwpImg', rgb_lwpImg)
    cv2.imshow('hsi_lwpImg', hsi_lwpImg)

    key = cv2.waitKey(0) & 0xFF
    if key == ord('q'):
        cv2.destroyAllWindows()