Python-openCV對比度增強
阿新 • • 發佈:2019-02-19
#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()