Python实现PS滤镜特效之扇形变换效果示例
作者:Matrix_11 发布时间:2021-05-08 17:58:03
标签:Python,PS滤镜
本文实例讲述了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


猜你喜欢
- Nonetype和空值是不一致的,可以理解为Nonetype为不存在这个参数,空值表示参数存在,但是值为空判断方式如下:if hostip
- 1、一些准备工作 安装djangopip install django创建django项目进入项目代码存放目录执行命令:djang
- 在提交添加或修改内容时,需要对关键数据进行判空处理,如何在js中判断checkboxlist是否有选择项呢? 具体操作如下: var Che
- 编这个程序是想过节过年,一些重要的纪念日,给亲戚好友发祝福之类的,但要凌晨0点才显得比较有诚意,可我又比较贪睡,常常忘了,所以就有了编个微信
- 核心代码function convert2utf8($string) { return iconv(&
- 我使用的是anaconda安装的环境,其中有一个是h5py,自动安装的是2.7.0的版本,这个版本会导致保存模型时python奔溃。cond
- 微信小程序 滚动选择器(时间日期)详解微信小程序自己封装了很多控件,用起来确实很方便,如果这是Android里面,还需要自己去定
- 0.引言利用python开发,借助Dlib库进行人脸识别,然后将检测到的人脸剪切下来,依次排序显示在新的图像上;实现的效果如下图所示,将图1
- Q&AQ: .js和.min.js文件分别是什么?A: .js是JavaScript 源码文件, .min.js是压缩版的js文件。
- php二维数组排序测试数据 $arr = [
- python线程池ThreadPoolExecutor,传单个参数和多个参数这是线程池传单个参数的from concurrent.futur
- question: Django中对数据库的调用非常的隐蔽,在各种复杂的模块互相拼接继承中很难发现获取数据库内容的部分来,开始试图理解一下下
- 读取和存储dict()与.json格式文件读取.json格式文件并将数据保存到字典中数据文件:hg.json{"商家名称"
- MySQL的sql_mode合理设置sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些
- 要想在不宽裕的页面展现丰富的内容,现在通用的做法使用tab,在一块区域通过tab切换来更换该区域的内容。这篇文章分析了tab设计很在理,今天
- 本文实例讲述了js实现鼠标感应向下滑动隐藏菜单的方法。分享给大家供大家参考。具体实现方法如下:<html><head>
- asp.net的dropdownlist控件为我们的web应用提供了许多用处,但有一点我总感觉不爽的就是在使用dropdownlist的事件
- MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法1: 使用set password语句修改mysql
- 特点:1.图片预载入,载入后再显示。意图一次呈现,不会让一块一块下载破坏你的页面,绝佳的用户体验,颠覆传统的浏览器呈现图片的处理方式(需要后
- 【简介】django-admin.py是Django的一个用于管理任务的命令行工具。本文将描述它的大概用法。另外,在每一个Django pr