将RGB图像转化成HSI图像,并对I通道进行MSR算法图像增强代码
时间: 2023-12-20 07:03:49 浏览: 241
以下是将RGB图像转换为HSI图像,并对I通道进行MSR算法图像增强的Python代码:
```python
import cv2
import numpy as np
# 将 RGB 图像转换为 HSI 图像
def rgb_to_hsi(img):
img = img.astype(np.float32) / 255.0
r, g, b = cv2.split(img)
eps = 1e-6
num = 0.5 * ((r - g) + (r - b))
den = np.sqrt((r - g)**2 + (r - b)*(g - b)) + eps
theta = np.arccos(num / den)
h = theta.copy()
h[b > g] = 2 * np.pi - h[b > g]
s = 1 - 3 * np.minimum(r, np.minimum(g, b)) / (r + g + b + eps)
i = (r + g + b) / 3
hsi = cv2.merge((h, s, i))
return hsi
# 对 I 通道进行 MSR 算法图像增强
def msr_enhancement(img, a, b, d):
img = img.astype(np.float32)
i = img[:, :, 2]
log_i = np.log(i)
log_i_avg = cv2.boxFilter(log_i, -1, (d, d))
log_i_diff = log_i - log_i_avg
log_i_diff_var = cv2.boxFilter(log_i_diff**2, -1, (d, d))
alpha = b * log_i_diff_var / (i**a + 1e-6)
enhanced_i = i + alpha * log_i_diff
enhanced_i = np.clip(enhanced_i, 0, 255)
enhanced = img.copy()
enhanced[:, :, 2] = enhanced_i
return enhanced.astype(np.uint8)
# 加载图像
img = cv2.imread('test_image.jpg')
# 将 RGB 图像转换为 HSI 图像
hsi = rgb_to_hsi(img)
# 对 I 通道进行 MSR 算法图像增强
enhanced = msr_enhancement(hsi, a=0.1, b=0.5, d=5)
# 将增强后的 HSI 图像转换回 RGB 图像
enhanced_rgb = cv2.cvtColor(enhanced, cv2.COLOR_HSV2BGR)
# 显示原始图像和增强后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Enhanced Image', enhanced_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
需要注意的是,MSR算法的参数a、b、d需要根据图像的特性进行调整。
阅读全文