The folder for all pictures conversion from RGB space to HSI space

RGB to HSI, python achieve

RGB images for the most basic features, but many times the RGB color space is not a good picture basis as the clustering of clusters, usually do not meet people's subjective judgment of color similarity.
HSI color space is from the human visual system, a hue (Hue), saturation (Saturation, or by Chroma) and luminance (or the Brightness Intensity) to describe color. HSI color space can be described with a conical model space. This model is described by a cone rather complicated HIS color space, but they can hue, brightness, and color change situation is shown clearly saturation. Usually known as hue and saturation of color, for indicating the type and the degree of color depth. Since the human visual sensitivity to brightness is much stronger than the sensitivity of the color shade, color processing and in order to facilitate identification, the human visual system is often used HSI color space, it is more consistent with human visual characteristics than the RGB color space. In the image processing and computer vision algorithms can be conveniently used a lot in the HSI color space, they can be handled separately and are mutually independent. Therefore, in the HSI color space can greatly simplify the workload analysis and image processing. HSI color space and RGB color space just different representations of the same quantity, thus there is conversion relationship between them.

import cv2
import numpy as np
import scipy.misc
import os


def rgbtohsi(rgb_lwpImg):
    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]))
            if den != 0:
                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
    return hsi_lwpImg


if __name__ == '__main__':
    imgDir = "./"
    imgFoldName = "RGBtoHSI"
    imgs = os.listdir(imgDir + imgFoldName)
    imgNum = len(imgs)  # 图像的个数
    for i in range(imgNum):  # 遍历每张图片
        # img = Image.open(imgDir + imgFoldName + "/" + imgs[i])
        rgb_lwpImg = cv2.imread(imgDir + imgFoldName + "/" + imgs[i])
        hsi_Img = rgbtohsi(rgb_lwpImg)
        scipy.misc.imsave('hsi_Img'+str(i)+'.jpg', hsi_Img)
    # 显示RGB图像及转换的HSI图像
    # rgb_lwpImg = cv2.imread("v2_0140.jpg")
    # hsi_Img = rgbtohsi(rgb_lwpImg)
    # cv2.imshow('rgb_lwpImg', rgb_lwpImg)
    # cv2.imshow('hsi_lwpImg', hsi_lwpImg)
    # key = cv2.waitKey(0) & 0xFF
    # if key == ord('q'):
    #     cv2.destroyAllWindows()

RGB to HSI comparison:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/hnu_zzt/article/details/84872655