#计算灰度级别的累加概率,然后:当前像素值 = 当前像素的累加概率 * 255
import cv2
import numpy as np
img = cv2.imread('D:/pythonob/imageinpaint/img/flower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
count = np.zeros(256,np.float)
for i in range(0,height):
for j in range(0,width):
pixel = gray[i,j]
index = int(pixel)
count[index] = count[index]+1
for i in range(0,256):
count[i] = count[i]/(height*width)
#计算累计概率
sum = float(0)
for i in range(0,256):
sum = sum + count[i]
count[i] = sum
#计算映射表
map = np.zeros(256,np.uint16)
for i in range(0,256):
map[i] = count[i]*255
for i in range(0,height):
for j in range(0,width):
index = gray[i,j]
gray[i,j] = map[index]
cv2.imshow('aftergGray',gray)
cv2.waitKey(0)
效果图: