Python实现PS滤镜特效之扇形变换效果示例
作者:Matrix_11 时间:2021-05-08 17:58:03
本文实例讲述了Python实现PS滤镜特效之扇形变换效果。分享给大家供大家参考,具体如下:
这里用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形,具体的算法原理和效果图可以参考附录说明
import numpy as np
from skimage import img_as_float
import matplotlib.pyplot as plt
from skimage import io
import math
import numpy.matlib
file_name2='D:/Visual Effects/PS Algorithm/4.jpg'
img=io.imread(file_name2)
img = img_as_float(img)
# control the radius of the inner circle
radius = 150
# control the distance between the inner circle and outer circle
high = 200
angle = 0
spreadAngle = math.pi
# set the center of the circle, proportion of the image size
centerX = 0.5
centerY = 1.0
row, col, channel = img.shape
icenterX = col * centerX
icenterY = row * centerY
img_out = img * 0
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)
xx_dif = x_mask - icenterX
yy_dif = y_mask - icenterY
theta = np.arctan2(-yy_dif, -xx_dif+0.0001)
r = np.sqrt(xx_dif*xx_dif + yy_dif * yy_dif)
theta = np.mod(theta, 2 * math.pi)
x1_mask = col * theta/(spreadAngle+0.00001)
y1_mask = row * (1-(r-radius)/(high+0.00001))
'''
mask = x1_mask < 0
x1_mask = x1_mask * (1 - mask)
mask = x1_mask > (col - 1)
x1_mask = x1_mask * (1 - mask) + (x1_mask * 0 + col -2) * mask
mask = y1_mask < 0
y1_mask = y1_mask * (1 - mask)
mask = y1_mask > (row -1)
y1_mask = y1_mask * (1 - mask) + (y1_mask * 0 + row -2) * mask
'''
int_x = np.floor (x1_mask)
int_x = int_x.astype(int)
int_y = np.floor (y1_mask)
int_y = int_y.astype(int)
for ii in range(row):
for jj in range (col):
new_xx = int_x [ii, jj]
new_yy = int_y [ii, jj]
if x1_mask [ii, jj] < 0 or x1_mask [ii, jj] > col -1 :
continue
if y1_mask [ii, jj] < 0 or y1_mask [ii, jj] > row -1 :
continue
img_out[ii, jj, :] = img[new_yy, new_xx, :]
plt.figure (1)
plt.title('www.jb51.net')
plt.imshow (img)
plt.axis('off')
plt.figure (2)
plt.title('www.jb51.net')
plt.imshow (img_out)
plt.axis('off')
plt.show()
附录:PS 滤镜— —扇形warp
clc;
clear all;
close all;
addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
I=imread('4.jpg');
I=double(I);
Image=I/255;
[height, width, depth]=size(Image);
% set the parameters
radius = 150; % control the radius of the inner circle
high = 200; % control the distance between the inner circle and outer circle
angle = 0;
spreadAngle=pi;
centerX = 0.5; % set the center of the circle, proportion of the image size
centerY = 1.0;
icenterX=width*centerX;
icenterY=height*centerY;
Image_new=Image*0;
for i=1:height
for j=1:width
dx=j-icenterX;
dy=i-icenterY;
theta=atan2(-dy, -dx)+angle;
r=sqrt(dy*dy+dx*dx);
theta=mod(theta, 2*pi);
x=width * theta/(spreadAngle+0.00001);
y=height * (1-(r-radius)/(high+0.00001));
% % 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(i,j,:)=(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运行效果:
原图
效果图
希望本文所述对大家Python程序设计有所帮助。
来源:http://blog.csdn.net/matrix_space/article/details/72286194
标签:Python,PS滤镜
0
投稿
猜你喜欢
基于javascript实现全国省市二级联动下拉选择菜单
2023-09-14 06:08:52
pandas按条件筛选数据的实现
2021-10-04 03:07:54
PDO::_construct讲解
2023-06-06 03:22:31
vue关于eslint空格缩进等的报错问题及解决
2024-05-10 14:09:26
深入浅出的SQL server 查询优化
2010-07-02 20:58:00
Oracle数据表分区的策略
2010-07-28 12:59:00
C#三种判断数据库中取出的字段值是否为空(NULL) 的方法
2024-01-22 06:57:17
vue下载excel文件的四种方法实例
2024-04-09 10:44:56
Python基于内置库pytesseract实现图片验证码识别功能
2022-05-26 07:40:53
python利用百度云接口实现车牌识别的示例
2021-06-05 12:52:34
浅析vue-router原理
2023-07-02 17:09:19
python使用技巧-查找文件
2021-11-10 18:48:21
Python常见数字运算操作实例小结
2022-01-02 21:04:16
简单介绍Python中的len()函数的使用
2022-08-19 16:27:02
golang之判断元素是否在数组内问题
2024-03-15 02:24:49
简单代码屏蔽超级链接虚线框
2008-02-03 11:34:00
pytorch训练时的显存占用递增的问题解决
2021-04-20 07:12:45
解读python中的类型提示(type hint)
2023-07-15 20:16:27
asp数据转换函数示例
2008-04-13 06:52:00
Oracle的默认用户密码
2012-07-11 15:29:22