将rgb转化成为hsi 并利用retinex算法将亮度分量分解成L和R成分python代码
时间: 2023-11-28 11:49:50 浏览: 102
rgb到hsi的转化
3星 · 编辑精心推荐
以下是将RGB转换为HSI的Python代码:
```python
import numpy as np
def rgb_to_hsi(rgb_img):
# 将RGB图像的像素值归一化到[0, 1]范围内
r, g, b = rgb_img[:, :, 0] / 255.0, rgb_img[:, :, 1] / 255.0, rgb_img[:, :, 2] / 255.0
# 计算亮度、饱和度、色相
i = (r + g + b) / 3.0
s = 1 - (3.0 / (r + g + b + 1e-10) * np.minimum(np.minimum(r, g), b))
h = np.arccos((0.5 * ((r - g) + (r - b))) / (np.sqrt((r - g) ** 2 + (r - b) * (g - b)) + 1e-10))
h[b > g] = 2 * np.pi - h[b > g]
h /= 2 * np.pi
# 将HSI图像的像素值从[0, 1]范围内转换回[0, 255]
h, s, i = h * 255, s * 255, i * 255
hsi_img = np.zeros_like(rgb_img)
hsi_img[:, :, 0], hsi_img[:, :, 1], hsi_img[:, :, 2] = h, s, i
return hsi_img.astype(np.uint8)
```
以下是利用Retinex算法将亮度分量分解成L和R成分的Python代码:
```python
import cv2
import numpy as np
def retinex_decomposition(img, sigma_list):
img = img.astype(np.float32)
img_sum = np.zeros_like(img)
img_min = np.ones_like(img) * np.inf
for sigma in sigma_list:
img_blur = cv2.GaussianBlur(img, (0, 0), sigma)
img_diff = img - img_blur
img_sum += np.log10(img_diff + 1e-10)
img_min = np.minimum(img_min, img_diff)
R = np.exp(img_sum / len(sigma_list))
L = np.log10(np.maximum(img_min, 1e-10))
return L, R
```
其中,`img`为输入图像,`sigma_list`是高斯核的标准差列表,返回值`L`和`R`分别为亮度分量和反射分量。
阅读全文