图像扭曲Python
我有以下两张图片: 我想使用投影变换将源图像扭曲到目标图像中的第一个(左)形状,并将源图像扭曲到目标图像中的第二个(右)形状。 所以我首先要做的是找到兴趣点,它们是: src_interest_pts=np.float32([[0,0],[0,640],[480,0],[480,640]]),它们是我图像的角点 仿射兴趣点=np.float32([41215]、[849,54]、[602458]、[608300]),它们是正确形状的角点 投影利息=np.float32([[195,56],[494158],[36183],[432498]) 并编写了以下代码:图像扭曲Python,python,opencv,computer-vision,transform,Python,Opencv,Computer Vision,Transform,我有以下两张图片: 我想使用投影变换将源图像扭曲到目标图像中的第一个(左)形状,并将源图像扭曲到目标图像中的第二个(右)形状。 所以我首先要做的是找到兴趣点,它们是: src_interest_pts=np.float32([[0,0],[0,640],[480,0],[480,640]]),它们是我图像的角点 仿射兴趣点=np.float32([41215]、[849,54]、[602458]、[608300]),它们是正确形状的角点 投影利息=np.float32([[195,56],[4
img = cv2.imread("Q3/Dylan.jpg")
frame=cv2.imread("Q3/frames.jpg")
rows,cols,ch = frame.shape
src_interest_pts = np.float32([[0, 0],[0, 640],[480,0],[480, 640]])
Affine_interest_pts = np.float32([[41,215],[849,54],[602,458],[608,300]])
Projective_interest_pts = np.float32([[195, 56],[494,158],[36, 183],[432, 498]])
M = cv2.getAffineTransform(src_interest_pts ,Affine_interest_pts)
Affinedst = cv2.warpAffine(img,M,(cols,rows))
M=cv2.getPerspectiveTransform(src_interest_pts ,Projective_interest_pts)
Projectivedst=cv2.warpPerspective(img,M,(cols,rows))
dst=Affinedst+Projectivedst
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
结果是getAffineTransform返回一个错误,因为从源到目标必须有3个兴趣点,但在我的例子中有4个
删除仿射的代码后,我们得到:
src_interest_pts = np.float32([[0, 0],[0, 640],[480,0],[480, 640]])
Projective_interest_pts = np.float32([[195, 56],[494,158],[36, 183],[432, 498]])
M=cv2.getPerspectiveTransform(src_interest_pts ,Projective_interest_pts)
Projectivedst=cv2.warpPerspective(img,M,(cols,rows))
plt.subplot(121),plt.imshow(frame),plt.title('The frame')
plt.subplot(122),plt.imshow(Projectivedst),plt.title('Warped')
plt.show()
这是我得到的输出图像:
我的问题是:
如何获得所需的输出
也许我的问题是兴趣点
为什么getAffineTransform不适用于第二个(右)形状?对于这种扭曲,我只能使用仿射变换进行扭曲
编辑:
我把要点改为:
pts1 = np.float32([[0, 0],[640, 0],[0,480],[640, 480]])
# ptsAffine = np.float32([[215,41],[54,849],[458,602],[300,608]])
ptsProjective = np.float32([[55, 195],[158,494],[183, 36],[498, 432]])
正如@fmw42在他的评论中所指出的,我一直在指数之间切换(x中的什么应该在y中,反之亦然)
但我还是得到了这个结果,我想我很难找出正确的点。
Opencv仿射变换函数需要三个源点和目标点才能得到仿射2x3矩阵。您还可以为超定方程组找到近似仿射变换。尝试使用此函数为给定问题生成仿射变换矩阵
def affine(x, y):
b = np.array([y[0][0], y[0][1], y[1][0], y[1][1], y[2][0], y[2][1], y[3][0], y[3][1]])
A = np.array([[x[0][0], x[0][1], 1, 0, 0, 0],[0, 0, 0, x[0][0], x[0][1], 1],
[x[1][0], x[1][1], 1, 0, 0, 0],[0, 0, 0, x[1][0], x[1][1], 1],
[x[2][0], x[2][1], 1, 0, 0, 0],[0, 0, 0, x[2][0], x[2][1], 1],
[x[3][0], x[3][1], 1, 0, 0, 0],[0, 0, 0, x[3][0], x[3][1], 1]])
coef = np.linalg.inv(A.T @ A) @ A.T @ b
M = np.array([[coef[0], coef[1], coef[2]],[coef[3], coef[4], coef[5]]], dtype=np.float64)
return M
试着这样称呼它:
rows,cols,ch = frame.shape
src_interest_pts = np.float32([[0, 0],[640,0],[640,480], [0,480]])
Affine_interest_pts = np.float32([[548,215],[849,54],[908,300],[602,458]])
Projective_interest_pts = np.float32([[195, 56],[494,158],[432, 498],[36, 183]])
M = affine(src_interest_pts, Affine_interest_pts)
Affinedst = cv2.warpAffine(img,M,(cols,rows))
M=cv2.getPerspectiveTransform(src_interest_pts ,Projective_interest_pts)
Projectivedst=cv2.warpPerspective(img,M,(cols,rows))
dst=Affinedst+Projectivedst
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
您将得到这样的输出。我希望这能解决你的问题。
正如我在评论中提到的,您有几个问题。您的点定义不正确,需要使用estimateAffine2D代替getAffineTransform。这在Python/OpenCV中适用 输入: 输出帧:
结果:
请发布准确的错误消息,以及在代码中出现的位置。我认为问题在于你已经将点定义为y,x,它们需要在你的np.float32数组中定义为x,y。输入的宽度为640,高度为480,但将第二个点指定为0640。那就是x=0,y=640。但它应该是x=640,y=0,所以是640,0。查看仿射点,首先列出的是41215。但那不是任何靠近右边四边形的地方。它接近于左四边形,但在我看来不好,215,41也不好。因此,您的输出点没有准确测量您的另一个问题是您需要使用estimateAffine2D代替GetAffineTransform谢谢您的回答,我想我没有使用正确的点!!
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("Dylan.jpg")
frame=cv2.imread("frames.jpg")
rows,cols,ch = frame.shape
# x,y points are cw from top left
src_interest_pts = np.float32([[0,0],[640,0],[640,480],[0,480]])
Affine_interest_pts = np.float32([[551,224],[843,67],[903,301],[608,455]])
Projective_interest_pts = np.float32([[195,56],[494,158],[432,498],[36,183]])
M = cv2.estimateAffine2D(src_interest_pts ,Affine_interest_pts)[0]
Affinedst = cv2.warpAffine(img,M,(cols,rows))
M=cv2.getPerspectiveTransform(src_interest_pts ,Projective_interest_pts)
Projectivedst=cv2.warpPerspective(img,M,(cols,rows))
dst=Affinedst+Projectivedst
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()