将RGB图像转化成HSI图像,并对I通道进行MSR算法分离出反射分量和照射分量代码
时间: 2024-01-15 13:02:42 浏览: 157
C++实现图像处理RGB与HSI相互转换代码
以下是Python实现代码:
```python
import numpy as np
import cv2
def rgb_to_hsi(image):
"""
Convert RGB image to HSI image.
"""
r, g, b = cv2.split(image)
# normalize to [0, 1]
r, g, b = r / 255.0, g / 255.0, b / 255.0
num = 0.5 * ((r - g) + (r - b))
den = np.sqrt((r - g)**2 + (r - b) * (g - b))
theta = np.arccos(num / (den + 1e-6))
h = np.zeros_like(theta)
# H channel
h[b > g] = 2 * np.pi - theta[b > g]
h[b <= g] = theta[b <= g]
# S channel
s = 1 - 3 * np.minimum(r, np.minimum(g, b))
# I channel
i = (r + g + b) / 3.0
# Scale I channel to [0, 255]
i = i * 255
# Convert H channel to degrees
h = h * 180 / np.pi
# Combine HSI channels
hsi = cv2.merge((h, s, i))
return hsi
def msr(image):
"""
MSR algorithm for image dehazing.
"""
hsi = rgb_to_hsi(image)
# I channel
i = hsi[:, :, 2]
# Calculate the minimum value of I channel
min_I = np.min(i)
# Compute the ratio map
ratio = (i - min_I) / (i + 1e-6)
# Clip the ratio map
ratio = np.clip(ratio, 0.0, 1.0)
# Apply the ratio map to the HSI channels
hsi[:, :, 0] = hsi[:, :, 0] * ratio
hsi[:, :, 1] = hsi[:, :, 1] * ratio
hsi[:, :, 2] = i * ratio
# Convert back to RGB
rgb = cv2.cvtColor(hsi, cv2.COLOR_HSV2BGR)
return rgb
# Load RGB image
image = cv2.imread('image.jpg')
# Apply MSR algorithm for image dehazing
dehazed_image = msr(image)
# Display input and output images
cv2.imshow('Input', image)
cv2.imshow('Output', dehazed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`rgb_to_hsi`函数用于将RGB图像转化成HSI图像,`msr`函数用于对I通道进行MSR算法分离出反射分量和照射分量。最后,加载RGB图像并应用MSR算法进行去雾处理,将结果显示出来。
阅读全文