发布于 2018-02-19 00:26:41 | 254 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python教程,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


这篇文章主要介绍了Python实现PS图像调整黑白效果,结合实例形式分析了Python实现PS图像的黑白效果原理与相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现PS图像调整黑白效果。分享给大家供大家参考,具体如下:

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

比起之前的程序,对代码进行了优化,完全用矩阵运算代替了 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 图像调整算法——黑白

黑白调整

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:


%%%%% 程序实现图像的黑白调整功能
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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务