Python: PS 图像调整--亮度调整

本文用 Python 实现 PS 图像调整中的亮度调整,具体的算法原理和效果可以参考之前的博客:

http://blog.csdn.net/matrix_space/article/details/22991683

import matplotlib.pyplot as plt
from skimage import io

file_name='D:/Image Processing/PS Algorithm/4.jpg';
img=io.imread(file_name)

Increment = -10.0

img = img * 1.0 
I = (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])/3.0 + 0.001

mask_1 = I > 128.0

r = img [:, :, 0]
g = img [:, :, 1]
b = img [:, :, 2]

rhs = (r*128.0 - (I - 128.0) * 256.0) / (256.0 - I) 
ghs = (g*128.0 - (I - 128.0) * 256.0) / (256.0 - I)
bhs = (b*128.0 - (I - 128.0) * 256.0) / (256.0 - I)

rhs = rhs * mask_1 + (r * 128.0 / I) * (1 - mask_1)
ghs = ghs * mask_1 + (g * 128.0 / I) * (1 - mask_1)
bhs = bhs * mask_1 + (b * 128.0 / I) * (1 - mask_1)

I_new = I + Increment - 128.0

mask_2 = I_new > 0.0

R_new =  rhs + (256.0-rhs) * I_new / 128.0
G_new =  ghs + (256.0-ghs) * I_new / 128.0
B_new =  bhs + (256.0-bhs) * I_new / 128.0

R_new = R_new * mask_2 + (rhs + rhs * I_new/128.0) * (1-mask_2)
G_new = G_new * mask_2 + (ghs + ghs * I_new/128.0) * (1-mask_2)
B_new = B_new * mask_2 + (bhs + bhs * I_new/128.0) * (1-mask_2)

Img_out = img * 1.0

Img_out[:, :, 0] = R_new
Img_out[:, :, 1] = G_new
Img_out[:, :, 2] = B_new

Img_out = Img_out/255.0

# 饱和处理
mask_1 = Img_out  < 0 
mask_2 = Img_out  > 1

Img_out = Img_out * (1-mask_1)
Img_out = Img_out * (1-mask_2) + mask_2

plt.figure()
plt.imshow(img/255.0)
plt.axis('off')

plt.figure(2)
plt.imshow(Img_out)
plt.axis('off')

plt.figure(3)
plt.imshow(I/255.0, plt.cm.gray)
plt.axis('off')

plt.show()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值