Skip to content

Instantly share code, notes, and snippets.

@MikeK4y
Last active September 6, 2019 17:55
Show Gist options
  • Save MikeK4y/c3bcfc94ced9a476db19c629cf05a02e to your computer and use it in GitHub Desktop.
Save MikeK4y/c3bcfc94ced9a476db19c629cf05a02e to your computer and use it in GitHub Desktop.
Given the 3D pose of a camera with known field of view get the distance of a point to the plane of view
'''
MIT License
Copyright (c) 2019 Michail Kalaitzakis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import numpy as np
class Camera_Utils:
'''
Given the 3D pose of a camera with known field of view get the distance of a point to the plane of view
'''
# Initialize with camera characteristics
# foV_X -> Field of view on x image axis (rad)
# foV_Y -> Field of view on y image axis (rad)
# depthOfField -> Distance of
def __init__(self, foV_X, foV_Y, depthOfField):
# Camera characteristics
self.fovX = foV_X / 2.0
self.fovY = foV_Y / 2.0
self.doF = depthOfField
self.state = None
self.R = None
self.t = None
self.planeOfView = None
self.normalV = None
self.RuL = None
self.RuR = None
self.RlR = None
self.RlL = None
self.m = None
# Initialize Rotation matrices
self.initializeRotations()
# Updates the camera state
# newState -> np.array([x, y, z, qw, qx, qy, qz])
def updateState(self, newState):
self.state = newState
self.R = self.quat2R(self.state[3:])
self.t = np.asmatrix(self.state[:3]).T
self.getPlaneOfView()
# Retturns distance from point
# point -> np.array([x, y, z])
def getDistance(self, point):
poi = np.asmatrix(point).T
poiP = self.getPlaneProjection(poi)
if self.isInPlaneOfView(poiP):
return np.linalg.norm(poi - poiP)
else:
dMin = 1.0e9
for i in range(self.planeOfView.shape[0] - 1):
res = self.getLineProjection(self.planeOfView[i].T, self.planeOfView[i + 1].T, poi)
if (res[0] >= 0.0 and res[0] <= 1.0):
dL = np.linalg.norm(poi - res[1])
if dL < dMin:
dMin = dL
if dMin == 1.0e9:
for i in range(self.planeOfView.shape[0] - 1):
dP = np.linalg.norm(self.planeOfView[i].T - poi)
if dP < dMin:
dMin = dP
return dMin
# Updates plane of view corner points and normal vector
def getPlaneOfView(self):
self.normalV = self.R * np.matrix([[self.doF], [0.0], [0.0]])
uLc = self.m * self.R * self.RuL * self.R.T * self.normalV + self.t
uRc = self.m * self.R * self.RuR * self.R.T * self.normalV + self.t
lRc = self.m * self.R * self.RlR * self.R.T * self.normalV + self.t
lLc = self.m * self.R * self.RlL * self.R.T * self.normalV + self.t
# Push the four corners and an extra copy of the first corner for ease in loops
self.planeOfView = np.vstack([uLc.T, uRc.T, lRc.T, lLc.T, uLc.T])
# Returns projection of point on a plane
def getPlaneProjection(self, point):
m = ((self.planeOfView[1].T - point).T * self.normalV) / (self.normalV.T * self.normalV)
return m.item(0) * self.normalV + point
# Returns the projection of a point to a line along its cos(theta)
def getLineProjection(self, a, b, p):
cTheta = ((p - a).T * (b - a)) / ((b - a).T * (b - a))
pP = a + (b - a) * cTheta
return cTheta, pP
# Returns true if the point lies inside the plane of view
def isInPlaneOfView(self, point):
theta = 0.0
for i in range(self.planeOfView.shape[0] - 1):
v1 = point - self.planeOfView[i].T
v2 = self.planeOfView[i + 1].T - point
theta += np.arccos((v1.T * v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)))
if (np.abs(theta - 2*np.pi) < 1.0e-6):
return True
else:
return False
# Return the Rotation matrix derived from the quaternion
# q -> np.array([qw, qx, qy, qz])
def quat2R(self, q):
r00 = 1 - 2*(q[2]*q[2] + q[3]*q[3])
r01 = 2*(q[1]*q[2] - q[3]*q[0])
r02 = 2*(q[1]*q[3] + q[2]*q[0])
r10 = 2*(q[1]*q[2] + q[3]*q[0])
r11 = 1 - 2*(q[1]*q[1] + q[3]*q[3])
r12 = 2*(q[2]*q[3] - q[1]*q[0])
r20 = 2*(q[1]*q[3] - q[2]*q[0])
r21 = 2*(q[2]*q[3] + q[1]*q[0])
r22 = 1 - 2*(q[1]*q[1] + q[2]*q[2])
R = np.matrix([[r00, r01, r02], [r10, r11, r12], [r20, r21, r22]])
return R
# Initializes the standard rotation matrices
def initializeRotations(self):
cosX = np.cos(self.fovX)
cosY = np.cos(self.fovY)
sinX = np.sin(self.fovX)
sinY = np.sin(self.fovY)
# Initialize vector multiplier
self.m = 1.0 / (cosX * cosY)
R_y = np.asmatrix(np.eye(3))
R_y[0, 0] = cosY
R_y[2, 2] = cosY
R_y[0, 2] = sinY
R_y[2, 0] = -sinY
R_x = np.asmatrix(np.eye(3))
R_x[0, 0] = cosX
R_x[1, 1] = cosX
R_x[0, 1] = -sinX
R_x[1, 0] = sinX
# Initialize matrices
self.RuL = R_y.T * R_x
self.RuR = R_y.T * R_x.T
self.RlR = R_y * R_x.T
self.RlL = R_y * R_x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment