Back to Parent

## This is the code that changed the panoramic picture into the respective 6 side
## pictures, so that I could funnel these pictures into ReMake/ReCap to from photos
## to 3D renderings of the room through photogrammetry

import math
import numpy as np
import cv2
import cv

def changing_uv_to_xyz(width, height):
    X, Y = np.meshgrid(np.linspace(-1.0, 1.0, width), np.linspace(-1.0, 1.0, height))
    Z = np.ones((width, height))
    return (X, Y, Z)

def rotating(coords, dir, width, height):
    X, Y, Z = coords
    R = compute_rot(dir)
    coords_rotated = R.dot([X.reshape(-1), Y.reshape(-1), Z.reshape(-1)])
    X_mod = coords_rotated[0, :].reshape(width, height)
    Y_mod = coords_rotated[1, :].reshape(width, height)
    Z_mod = coords_rotated[2, :].reshape(width, height)
    return (X_mod, Y_mod, Z_mod)

def compute_rot(dir):
    if ("front" == dir): #f
        return np.array([[-1.0, 0.0, 0.0],[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]])
    elif ("left" == dir): #l
        return np.array([[1.0, 0.0, 0.0],[0.0, 0.0, 1.0],[0.0, 1.0, 0.0]])
    elif ("back" == dir): #b
        return np.array([[-1.0, 0.0, 0.0],[0.0, -1.0, 0.0],[0.0, 0.0, -1.0]])
    elif ("right" == dir): #r
        return np.array([[-1.0, 0.0, 0.0],[0.0, 0.0, -1.0],[0.0, 1.0, 0.0]])
    #figure out the next 2
    elif ("up" == dir): #u
        return np.array([[0.0, 0.0, 1.0],[0.0, 1.0, 0.0],[1.0, 0.0, 0.0]])
    else:               #down
        return np.array([[0.0, 0.0, -1.0],[0.0, 1.0, 0.0],[-1.0, 0.0, 0.0]])

def compute_trans (dir):
    if(dir == "up"):
        return 5 #y z
    elif(dir == "down"):
        return 6 #y z
    elif(dir == "left"):
        return 2 #x z
    elif(dir == "right"):
        return 4 #x z
    elif(dir == "front"):
        return 1 #identiy matrix
    else: #(dir == "back")
        return 3 #x z

def changing_into_theta_phi(coords_mod, src_width, src_height):
    X, Y, Z = coords_mod
    radius = (X ** 2.0 + Y ** 2.0) ** 0.5
    thetas = np.arctan2(X, Y)
    phis = np.arctan2(Z, radius)

    #now adust the thetas and phis to like the actual sorce image size
    thetas_shrunk = ((thetas / math.pi)) #now everything is from (-1.0, 1.0)
    thetas_shrunk_shifted = thetas_shrunk + 1.0 #now everything is from (0.0, 2.0)
    thetas_shrunk_some_more = thetas_shrunk_shifted * 0.5 #now everything is from (0.0, 1.0)
    thetas_mod = thetas_shrunk_some_more * src_width

    phis_shrunk = ((phis / math.pi)) #now everything is from (-0.5, 0.5)
    phis_shrunk_shifted = phis_shrunk + 0.5 #now everything is from (0.0, 1.0)
    phis_mod = phis_shrunk_shifted * src_height

    return (thetas_mod, phis_mod)

if __name__ == '__main__':
    source_images = [cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic1.JPG"), 
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic2.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic3.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic4.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic5.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic6.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic7.JPG"),
                     cv2.imread("C:\\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\Pic8.JPG")]
    #This is where I stored the panoramic images that I used

    dirs = ["front", "left", "back", "right", "up", "down"]

    count = 1

    for img in source_images:
        for dir in dirs:
            (X, Y, Z) = changing_uv_to_xyz(2048, 2048)
            (X_mod, Y_mod, Z_mod) = rotating((X, Y, Z), dir, 2048, 2048)
            (U, V) = changing_into_theta_phi((X_mod, Y_mod, Z_mod), 5376, 2688) ##5376, 2688
            pic = cv2.remap(img, np.array(U, dtype=np.float32),  np.array(V, dtype=np.float32), cv2.INTER_LINEAR)
            cv2.imwrite("C:\Users\\Mitu\\Documents\\16457_project_stuff\\more_panos\\smaller_room_1\\pics\\pic" + str(count) + "_" + str(compute_trans(dir)) + ".jpg", pic)
            #testing\\pics\\pic" + str(count) + "_" + str(compute_trans(dir)) + ".jpg", pic)
        count = count + 1
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0