在wrapTransform之后如何找到一个点?

use*_*301 3 python opencv

我在 warpPerspective 之前在原始图像下找到了一组坐标/点,如何在现在裁剪和校正的透视校正图像中获取相应的点?

例如:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('sudoku.png')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

point = np.array([[10,10]])

M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))

plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
Run Code Online (Sandbox Code Playgroud)

如何将 img 地图中的新坐标 [10,10] 获取到 dst 图像?

Dan*_*šek 5

您必须执行与对图像所做的相同的转换(数学上)。在这种情况下,它意味着使用cv2.perspectiveTransform(请注意,输入需要每点 1 行、1 列和 2 个通道——第一个是 X 坐标,第二个是 Y 坐标)。

该函数将变换所有输入点,它不执行和裁剪。您将需要对转换后的坐标进行后处理,并丢弃位于裁剪区域之外的坐标。在你的情况下,你想保留点,其中(0 <= x < 300) and (0 <= y < 300).


示例代码:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('sudoku.png')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

points = np.float32([[[10, 10]], [[116,128]], [[254,261]]])

M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))

# Transform the points
transformed = cv.perspectiveTransform(points, M)

# Perform the cropping -- filter out points that are outside the crop area
cropped = []
for pt in transformed:
    x, y = pt[0]
    if x >= 0 and x < dst.shape[1] and y >= 0 and y < dst.shape[0]:
        print "Valid point (%d, %d)" % (x, y)
        cropped.append([[x,y]])
    else:
        print "Out-of-bounds point (%d, %d)" % (x, y)

# Turn it back into a single numpy array
cropped = np.hstack(cropped)

# Visualize
plt.subplot(121)
plt.imshow(img)
for pt in points:
    x, y = pt[0]
    plt.scatter(x, y, s=100, c='red', marker='x')

plt.title('Input')

plt.subplot(122)
plt.imshow(dst)
for pt in transformed:
    x, y = pt[0]
    plt.scatter(x, y, s=100, c='red', marker='x')

plt.title('Output')

plt.show()
Run Code Online (Sandbox Code Playgroud)

控制台输出:

Out-of-bounds point (-53, -63)
Valid point (63, 67)
Valid point (192, 194)
Run Code Online (Sandbox Code Playgroud)

可视化:

可视化