单应变换

问题描述 投票:0回答:0

我有一个概念上的误解。我只是想检查一下,如果我的单应变换工作正常(看起来非常好)

我写了一个小测试,我想做的就是将转换后的图像转换回原始图像。然而,它看起来不像原来的,甚至不接近

我想,我所要做的就是计算逆单应矩阵并将其用于变换后的图像,但是没有。

(这只是一个程序,我以前用过并适应了这个测试)

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


img  = cv2.imread("colour.jpg")
height= img.shape[0] 
width = img.shape[1]
#################   Vorgaben ###################################

f=200
rotX= 0.2
rotZ= 0.02
rotY= 0.002
distX = 0.5
distY =0.7
distZ= 0.1



K = np.array([[f, 0, width/2, 0],
            [0, f, height/2, 0],
            [0, 0,   1, 0]])

# K inverse
Kinv = np.zeros((4,3))
Kinv[:3,:3] = np.linalg.inv(K[:3,:3])*f
Kinv[-1,:] = [0, 0, 1]


RX = np.array([[1,           0,            0, 0],
                        [0,np.cos(rotX),-np.sin(rotX), 0],
                        [0,np.sin(rotX),np.cos(rotX) , 0],
                        [0,           0,            0, 1]])

RY = np.array([[ np.cos(rotY), 0, np.sin(rotY), 0],
                        [            0, 1,            0, 0],
                        [ -np.sin(rotY), 0, np.cos(rotY), 0],
                        [            0, 0,            0, 1]])

RZ = np.array([[ np.cos(rotZ), -np.sin(rotZ), 0, 0],
                            [ np.sin(rotZ), np.cos(rotZ), 0, 0],
                            [            0,            0, 1, 0],
                            [            0,            0, 0, 1]])

        # Composed rotation matrix with (RX,RY,RZ)
R = np.linalg.multi_dot([ RX , RY , RZ ])

        # Translation matrix
T = np.array([[1,0,0,distX],
                            [0,1,0,distY],
                            [0,0,1,distZ],
                            [0,0,0,1]])

        # Overall homography matrix
H = np.linalg.multi_dot([K, R, T, Kinv])
Hinv = np.linalg.inv(H)


transformed = cv2.warpPerspective(img, H, img.shape[:2][::-1])
back = cv2.warpPerspective(transformed, Hinv, transformed.shape[:2][::-1])




cv2.imshow("test", img)
cv2.waitKey(2000)

cv2.imshow("test", transformed)
cv2.waitKey(2000)

cv2.imshow("test", back)
cv2.waitKey(2000)

编辑:为了更好地理解,我包含了图片。

原文:

Original

改造:

transformed

反向转换:

backtransformed

python opencv homography
© www.soinside.com 2019 - 2024. All rights reserved.