def calcGrayHist(image):
rows,cols=image.shape
grayHist=np.zeros([256],np.uint64)
for r in range(rows):
for c in range(cols):
grayHist[image[r][c]]+=1
return grayHist
def equalHist(imgDir):
imgGray=cv2.imread(imgDir,cv2.IMREAD_GRAYSCALE)
cv2.imshow("raw",imgGray)
rows,cols=imgGray.shape
grayHist=calcGrayHist(imgGray)
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_q=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_q[p]=math.floor(q)
else:
output_q[p]=0
equalHistImage=np.zeros(imgGray.shape,np.uint8)
for r in range(rows):
for c in range(cols):
equalHistImage[r][c]=output_q[imgGray[r][c]]
return equalHistImage
效果图:
?
|