Python 如何通过移动图像的顶点来扭曲图像?
我试图找到一种方法,通过转换图像的一个顶点来转换图像 我已经找到了各种变换图像的方法,如旋转和缩放,但没有一种方法像这样涉及倾斜: 有剪切,但它不一样,因为它可以移动图像的两个或多个顶点,而我只想移动一个 我能用什么来执行这样的操作呢?我拿了你的“猫的东西”并将它调整到一个合适的大小,添加了一些垂直和水平的白色网格线,并在底部添加了一些额外的红色画布,给我自己空间来转换它。这给了我这个400像素宽450像素高的: 然后,我使用ImageMagick在终端中进行“双线性前向变换”。基本上你给它4对点,第一对是左上角在变换之前的位置,然后是它必须移动到的位置。下一对是右上角最初的位置,然后是它结束的位置。然后是右下角。然后是左下角。如您所见,4对中有3对未移动-只有右下角移动。我还将虚拟像素设置为黑色,这样您就可以看到在哪里,像素是通过黑色变换发明的:Python 如何通过移动图像的顶点来扭曲图像?,python,image,image-processing,Python,Image,Image Processing,我试图找到一种方法,通过转换图像的一个顶点来转换图像 我已经找到了各种变换图像的方法,如旋转和缩放,但没有一种方法像这样涉及倾斜: 有剪切,但它不一样,因为它可以移动图像的两个或多个顶点,而我只想移动一个 我能用什么来执行这样的操作呢?我拿了你的“猫的东西”并将它调整到一个合适的大小,添加了一些垂直和水平的白色网格线,并在底部添加了一些额外的红色画布,给我自己空间来转换它。这给了我这个400像素宽450像素高的: 然后,我使用ImageMagick在终端中进行“双线性前向变换”。基本上你给它
convert cat.png -matte -virtual-pixel black -interpolate Spline -distort BilinearForward '0,0 0,0 399,0 399,0 399,349 330,430 0,349 0,349' bilinear.png
我还使用相同的变换坐标进行了“透视变换”:
convert cat.png -matte -virtual-pixel black -distort Perspective '0,0 0,0 399,0 399,0 399,349 330,430 0,349 0,349' perspective.png
最后,为了说明差异,我对两幅图像进行了闪烁比较,以便您可以看到差异:
我感谢安东尼·蒂森的出色工作,我向你们推荐他
我知道您正在寻找Python解决方案,并指出有一个名为Wand的Python绑定到ImageMagick,您可能希望使用-
请注意,我只使用了红色和黑色来说明正在发生的事情(在堆栈溢出白色背景的顶部),以及结果的各个方面来自何处,很明显,您将使用白色来表示这两个方面 透视变换可能是您想要的,因为它在任何角度都保留直线。(反向双线性仅保留水平和垂直直线) 下面是如何在ImageMagick、PythonWand(基于ImageMagick)和PythonOpenCV中实现这一点 输入: ImageMagick (请注意,+扭曲使输出达到保存完整结果所需的大小,并且不限于输入的大小。此外,-virtual pixel white将图像像素外部区域的颜色设置为白色。点从左上角成对顺时针排列为inx、iny outx、outy)
蟒蛇魔杖 (请注意,best_fit=true使输出具有保存完整结果所需的大小,并且不限于输入的大小。)
Python OpenCV
#!/bin/python3.7
import cv2
import numpy as np
# Read source image.
img_src = cv2.imread('cat.png')
# Four corners of source image
# Coordinates are in x,y system with x horizontal to the right and y vertical downward
pts_src = np.float32([[0,0], [359,0], [379,333], [0,333]])
# Four corners of destination image.
pts_dst = np.float32([[0, 0], [359,0], [306,376], [0,333]])
# Get perspecive matrix if only 4 points
m = cv2.getPerspectiveTransform(pts_src,pts_dst)
# Warp source image to destination based on matrix
# size argument is width x height
# compute from max output coordinates
img_out = cv2.warpPerspective(img_src, m, (359+1,376+1), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255))
# Save output
cv2.imwrite('cat_perspective_opencv.png', img_out)
# Display result
cv2.imshow("Warped Source Image", img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()
链接图像看起来像是可以通过2D生成的。变换是非仿射的,因此不能表示为矩阵。正如马蒂诺所说,一般来说,纹理贴图是一种方法。您试图使用什么软件包进行操作?上述注释不正确。4点透视图转换应该可以工作。它保持直线。请参见下面我的答案以及马克·塞切尔的答案。
#!/bin/python3.7
from wand.image import Image
from wand.display import display
with Image(filename='cat.png') as img:
img.virtual_pixel = 'white'
img.distort('perspective', (0,0, 0,0, 359,0, 359,0, 379,333, 306,376, 0,333, 0,333), best_fit=True)
img.save(filename='cat_perspective_wand.png')
display(img)
#!/bin/python3.7
import cv2
import numpy as np
# Read source image.
img_src = cv2.imread('cat.png')
# Four corners of source image
# Coordinates are in x,y system with x horizontal to the right and y vertical downward
pts_src = np.float32([[0,0], [359,0], [379,333], [0,333]])
# Four corners of destination image.
pts_dst = np.float32([[0, 0], [359,0], [306,376], [0,333]])
# Get perspecive matrix if only 4 points
m = cv2.getPerspectiveTransform(pts_src,pts_dst)
# Warp source image to destination based on matrix
# size argument is width x height
# compute from max output coordinates
img_out = cv2.warpPerspective(img_src, m, (359+1,376+1), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255))
# Save output
cv2.imwrite('cat_perspective_opencv.png', img_out)
# Display result
cv2.imshow("Warped Source Image", img_out)
cv2.waitKey(0)
cv2.destroyAllWindows()