本文实例讲述了Python实现PS图像调整黑白效果。分享给大家供大家参考,具体如下:
这里用Python 实现 PS 里的图像调整–黑白,PS 里的黑白并不是简单粗暴的将图像转为灰度图,而是做了非常精细的处理,具体的算法原理和效果图可以参考附录说明。
比起之前的程序,对代码进行了优化,完全用矩阵运算代替了 for 循环,运算效率提升了很多。具体的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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 图像调整算法——黑白
黑白调整
Photoshop CS的图像黑白调整功能,是通过对红、黄、绿、青、蓝和洋红等6种颜色的比例调节来完成的。能更精细地将彩色图片转换为高质量的黑白照片。
Photoshop CS图像黑白调整功能的计算公式为:
gray= (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min
公式中:gray为像素灰度值,max、mid和min分别为图像像素R、G、B分量颜色的最大值、中间值和最小值,ratio_max为max所代表的分量颜色(单色)比率,ratio_max_mid则为max与mid两种分量颜色所形成的复色比率。
默认的单色及复色比率为:
Color_Ratio(1)=0.4; %%%% Red
Color_Ratio(2)=0.6; %%%% Yellow
Color_Ratio(3)=0.4; %%%% Green
Color_Ratio(4)=0.6; %%%% Cyan
Color_Ratio(5)=0.2; %%%% Blue
Color_Ratio(6)=0.8; %%%% Magenta
Program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
% % % % % 程序实现图像的黑白调整功能 clc; clear all ; close all ; Image = imread( '9.jpg' ); Image = double(Image); R = Image(:,:, 1 ); G = Image(:,:, 2 ); B = Image(:,:, 3 ); [row, col] = size(R); Gray_img( 1 :row, 1 :col) = 0 ; Sum_rgb = R + G + B; % % % % 各种颜色的默认比率 Color_Ratio( 1 : 6 ) = 0 ; Color_Ratio( 1 ) = 0.4 ; % % % % Red Color_Ratio( 2 ) = 0.6 ; % % % % Yellow Color_Ratio( 3 ) = 0.4 ; % % % % Green Color_Ratio( 4 ) = 0.6 ; % % % % Cyan Color_Ratio( 5 ) = 0.2 ; % % % % Blue Color_Ratio( 6 ) = 0.8 ; % % % % Magenta for i = 1 :row for j = 1 :col r = R(i,j); g = G(i,j); b = B(i,j); Max_value = max (r, max (g,b)); Min_value = min (r, min (g,b)); Mid_value = Sum_rgb(i,j) - Max_value - Min_value; if (Min_value = = r) Index = 0 ; elseif(Min_value = = g) Index = 2 ; else Index = 4 ; end ratio_max_mid = Color_Ratio(mod(Index + 3 , 6 ) + 1 ); if (Max_value = = r) Index = 1 ; elseif(Max_value = = g) Index = 3 ; else Index = 5 ; end ratio_max = Color_Ratio(Index); Temp = (Max_value - Mid_value) * ratio_max + (Mid_value - Min_value)... * ratio_max_mid + Min_value; Gray_img(i,j) = (Max_value - Mid_value) * ratio_max + (Mid_value - Min_value)... * ratio_max_mid + Min_value; end end imshow(Image / 255 ); figure, imshow(Gray_img / 255 ); |
本例Python运行结果如下:
原图:
运行效果图:
希望本文所述对大家Python程序设计有所帮助。
原文链接:http://blog.csdn.net/matrix_space/article/details/72285588