猿问

如何在 OpenCV 中进行变形?

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

我没有代码,因为我没有找到正确的代码。


RISEBY
浏览 142回答 1
1回答

江户川乱折腾

您可以使用重新映射。这是一个快速演示,但请注意,您必须知道确切的失真功能才能复制 Photoshop 的功能import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimport mathimg = cv.imread('kit.jpg')right_eye = (215,105)radius = 30power = 1.6 # >1.0 for expansion, <1.0 for shrinkageheight, width, _ = img.shapemap_y = np.zeros((height,width),dtype=np.float32)map_x = np.zeros((height,width),dtype=np.float32)# create index mapfor i in range(height):&nbsp; &nbsp; for j in range(width):&nbsp; &nbsp; &nbsp; &nbsp; map_y[i][j]=i&nbsp; &nbsp; &nbsp; &nbsp; map_x[i][j]=j# deform around the right eyefor i in range (-radius, radius):&nbsp; &nbsp; for j in range(-radius, radius):&nbsp; &nbsp; &nbsp; &nbsp; if (i**2 + j**2 > radius ** 2):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; if i > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] + (i/radius)**power * radius&nbsp; &nbsp; &nbsp; &nbsp; if i < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] - (-i/radius)**power * radius&nbsp; &nbsp; &nbsp; &nbsp; if j > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] + (j/radius)**power * radius&nbsp; &nbsp; &nbsp; &nbsp; if j < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] - (-j/radius)**power * radiuswarped=cv.remap(img,map_x,map_y,cv.INTER_LINEAR)cv.imwrite('warp.jpg', warped)&nbsp;
随时随地看视频慕课网APP

相关分类

Python
我要回答