带色彩恢复因子的多尺度Retinex算法图像亮度增强代码
时间: 2024-05-10 13:16:07 浏览: 105
以下是带色彩恢复因子的多尺度Retinex算法图像亮度增强的Python代码:
```python
import cv2
import numpy as np
def MSR(image, scales):
weight = np.zeros_like(image)
for scale in scales:
blur = cv2.GaussianBlur(image, (0, 0), scale * 0.1)
diff = cv2.absdiff(image, blur)
diff = cv2.GaussianBlur(diff, (0, 0), scale * 0.1)
weight += diff
weight = (weight / len(scales)) + 1.0
result = np.zeros_like(image)
for i in range(3):
result[:, :, i] = np.maximum(np.minimum((image[:, :, i] - weight[:, :, i] + 1.0) * 255.0, 255.0), 0.0)
return result
def MSRCR(image, scales, radius, gamma):
scales_1 = [0.25, 0.5, 1.0, 2.0, 4.0]
scales_2 = [0.5, 1.0, 2.0, 4.0, 8.0]
weights = []
for scale in scales_2:
weight = np.zeros_like(image)
for radius in range(1, 6):
weight += MSR(image, [scale]) * (gamma ** radius)
weights.append(weight)
weight = np.zeros_like(image)
for i in range(3):
weight[:, :, i] = np.mean([weight[:, :, i] for weight in weights], axis=0)
weight = cv2.GaussianBlur(weight, (radius, radius), 0)
result = np.zeros_like(image)
for i in range(3):
result[:, :, i] = np.maximum(np.minimum((image[:, :, i] - weight[:, :, i] + 1.0) * 255.0, 255.0), 0.0)
return result
# 读取原始图像
image = cv2.imread("test.jpg")
# 调用MSRCR函数进行图像亮度增强
result = MSRCR(image, scales=[1.0], radius=15, gamma=1.0)
# 显示增强后的图像
cv2.imshow("Enhanced", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`MSR`函数为多尺度Retinex算法,`MSRCR`函数为带色彩恢复因子的多尺度Retinex算法。在`MSR`函数中,`image`参数为原始图像,`scales`参数为用于计算权重的高斯核尺度列表。在`MSRCR`函数中,`image`参数为原始图像,`scales`参数为用于计算权重的高斯核尺度列表,`radius`参数为色彩恢复因子的高斯核半径,`gamma`参数为色彩恢复因子的衰减系数。最终,`result`为增强后的图像。
阅读全文