低照度图像增强python代码
这是一篇2003年的低照度图像增强论文《Adaptive Logarithmic Mapping For Displaying High Contrast Scenes》,论文地址为:
https://domino.mpi-inf.mpg.de/intranet/ag4/ag4publ.nsf/0/53A4B81D590A3EEAC1256CFD003CE441/$file/logmap.pdf
。
主要参考
上面是使用c++复现,我改成python复现一下.
import numpy as np
import cv2
from skimage import transform
def Transform(x):
if x <= 0.05:
return x * 2.64
else:
return 1.099 * np.power(x, 0.9/2.2) - 0.099
if __name__ == '__main__':
fn = "1.jpg"
img = cv2.imread(fn)
#img = transform.resize(img,(600,680)) #将灰度图片大小转换为1024*860
shape = img.shape
s = np.zeros((shape[0],shape[1],3),dtype=np.float)
out_img = np.zeros((shape[0],shape[1],3),dtype=np.uint8)
rows = shape[0]
cols = shape[1]
r = 0; g = 0; b = 0
lwmax = -1.0; base = 0.75
for i in range(rows):
for j in range(cols):
b = img[i][j][0] / 255.0
g = img[i][j][1] / 255.0
r = img[i][j][2] / 255.0
s[i][j][0] = (0.4124*r + 0.3576*g + 0.1805*b)
s[i][j][1] = (0.2126*r + 0.7152*g + 0.0722*b)
s[i][j][2] = (0.0193*r + 0.1192*g + 0.9505*b)
lwmax = max(lwmax, s[i][j][1])
for i in range(rows):
for j in range(cols):
xx = s[i][j][0] / (s[i][j][0] + s[i][j][1] + s[i][j][2])
yy = s[i][j][1] / (s[i][j][0] + s[i][j][1] + s[i][j][2])
tp = s[i][j][1]
#修改CIE:X,Y,Z
s[i][j][1] = 1.0 * np.log(s[i][j][1] + 1) / np.log(2 + 8.0*np.power((s[i][j][1] / lwmax), np.log(base) / np.log(0.5))) / np.log10(lwmax + 1)
x = s[i][j][1] / yy*xx
y = s[i][j][1]
z = s[i][j][1] / yy*(1 - xx - yy)
#转化为用RGB表示
r = 3.2410*x - 1.5374*y - 0.4986*z
g = -0.9692*x + 1.8760*y + 0.0416*z
b = 0.0556*x - 0.2040*y + 1.0570*z
r = max(0, min(r, 1))
g = max(0, min(g, 1))
b = max(0, min(b, 1))
#修正补偿
r = Transform(r); g = Transform(g); b = Transform(b)
out_img[i][j][0] = int(b * 255)
out_img[i][j][1] = int(g * 255)
out_img[i][j][2] = int(r * 255)