# -*- coding: utf-8 -*-
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
if __name__=="__main__":
if len(sys.argv)>1 :
img = cv2.imread(sys.argv[1],0)
else:
print('None')
cv2.imshow('img',img)
h,w=img.shape
pixelSequence=img.reshape([h*w,])
numbins=256
histogram,bins,patch=plt.hist(pixelSequence,numbins,facecolor='blue',histtype='bar')
plt.xlabel('gray level')
plt.ylabel('number of pixels')
y_maxValue=np.max(histogram)
plt.axis([0,255,0,y_maxValue])
plt.show()
fi=img/255.0 #图像归一化
gamma=0.5 #伽马值,0<gamma<1:对比度增加
o=np.power(fi,gamma)
cv2.imshow("o",o)
pixelSequence=o.reshape([h*w,])
histogram,bins,patch=plt.hist(pixelSequence,numbins,facecolor='blue',histtype='bar')
plt.xlabel('gray level')
plt.ylabel('number of pixels')
y_maxValue=np.max(histogram)
plt.axis([0,1,0,y_maxValue])
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
原图:
伽马变换后: