I have a conceptual misunderstanding. I just wanted to check, if my homography transformation works fine (it looks very fine)
I wrote a small test, and all i want to do, is transform the transformed image back to the original. However, it doesn't look like the original, not even close
I thought, all i have to do, is to calculate the inverse homography matrix and use that on the transformed image, but no.
(this is just a program, which i used before and adapted it to this test)
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)
Edit: I included images for better understanding.
Original:
transformed:
backtransformed: