论文:interactive image warping(1993年andreas gustafsson)

算法思路:

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

  • 圆内所有像素均沿着变形向量的方向发生偏移
  • 距离圆心越近,变形程度越大
  • 距离圆周越近,变形程度越小,当像素点位于圆周时,该像素不变形
  • 圆外像素不发生偏移

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

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

优缺点:

优点:形变思路简单直接

缺点:

  • 局部变形算法,只能基于一个中心点,向另外一个点的方向啦。如果想多个点一起拉伸,只能每个点分别做一次液化,通过针对多个部位多次液化来实现。
  • 单点拉伸的变形,可以实现瘦脸的效果,但是效果自然度有待提升。

代码实现:

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 液态算法实现瘦脸效果的文章就介绍到这了,更多相关pytorch 液态算法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!