'Epipolar geometry opencv python

I have a set of two images of the sae scene, taken from different views, and I'm trying to use the epipolar geometry to compute the distance of the object to the camera.

Considering a point on the left image, I'm trying to find the matching point on the right image. To do this, I compute the fundamental matrix of the scene, then I compute the epipolar line of the point on the right image. After that, I think I would be able to find the matching point on the line by testing point of the line and use the condition x.T @ F @ x' = 0.

The problem is that the point I find is wrong. When I draw the epiline on the right image, it semms perfect as the matching point belongs to it. But when I make sure my fundamental matrix is right, the condition mentionned before is never right, the result is far from 0.

Here is my code:

discret = 500
pointg = np.array([mx, my, 1])
pts1 = np.array([[mx, my]])

lines2 = cv2.computeCorrespondEpilines(pts1, 1, F)
lines2 = lines2.reshape(-1, 3)
r, c, rien = frame1.shape

x0, y0 = map(int, [0, -lines2[0, 2] / lines2[0, 1]])
x1, y1 = map(int, [c, -(lines2[0, 2] + lines2[0, 0] * c) / lines2[0, 1]])

a = (y1 - y0) / (x1 - x0)
x, y = x0, y0
k = (x1 - x0) / discret
matd = np.array([[x], [y], [1]])
produit = (pointg @ F) @ matd
mini = produit
x_selec, y_selec = x, y
for i in range(discret):
    x += k
    y = a * x + y0
    matd = np.array([[x], [y], [1]])
    produit = (pointg @ F) @ matd
    if abs(produit) < mini:
        mini = produit
        x_selec, y_selec = int(x), int(y)
        
pointd = np.array([x_selec, y_selec, 1])

(frame1 is an image read with opencv)

Since the result will surely not be 0, I'm trying to find the minimum of the values, but the programm returns x_selec, y_selec = x0, y0 And when I compute by myself the product x.T @ F @ x', the result is never right, despite my epiline being right.

Does anyone know how to fix this ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source