基于均值和方差实现彩色图像的局部增强python代码
时间: 2023-05-14 17:04:56 浏览: 133
以下是基于均值和方差实现彩色图像的局部增强的 Python 代码:
```python
import cv2
import numpy as np
def local_contrast_enhancement(img, ksize=15, alpha=1.5, beta=0.5):
# Convert the image to float32
img = np.float32(img) / 255.0
# Split the image into three channels
b, g, r = cv2.split(img)
# Calculate the mean and standard deviation of each channel
mean_b, std_b = cv2.meanStdDev(b)
mean_g, std_g = cv2.meanStdDev(g)
mean_r, std_r = cv2.meanStdDev(r)
# Apply the local contrast enhancement to each channel
b = (b - mean_b) * alpha / std_b + mean_b + beta
g = (g - mean_g) * alpha / std_g + mean_g + beta
r = (r - mean_r) * alpha / std_r + mean_r + beta
# Merge the channels back into an image
img = cv2.merge((b, g, r))
# Convert the image back to uint8
img = np.uint8(img * 255.0)
return img
```
这个函数接受一个彩色图像作为输入,以及可选的参数 ksize、alpha 和 beta。ksize 是局部均值和方差计算的窗口大小,alpha 和 beta 是增强参数。函数返回增强后的彩色图像。
阅读全文