平方变换
将灰度进行平方再除以255,用于
- 降低图像亮度
- 增强亮部对比度
- 减缓暗部对比度
对比度可以看作灰度的斜率
对数变换
将灰度取对数再乘个常数,可以
- 提高图像亮度
- 增强暗部对比度
- 减缓亮部对比度
Q:为什么要加1?
A:为了确保结果是正数
比如拍了夜景图片,就可以用这个处理。
实验中我取c=46,log底数为e
因为np.log默认底数就是e, 255/np.log(256)=45.9。
伽马变换
将灰度幂一下再乘个常数,一般c都取得很小
- 降低图像亮度
- 增强亮部对比度
- 减缓暗部对比度
如果你再阳光下拍了个照,就可以用这个处理
样例
代码
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import cv2
matplotlib.rcParams['font.family'] = 'simHei'
matplotlib.rcParams['axes.unicode_minus'] = False
%matplotlib qt5
img = cv2.imread('666.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
rows,cols = img_gray.shape
img1 = np.ones((rows,cols),dtype = np.uint8)
img2 = np.ones((rows,cols),dtype = np.uint8)
img3 = np.ones((rows,cols),dtype = np.uint8)
#DB = (DA^2)/255
for i in range(rows):
for j in range(cols):
img1[i,j] = pow(img_gray[i,j],2)/255
#DB = 46*ln(DA)
for i in range(rows):
for j in range(cols):
img2[i,j] = np.uint8(np.log(img_gray[i,j]+1)*46)
#DB = 0.0000006031*DA^4
for i in range(rows):
for j in range(cols):
img3[i,j] = np.uint8(6.031e-8*pow(img_gray[i,j],4))
plt.subplot(321)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(322)
plt.imshow(img1,'gray',vmin=0,vmax=255)
plt.title('平方变换')
plt.subplot(323)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(324)
plt.title('对数变换')
plt.imshow(img2,'gray',vmin=0,vmax=255)
plt.subplot(325)
plt.title('原图')
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.subplot(326)
plt.title('伽马变换')
plt.imshow(img3,'gray',vmin=0,vmax=255)