限制对比的自适应直方图均衡化未调用封装函数库的python代码
时间: 2023-12-12 19:04:22 浏览: 81
CLAHE.zip_CLAHE_CLAHE简单实现_scientific8bk_对比度自适应直方图均衡化_限制对比度
以下是限制对比的自适应直方图均衡化的Python代码:
```python
import numpy as np
import cv2
def clahe(image, clip_limit=2.0, grid_size=(8, 8)):
# Convert image to LAB color space
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
# Split the LAB image into L, A, and B channels
l_channel, a_channel, b_channel = cv2.split(lab_image)
# Apply CLAHE to the L channel
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=grid_size)
l_channel = clahe.apply(l_channel)
# Merge the L, A, and B channels back into the LAB image
lab_image = cv2.merge((l_channel, a_channel, b_channel))
# Convert the LAB image back to RGB color space
output_image = cv2.cvtColor(lab_image, cv2.COLOR_LAB2BGR)
return output_image
def adaptive_histogram_equalization(image, clip_limit=2.0, grid_size=(8, 8)):
# Convert image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Calculate the size of the image
height, width = gray_image.shape
# Calculate the block size for the adaptive histogram equalization
block_size = int(np.round(np.sqrt(height * width) / 8) * 2 + 1)
# Divide the image into blocks
block_rows = height // block_size
block_columns = width // block_size
# Calculate the maximum value for each block
max_values = np.zeros((block_rows, block_columns))
for i in range(block_rows):
for j in range(block_columns):
block = gray_image[i*block_size:(i+1)*block_size, j*block_size:(j+1)*block_size]
max_values[i, j] = np.max(block)
# Apply CLAHE to each block
for i in range(block_rows):
for j in range(block_columns):
block = gray_image[i*block_size:(i+1)*block_size, j*block_size:(j+1)*block_size]
clahe_block = clahe(block, clip_limit, grid_size)
gray_image[i*block_size:(i+1)*block_size, j*block_size:(j+1)*block_size] = clahe_block
# Interpolate the maximum values for each pixel
max_image = np.zeros_like(gray_image)
for i in range(height):
for j in range(width):
i_block = i // block_size
j_block = j // block_size
if i_block < block_rows - 1 and j_block < block_columns - 1:
max_value = ((i % block_size) / block_size * (j % block_size) / block_size * max_values[i_block+1, j_block+1] +
(i % block_size) / block_size * (block_size - (j % block_size)) / block_size * max_values[i_block+1, j_block] +
(block_size - (i % block_size)) / block_size * (j % block_size) / block_size * max_values[i_block, j_block+1] +
(block_size - (i % block_size)) / block_size * (block_size - (j % block_size)) / block_size * max_values[i_block, j_block])
elif i_block < block_rows - 1:
max_value = ((i % block_size) / block_size * max_values[i_block+1, j_block] +
(block_size - (i % block_size)) / block_size * max_values[i_block, j_block])
elif j_block < block_columns - 1:
max_value = ((j % block_size) / block_size * max_values[i_block, j_block+1] +
(block_size - (j % block_size)) / block_size * max_values[i_block, j_block])
else:
max_value = max_values[i_block, j_block]
max_image[i, j] = max_value
# Apply the adaptive histogram equalization
output_image = np.zeros_like(gray_image)
for i in range(height):
for j in range(width):
output_image[i, j] = 255 * (gray_image[i, j] / max_image[i, j]) ** 0.5
# Convert the output image back to BGR color space
output_image = cv2.cvtColor(output_image, cv2.COLOR_GRAY2BGR)
return output_image
```
这个代码使用了OpenCV库,但没有使用OpenCV的封装函数库。它实现了限制对比的自适应直方图均衡化算法,包括以下步骤:
1. 将输入图像转换为LAB色彩空间,并将其分成L、A和B通道。
2. 对L通道应用CLAHE算法。
3. 将L、A和B通道合并为LAB图像。
4. 将LAB图像转换回BGR色彩空间,并返回输出图像。
5. 将输入图像转换为灰度图像。
6. 计算自适应直方图均衡化的块大小。
7. 将图像分成块。
8. 对每个块应用CLAHE算法。
9. 对每个像素进行最大值插值。
10. 对每个像素应用自适应直方图均衡化。
11. 将输出图像转换回BGR色彩空间,并返回输出图像。
阅读全文