Python-openCV对比度增强

#coding=utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

img=cv2.imread('timg.jpeg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
h,w=img.shape

'''分段线性变换'''
'''
img=np.array(img,dtype=np.uint8)
for i in range(h):
    for j in range(w):
       if (img[i][j]<=100):
        img[i][j]=img[i][j]/2
       else:
        img[i][j] = img[i][j]
'''

'''画出图像直方图'''
#pixelSequence=img.flatten()
#plt.hist(pixelSequence, bins=50, color='steelblue', normed=False)
#plt.show()

'''直方图均匀化'''
Imax=np.max(img)
Imin=np.min(img)
a=float((255-0)/Imax-Imin)
b=0-a*Imin
output_img=a*img+b
output_img=output_img.astype(np.uint8)
pixelSequence=output_img.flatten()
plt.hist(pixelSequence, bins=50, color='steelblue', normed=False)
plt.show()
cv2.imshow('img',output_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(Python)