溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

發(fā)布時(shí)間:2021-11-26 10:29:54 來源:億速云 閱讀:139 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果”吧!

算法思路:

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

以眼睛中心為中心點(diǎn),對眼睛區(qū)域向外放大,就實(shí)現(xiàn)了大眼的效果。大眼的基本公式如下,

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

假設(shè)眼睛中心點(diǎn)為O(x,y),大眼區(qū)域半徑為Radius,當(dāng)前點(diǎn)位為A(x1,y1),對其進(jìn)行改進(jìn),加入大眼程度控制變量Intensity,其中Intensity的取值范圍為0~100。 

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

其中,dis表示AO的歐式距離,k表示縮放比例因子,k0表示大眼程度,xd,yd表示A點(diǎn)經(jīng)過大眼變換后的目標(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)遞減,眼睛縮小。

人眼半徑求法,

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

根據(jù)眼睛左右2個(gè)關(guān)鍵點(diǎn)來計(jì)算大眼區(qū)域所在的半徑Radius 

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

大眼程度Intensity求法,

根據(jù)圖像分辨率,結(jié)合實(shí)際經(jīng)驗(yàn)來計(jì)算大眼程度Intensity。

比如Intensity = 15*512*512/(width*height)

應(yī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)效果: 

怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果

到此,相信大家對“怎么用pytorch膨脹算法實(shí)現(xiàn)大眼效果”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI