首页 > 解决方案 > Opencv getPerspective Transform 反向点变换

问题描述

我有点困惑。

我尝试做four_point_transform并且我成功了,但我无法得到逆变换。

助手代码

# https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
import cv2
import numpy as np

import matplotlib.pyplot as plt

def pltshow(img,size=10):
    plt.figure(figsize=(size,size))
    plt.axis('off')
    plt.imshow(img)
    plt.show()   

def order_points(pts):
    rect = np.zeros((4, 2), dtype = np.float32)
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]
    return rect

def four_point_transform(image, pts):
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype = "float32")

    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    # return the warped image
    return warped, M

我的代码

img = np.ones((110,100,3),dtype=np.uint8)*10
rect = np.int0([
    [45, 40],
    [85, 40],
    [90, 80], 
    [40, 80]]
)

cv2.drawContours(img,[rect],-1,(0,0,255),1)
pltshow(img,5)

img_pltshow

test, M = four_point_transform(img,rect)

t_point = (45,35)
cv2.circle(test,t_point,(2),(0,255,0),1)
pltshow(test,5)

test_pltshpw

我试过了

M11,M12,M13 = M[0,:]
M21,M22,M23 = M[1,:]
M31,M32,M33 = M[2,:]

x,y = t_point

world_x = np.array([M11*x + M12*y + M13])/ np.array([M31*x + M32*y + M33])
world_y =  np.array([M21*x + M22*y + M23])/ np.array([M31*x + M32*y + M33])

world_x,world_y

却得到了不可理解的含义。我不知道如何应用它们

(array([-0.79032258]), array([-6.29032258]))
p = np.float32([[[x,y]]])
cv2.perspectiveTransform(p, M)
# get array([[[-0.7903226, -6.290323 ]]], dtype=float32)

point = np.array([x, y])
homg_point = [point[0], point[1], 1] # homogeneous coords
transf_homg_point = M.dot(homg_point) # transform
transf_homg_point /= transf_homg_point[2] # scale
transf_point = transf_homg_point[:2] # remove Cartesian coords
# get array([-0.79032258, -6.29032258])

p = np.array((x,y,1)).reshape((3,1))
temp_p = M.dot(p)
sum = np.sum(temp_p ,1)
sum[0]/sum[2],sum[1]/sum[2]

# get (-0.7903225806451583, -6.290322580645166)

我试图得到这个

我试图得到这个

但我不明白怎么做。

我找到了解决方案

    # compute the perspective transform matrix and then apply it
    M     = cv2.getPerspectiveTransform(rect, dst)
    m_inv = cv2.getPerspectiveTransform(dst, rect)
p = np.float32([[[x,y]]])
n_point = cv2.perspectiveTransform(p, MI)
n_point = tuple(np.int0(n_point.reshape(1,2)[0]))

cv2.circle(img,n_point,(2),(0,255,0),1)
pltshow(img,5)

标签: pythonarraysnumpyopencvmath

解决方案


我找到了一个解决方案:

m_inv = cv2.getPerspectiveTransform(dst, rect)

推荐阅读