def calcGrayHist(image):
rows,clos = image.shape
grahHist = np.zeros([256],np.uint64)
for r in range(rows):
for c in range(clos):
grahHist[image[r][c]] +=1
return grahHist
if __name__ == "__main__":
image = cv2.imread("D://python/p1.jpg", cv2.IMREAD_GRAYSCALE)
rows,cols = image.shape
grayHist = calcGrayHist(image)
zeroCumuMoment = np.zeros([256], np.uint32)
for p in range(256):
if p == 0:
zeroCumuMoment[p] = grayHist[0]
else:
zeroCumuMoment[p] = zeroCumuMoment[p-1] + grayHist[p]
output = np.zeros([256],np.uint8)
cofficient = 256.0/(rows*cols)
for p in range(256):
q = cofficient * float(zeroCumuMoment[p])-1
if q >=0:
output[p] = np.math.floor(q)
else:
output[p] = 0
equalHistimg = np.zeros(image.shape,np.uint8)
for r in range(rows):
for c in range(cols):
equalHistimg[r][c] = output[image[r][c]]
cv2.imshow('image',image)
cv2.imshow('histimage',equalHistimg)
cv2.waitKey(0)
cv2.destroyAllWindows()