Python: PS 图层混合算法汇总
本文用 Python 实现了PS 中的图层混合算法,把很多常见的图层混合算法都汇总到了一起,比起以前写的算法,就是用矩阵运算代替了很耗时的for 循环,运行效率有所提升。具体的代码如下:
import matplotlib.pyplot as plt
from skimage import io
import math
import numpy as np
# image fusion
file_name='D:/Visual Effects/PS Algorithm/2.jpg';
img_1=io.imread(file_name)
img_1 = img_1/255.0
file_name2='D:/Visual Effects/PS Algorithm/3.jpg'
img_2=io.imread(file_name2)
img_2 = img_2/255.0
# 不透明度
def Transparent(img_1, img_2, alpha):
img = img_1 * alpha + img_2 * (1-alpha)
return img
# 正片叠底
def Multiply (img_1, img_2):
img = img_1 * img_2
return img
# 颜色加深
def Color_burn (img_1, img_2):
img = 1 - (1 - img_2) / (img_1 + 0.001)
mask_1 = img < 0
mask_2 = img > 1
img = img * (1-mask_1)
img = img * (1-mask_2) + mask_2
'''
row, col, channel = img.shape
for i in range(row):
for j in range(col):
img[i, j, 0] = min(max(img[i, j, 0], 0), 1)
img[i, j, 1] = min(max(img[i, j, 1], 0), 1)
img[i, j, 2] = min(max(img[i, j, 2], 0), 1)
'''
return img
# 颜色减淡
def Color_dodge(img_1, img_2):
img = img_2 / (1.0 - img_1 + 0.001)
mask_2 = img > 1
img = img * (1-mask_2) + mask_2
return img
# 线性加深
def Linear_burn(img_1, img_2):
img = img_1 + img_2 - 1
mask_1 = img < 0
img = img * (1-mask_1)
return img
# 线性减淡
def Linear_dodge(img_1, img_2):
img = img_1 + img_2
mask_2 = img > 1
img = img * (1-mask_2) + mask_2
return img
# 变亮
def Lighten(img_1, img_2):
img = img_1 - img_2
mask = img > 0
img = img_1 * mask + img_2 * (1-mask)
return img
# 变暗
def Dark(img_1, img_2):
img = img_1 - img_2
mask = img < 0
img = img_1 * mask + img_2 * (1-mask)
return img
# 滤色
def Screen(img_1, img_2):
img = 1- (1-img_1)*(1-img_2)
return img
# 叠加
def Overlay(img_1, img_2):
mask = img_2 < 0.5
img = 2 * img_1 * img_2 * mask + (1-mask) * (1- 2 * (1-img_1)*(1-img_2))
return img
# 柔光
def Soft_light(img_1, img_2):
mask = img_1 < 0.5
T1 = (2 * img_1 -1)*(img_2 - img_2 * img_2) + img_2
T2 = (2 * img_1 -1)*(np.sqrt(img_2) - img_2) + img_2
img = T1 * mask + T2 * (1-mask)
return img
# 强光
def Hard_light(img_1, img_2):
mask = img_1 < 0.5
T1 = 2 * img_1 * img_2
T2 = 1 - 2 * (1 - img_1) * (1 - img_2)
img = T1 * mask + T2 * (1-mask)
return img
# 亮光
def Vivid_light(img_1, img_2):
mask = img_1 < 0.5
T1 = 1 - (1 - img_2)/(2 * img_1 + 0.001)
T2 = img_2 / (2*(1-img_1) + 0.001)
mask_1 = T1 < 0
mask_2 = T2 > 1
T1 = T1 * (1-mask_1)
T2 = T2 * (1-mask_2) + mask_2
img = T1 * mask + T2 * (1 - mask)
return img
# 点光
def Pin_light(img_1, img_2):
mask_1 = img_2 < (img_1 * 2 -1)
mask_2 = img_2 > 2 * img_1
T1 = 2 * img_1 -1
T2 = img_2
T3 = 2 * img_1
img = T1 * mask_1 + T2 * (1 - mask_1) * (1 - mask_2) + T3 * mask_2
return img
# 线性光
def Linear_light(img_1, img_2):
img = img_2 + img_1 * 2 - 1
mask_1 = img < 0
mask_2 = img > 1
img = img * (1-mask_1)
img = img * (1-mask_2) + mask_2
return img
# 实色混合
def Hard_mix(img_1, img_2):
img = img_1 + img_2
mask = img_1 + img_2 > 1
img = img * (1-mask) + mask
img = img * mask
return img
alpha = 0.5
# img = Transparent(img_1, img_2, alpha)
# img = Multiply (img_1, img_2)
# img = Color_burn(img_1, img_2)
# img = Color_dodge(img_1, img_2)
# img = Linear_burn(img_1, img_2)
# img = Linear_dodge(img_1, img_2)
# img = Lighten(img_1, img_2)
# img = Dark (img_1, img_2)
# img = Screen(img_1, img_2)
# img = Overlay(img_1, img_2)
# img = Soft_light(img_1, img_2)
# img = Hard_light(img_1, img_2)
# img = Vivid_light(img_1, img_2)
# img = Pin_light(img_1, img_2)
# img = Linear_light(img_1, img_2)
img = Hard_mix(img_1, img_2)
# show the image
plt.figure(1)
plt.imshow(img_1)
plt.axis('off');
plt.figure(2)
plt.imshow(img_2)
plt.axis('off');
plt.figure(3)
plt.imshow(img)
plt.axis('off');
plt.show()
所有的算法原理以及效果图可以参考我以前的博客:
http://blog.csdn.net/matrix_space/article/details/22416241
http://blog.csdn.net/matrix_space/article/details/22425209
http://blog.csdn.net/matrix_space/article/details/22426633
http://blog.csdn.net/matrix_space/article/details/22427285
http://blog.csdn.net/matrix_space/article/details/22488159
http://blog.csdn.net/matrix_space/article/details/22488467
Python: PS 图层混合算法汇总的更多相关文章
- OpenCV——PS 图层混合算法(一)
详细的算法原理能够參考 PS图层混合算法之中的一个(不透明度,正片叠底,颜色加深,颜色减淡) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #de ...
- OpenCV——PS 图层混合算法 (三)
具体的算法原理可以参考 PS图层混合算法之三(滤色, 叠加, 柔光, 强光) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ ...
- OpenCV——PS 图层混合算法 (二)
具体的算法原理可以参考 PS图层混合算法之二(线性加深,线性减淡,变亮,变暗) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define PS ...
- OpenCV——PS图层混合算法(六)
具体的算法原理可以参考: PS图层混合算法之六(差值,溶解, 排除) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGO ...
- OpenCV——PS 图层混合算法 (四)
具体的算法原理可以参考 PS图层混合算法之四(亮光, 点光, 线性光, 实色混合) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define ...
- PS图层混合算法之六(差值,溶解, 排除)
差值模式: 查看每个通道中的颜色信息,比较底色和绘图色,用较亮的像素点的像素值减去较暗的像素点的像素值.与白色混合将使底色反相:与黑色混合则不产生变化. 排除模式可生成和差值模式相似的效果,但比差值模 ...
- PS图层混合算法之三(滤色, 叠加, 柔光, 强光)
滤色模式: 作用结果和正片叠底刚好相反,它是将两个颜色的互补色的像素值相乘,然后除以255得到的最终色的像素值.通常执行滤色模式后的颜色都较浅.任何颜色和黑色执行滤色,原色不受影响;任何颜色和白色执行 ...
- PS图层混合算法之二(线性加深,线性减淡,变亮,变暗)
线性加深模式: 查看每个通道的颜色信息,通过降低"亮度"使底色的颜色变暗来反映绘图色,和白色混合没变化. Linear Burn 线形加深 C=A+B-1 如果上下层的像素值之和小 ...
- PS图层混合算法之一(不透明度,正片叠底,颜色加深,颜色减淡)
下列公式中,A代表了上面图层像素的色彩值(A=像素值/255),B代表下面图层像素的色彩值(B=像素值/255),C代表了混合像素的色彩值(真实的结果像素值应该为255*C).该公式也应用于层蒙板. ...
随机推荐
- yii框架原生代码
http://www.cnblogs.com/duanxz/p/3480254.htm
- 链表(list)--c实现
做c的开发有1年多了,期间写过c++,感觉基础不够好,补上去,不丢人.o(^▽^)o to better myself. #include <stdio.h> #include <s ...
- 重载和const形参
1.int lookup(string p); 2.int lookup(const string p);//同1 3.int lookup(string *);//传入一个指针,指针指向string ...
- Python学习————字典的增删改查
增加:dic1['KEY'] = value -->若之前有KEY,则会覆盖.若没有KEY,则新增至尾处dic.setdefault('KEY',value/None) --->若之前有K ...
- 【codeforces 816A】Karen and Morning
[题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...
- 收集整理的openstack java封装 api的第三方实现的选择
Apache jclouds 地址:http://jclouds.apache.org/guides/openstack/ 一个开源库,java实现,支持cloudstack,openstack以及各 ...
- @SpringBootApplication cannot be resolved to a type In STS
@SpringBootApplication cannot be resolved to a type In STS 学习了:https://stackoverflow.com/questions/4 ...
- nginx模块开发
开发方法參考淘宝的教程 这个模块的功能是向client发送一个文件,类似于网页上的另存为功能 #include <ngx_config.h> #include <ngx_core.h ...
- List of content management systems
https://en.wikipedia.org/wiki/List_of_content_management_systems Microsoft ASP.NET Name Platform Sup ...
- 安卓操作sqlite3,增删改查
创建 layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...