栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

RGB颜色空间和HSI颜色空间相互转换之python实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RGB颜色空间和HSI颜色空间相互转换之python实现

RGB颜色空间和HSI颜色空间相互转换之python实现

// An highlighted block
# RGB2HSI
def RGB2HSI(img1):
    img1 = img1.astype('float32')
    b, g, r = img1[:, :, 0]/255.0, img1[:, :, 1]/255.0, img1[:, :, 2]/255.0

    I = (r+g+b)/3.0

    tem = np.where(b >= g, g, b)
    minValue = np.where(tem >= r, r, tem)
    S = 1 - (3 / (r + g + b)) * minValue

    num1 = 2*r - g - b
    num2 = 2*np.sqrt(((r - g) ** 2) + (r-b)*(g-b))
    deg = np.arccos(num1/num2)
    H = np.where(g >= b, deg, 2*np.pi - deg)

    resImg = np.zeros((img1.shape[0], img1.shape[1],
                    img1.shape[2]), dtype=np.float)
    resImg[:, :, 0], resImg[:, :, 1], resImg[:, :, 2] = H*255, S*255, I*255
    resImg = resImg.astype('uint8')
    return resImg


def HSI2RGB(img):
    H1, S1, I1 = img[:,:,0]/255.0, img[:,:,1]/255.0, img[:,:,2]/255.0
    B = np.zeros((H1.shape[0], H1.shape[1]), dtype='float32')
    G = np.zeros((S1.shape[0], S1.shape[1]), dtype='float32')
    R = np.zeros((I1.shape[0], I1.shape[1]), dtype='float32')
    H = np.zeros((H1.shape[0], H1.shape[1]), dtype='float32')
    
    for i in range(H1.shape[0]):
        for j in range(H1.shape[1]):
            H = H1[i][j]
            S = S1[i][j]
            I = I1[i][j]   
            if (H >=0) & (H < (np.pi * (2/3))):
                B[i][j] = I*(1-S)
                R[i][j] = I * (1 + ((S*np.cos(H))/np.cos(np.pi * (1/3) - H)))
                G[i][j] = 3*I - (B[i][j]+R[i][j])
                
            elif (H >= (np.pi * (2/3))) & (H < np.pi * (4/3)):
                R[i][j] = I*(1-S)
                G[i][j] = I * (1 + ((S*np.cos(H - np.pi * (2/3)))/np.cos(np.pi * (1/2) - H)))
                B[i][j] = 3*I - (G[i][j]+R[i][j])
            elif (H >= (np.pi * (2/3))) & (H < (np.pi * 2)):
                G[i][j] = I*(1-S)
                B[i][j] = I * (1 + ((S*np.cos(H - np.pi * (4/3)))/np.cos(np.pi * (10/9) - H)))
                R[i][j] = 3*I - (G[i][j]+B[i][j])
    img = cv2.merge((B*255, G*255, R*255))
    img = img.astype('uint8')
    return img



img1 = cv2.imread('./t1.jpg')
show('img', img1)
res = RGB2HSI(img1)
show('RGB2HSI', res)
a = HSI2RGB(res)
show('HSI2RGB', a)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号