图像的循环归一化代码python3
时间: 2023-11-04 15:18:35 浏览: 70
循环归一化(Cycle normalization)是一种常用的图像增强方法,可以使得图像在色彩和光照方面更加稳定。以下是使用Python 3实现循环归一化的代码:
```python
import numpy as np
def cycle_normalization(img):
"""
循环归一化
:param img: 输入图像,必须为RGB格式
:return: 循环归一化后的图像
"""
h, w, c = img.shape
img = img.astype(np.float32)
for i in range(c):
channel = img[:, :, i]
max_val = np.max(channel)
min_val = np.min(channel)
mid_val = (max_val + min_val) / 2.0
if mid_val <= 128:
img[:, :, i] = (channel - min_val) / (2 * mid_val)
else:
img[:, :, i] = (channel - mid_val) / (2 * (255 - mid_val))
img = np.clip(img, 0, 1)
return img.astype(np.uint8)
```
使用方法:
```python
import cv2
# 读取图像
img = cv2.imread('test.jpg')
# 循环归一化
img_norm = cycle_normalization(img)
# 显示原图和归一化后的图像
cv2.imshow('Original', img)
cv2.imshow('Normalization', img_norm)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文