Skip to content

图像风格化之老照片风格

老照片风格英文名叫做 Sepia Tone algorithm,可以用这个关键词搜索。

变化方式一

一般搜索的算法都是如下公式,非常神奇,简单但却有实效:

nr = (r * 0.393) + (g * 0.769) + (b * 0.189)
ng = (r * 0.349) + (g * 0.686) + (b * 0.168)
nb = (r * 0.272) + (g * 0.534) + (b * 0.131)

变化方式二

上面的方式太不自由了,比较不好的一点是它转换后的图片往往太亮了,所以还有新的方法,其过程如下:

1. 彩色转成黑白图
2. 根据黑白的图给一个新的颜色,这个步骤可以用公式,也可以直接用 LUT 表直接映射

也很简单,本质上也就是转成黑白的图再去上色。下面是参考代码:

src = cv2.imread('./src.jpg')
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY).astype(float)

# LUT 表文件放在 code 目录中,来源:https://www.cnblogs.com/Imageshop/p/3460951.html
lut = np.zeros((256, 3), dtype=np.uint8)
with open('./老照片LUT表.txt', 'r') as f:
    for idx, line in enumerate(f.readlines()):
        lut[idx] = np.array(line.split(',')[0:3], dtype=np.uint8)

nowimg = lut[gray.astype(int)]
cv2.imwrite('./newimg2.jpg', cv2.cvtColor(nowimg, cv2.COLOR_BGR2RGB))

nowimg = src.copy().astype(float)
ar, ag, ab = 40, 20, 0
nowimg[..., 0] = (gray + ab)
nowimg[..., 1] = (gray + ag)
nowimg[..., 2] = (gray + ar)

nowimg = nowimg.clip(0, 255).astype(np.uint8)
cv2.imwrite(f'./newimg3.jpg', nowimg)

结果展示

原图 变换方式一 变换方式二(LUT表) 变换方式二(公式)
src newimg1 newimg2 newimg3
Was this page helpful?

Comments