Giter Club home page Giter Club logo

camtools's Introduction

CamTools

Tools for handling pinhole camera parameters and plotting cameras.

Install

# For development.
pip install -e .

# For install.
pip install .

Matrix conventions

K        : (3, 3) # Camera intrinsic matrix.
                  # [[fx,  s, cx],
                  #  [ 0, fy, cy],
                  #  [ 0,  0,  1]]
                  # x: goes from top-left to top-right.
                  # y: goes from top-left to bottom-left.
R        : (3, 3) # Rotation matrix.
Rc       : (3, 3) # Rc = R.T = R.inv().
t        : (3,)   # Translation.
T        : (4, 4) # Extrinsic matrix with (0, 0, 0, 1) row below.
                  # T = [R  | t
                  #      0  | 1]
                  # T projects world space coordinate to the camera space
                  # (a.k.a view space or eye space). The camera center in world
                  # space projected by T becomes [0, 0, 0, 1]^T, i.e. the camera
                  # space has its origin at the camera center.
                  # T @ [[|], = [[0],
                  #      [C],    [0],
                  #      [|],    [0],
                  #      [1]]    [1]]
P        : (3, 4) # World-to-pixel projection matrix. P = K @ [R | t] = K @ T[:3, :].
W2P      : (4, 4) # World-to-pixel projection matrix. It is P with (0, 0, 0, 1)
                  # row below. When using W2P @ point_homo, the last
                  # element is always 1, thus it is ignored.
pose     : (4, 4) # Camera pose. pose = T.inv(). pose[:3, :3] = R.T = Rc. pose[:3, 3] = C.
C        : (3,)   # Camera center.

Coordinate conventions

3D to 2D projection

Project 3D point [X, Y, Z, 1] to 2D [x, y, 1] pixel, e.g. with pixels = ct.project.points_to_pixel(points, K, T).

# 0 -------> 1 (x)
# |
# |
# v (y)

cols = pixels[:, 0]  # cols, width,  x, top-left to top-right
rows = pixels[:, 1]  # rows, height, y, top-left to bottom-left
cols = np.round(cols).astype(np.int32)
rows = np.round(rows).astype(np.int32)
cols[cols >= width] = width - 1
cols[cols < 0] = 0
rows[rows >= height] = height - 1
rows[rows < 0] = 0

It can be confusing to use x, y, u, v. Prefer row and col.

UV coordinates

# OpenGL convention:
# 1 (v)
# ^
# |
# |
# 0 -------> 1 (u)

# The following conversion accounts for pixel size
us = 1 / width *  (0.5 + cols)
vs = 1 / height * (0.5 + (height - rows - 1))

Notes on vector vs. matrix

We choose to use 1D array for vector values like t and C. For example, t is of shape (3, ) instead of (3, 1).

# The `@` operator can be directly used to dot a matrix and a vector
# - If both arguments are 2-D they are multiplied like conventional matrices.
# - If either argument is N-D, N > 2, it is treated as a stack of matrices
#   residing in the last two indexes and broadcast accordingly.
# - If the first argument is 1-D, it is promoted to a matrix by prepending a 1
#   to its dimensions. After matrix multiplication the prepended 1 is removed.
# - If the second argument is 1-D, it is promoted to a matrix by appending a 1
#   to its dimensions. After matrix multiplication the appended 1 is removed.

# t is (3, ) and it is promoted to be (3, 1).
C = - R.T @ t

Unit tests

pytest . -s
pytest camtools -s

TODO

  • Full unit tests
  • PyTorch/Numpy wrapper (e.g. with eagerpy)

camtools's People

Contributors

yxlao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.