我对如何使图像变形感兴趣,例如,有一张 1000x1000 的照片,在 500 400 点,我想在 Photoshop 中用塑料来膨胀它。
我没有代码,因为我没有找到合适的代码。
您可以使用 remapping .这是一个快速演示,但请注意,您必须知道准确的失真函数才能复制 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)
我是一名优秀的程序员,十分优秀!