updated code with more comments
This commit is contained in:
parent
460a71d2b3
commit
67123bb970
11 changed files with 262 additions and 1116 deletions
61
code/geom.py
61
code/geom.py
|
@ -2,9 +2,13 @@ from __future__ import division
|
|||
import numpy as np
|
||||
from random import sample
|
||||
|
||||
def_fov = np.pi * 2./3
|
||||
|
||||
def generatePoints(n, min_xyz, max_xyz, grid=False, randomZ=True, randFixedZ=False, depth=None, offset=0.5, xoffset=0, yoffset=0, zoffset=0):
|
||||
'''
|
||||
Generates 3D points either on a grid or randomly. the depth of each point can also be selected
|
||||
at random, given fixed depth for all points, or a random fixed depth for the points.
|
||||
|
||||
UPDATE: the order of the points in the end is also randomly shuffled.
|
||||
'''
|
||||
if randFixedZ: # means all points have the same z but z is chosen at random between max and min z
|
||||
z = min_xyz[2] + np.random.random() * (max_xyz[2] - min_xyz[2])
|
||||
else: # same depth
|
||||
|
@ -65,7 +69,7 @@ def getSphericalCoords(x, y, z):
|
|||
/
|
||||
Z
|
||||
|
||||
with a CounterClockwise rotation of the axis vectors
|
||||
with a CCW rotation of the axis vectors
|
||||
'''
|
||||
r = np.sqrt(x*x + y*y + z*z)
|
||||
teta = np.arctan(x/z)
|
||||
|
@ -96,6 +100,10 @@ def getRotationMatrixFromAngles(r):
|
|||
[R_11 R_12 R_13]
|
||||
[R_11 R_12 R_13]
|
||||
[0 0 0 ]
|
||||
|
||||
This is convenient since we can insert a translation column
|
||||
and get a full transformation matrix for vectors in homogeneous
|
||||
coordinates
|
||||
'''
|
||||
cos = map(np.cos, r)
|
||||
sin = map(np.sin, r)
|
||||
|
@ -155,20 +163,22 @@ def getRotationMatrix(a, b):
|
|||
return np.concatenate((np.eye(3) + vx, [[0, 0, 0]]))
|
||||
return np.concatenate((np.eye(3) + vx + vx.dot(vx)*((1-c)/s/s), [[0, 0, 0]]))
|
||||
|
||||
|
||||
|
||||
# Default FOV angle for a camera
|
||||
def_fov = np.pi * 2./3
|
||||
|
||||
class PinholeCamera:
|
||||
'''
|
||||
Models a basic Pinhole Camera with 9 degrees of freedom
|
||||
Models a Pinhole Camera with 9 degrees of freedom
|
||||
'''
|
||||
# Intrinsic parameters
|
||||
################################################################################################
|
||||
## Intrinsic parameters
|
||||
f = 1 # focal length
|
||||
p = (0, 0) # position of principal point in the image plane
|
||||
|
||||
# Extrinsic parameters
|
||||
# this rotation corresponds to a camera setting pointing towards (0, 0, -1)
|
||||
r = (0, 0, 0) # rotations in x, y', and z'' planes respectively
|
||||
t = (0, 0, 0) # camera center translation w.r.t world coordinate system (with no rotation)
|
||||
t = (0, 0, 0) # camera center translation w.r.t world coordinate system
|
||||
#
|
||||
# Using the above parameters we can construct the camera matrix
|
||||
#
|
||||
|
@ -178,15 +188,15 @@ class PinholeCamera:
|
|||
#
|
||||
# and thus we have x = PX for every point X in the word coordinate system
|
||||
# and its corresponding projection in the camera image plane x
|
||||
# NOTE: points are assumed to be represented by homogeneous vectors
|
||||
#
|
||||
# NOTE: points are assumed to be represented in homogeneous coordinates
|
||||
|
||||
# Other parameters
|
||||
label = ''
|
||||
|
||||
direction = (0, 0, 1) # camera direction
|
||||
direction = (0, 0, 1) # camera direction vector (viewpoint direction)
|
||||
fov = def_fov # field of view (both horizontal and vertical)
|
||||
image_width = 2*f*np.tan(fov/2.)
|
||||
################################################
|
||||
################################################################################################
|
||||
|
||||
def __init__(self, label, f = 1, r = (0, 0, 0), t = (0, 0, 0), direction = (0, 0, 1), fov=def_fov):
|
||||
self.label = label
|
||||
self.f = f
|
||||
|
@ -197,8 +207,11 @@ class PinholeCamera:
|
|||
self.recomputeCameraMatrix(True, True)
|
||||
|
||||
def recomputeCameraMatrix(self, changedRotation, changedIntrinsic):
|
||||
'''
|
||||
Updates camera matrix
|
||||
'''
|
||||
if changedRotation:
|
||||
# # Computing rotation matrix using elemental rotations
|
||||
# Computing rotation matrix using elemental rotations
|
||||
self.R = getRotationMatrixFromAngles(self.r)
|
||||
# by default if rotation is 0 then camera optical axis points to positive Z
|
||||
self.direction = np.array([0, 0, 1, 0]).dot(self.R)
|
||||
|
@ -208,8 +221,8 @@ class PinholeCamera:
|
|||
self.Rt = np.concatenate((self.R, np.array([[_t[0], _t[1], _t[2], 1]]).T), axis=1)
|
||||
# instead of the above, we could also represent translation matrix as [I|-C] and
|
||||
# [R -RC]
|
||||
# then compute Rt as R[I|-C] = [0 1] [R] [-RC]
|
||||
# but we're basically do the same thing by concatenating [0] with [ 1]
|
||||
# then compute Rt as R[I|-C] = [0 1] [R] [-RC]
|
||||
# but we basically do the same thing by concatenating [0] with [ 1]
|
||||
|
||||
if changedIntrinsic:
|
||||
# Computing intrinsic matrix
|
||||
|
@ -218,10 +231,11 @@ class PinholeCamera:
|
|||
[f, 0, px, 0],
|
||||
[0, f, py, 0],
|
||||
[0, 0, 1, 0]])
|
||||
|
||||
# Full Camera Projection Matrix
|
||||
self.P = self.K.dot(self.Rt)
|
||||
|
||||
################################################
|
||||
################################################################################################
|
||||
## Intrinsic parameter setters
|
||||
def setF(self, f, auto_adjust = True):
|
||||
self.f = f
|
||||
|
@ -235,7 +249,8 @@ class PinholeCamera:
|
|||
def setFOV(self, fov):
|
||||
self.fov = fov
|
||||
self.image_width = 2*self.f*np.tan(fov/2.)
|
||||
################################################
|
||||
|
||||
################################################################################################
|
||||
## Extrinsic parameter setters
|
||||
def setT(self, t, auto_adjust = True):
|
||||
self.t = t
|
||||
|
@ -245,11 +260,13 @@ class PinholeCamera:
|
|||
self.r = r
|
||||
if auto_adjust:
|
||||
self.recomputeCameraMatrix(True, False)
|
||||
################################################
|
||||
|
||||
################################################################################################
|
||||
## Main methods
|
||||
def project(self, p):
|
||||
'''
|
||||
Computes projection of a point p in world coordinate system
|
||||
using its homogeneous vector coordinates [x, y, z, 1]
|
||||
using its homogeneous coordinates [x, y, z, 1]
|
||||
'''
|
||||
if len(p) < 4: p = (p[0], p[1], p[2], 1)
|
||||
projection = self.P.dot(np.array(p))
|
||||
|
@ -272,6 +289,10 @@ class PinholeCamera:
|
|||
return map(lambda p:(p*self.image_width)-offset, pts)
|
||||
|
||||
class Camera(PinholeCamera):
|
||||
'''
|
||||
A wrapper around abstract Pinhole camera object with further
|
||||
methods for visualization.
|
||||
'''
|
||||
default_radius = 2.0
|
||||
|
||||
def updateShape(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue