Python: PS 图像调整--黑白

本文用Python 实现 PS 里的图像调整–黑白,PS 里的黑白并不是简单粗暴的将图像转为灰度图,而是做了非常精细的处理,具体的算法原理和效果图可以参考以前的博客:

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

比起之前的程序,对代码进行了优化,完全用矩阵运算代替了 for 循环,运算效率提升了很多。具体的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from skimage import io file_name='D:/Image Processing/PS Algorithm/4.jpg';
img=io.imread(file_name) img = img * 1.0 Color_ratio = np.zeros(6) Color_ratio[0]=0.4; # Red
Color_ratio[1]=0.6; # Yellow
Color_ratio[2]=0.4; # Green
Color_ratio[3]=0.6; # Cyan
Color_ratio[4]=0.2; # Blue
Color_ratio[5]=0.8; # Magenta max_val = img.max(axis = 2)
min_val = img.min(axis = 2)
sum_val = img.sum(axis = 2)
mid_val = sum_val - max_val - min_val mask_r = (img[:, :, 0] - min_val - 0.01) > 0
mask_r = 1 - mask_r
mask_g = (img[:, :, 1] - min_val - 0.01) > 0
mask_g = 1 - mask_g
mask_b = (img[:, :, 2] - min_val - 0.01) > 0
mask_b = 1 - mask_b ratio_max_mid = mask_r * Color_ratio[3] + mask_g * Color_ratio[5] + mask_b * Color_ratio[1] mask_r = (img[:, :, 0] - max_val + 0.01) < 0
mask_r = 1 - mask_r mask_g = (img[:, :, 1] - max_val + 0.01) < 0
mask_g = 1 - mask_g mask_b = (img[:, :, 2] - max_val + 0.01) < 0
mask_b = 1 - mask_b ratio_max= mask_r * Color_ratio[4] + mask_g * Color_ratio[0] + mask_b * Color_ratio[2] I_out = max_val * 1.0 I_out = (max_val-mid_val)*ratio_max + (mid_val-min_val)*ratio_max_mid + min_val plt.figure()
plt.imshow(img/255.0)
plt.axis('off') plt.figure(2)
plt.imshow(I_out/255.0, plt.cm.gray)
plt.axis('off') plt.show()
上一篇:PS 图像调整算法——黑白


下一篇:【具体数学--读书笔记】1.1 The Power of Hanoi