gpt4 book ai didi

python - 如何在 OpenCV 中进行变形?

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:14 26 4
gpt4 key购买 nike

我对如何使图像变形感兴趣,例如,有一张 1000x1000 的照片,在 500 400 点,我想在 Photoshop 中用塑料来膨胀它。

我没有代码,因为我没有找到合适的代码。

enter image description here

最佳答案

您可以使用 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)

kit.jpg enter image description here

关于python - 如何在 OpenCV 中进行变形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com