論文:Interactive Image Warping(1993年Andreas Gustafsson)
算法思路:
以眼睛中心為中心點(diǎn),對(duì)眼睛區(qū)域向外放大,就實(shí)現(xiàn)了大眼的效果。大眼的基本公式如下,
假設(shè)眼睛中心點(diǎn)為O(x,y),大眼區(qū)域半徑為Radius,當(dāng)前點(diǎn)位為A(x1,y1),對(duì)其進(jìn)行改進(jìn),加入大眼程度控制變量Intensity,其中Intensity的取值范圍為0~100。
其中,dis表示AO的歐式距離,k表示縮放比例因子,k0表示大眼程度,xd,yd表示A點(diǎn)經(jīng)過(guò)大眼變換后的目標(biāo)點(diǎn)B的坐標(biāo)。
當(dāng)k=0時(shí),目標(biāo)點(diǎn)B與O點(diǎn)重合。
當(dāng)k=1時(shí),目標(biāo)點(diǎn)B與A點(diǎn)重合。
當(dāng)k<1.0時(shí),目標(biāo)點(diǎn)B的計(jì)算函數(shù)單調(diào)遞增,眼睛放大。
當(dāng)k>1.0時(shí),目標(biāo)點(diǎn)B的計(jì)算函數(shù)單調(diào)遞減,眼睛縮小。
人眼半徑求法,
根據(jù)眼睛左右2個(gè)關(guān)鍵點(diǎn)來(lái)計(jì)算大眼區(qū)域所在的半徑Radius
大眼程度Intensity求法,
根據(jù)圖像分辨率,結(jié)合實(shí)際經(jīng)驗(yàn)來(lái)計(jì)算大眼程度Intensity。
比如Intensity = 15*512*512/(width*height)
應(yīng)用場(chǎng)景:
適用于任何球形局部形變的場(chǎng)景,比如大眼,比如嘴唇微笑。
代碼實(shí)現(xiàn):
import cv2 import math import numpy as np def big_eye_adjust_fast(src, PointX, PointY, Radius, Strength): processed_image = np.zeros(src.shape, np.uint8) processed_image = src.copy() height = src.shape[0] width = src.shape[1] PowRadius = Radius * Radius maskImg = np.zeros(src.shape[:2], np.uint8) cv2.circle(maskImg, (PointX, PointY), math.ceil(Radius), (255, 255, 255), -1) mapX = np.vstack([np.arange(width).astype(np.float32).reshape(1, -1)] * height) mapY = np.hstack([np.arange(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 np.copyto(UX, mapX, where=maskImg == 0) np.copyto(UY, mapY, where=maskImg == 0) UX = UX.astype(np.float32) UY = UY.astype(np.float32) processed_image = cv2.remap(src, UX, UY, interpolation=cv2.INTER_LINEAR) return processed_image image = cv2.imread("tests/images/klst.jpeg") processed_image = image.copy() 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) cv2.imwrite("big.jpg", processed_image)
實(shí)驗(yàn)效果:
到此這篇關(guān)于pytorch 膨脹算法實(shí)現(xiàn)大臉效果的文章就介紹到這了,更多相關(guān)pytorch 膨脹算法內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_14845119/article/details/121516646