python rgb转HSI np.where
时间: 2023-10-17 12:26:17 浏览: 96
在Python中,可以使用NumPy库中的`np.where`函数来进行RGB到HSI的转换。
```python
import numpy as np
def rgb_to_hsi(image):
# 将RGB图像的通道值归一化到范围
r, g, b = image[:, :, 0]/255, image[:, :, 1]/255, image[:, :, 2]/255
# 计算亮度分量(Intensity)
intensity = (r + g + b) / 3
# 计算饱和度分量(Saturation)
min_val = np.minimum(np.minimum(r, g), b)
saturation = 1 - (3 / (r + g + b + 1e-10)) * min_val
# 计算色调分量(Hue)
numerator = 0.5 * ((r - g) + (r - b))
denominator = np.sqrt((r - g) ** 2 + (r - b) * (g - b))
hue = np.arccos(numerator / (denominator + 1e-10))
hue = np.where(b <= g, hue, 2 * np.pi - hue)
# 将HSI的值缩放到范围
hue = hue * 255 / (2 * np.pi)
saturation = saturation * 255
intensity = intensity * 255
# 将HSI的三个分量合并成一个图像
hsi_image = np.stack((hue, saturation, intensity), axis=-1)
return hsi_image
# 使用示例
image = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]], dtype=np.uint8)
hsi_image = rgb_to_hsi(image)
```
上述代码中,`rgb_to_hsi`函数将RGB图像转换为HSI图像。其中,`np.minimum`函数用于计算RGB通道的最小值,然后使用`np.where`函数来进行条件判断,根据条件选择对应的值。最后,将HSI的三个分量合并为一个图像。<span class="em">1</span>
#### 引用[.reference_title]
- *1* [OpenCV+Python–RGB转HSI的实现](https://download.csdn.net/download/weixin_38689338/14877908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文