Skip to content

Instantly share code, notes, and snippets.

View sergeyprokudin's full-sized avatar

Sergey Prokudin sergeyprokudin

  • ETH Zürich
  • Zürich
View GitHub Profile
import numpy as np
def load_colmap_sparse_points(points3d_path):
'''Load COLMAP points3D.txt as numpy array
'''
with open(points3d_path, 'r') as f:
points_txt = f.readlines()[3:]
n_points = len(points_txt)
points_np = np.zeros([n_points, 6])
@sergeyprokudin
sergeyprokudin / get_uv_coords.py
Created March 30, 2021 10:28
get (u,v) coords of every image pixel
def get_img_uv_coords(img):
"""
Get u,v coords for image pixels
"""
img_height, img_width = img.shape[0], img.shape[1]
uv_map = np.zeros([img_height, img_width, 2])
u = np.arange(0, img_height)
v = np.arange(0, img_width)
@sergeyprokudin
sergeyprokudin / get_rotation_mats.py
Last active May 20, 2021 11:11
generate rotation matrices for each 3D axis given an angle
import numpy as np
def get_rotation_mat(theta=np.pi, axis=0):
# https://en.wikipedia.org/wiki/Rotation_matrix
sin = np.sin(theta)
cos = np.cos(theta)
if axis==0:
R = np.asarray([[1, 0, 0],
[0, cos, -sin],
@sergeyprokudin
sergeyprokudin / show_img.py
Created August 12, 2020 10:53
Simple functions to plot images
import numpy as np
import matplotlib.pyplot as plt
def show_img(img, figsize=(10, 10)):
'''
Show image using matplotlib
Parameters
----------
img : array of shape [img_width, img_height, 3]
@sergeyprokudin
sergeyprokudin / pairwise_distances.py
Created August 9, 2020 07:55
Compute the matrix of pairwise L2 distances between two arrays
import numpy as np
def pdist(X1, X2):
"""Computes the matrix of pairwise distances between X1 and X2.
The implementation is based on the following observation for matrices:
(X1-X2)^2 = X1^2 - 2*X1*X2 + X2^2
Parameters
----------
@sergeyprokudin
sergeyprokudin / keras_prob_dnn.py
Last active February 22, 2022 17:36
Simple Keras DNN with probabilistic Gaussian output (mean + variance)
import numpy as np
from keras.layers import Input, Dense
from keras.models import Model, Sequential
def dnn(n_inputs, n_outputs, n_hidden_layers=3, hlayer_size=128, probabilistic=True):
"""Defines simple DNN model
"""
x_input = Input(shape=[n_inputs])
@sergeyprokudin
sergeyprokudin / chamfer_distance.py
Created April 30, 2020 14:04
Vanilla Chamfer distance computation in NumPy
import numpy as np
from sklearn.neighbors import NearestNeighbors
def chamfer_distance(x, y, metric='l2', direction='bi'):
"""Chamfer distance between two point clouds
Parameters
----------
x: numpy array [n_points_x, n_dims]
@sergeyprokudin
sergeyprokudin / set_latex_font_matplotlib.py
Last active April 28, 2020 13:08
Setting latex font for matplotlib plots
# details:
# https://stackoverflow.com/questions/11367736/matplotlib-consistent-font-using-latex/27697390
from matplotlib import rc
import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
@sergeyprokudin
sergeyprokudin / point_cloud_viz.py
Last active April 17, 2020 08:28
Simple point cloud visualization with pyntcloud library
"""
Simple point cloud visualization with pyntcloud library
"""
import pyntcloud as pynt
import pandas as pd
import numpy as np
def show_cloud(xyz, rgb=None, initial_point_size=0.01):
@sergeyprokudin
sergeyprokudin / ball_pivoting.py
Last active November 30, 2023 15:37
Given a 3D point cloud, get unstructured mesh using Ball Pivoting algorithm
import open3d as o3d
import trimesh
import numpy as np
def ball_pivoting_reconstruction(xyz, radii=None):
"""Given a 3D point cloud, get unstructured mesh using ball pivoting algorithm
Based on this stack overflow code snippet:
https://stackoverflow.com/questions/56965268/how-do-i-convert-a-3d-point-cloud-ply-into-a-mesh-with-faces-and-vertices