1#coding:utf-8 2''' 3灰度拉伸 4定义:灰度拉伸,也称对比度拉伸,是一种简单的线性点运算。作用:扩展图像的 5 直方图,使其充满整个灰度等级范围内 6公式: 7g(x,y) = 255 / (B - A) * [f(x,y) - A], 8其中,A = min[f(x,y)],最小灰度级;B = max[f(x,y)],最大灰度级; 9 f(x,y)为输入图像,g(x,y)为输出图像 10 11缺点:如果灰度图像中最小值A=0,最大值B=255,则图像没有什么改变 12 13''' 14 15import cv2 16import numpy as np 17 18def grey_scale(image): 19 img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) 20 21 rows,cols = img_gray.shape 22 flat_gray = img_gray.reshape((cols * rows,)).tolist() 23 A = min(flat_gray) 24 B = max(flat_gray) 25 print('A = %d,B = %d' %(A,B)) 26 output = np.uint8(255 / (B - A) * (img_gray - A) + 0.5) 27 return output 28 29src = cv2.imread('datas/f4.jpg') 30result = grey_scale(src) 31cv2.imshow('src',cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)) 32cv2.imshow('result',result) 33 34cv2.waitKey() 35cv2.destroyAllWindows()
