Histogram equalization (source)

# Of gray levels is calculated cumulative probability, then: current = current pixel value of the accumulated probability of the pixel 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. a 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'd
for I in Range (0,256):
COUNT [I] = COUNT [I] / (height * width)
# calculates cumulative probability
SUM = a 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)

效果图:

 

Guess you like

Origin www.cnblogs.com/cxxBoo/p/11480681.html