Пример #1
0
def house():
    input_path = 'imgs/house/'
    camera_filepath = 'imgs/house/3D/house.00{0}.P'

    cameras = [
        Camera(processor.read_matrix(camera_filepath.format(i)))
        for i in range(9)
    ]
    [c.factor() for c in cameras]

    points3d = processor.read_matrix(input_path + '3D/house.p3d').T  # 3 x n
    points4d = np.vstack((points3d, np.ones(points3d.shape[1])))  # 4 x n
    points2d = [
        processor.read_matrix(input_path + '2D/house.00' + str(i) + '.corners')
        for i in range(9)
    ]

    index1 = 2
    index2 = 4
    img1 = cv2.imread(input_path + 'house.00' + str(index1) +
                      '.pgm')  # left image
    img2 = cv2.imread(input_path + 'house.00' + str(index2) + '.pgm')

    # fig = plt.figure()
    # ax = fig.gca(projection='3d')
    # ax.plot(points3d[0], points3d[1], points3d[2], 'b.')
    # ax.set_xlabel('x axis')
    # ax.set_ylabel('y axis')
    # ax.set_zlabel('z axis')
    # ax.view_init(elev=135, azim=90)

    # x = cameras[index2].project(points4d)
    # plt.figure()
    # plt.plot(x[0], x[1], 'b.')
    # plt.show()

    corner_indexes = processor.read_matrix(
        input_path + '2D/house.nview-corners', np.int)
    corner_indexes1 = corner_indexes[:, index1]
    corner_indexes2 = corner_indexes[:, index2]
    intersect_indexes = np.intersect1d(np.nonzero([corner_indexes1 != -1]),
                                       np.nonzero([corner_indexes2 != -1]))
    corner_indexes1 = corner_indexes1[intersect_indexes]
    corner_indexes2 = corner_indexes2[intersect_indexes]
    points1 = processor.cart2hom(points2d[index1][corner_indexes1].T)
    points2 = processor.cart2hom(points2d[index2][corner_indexes2].T)

    height, width, ch = img1.shape
    intrinsic = np.array([  # for imgs/house
        [2362.12, 0, width / 2], [0, 2366.12, height / 2], [0, 0, 1]
    ])

    return points1, points2, intrinsic
Пример #2
0
def test():
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import cv2

    # load points
    points = processor.read_matrix('testsets/house.p3d').T  # 3 x n
    points = processor.cart2hom(points)  # 4 x n

    img = cv2.imread('testsets/house1.jpg')
    height, width, ch = img.shape

    K = np.array([[width * 30, 0, width / 2], [0, height * 30, height / 2],
                  [0, 0, 1]])
    R = np.array([  # No rotation
        [1, 0, 0], [0, 1, 0], [0, 0, 1]
    ])
    t = np.array([[0], [0], [100]])  # t != 0 to be away from image plane

    # Setup cameras
    cam = Camera(K=K, R=R, t=t)
    x = cam.project(points)

    rotation_angle = 20
    rotation_mat = transformers.rotation_3d_from_angles(rotation_angle)
    cam = Camera(K=K, R=rotation_mat, t=t)
    x2 = cam.project(points)

    # Plot actual 3d points
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.set_aspect('equal')
    ax.plot(points[0], points[1], points[2], 'b.')
    ax.set_xlabel('x axis')
    ax.set_ylabel('y axis')
    ax.set_zlabel('z axis')
    ax.view_init(elev=140, azim=0)

    # Plot 3d to 2d projection
    f, ax = plt.subplots(2, sharex=True, sharey=True)
    plt.subplots_adjust(left=0.08,
                        bottom=0.08,
                        right=0.99,
                        top=0.95,
                        wspace=0,
                        hspace=0.01)
    ax[0].set_aspect('equal')
    ax[0].set_title(
        '3D to 2D projection. Bottom x-axis rotated by {0}°'.format(
            rotation_angle))
    ax[0].plot(x[0], x[1], 'k.')
    ax[1].plot(x2[0], x2[1], 'k.')
    plt.show()