Transform/warp坐标
Transform/warp coordinates
我有一个坐标数组,用于标记地板上的一个区域。
我想生成一个新的数组,其中所有的坐标都被转换,这样我就得到了一个扭曲的数组。这些点应该如下图所示。请注意,我想使用 新数组生成图形 。它还不存在。它是在必须使用新数组后生成的。
如果有帮助,我知道所有坐标之间的距离。坐标 json 看起来像这样,其中 distance_to_next
包含到下一个点的距离(以厘米为单位):
[
{
"x": 295,
"y": 228,
"distance_to_next": 200
},
{
"x": 559,
"y": 263,
"distance_to_next": 30
},
{
"x": 551,
"y": 304,
"distance_to_next": 50
},
{
"x": 473,
"y": 290,
"distance_to_next": 70
},
{
"x": 451,
"y": 352,
"distance_to_next": 150
},
{
"x": 249,
"y": 313,
"distance_to_next": 100
}
]
第一个点总是在左上角。
我正在使用 python 和 opencv2,我知道 cv.warpAffine
等各种函数,
cv.warpPerspective
、cv.findHomography
、cv.perspectiveTransform
等,但我不确定在这里使用哪一个。
有人能指出我正确的方向吗?我是否漏掉了一些明显的东西?
您的坐标 json 中的点未与白色多边形对齐。如果我使用它们,我会得到如下所示的绿色多边形:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Load the image
img = cv2.imread('./input.jpg')
# Create a copy of the image
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
pts = np.array([[295,228],[559,263],[551,304],[473,290],[451,352],[249,313]], np.int32)
pts = pts.reshape((-1,1,2))
img_copy2 = cv2.polylines(img_copy,[pts],True,(0,255,0), thickness=3)
plt.imshow(img_copy2)
所以我手动找到了你的白色多边形的近似坐标,并将它们覆盖在绿色的白色多边形上。 您需要所需多边形的坐标。我用红色显示了它们。
img = cv2.imread('./input.jpg')
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
input_pts = np.array([[300,300],[890,380],[830,600],[200,480]], np.int32)
pts = input_pts.reshape((-1,1,2))
img_copy3 = cv2.polylines(img_copy,[pts],True,(0,255,0), thickness=3)
output_pts= np.array([[300,300],[850,300],[850,520],[300,520]], np.int32)
pts = output_pts.reshape((-1,1,2))
img_copy3 = cv2.polylines(img_copy3,[pts],True,(255,0,0), thickness=3)
plt.imshow(img_copy3)
这里需要用到cv2.getPerspectiveTransform
。但它只需要 4 个点 作为输入,所以丢弃 2 个点。
img = cv2.imread('./input.jpg')
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
input_pts = np.float32(input_pts)
output_pts = np.float32(output_pts)
# Compute the perspective transform M
M = cv2.getPerspectiveTransform(input_pts,output_pts)
# Apply the perspective transformation to the image
out = cv2.warpPerspective(img_copy,M,(img_copy.shape[1], img_copy.shape[0]),flags=cv2.INTER_LINEAR)
# Display the transformed image
plt.imshow(out)
我有一个坐标数组,用于标记地板上的一个区域。
我想生成一个新的数组,其中所有的坐标都被转换,这样我就得到了一个扭曲的数组。这些点应该如下图所示。请注意,我想使用 新数组生成图形 。它还不存在。它是在必须使用新数组后生成的。
如果有帮助,我知道所有坐标之间的距离。坐标 json 看起来像这样,其中 distance_to_next
包含到下一个点的距离(以厘米为单位):
[
{
"x": 295,
"y": 228,
"distance_to_next": 200
},
{
"x": 559,
"y": 263,
"distance_to_next": 30
},
{
"x": 551,
"y": 304,
"distance_to_next": 50
},
{
"x": 473,
"y": 290,
"distance_to_next": 70
},
{
"x": 451,
"y": 352,
"distance_to_next": 150
},
{
"x": 249,
"y": 313,
"distance_to_next": 100
}
]
第一个点总是在左上角。
我正在使用 python 和 opencv2,我知道 cv.warpAffine
等各种函数,
cv.warpPerspective
、cv.findHomography
、cv.perspectiveTransform
等,但我不确定在这里使用哪一个。
有人能指出我正确的方向吗?我是否漏掉了一些明显的东西?
您的坐标 json 中的点未与白色多边形对齐。如果我使用它们,我会得到如下所示的绿色多边形:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Load the image
img = cv2.imread('./input.jpg')
# Create a copy of the image
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
pts = np.array([[295,228],[559,263],[551,304],[473,290],[451,352],[249,313]], np.int32)
pts = pts.reshape((-1,1,2))
img_copy2 = cv2.polylines(img_copy,[pts],True,(0,255,0), thickness=3)
plt.imshow(img_copy2)
所以我手动找到了你的白色多边形的近似坐标,并将它们覆盖在绿色的白色多边形上。 您需要所需多边形的坐标。我用红色显示了它们。
img = cv2.imread('./input.jpg')
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
input_pts = np.array([[300,300],[890,380],[830,600],[200,480]], np.int32)
pts = input_pts.reshape((-1,1,2))
img_copy3 = cv2.polylines(img_copy,[pts],True,(0,255,0), thickness=3)
output_pts= np.array([[300,300],[850,300],[850,520],[300,520]], np.int32)
pts = output_pts.reshape((-1,1,2))
img_copy3 = cv2.polylines(img_copy3,[pts],True,(255,0,0), thickness=3)
plt.imshow(img_copy3)
这里需要用到cv2.getPerspectiveTransform
。但它只需要 4 个点 作为输入,所以丢弃 2 个点。
img = cv2.imread('./input.jpg')
img_copy = np.copy(img)
img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
input_pts = np.float32(input_pts)
output_pts = np.float32(output_pts)
# Compute the perspective transform M
M = cv2.getPerspectiveTransform(input_pts,output_pts)
# Apply the perspective transformation to the image
out = cv2.warpPerspective(img_copy,M,(img_copy.shape[1], img_copy.shape[0]),flags=cv2.INTER_LINEAR)
# Display the transformed image
plt.imshow(out)