retinex图像增强算法代码
时间: 2023-09-25 14:07:09 浏览: 117
以下是Python中使用OpenCV实现Retinex图像增强算法的示例代码:
```
import cv2
import numpy as np
def singleScaleRetinex(img, sigma):
ret = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
return np.uint8(cv2.normalize(ret, None, 0, 255, cv2.NORM_MINMAX))
def multiScaleRetinex(img, sigma_list):
retinex = np.zeros_like(img)
for sigma in sigma_list:
retinex += singleScaleRetinex(img, sigma)
retinex = retinex / len(sigma_list)
return np.uint8(cv2.normalize(retinex, None, 0, 255, cv2.NORM_MINMAX))
def colorRestoration(img, alpha, beta):
img_sum = np.sum(img, axis=2, keepdims=True)
color_restoration = beta * (np.log10(alpha * img) - np.log10(img_sum))
return np.uint8(cv2.normalize(color_restoration, None, 0, 255, cv2.NORM_MINMAX))
def automaticColorEqualization(img, alpha=0.125, beta=0.25, low_clip=0.01, high_clip=0.99):
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
img_yuv[:, :, 0] = cv2.equalizeHist(img_yuv[:, :, 0])
img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
img_output = colorRestoration(img_output, alpha, beta)
for i in range(3):
img_output[:, :, i] = cv2.normalize(img_output[:, :, i], None, 0, 255, cv2.NORM_MINMAX)
img_output[:, :, i] = np.uint8(img_output[:, :, i])
img_output[:, :, i] = np.uint8(
np.clip(img_output[:, :, i], 255 * low_clip, 255 * high_clip))
return img_output
def MSRCP(img, sigma_list=[15, 80, 250], alpha=125, beta=46, low_clip=0.01, high_clip=0.99):
img = np.float64(img) + 1.0
img_retinex = multiScaleRetinex(img, sigma_list)
img_output = automaticColorEqualization(img, alpha, beta, low_clip, high_clip)
img_msrcp = np.zeros_like(img)
for y in range(img_msrcp.shape[0]):
for x in range(img_msrcp.shape[1]):
for c in range(img_msrcp.shape[2]):
img_msrcp[y, x, c] = np.uint8(np.clip(img_output[y, x, c] * (img_retinex[y, x] / 255.0), 0, 255))
return np.uint8(img_msrcp - 1.0)
# 调用示例
img = cv2.imread("test.jpg")
img_msrcp = MSRCP(img)
cv2.imshow("MSRCP", img_msrcp)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`singleScaleRetinex`和`multiScaleRetinex`分别实现了单尺度和多尺度Retinex算法;`colorRestoration`实现了颜色恢复算法;`automaticColorEqualization`实现了自动颜色均衡化算法;`MSRCP`是将前面的算法结合起来实现的MSRCP算法。在调用`MSRCP`函数时,可以根据需要调整参数以达到更好的图像增强效果。
阅读全文