pytorch 液态算法实现瘦脸效果

作者:watersink 时间:2021-12-05 19:28:17 

论文:Interactive Image Warping(1993年Andreas Gustafsson)

算法思路:

pytorch 液态算法实现瘦脸效果

假设当前点为(x,y),手动指定变形区域的中心点为C(cx,cy),变形区域半径为r,手动调整变形终点(从中心点到某个位置M)为M(mx,my),变形程度为strength,当前点对应变形后的目标位置为U。变形规律如下,

  • 圆内所有像素均沿着变形向量的方向发生偏移

  • 距离圆心越近,变形程度越大

  • 距离圆周越近,变形程度越小,当像素点位于圆周时,该像素不变形

  • 圆外像素不发生偏移

pytorch 液态算法实现瘦脸效果

其中,x是圆内任意一点坐标,c是圆心点,rmax为圆心半径,m为调整变形的终点,u为圆内任意一点x对应的变形后的位置。

对上面公式进行改进,加入变形程度控制变量strength,改进后瘦脸公式如下,

pytorch 液态算法实现瘦脸效果

优缺点:

优点:形变思路简单直接

缺点:

  • 局部变形算法,只能基于一个中心点,向另外一个点的方向啦。如果想多个点一起拉伸,只能每个点分别做一次液化,通过针对多个部位多次液化来实现。

  • 单点拉伸的变形,可以实现瘦脸的效果,但是效果自然度有待提升。

代码实现:


import cv2
import math
import numpy as np

def localTranslationWarpFastWithStrength(srcImg, startX, startY, endX, endY, radius, strength):
   ddradius = float(radius * radius)
   copyImg = np.zeros(srcImg.shape, np.uint8)
   copyImg = srcImg.copy()

maskImg = np.zeros(srcImg.shape[:2], np.uint8)
   cv2.circle(maskImg, (startX, startY), math.ceil(radius), (255, 255, 255), -1)

K0 = 100/strength

# 计算公式中的|m-c|^2
   ddmc_x = (endX - startX) * (endX - startX)
   ddmc_y = (endY - startY) * (endY - startY)
   H, W, C = srcImg.shape

mapX = np.vstack([np.arange(W).astype(np.float32).reshape(1, -1)] * H)
   mapY = np.hstack([np.arange(H).astype(np.float32).reshape(-1, 1)] * W)

distance_x = (mapX - startX) * (mapX - startX)
   distance_y = (mapY - startY) * (mapY - startY)
   distance = distance_x + distance_y
   K1 = np.sqrt(distance)
   ratio_x = (ddradius - distance_x) / (ddradius - distance_x + K0 * ddmc_x)
   ratio_y = (ddradius - distance_y) / (ddradius - distance_y + K0 * ddmc_y)
   ratio_x = ratio_x * ratio_x
   ratio_y = ratio_y * ratio_y

UX = mapX - ratio_x * (endX - startX) * (1 - K1/radius)
   UY = mapY - ratio_y * (endY - startY) * (1 - K1/radius)

np.copyto(UX, mapX, where=maskImg == 0)
   np.copyto(UY, mapY, where=maskImg == 0)
   UX = UX.astype(np.float32)
   UY = UY.astype(np.float32)
   copyImg = cv2.remap(srcImg, UX, UY, interpolation=cv2.INTER_LINEAR)

return copyImg

image = cv2.imread("./tests/images/klst.jpeg")
processed_image = image.copy()
startX_left, startY_left, endX_left, endY_left = 101, 266, 192, 233
startX_right, startY_right, endX_right, endY_right = 287, 275, 192, 233
radius = 45
strength = 100
# 瘦左边脸                                                                          
processed_image = localTranslationWarpFastWithStrength(processed_image, startX_left, startY_left, endX_left, endY_left, radius, strength)
# 瘦右边脸                                                                          
processed_image = localTranslationWarpFastWithStrength(processed_image, startX_right, startY_right, endX_right, endY_right, radius, strength)
cv2.imwrite("thin.jpg", processed_image)

实验效果:

pytorch 液态算法实现瘦脸效果

来源:https://blog.csdn.net/qq_14845119/article/details/121500720

标签:pytorch,液态算法,瘦脸
0
投稿

猜你喜欢

  • 关于H1的用法探讨

    2008-03-18 12:55:00
  • Python2.7.10以上pip更新及其他包的安装教程

    2022-12-17 18:44:55
  • 一文详解pygame.sprite的精灵碰撞

    2022-08-13 23:09:11
  • 使用python的turtle绘画滑稽脸实例

    2022-04-18 02:24:49
  • Python使用ClickHouse的实践与踩坑记录

    2023-06-12 21:45:44
  • Python requests的SSL证书验证方式

    2021-10-06 09:21:51
  • php防止SQL注入详解及防范

    2023-07-22 19:25:53
  • 基于Python编写一个语音合成系统

    2021-10-14 03:28:16
  • Python如何定义有默认参数的函数

    2023-08-05 14:38:30
  • python配置文件写入过程详解

    2021-02-06 01:46:19
  • 浅谈pytorch中stack和cat的及to_tensor的坑

    2022-12-14 11:53:23
  • asp如何用FSO对象显示一个文本文件?

    2010-06-09 18:41:00
  • 简单了解python调用其他脚本方法实例

    2022-12-07 08:53:36
  • Pycharm安装并配置jupyter notebook的实现

    2022-11-14 14:24:28
  • 上手简单,功能强大的Python爬虫框架——feapder

    2023-11-28 15:50:39
  • 利用Python实现读取Word表格计算汇总并写入Excel

    2021-06-24 06:15:47
  • python使用timeit时间模块

    2023-03-05 16:27:37
  • JS语法检查插件 jsLint for Vim

    2010-11-15 21:31:00
  • 使用 Osql 工具管理 SQL Server 桌面引擎 (MSDE 2000)应用介绍

    2020-07-01 22:12:59
  • python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例

    2022-09-27 13:07:54
  • asp之家 网络编程 m.aspxhome.com