本文用 Python 实现 PS 滤镜特效,Marble Filter, 这种滤镜使图像产生不规则的扭曲,看起来像某种玻璃条纹, 具体的代码如下:

import numpy as np
import math
import numpy.matlib
from skimage import io
import random
from skimage import img_as_float
import matplotlib.pyplot as plt def Init_arr():
B = 256
P = np.zeros((B+B+2, 1))
g1 = np.zeros((B+B+2, 1))
g2 = np.zeros((B+B+2, 2))
g3 = np.zeros((B+B+2, 3))
N_max = 1e6
for i in range(B+1):
P[i] = i
g1[i] = (((math.floor(random.random()*N_max)) % (2*B))-B)*1.0/B
g2[i, :] = (np.mod((np.floor(np.random.rand(1, 2)*N_max)), (2*B))-B)*1.0/B
g2[i, :] = g2[i, :] / np.sum(g2[i, :] **2)
g3[i, :] = (np.mod((np.floor(np.random.rand(1, 3)*N_max)), (2*B))-B)*1.0/B
g3[i, :] = g3[i, :] / np.sum(g3[i, :] **2) for i in range(B, -1, -1):
k = P[i]
j = math.floor(random.random()*N_max) % B
P [i] = P [j]
P [j] = k P[B+1:2*B+2]=P[0:B+1];
g1[B+1:2*B+2]=g1[0:B+1];
g2[B+1:2*B+2, :]=g2[0:B+1, :]
g3[B+1:2*B+2, :]=g3[0:B+1, :]
P = P.astype(int)
return P, g1, g2, g3 def Noise_2(x_val, y_val, P, g2): BM=255
N=4096 t = x_val + N
bx0 = ((np.floor(t).astype(int)) & BM) + 1
bx1 = ((bx0 + 1).astype(int) & BM) + 1
rx0 = t - np.floor(t)
rx1 = rx0 - 1.0 t = y_val + N
by0 = ((np.floor(t).astype(int)) & BM) + 1
by1 = ((bx0 + 1).astype(int) & BM) + 1
ry0 = t - np.floor(t)
ry1 = rx0 - 1.0 sx = rx0 * rx0 * (3 - 2.0 * rx0)
sy = ry0 * ry0 * (3 - 2.0 * ry0) row, col = x_val.shape q1 = np.zeros((row, col ,2))
q2 = q1.copy()
q3 = q1.copy()
q4 = q1.copy() for i in range(row):
for j in range(col):
ind_i = P[bx0[i, j]]
ind_j = P[bx1[i, j]]
b00 = P[ind_i + by0[i, j]]
b01 = P[ind_i + by1[i, j]]
b10 = P[ind_j + by0[i, j]]
b11 = P[ind_j + by1[i, j]] q1[i, j, :] = g2[b00, :]
q2[i, j, :] = g2[b10, :]
q3[i, j, :] = g2[b01, :]
q4[i, j, :] = g2[b11, :] u1 = rx0 * q1[:, :, 0] + ry0 * q1[:, :, 1]
v1 = rx1 * q2[:, :, 0] + ry1 * q2[:, :, 1]
a = u1 + sx * (v1 - u1) u2 = rx0 * q3[:, :, 0] + ry0 * q3[:, :, 1]
v2 = rx1 * q4[:, :, 0] + ry1 * q4[:, :, 1]
b = u2 + sx * (v2 - u2) out = (a + sy * (b - a)) * 1.5
return out file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name) img = img_as_float(img)
row, col, channel = img.shape xScale = 25.0
yScale = 25.0
turbulence =0.25 xx = np.arange (col)
yy = np.arange (row) x_mask = numpy.matlib.repmat (xx, row, 1)
y_mask = numpy.matlib.repmat (yy, col, 1)
y_mask = np.transpose(y_mask) x_val = x_mask / xScale
y_val = y_mask / yScale Index = np.arange(256) sin_T=-yScale*np.sin(2*math.pi*(Index)/255*turbulence);
cos_T=xScale*np.cos(2*math.pi*(Index)/255*turbulence) P, g1, g2, g3 = Init_arr() Noise_out = Noise_2(x_val, y_val, P, g2) Noise_out = 127 * (Noise_out + 1) Dis = np.floor(Noise_out) Dis[Dis>255] = 255
Dis[Dis<0] = 0
Dis = Dis.astype(int) img_out = img.copy() for ii in range(row):
for jj in range(col): new_x = jj + sin_T[Dis[ii, jj]]
new_y = ii + cos_T[Dis[ii, jj]] if (new_x > 0 and new_x < col-1 and new_y > 0 and new_y < row-1):
int_x = int(new_x)
int_y = int(new_y) img_out[ii, jj, :] = img[int_y, int_x, :] plt.figure(1)
plt.imshow(img)
plt.axis('off'); plt.figure(2)
plt.imshow(img_out)
plt.axis('off'); plt.show();

图像的效果可以参考我之前写的博客:

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

Python: PS 滤镜特效 -- Marble Filter的更多相关文章

  1. Python: PS 滤镜--水波特效

    本文用 Python 实现 PS 滤镜中的 水波特效 import numpy as np from skimage import img_as_float import matplotlib.pyp ...

  2. Python: PS 滤镜--旋涡特效

    本文用Python 实现 PS 滤镜的旋涡特效,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/42215 ...

  3. Python: PS 滤镜--碎片特效

    本文用 Python 实现 PS 滤镜中的碎片特效,这个特效简单来说就是将图像在 上,下,左,右 四个方向做平移,然后将四个方向的平移的图像叠加起来做平均.具体的效果图可以参考之前的博客 http:/ ...

  4. Python: PS 滤镜--波浪特效

    本文用 Python 实现 PS 滤镜的波浪特效,具体效果可以参考之前的博客 http://blog.csdn.net/matrix_space/article/details/42215221 im ...

  5. Python: PS 滤镜--素描

    本文用 Python 实现 PS 滤镜中的素描特效,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/386 ...

  6. Python: PS滤镜--径向模糊

    本文用 Python 实现 PS 滤镜中的径向模糊特效,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/3 ...

  7. Python: PS 滤镜--高反差保留 (High pass)

    本文用 Python 实现 PS 滤镜中的 高反差保留 特效,具体的算法原理和图像效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/deta ...

  8. Python: PS 滤镜--扇形变换

    本文用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形,具体的算法原理和效果图可以参考以前的博客: http://blog.csdn.net/matrix_sp ...

  9. Python: PS 滤镜--USM 锐化

    本文用 Python 实现 PS 滤镜中的 USM 锐化效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...

随机推荐

  1. POJ 1151 线段树+扫描线

    题意:求矩形面积的并 思路: 注意是[l,mid][mid,r] 这是真正的线段了 就当扫描线模板使吧~ //By SiriusRen #include <cmath> #include ...

  2. POJ 1274 二分图匹配

    匈牙利算法 裸题 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  3. hdu 1022 - 数据结构 栈

    题目链接 按序列a进栈,问能不能按序列b出栈. 遍历b,如果当前元素进过栈了,那么必须和栈顶元素相同.如果没进过栈则按a序列压栈,直到遇到当前元素. #include <iostream> ...

  4. PHP安装curl扩展

    昨天在写文章的时候,突然出现了一个很顽皮的bug. 一直跳到404页面??? 于是我赶紧打开debug,看看什么情况! 弹出的错误是 :Call to undefined function Home\ ...

  5. yii2.0缓存篇之片段缓存

    片段缓存指的是缓存页面内容中的某个片段.默认缓存 60秒. return $this->renderPartial("ca");                        ...

  6. NodeJS代码调试

    1.在Chrome打开chrome://flags/#enable-devtools-experiments 2.激活Developer Tools experiments 3.重启Chrome 4. ...

  7. 作诗(si)[分块]

    题目描述 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好是作诗. 由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅读 ...

  8. js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join

    js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join

  9. 题解 P1179 【数字统计】

    嚯嚯嚯,这道题很显然是削弱版的51nod P1042. 那么显然我们需要使用数位DP解题. 思路大致是这样的: 对于每一个数字,考虑三种影响关系: 1. 它对低位的影响 2. 它对高位的影响 3. 高 ...

  10. struts2文件上传需要注意的

    ① 必须封装三个字段:文件.文件类型.文件名,而且这三个字段的名字的前面几个字母是一样的 如: private File upload; private String uploadContentTyp ...