basic_robotics.metrology.virtual_vision module
Virtual camera and vision simulation tools for estimating object poses from viewed scenes.
Models a scene populated by simulated pinhole Cameras and trackable SceneObj/Observed points, and provides triangulation-style routines for reconstructing 3D point and object positions from multiple simulated camera views.
- class basic_robotics.metrology.virtual_vision.Scene[source]
Bases:
objectContainer for all Cameras and SceneObjs in a simulated vision scene.
Coordinates gathering observations from every camera and reconstructing 3D point positions from those observations.
- newSceneObj(objList, tol=0.01, name='OBJ')[source]
Create a new SceneObj from a list (or single transform) of points and add it to the scene.
- addSceneObj(obj)[source]
Add an existing SceneObj to the scene.
- Parameters:
obj (SceneObj) – Object to add.
- CalculateGrid()[source]
Compute the pairwise distance matrix between all currently observed points.
- Returns:
ndarray – Symmetric matrix of distances between every pair of entries in self.observed.
- GetObjPositionsFromPoints()[source]
Gather observations from every camera and reconstruct 3D positions of the seen points.
Each camera captures every SceneObj’s points; matching observations of the same point across different cameras are merged, and points seen by fewer than two cameras are discarded since their position cannot be triangulated.
- Returns:
list[Observed] – Reconstructed points, each with an averaged 3D position, or None if no points were observed by any camera.
- class basic_robotics.metrology.virtual_vision.Observed(pixpos, q, camera, tol=0.0003)[source]
Bases:
objectA single 3D point as observed by one or more Cameras, used to triangulate its world position.
- __init__(pixpos, q, camera, tol=0.0003)[source]
Create a new Observed point from a single camera’s observation.
- Parameters:
- CalcAvgGuess()[source]
Average the draft position guesses collected from matching observations.
- Returns:
tm – Averaged position estimate, also stored on self.cPos.
- AvgWDist(x)[source]
Compute an averaged 3D position estimate and its residual error for a set of ray scales.
For each viewing camera, projects along that camera’s collated direction vector by the corresponding scale in x, averages the resulting points, and measures how far each individual projected point is from that average.
- Parameters:
x – Per-camera scale factors along each camera’s viewing ray.
- Returns:
tuple – (davg, ts) where davg (float) is the average distance of each per-camera projected point from the averaged position, and ts (tm) is the averaged position estimate.
- CalcAvgCoalated()[source]
Solve for the ray scales that minimize triangulation residual error and return the position.
- Returns:
tm – Averaged position estimate corresponding to the optimized ray scales.
- collateVector()[source]
Compute the unit direction vector from the camera through the observed pixel.
- Parameters:
None
- Returns:
tm – Unit vector (as a tm with zero rotation) pointing from the camera’s position toward the observed point, expressed in the camera’s local frame.
- eq(other)[source]
Determine whether this observation and another (from a different camera) view the same point.
Finds the pair of ray scales that minimizes the distance between the two cameras’ projected rays; if that minimum distance is within tolerance, the candidate 3D position(s) are recorded as draft poses.
- Parameters:
other (Observed) – Observation from another camera to compare against.
- Returns:
bool – True if the two observations are judged to be the same point.
- class basic_robotics.metrology.virtual_vision.SceneObj(objList, tol=0.01, name='OBJ')[source]
Bases:
objectA rigid object in the scene, defined by a set of points with known relative geometry.
Used to identify and reconstruct the object’s pose from observed points by matching the known relative distances/transforms between its defining points.
- __init__(objList, tol=0.01, name='OBJ')[source]
Create a new SceneObj from a list of points, or generate one from a single point.
If objList is a list of tm points, it is used directly with the first element treated as the lead point. Otherwise, objList is treated as a single lead point and three additional points are generated around it at random offsets along each axis.
- getRels()[source]
Compute each point’s relative transform and distance from the lead point.
Populates self.rels and self.dists in place.
- adjRot(x, y)[source]
Score a candidate lead-point orientation against the object’s known point geometry.
Applies the orientation in x to pose y, then sums the distance error between where each defining point would be under that orientation and its actual observed position.
- Parameters:
x – Candidate orientation (roll, pitch, yaw) to test.
y (tm) – Candidate lead-point pose to adjust with x.
- Returns:
float – Sum of distance errors across all defining points.
- testAll(x, ob)[source]
Score a candidate orientation against every candidate lead pose and track the best match.
- Parameters:
x – Candidate orientation to test against each pose in ob.
ob – Candidate lead poses to score.
- Returns:
float – The smallest (best) score found across all candidate poses; the index of that best match is stored in self.min.
- getPos(scene)[source]
Attempt to locate this object’s position among a scene’s observed points.
Searches for a set of observed points in the scene whose mutual distances match this object’s expected geometry within tolerance, then selects the best-fitting candidate pose.
- Parameters:
scene (Scene) – Scene whose observed points are searched.
- Returns:
tm – Best-fit position for this object, also stored on self.cPos.
- class basic_robotics.metrology.virtual_vision.Camera(aptx, apty, pixX, pixY, maxX, maxY, sigma, camT, id=0)[source]
Bases:
objectSimulated pinhole camera that projects 3D scene points into pixel observations.
- __init__(aptx, apty, pixX, pixY, maxX, maxY, sigma, camT, id=0)[source]
Create a new simulated Camera.
- Parameters:
aptx (float) – Focal length / aperture parameter along the x axis.
apty (float) – Focal length / aperture parameter along the y axis.
pixX (float) – Pixel x coordinate of the image center.
pixY (float) – Pixel y coordinate of the image center.
maxX (float) – Maximum resolvable x resolution/extent of the sensor.
maxY (float) – Maximum resolvable y resolution/extent of the sensor.
sigma (float) – Measurement noise standard deviation (squared internally).
camT (tm) – Pose of the camera in the world/global frame.
id (int, optional) – Identifier used to compare cameras. Defaults to 0.
- getLocalPos(pix)[source]
Convert a pixel coordinate into a local, focal-length-normalized offset.
- Parameters:
pix – Pixel coordinate [x, y] to convert.
- Returns:
list – [x, y] offset from the frame center in focal-length-normalized units.
- getFrameSize(sz=1)[source]
Compute the half-frame size of the sensor in normalized units.
- Parameters:
sz (float, optional) – Scale factor applied to the computed frame size. Defaults to 1.
- Returns:
list – [x, y] half-frame size at the given scale.
- getScene(listPoints)[source]
Project a list of 3D points into this camera and return those that land within the sensor.
- Parameters:
listPoints – List of 3D points (as tm-compatible arrays) to project.
- Returns:
list – Entries of [pixel_position, covariance, original_point] for each input point that falls within the camera’s field of view.
- getPhoto(mat)[source]
Project a single 3D point into this camera’s pixel space.
- Parameters:
mat – 3D point (or tm) to project, using the first three elements as x, y, z.
- Returns:
tuple – (img, Q, success) where img is the projected [x, y] pixel location, Q is the associated measurement covariance (inflated if the point falls outside the sensor bounds), and success (bool) indicates whether the point landed within the sensor’s visible bounds.
- dhdx(mat)[source]
Compute the numerical Jacobian of pixel projection with respect to a 3D point.
- Parameters:
mat – 3D point (or tm) to linearize the projection around.
- Returns:
ndarray – Jacobian of the projected pixel coordinates with respect to the point’s x, y, z position.
- NumJac(f, x0, h)[source]
Compute the numerical (central-difference) Jacobian of a function at a point.
- Parameters:
f – Function to differentiate.
x0 – Point at which to evaluate the Jacobian.
h – Step size used for the central difference.
- Returns:
ndarray – Numerical Jacobian of f evaluated at x0.
- getProbability(mat, mean)[source]
Compute the likelihood of an observed pixel given an expected mean position.
- Parameters:
mat – Observed pixel position.
mean – 3D point whose projection gives the expected pixel position.
- Returns:
float – Probability density of observing mat given the projection of mean, or 0 if the expected point’s measurement covariance exceeds the camera’s noise level (i.e. it falls outside the sensor bounds).
- attachment(target, mat)[source]
Reposition the camera relative to a pose and measure the pixel offset to a target.
- Parameters:
target – Target point to project after moving the camera.
mat (tm) – Pose the camera is moved to, offset by a fixed rotation.
- Returns:
ndarray – Pixel offset of the projected target from the image center (1024, 1024).
- updateFocal(x, y)[source]
Update the camera’s focal length parameters.
- Parameters:
x – New focal length along the x axis.
y – New focal length along the y axis.