Search code examples
pythonpython-2.7opencvstereo-3d

Output 3D points change everytime in triangulatePoints using in Python 2.7 with OpenCV 3.3


I want to know 3D points from stereo cameras using triangulatePoints in Python 2.7 and OpenCV 3.3. For that, I calibrated stereo cameras and stored matrices in the folder. I also rectified my images using cv2.stereoRectify and by using cv2.initUndistortRectifyMap undistort the images. Then I saved those images as well as projection matrices P1 and P2 and find the corresponding point in both images. Point in left image ptl = np.array([304,277]) and corresponding point in right image ptr = np.array([255,277]). After that I tried points = cv2.triangulatePoints(P1,P2,ptl,ptr). Code is:

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

        cameraMatrixL = np.load('mtx_left.npy')
        distCoeffsL = np.load('dist_left.npy')
        cameraMatrixR = np.load('mtx_right.npy')
        distCoeffsR = np.load('dist_right.npy')
        R = np.load('R.npy')
        T = np.load('T.npy')
    # following matrices I saved which i got from stereoRectify
        R1 = np.load('R1.npy')
        R2 = np.load('R2.npy')
        P1 = np.load('P1.npy')
        P2 = np.load('P2.npy')
        Q = np.load('Q.npy')
# upload alreday distorted and rectified images
        imgL = cv2.imread('D:\python/triangulate in 3 D\left.png',0)
        imgR = cv2.imread('D:\python/triangulate in 3 D/right.png',0)
        ptl = np.array([304,277]) # point in left image
        ptr = np.array([255,277]) # Corresponding point in right image
        points = cv2.triangulatePoints(P1,P2,ptl,ptr)
        print points

But when ever I run this code my results got changed (also all results are wrong). One time results look like

[[  518845863]
 [ 1667138857]
 [-1189385102]
 [    -661713]]

Another time results look like

[[-1766436066]
 [          0]
 [          0]
 [-1299735447]]

Sometimes it looks like

[[        0]
 [        0]
 [697559541]
 [        0]]

I don't know why the results are changing even my all parameters are same only? Also, these 3D points are incorrect. How to correct these problems?

Edit: I observed one thing in this code, after running that it not get completed. It neither shows Process finished with exit code 0 nor Process finished with exit code 1. when I pressed red stop button it finished with Process finished with exit code 1. Why so? I think due to this only above error is coming. Why this code not running with Process finished with exit code 0?


Solution

  • Finally, after so many tries I found out what mistake I was doing. Actually, I was defining my points in code in the wrong way. According to triangulatePoints document, points should be

    projPoints1 – 2xN array of feature points in the first image.

    But in code I wrote

    ptl = np.array([304,277]) # point in left image
    ptr = np.array([255,277]) # Corresponding point in right image
    

    means I was defining 1x2 array while I should define 2x1 array for single point. likewise for 5 points array should be 2x5. For N points offcourse 2xN. Initially, I didn't notice this because I used to do traingulation in Matlab and there corresponding points are used as Nx2 array.Now I put my points like

    l = np.array([[ 304],[ 277]],dtype=np.float)
    r = np.array([[ 255 ],[ 277]],dtype=np.float)
    

    and I got above code working.

    One more point dtype=np.float is important in defining this point array to avoid wrong results.

    Results what I got is not very accurate and shows error of nearly 20-25 mm but I solved above problem so I answer this question and now I have to find out the way to minimise errors. If anybody knows how to reduce error please tell me.