Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the face Slim effect with pytorch liquid algorithm

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the pytorch liquid algorithm how to achieve face loss effect, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The idea of the algorithm:

Suppose the current point is (xcx,cy y), the center point of the deformation region is C (cx,cy), the radius of the deformation region is r, the deformation end point (from the center point to a certain position M) is M (mx,my), the deformation degree is strength, and the current point corresponds to the target position after deformation is U. The law of deformation is as follows

All pixels in the circle are offset in the direction of the deformation vector.

The closer to the center of the circle, the greater the degree of deformation.

The closer to the circumference, the smaller the deformation, and when the pixel is in the circumference, the pixel is not deformed.

Out-of-circle pixels do not offset

Where x is the coordinate of any point in the circle, c is the center point, rmax is the radius of the center, m is the end point of adjusting deformation, and u is the deformed position corresponding to any point x in the circle.

The above formula is improved by adding the deformation degree control variable strength. The improved face reduction formula is as follows.

Advantages and disadvantages:

Advantages: the idea of deformation is simple and direct

Disadvantages:

Local deformation algorithm can only be based on one central point, to the direction of another point. If you want to stretch multiple points together, you can only do liquefaction once for each point, which can be achieved by liquefaction multiple times for multiple parts.

The deformation of single point stretching can achieve the effect of face reduction, but the naturalness of the effect needs to be improved.

Code implementation:

Import cv2import mathimport 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 # in the formula | Mmerc | ^ 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) * 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, 233startX_right, startY_right, endX_right EndY_right = 287275192, 233radius = 45strength = 10mm thin left face processed_image = localTranslationWarpFastWithStrength (processed_image, startX_left, startY_left, endX_left, endY_left, radius Strength) # thin right face processed_image = localTranslationWarpFastWithStrength (processed_image, startX_right, startY_right, endX_right, endY_right, radius, strength) cv2.imwrite ("thin.jpg", processed_image)

Experimental results:

Thank you for reading this article carefully. I hope the article "how to achieve face loss with pytorch liquid algorithm" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report