将矩形转换为梯形以进行透视。

6
我有一张正视图的图片,我想将它转换成鸟瞰图。现在我想计算矩形中每个点的变换后的梯形坐标(x,y)。 enter image description here 给定矩形中的x和y以及梯形的角度(a),必须有一个公式可用于进行此变换。
我正在使用C编程并使用OpenCV。
非常感谢提前。
2个回答

6

是的,相信我,我已经在各个方向上谷歌过了,但我还是想不出来。 - Khashayar

4
我已经想出了解决您问题的方法。
以下是我使用的代码:
```html

我找到了解决方案。

这是我用来解决问题的代码:

```
import cv2
import numpy as np

filename = '1.jpg'
img = cv2.imread(filename)
cv2.imwrite('img.jpg',img)

enter image description here

ih, iw, _ = img.shape

black = np.zeros((ih + 300, iw + 300, 3), np.uint8)
cv2.imwrite('black.jpg',black)
bh, bw, _ = black.shape

enter image description here

pts_src = np.array([[0.0, 0.0],[float(iw), 0.0],[float(iw), float(ih)],[0.0,float(ih)]])

pts_dst = np.array([[bw * 0.25, 0],[bw * 0.75, 0.0],[float(bw), float(bh)],[0.0,float(bh)]])
pts_src pts_dst
h, status = cv2.findHomography(pts_src, pts_dst)

im_out = cv2.warpPerspective(img, h, (black.shape[1],black.shape[0]))
cv2.imwrite("im_outImage.jpg", im_out)

enter image description here

cv2.waitKey(0)
cv2.destroyAllWindows()

如果您更改数组pts_dst中的值,就可以获得不同类型的四边形。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接