Camera models with large non-linear distortion can sometimes project points outside their field of view onto their image frame. This can lead to artifacts when for example using Camera.project_dem. The workaround I have found is to check whether a point is in the camera frame in normalized camera coordinates, before distortion is applied to the incoming points. The frame (box_xy below) could be pre-computed and cached for speed.
import glimpse
import numpy as np
import shapely
import shapely.geometry
def inframe_xyz(cam: glimpse.Camera, xyz: np.ndarray):
xy = cam._xyz_to_xy(xyz)
# Project camera box to camera coordinates and use that to filter points within field of view
box_uv = cam.edges()
box_xy = cam._uv_to_xy(box_uv)
# Test point in polygon
polygon = shapely.geometry.Polygon(box_xy)
shapely.prepare(polygon)
points = shapely.points(xy)
return shapely.contains(polygon, points)
Camera models with large non-linear distortion can sometimes project points outside their field of view onto their image frame. This can lead to artifacts when for example using
Camera.project_dem. The workaround I have found is to check whether a point is in the camera frame in normalized camera coordinates, before distortion is applied to the incoming points. The frame (box_xybelow) could be pre-computed and cached for speed.