1 回答
TA贡献1851条经验 获得超5个赞
您可以使用重新映射。这是一个快速演示,但请注意,您必须知道确切的失真功能才能复制 Photoshop 的功能
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import math
img = cv.imread('kit.jpg')
right_eye = (215,105)
radius = 30
power = 1.6 # >1.0 for expansion, <1.0 for shrinkage
height, width, _ = img.shape
map_y = np.zeros((height,width),dtype=np.float32)
map_x = np.zeros((height,width),dtype=np.float32)
# create index map
for i in range(height):
for j in range(width):
map_y[i][j]=i
map_x[i][j]=j
# deform around the right eye
for i in range (-radius, radius):
for j in range(-radius, radius):
if (i**2 + j**2 > radius ** 2):
continue
if i > 0:
map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] + (i/radius)**power * radius
if i < 0:
map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] - (-i/radius)**power * radius
if j > 0:
map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] + (j/radius)**power * radius
if j < 0:
map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] - (-j/radius)**power * radius
warped=cv.remap(img,map_x,map_y,cv.INTER_LINEAR)
cv.imwrite('warp.jpg', warped)
添加回答
举报