服务器之家

服务器之家 > 正文

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

时间:2021-01-09 00:48     来源/作者:Matrix_11

本文实例讲述了Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果。分享给大家供大家参考,具体如下:

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

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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.title('www.zyiz.net')
plt.imshow(img)
plt.axis('off');
plt.figure(2)
plt.title('www.zyiz.net')
plt.imshow(img_out)
plt.axis('off');
plt.show();

运行效果:

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

附:PS 滤镜 Marble 效果原理

?
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
clc;
clear all;
close all;
addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
I=imread('4.jpg');
I=double(I);
Image=I/255;
xScale = 20;
yScale = 20;
amount = 1;
turbulence =0.25;
Image_new=Image;
[height, width, depth]=size(Image);
Index=1:256;
sin_T=-yScale*sin(2*pi*(Index-1)/256*turbulence);
cos_T=xScale*cos(2*pi*(Index-1)/256*turbulence);
[ind, g1, g2, g3]=init_arr();
for ii=1:height
% %   [ind, g1, g2, g3]=init_arr();
  for jj=1:width
    dis=min(max( floor(127*(1+Noise2(jj/xScale, ii/yScale, ind, g2))), 1), 256);
    x=jj+sin_T(dis);
    y=ii+cos_T(dis);
% %     if (x<=1)   x=1; end
% %     if (x>=width)  x=width-1; end;
% %     if (y>=height) y=height-1; end;
% %     if (y<1) y=1;   end;
% %    
    if (x<=1)   continue; end
    if (x>=width)  continue; end;
    if (y>=height) continue; end;
    if (y<1) continue;   end;
    x1=floor(x);
    y1=floor(y);
    p=x-x1;
    q=y-y1;
    Image_new(ii,jj,:)=(1-p)*(1-q)*Image(y1,x1,:)+p*(1-q)*Image(y1,x1+1,:)...
      +q*(1-p)*Image(y1+1,x1,:)+p*q*Image(y1+1,x1+1,:);
  end
end
imshow(Image_new)
imwrite(Image_new, 'out.jpg');

参考来源:http://www.jhlabs.com/index.html

希望本文所述对大家Python程序设计有所帮助。

原文链接:http://blog.csdn.net/matrix_space/article/details/72283287

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部