SoFunction
Updated on 2024-11-17

pytorch bloat algorithm for big eye effect

Thesis:Interactive Image Warping(Andreas Gustafsson, 1993)

Algorithmic Thoughts:

Bigger eyes are achieved by using the center of the eye as the center point and enlarging the eye area outward. The basic formula for larger eyes is as follows.

Assuming that the center point of the eye is O(x,y), the radius of the wide-eyed region is Radius, and the current point position is A(x1,y1), it is improved by adding the wide-eyed degree control variable Intensity, where the value range of Intensity is 0 to 100.

where dis denotes the Euclidean distance of AO, k denotes the scaling factor, k0 denotes the degree of wide-eye, and xd,yd denote the coordinates of the target point B after wide-eye transformation of point A.

When k = 0, the target point B coincides with point O.

When k = 1, the target point B coincides with point A.

When k<1.0, the computational function of the target point B is monotonically increasing and the eyes are enlarged.

When k>1.0, the computational function of the target point B decreases monotonically and the eye narrows.

The human eye radius method.

Calculate the radius of the eye area based on the 2 key points on the left and right side of the eye.

Intensity method for large eyes.

Based on the image resolution, Intensity of large eyes is calculated by combining practical experience.

For example Intensity = 15*512*512/(width*height)

Application Scenarios:

Ideal for any scene with spherical localized deformation, such as large eyes, such as a lip smile.

Code Implementation:

import cv2
import math
import numpy as np
 
def big_eye_adjust_fast(src, PointX, PointY, Radius, Strength):
    processed_image = (, np.uint8)
    processed_image = ()
    height = [0]
    width = [1]
    PowRadius = Radius * Radius
 
    maskImg = ([:2], np.uint8)
    (maskImg, (PointX, PointY), (Radius), (255, 255, 255), -1)
 
    mapX = ([(width).astype(np.float32).reshape(1, -1)] * height)
    mapY = ([(height).astype(np.float32).reshape(-1, 1)] * width)
 
    OffsetX = mapX - PointX
    OffsetY = mapY - PointY
    XY = OffsetX * OffsetX + OffsetY * OffsetY
 
    ScaleFactor = 1 - XY / PowRadius
    ScaleFactor = 1 - Strength / 100 * ScaleFactor
    UX = OffsetX * ScaleFactor + PointX
    UY = OffsetY * ScaleFactor + PointY
    UX[UX < 0] = 0
    UX[UX >= width] = width - 1
    UY[UY < 0] = 0
    UY[UY >= height] = height - 1
 
    (UX, mapX, where=maskImg == 0)
    (UY, mapY, where=maskImg == 0)
 
    UX = (np.float32)
    UY = (np.float32)
 
    processed_image = (src, UX, UY, interpolation=cv2.INTER_LINEAR)
 
    return processed_image
 
image = ("tests/images/")
processed_image = ()
PointX_left, PointY_left, Radius_left, Strength_left = 150, 190, 44, 19.78
PointX_right, PointY_right, Radius_right, Strength_right = 244, 194, 42, 19.78
processed_image = big_eye_adjust_fast(processed_image, PointX_left, PointY_left, Radius_left, Strength_left)
processed_image = big_eye_adjust_fast(processed_image, PointX_right, PointY_right, Radius_right, Strength_right)
("", processed_image)

Experimental effects: 

To this article on pytorch expansion algorithm to achieve the effect of large face is introduced to this article, more related pytorch expansion algorithm content please search my previous articles or continue to browse the following related articles I hope that you will support me in the future more!