Skip to content
Snippets Groups Projects
Commit 1e77afbf authored by s192327's avatar s192327
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
# %% IMPORTS
from pix4DUtils import loadCameras
import numpy as np
import cv2
# %% LOAD CAMERAS
project_dir = "../datasets/2020-07-01-DTU-Risoe-80-meter"
cameras, offset = loadCameras(project_dir)
# %% DEFINE POINTS ON IMAGE PLANE BOUNDARIES
# select camera to be used
camera = cameras[0]
# number of points on each edge of the image plane
n_points = 100
# check that there are at least two points on each edge
if n_points < 2:
raise Exception("At least 2 points required for each edge!")
# define points
linwidth = np.linspace(0,camera.imgwidth,n_points)[:,np.newaxis]
linheight = np.linspace(0,camera.imgheight,n_points)[1:-1,np.newaxis]
points_distorted = np.vstack((
np.hstack((linwidth,np.zeros(linwidth.shape))), # top
np.hstack((linwidth,camera.imgheight*np.ones(linwidth.shape))), # bottom
np.hstack((np.zeros(linheight.shape),linheight)), # left
np.hstack((camera.imgwidth*np.ones(linheight.shape),linheight))) # right
)[np.newaxis,:]
# %% UNDISTORT POINTS
# get homogenoues undistorted points in camera coordinates
points_undistorted_3dhomog = cv2.undistortPoints(
points_distorted,
camera.K,
camera.dist
).squeeze()
# append ones to third column
points_undistorted_3dhomog = np.hstack((points_undistorted_3dhomog,np.ones((len(points_undistorted_3dhomog),1))))
points_undistorted_3dhomog_list = points_undistorted_3dhomog
# make list of scaled points
for i in range(2,100):
points_undistorted_3dhomog_list = np.vstack((points_undistorted_3dhomog_list,i*points_undistorted_3dhomog))
# append ones to fourth column
points_undistorted_3dhomog_list = np.hstack((points_undistorted_3dhomog_list,np.ones((len(points_undistorted_3dhomog_list),1))))
# %% TRANSFORM UNDISTORTED POINTS TO WORLD COORDINATE SYSTEM
# create inverse transformation matrix
R_t_inv = np.hstack((camera.R.T,camera.t.reshape(-1,1)))
# multiply each point for transformation matrix
points_undistorted_3dworld = np.tensordot(points_undistorted_3dhomog_list,R_t_inv,axes=((1,1)))
# %% DISPLAY RESULT
import open3d as o3d
pcd_camera = o3d.geometry.PointCloud()
pcd_camera_points = camera.t.reshape(1,-1)
pcd_camera_points = np.vstack((pcd_camera_points,points_undistorted_3dworld))
pcd_camera_points = pcd_camera_points + offset.reshape(1,-1)
pcd_camera.points = o3d.utility.Vector3dVector(pcd_camera_points)
pcd_scene = o3d.io.read_point_cloud(project_dir+"/pcd.ply").voxel_down_sample(voxel_size=0.1)
pcd_scene_points = np.asarray(pcd_scene.points)
pcd_tot = o3d.geometry.PointCloud()
pcd_points_tot = np.vstack((pcd_camera_points,pcd_scene_points))
pcd_colors_tot = np.vstack((np.array([1,0,0])*np.ones(pcd_camera_points.shape),np.asarray(pcd_scene.colors)))
pcd_tot.points = o3d.utility.Vector3dVector(pcd_points_tot)
pcd_tot.colors = o3d.utility.Vector3dVector(pcd_colors_tot)
o3d.io.write_point_cloud(project_dir+"/pcd_rays.ply", pcd_tot)
main.py 0 → 100644
# %% IMPORTS
import pix4DUtils as p4d
# %% LOAD CAMERAS AND SCENE
project_dir = "../datasets/OL-test"
scene = p4d.loadScene(project_dir)
cameras = p4d.loadCameras(project_dir)
# %% GET BOUNDARIES
bounds_scene, bounds_world, Hs = p4d.getBoundsHomography(scene,cameras,10)
# %% SAVE BOUNDARIES AS JSON FILE
p4d.saveBoundariesJSON(bounds_scene,scene.path,"bounds_mosaic")
p4d.saveBoundariesJSON(bounds_world,scene.path,"bounds_world")
# %% SAVE IMAGES OF MOSAICS WITH EACH CAMERA'S VIEWING AREA
p4d.drawCameras(bounds_scene,scene.img,scene.path,0.1)
# %% TAKE AND SAVE VIRTUAL CAMERA PICTURES OBTAINED WITH AN HOMOGRAPHY
p4d.saveHomographyPictures(scene.img,cameras,Hs,scene.path)
# %% COMPARE ORIGINAL PICTURES WITH HOMOGRAPHY IMAGES
p4d.compareHomographyPicture(scene,cameras,Hs,threshold=None)
# %% IMPORTS
import numpy as np
import cv2
import open3d as o3d
from pix4DUtils import loadCameras
# %% LOAD CAMERAS AND OFFSET
project_dir = "../datasets/2020-07-01-DTU-Risoe-80-meter"
cameras, offset = loadCameras(project_dir)
# %% LOAD POINT CLOUD
pcd = o3d.io.read_point_cloud(project_dir+"/pcd.ply")
# get xyz and rgb as numpy arrays
xyz = np.asarray(pcd.points)
rgb = np.asarray(pcd.colors)
# %% PROJECT 3D POINTS TO IMAGE PLANE
# define which camera to use
camera = cameras[0]
# subtract offset from xyz
xyz = xyz - offset
# multiply each point for the projection matrix
n_points = len(xyz)
uv = cv2.projectPoints(
xyz,
cv2.Rodrigues(camera.R)[0],
#camera.t.astype("float64"),
np.dot(-camera.R,camera.t),
camera.K,
camera.dist)[0].squeeze()
# %% RETAIN ONLY POINTS THAT ARE INSIDE IMAGE BOUNDARIES
mask_x = np.logical_and(uv[:,0]>0,uv[:,0]<camera.imgwidth)
mask_y = np.logical_and(uv[:,1]>0,uv[:,1]<camera.imgheight)
mask = np.logical_and(mask_x,mask_y)
uv = uv[mask]
# %% GENERATE POINT CLOUD FOR RESULT VISUALIZATION (DRAW IN RED THE POINTS THAT CONTRIBUTED TO THE IMAGE)
rgb[mask] = np.array([[1,0,0]])
pcd.colors = o3d.utility.Vector3dVector(rgb)
o3d.io.write_point_cloud(project_dir+"/pointsinimage.ply", pcd)
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment