0

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:

Original

transformed:

transformed

backtransformed:

backtransformed

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • what's your idea in using K and Kinv in your homography? – Micka Apr 21 '23 at 08:08
  • in your test, `transformed` can be extremely distorted (like strongly scaled or parts of the image moved outside of the image) and then, transforming back can't magically fix broken input image parts. – Micka Apr 21 '23 at 08:10
  • ok, I think you want to create a 2D homography (3x3 homography) from a 3D camera motion (4x4 homography)? Do you have a reference for your formulas? Can you share an example final homography? And sample images (input / results)? – Micka Apr 21 '23 at 08:18
  • Hi Micka! and thx again. It is you and always you, who tries to help superfast. I gave some images for better understanding. and yes, the formula you see in the code is for what you say, but it has nothing to do with my problem. – Philipp Enöckl Apr 21 '23 at 09:45
  • can you print H and Hinv? – Micka Apr 21 '23 at 13:39

1 Answers1

1

I was simply very careless and used different resolutions, without even noticing. My program calculated the homography from a downsized version of an image and i used the inverse homography on the original image. So the "test" was wrong from the start. The code - as used in the question - runs without a problem.

What can we learn out of this mistake? Nothing except maybe how to use homography on different resolution, which is explained here:

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 25 '23 at 23:25