52. 彩色直方图均衡化

彩色直方图均衡化的步骤:

  • 读取图片信息
  • 各通道值计数与归一化
  • 计算累积概率
  • 创建三通道映射表
  • 完成三通道映射
  • 显示彩色直方图均衡化效果
import cv2 
import numpy as np
import matplotlib.pyplot as plt

# 1 读取图片信息
img = cv2.imread('1.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height = imgInfo[0] 
width = imgInfo[1]

# 2 各通道值计数与归一化
# 创建float类型的一维数组
count_b = np.zeros(256, np.float)
count_g = np.zeros(256, np.float)
count_r = np.zeros(256, np.float)
# 通道值计数
for i in range(0, height):
    for j in range(0, width):
        (b, g, r) = img[i, j]
        index_b =  int(b)
        index_g =  int(g)
        index_r =  int(r)
        count_b[index_b] = count_b[index_b] + 1
        count_g[index_g] = count_g[index_g] + 1
        count_r[index_r] = count_r[index_r] + 1
# 数据归一化
for i in range(0, 255):
    count_b[i] = count_b[i] / (height * width)
    count_g[i] = count_g[i] / (height * width)
    count_r[i] = count_r[i] / (height * width)
    
# 3 计算累积概率
sum_b = float(0)
sum_g = float(0)
sum_r = float(0)
for i in range(0, 256):
    
    sum_b = sum_g + count_b[i]
    sum_g = sum_g + count_g[i]
    sum_r = sum_r + count_r[i]
    # 便于打印各累积概率
    count_b[i] = sum_b
    count_g[i] = sum_g
    count_r[i] = sum_r
print(count_b)  # 累积到1
print(count_g)
print(count_r)

# 4 创建三通道映射表
map_b = np.zeros(256, np.uint16) # 2^16
map_g = np.zeros(256, np.uint16)
map_r = np.zeros(256, np.uint16)
for i in range(0, 256):
    map_b[i] = np.uint16(count_b[i] * 255)
    map_g[i] = np.uint16(count_g[i] * 255)
    map_r[i] = np.uint16(count_r[i] * 255)
    
# 5 完成三通道映射
dst = np.zeros((height, width, 3), np.uint8)
for i in range(0, height):
    for j in range(0, width):
        (b, g, r) = img[i, j]
        b = map_b[b]
        g = map_g[g]
        r = map_r[r]
        dst[i, j] = (b, g, r)

# 6 显示彩色直方图均衡化效果
cv2.imshow('dst', dst)
cv2.waitKey(0)

BGR各自的部分累积概率如下:


蓝色通道的部分累积概率
绿色通道的部分累积概率
红色通道的部分累积概率

彩色直方图均衡化效果如下:


image.png

你可能感兴趣的:(52. 彩色直方图均衡化)