Closed EchoTHChen closed 1 year ago
Your code seems to work for me. Only the cubemaps are upside down because the y-axis in image pixels space is upside down compared to the up axis in world coordinates.
Just update def GetPerspective(self, FOV, THETA, PHI, height, width):
x = np.arange(width)
y = np.arange(height - 1, -1, -1)
When I try to test the perspective projections using the visualization code, the re-projections results are wrong. I guess the poses are wrong, but I do not find out the problem. Source view:
Target views:
The visualization code:
import os
import cv2
import numpy as np
data_dir="./save_pers"
img_names = os.listdir(data_dir+"/images")
current_img_name=data_dir+"/images/1_pose004.png"
current_rgb = cv2.imread(current_img_name, cv2.IMREAD_COLOR)
current_depth = np.load(data_dir+"/depths/1_pose004.npz")["depth"]
current_pose = np.load(data_dir+"/poses/1_pose004.npz")["pose"]#w2c
K = np.load(data_dir+"/K.npz")["K"]
print("K:",K)
H, W = current_rgb.shape[:2]
save_dir="./vis"
os.makedirs(save_dir, exist_ok=True)
cv2.imwrite(save_dir+"/src_orig.jpg", current_rgb)
#get keypoints in source views
gray = cv2.cvtColor(current_rgb, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create(nfeatures=200, contrastThreshold=0.02, edgeThreshold=20)
kp = sift.detect(gray, None)
print("len(kp):", len(kp))
keypoints = [[int(kp[idx].pt[0]), int(kp[idx].pt[1])] for idx in range(len(kp))]
keypoints = np.array(keypoints)
z_depth = current_depth[keypoints[: , 1], keypoints[:, 0]]
valid_list = np.where(z_depth>0)[0]
z_depth = z_depth[valid_list]#n, 1
keypoints = keypoints[valid_list] #n ,2
#write keypoints in src view.
for i in range(len(keypoints)):
coord_x, coord_y = keypoints[i, 0], keypoints[i, 1]
cv2.circle(current_rgb, (int(coord_x), int(coord_y)), color = (0, 0, 255), thickness = 1, radius=1)
cv2.imwrite(save_dir+"/src.jpg", current_rgb)
img_names = os.listdir(data_dir+"/images")
num_views = len(img_names)
# keypoints:Nx2
N, _ = keypoints.shape
points_homo = np.concatenate([keypoints, np.ones((N, 1))], axis=1)#N, 3
points_cam_norm = np.linalg.inv(K)@points_homo.T #3x3, 3xN->3xN
points_cam = points_cam_norm*(z_depth.reshape(1, N)).repeat([3], axis=0)#3xN, 1,N->3xN
pose_name = "w2c"
if pose_name=="w2c":
points_w = np.linalg.inv(current_pose) @ np.concatenate([points_cam, np.ones((1, points_cam.shape[1]))], axis=0)#4,N
else:
points_w = current_pose@ np.concatenate([points_cam, np.ones((1, points_cam.shape[1]))], axis=0)#4,N
for view_i in range(num_views):
print("view_i:", view_i)
img_name = img_names[view_i]
rgb_other = cv2.imread(data_dir+"/images/"+img_name, cv2.IMREAD_COLOR)
pose_name = img_names[view_i][:-4]+".npz"
pose_other = np.load(data_dir+"/poses/"+pose_name)["pose"]#w2c
if pose_name == "w2c":
points_c = pose_other @ points_w
else:
points_c = np.linalg.inv(pose_other) @ points_w #4,N
points_coords_h = K @ points_c[:3, :]#()@(3,N)->3,N (0, 0, 1)
valid_list = np.where(points_c[2, :]>0)[0]
points_coords_h = points_coords_h[:, valid_list]
points_coords = (points_coords_h/points_coords_h[2:, :]).T #->N,2
cv2.imwrite(save_dir+"/other_"+img_name[:-4]+"_orig.jpg", rgb_other)
for coord_i in range(len(points_coords)):
coord_x, coord_y = points_coords[coord_i, 0], points_coords[coord_i, 1]
if coord_x >=0 and coord_x < W and coord_y >=0 and coord_y < H:
cv2.circle(rgb_other, (int(coord_x), int(coord_y)), color = (0, 0, 255), thickness = 1, radius=1)
cv2.imwrite(save_dir+"/other_"+img_name[:-4]+".jpg", rgb_other)
Hi EchoTHChen,
Can we also have your name in the GitHub or via email privately as well?
I think the questions are more related to helping / debugging rather than questions for our paper / repo now. We may help but probably we should discuss it via email thread rather than GitHub (and on GitHub you are anonymous here).
Thank you so much and I want to make sure David is making the best use of his research time :)
Cheers, Ruofei
@ruofeidu Yes, you are right. Sorry for occupying you too much time. My name is Zheng Chen from Tsinghua University. My email is chenz20@mails.tsinghua.edu.cn.
I use github instead of email because there are many codes in my descriptions. it is more convenient to use to the markdown language in github than emal.
I will use email to contact David later privately.
Happy to chat with you more over emails. Feel free to also cc me at me [at] duruofei [dot] com :)
I think I fixed the bug. Try this: https://gist.github.com/dli7319/0198e52a443001a9413878b2a9a2dfd0
Thanks for your nice reply and the detailed code. It works fine for perspective projection.
Problem:
Sorry to bother you again! I want to split the panorama into perspective views and run perspective methods on them.
Although cube maps and panorama have been already given in this repo, I wonder how to split panorama into cube maps (for both images and poses). I have other panorama datasets in my hand, I also want to split them into perspective views.
Because you have very rich experience and knowledge of panorama, I would like to ask you how to turn panorama into perspective (such as cube maps)?
I tried to write the process of transforming habitat-matterport3d into perspective views based on the previous code, but I failed. How can I split panorama into cube maps (for both images and poses) based on habitat-matterport3d? Thanks!
Code:
1. The main script to split panorama into cube-maps. (python main.py)
2. The auxiliary function to get perspective view from panorama according to theta and phi. (my_e2p.py)
3. visualization code to check the results of splitting and perspective projections.
Previous Code(Basic):
1. create_rgb_dataset.py
2. generate test_data.npz
3. panorama projection