← Back to the animated process

nocs_dataset.py

Implementation snapshot supporting the published lesson. Only cited line ranges are included; this is not a runnable source file. Configuration and recorded examples describe this project snapshot.

… uncited lines omitted …
52def extract_masked_object_points(depth_mm, mask, label, box_tolerance_ratio=0.08):53    """Deproject masked depth and return matching camera/object-frame points.5455    ``T_camera_object`` follows ``P_camera = T_camera_object @ P_object``.56    Returned object points therefore use the calibrated box-center origin and57    axes directly; no later reference-axis assignment is required.58    """59    if not 0 <= box_tolerance_ratio <= 0.5:60        raise ValueError("box_tolerance_ratio must be between 0 and 0.5")61    (depth_mm, foreground, intrinsic, camera_object, dimensions,62     depth_unit_m) = _validated_inputs(depth_mm, mask, label)6364    depth_valid = foreground & (depth_mm > 0)65    ys, xs = np.nonzero(depth_valid)66    if len(xs):67        z = depth_mm[ys, xs].astype(np.float64) * depth_unit_m68        fx, fy = intrinsic[0, 0], intrinsic[1, 1]69        cx, cy = intrinsic[0, 2], intrinsic[1, 2]70        camera_points = np.column_stack(((xs - cx) * z / fx,71                                         (ys - cy) * z / fy,72                                         z))73        rotation = camera_object[:3, :3]74        translation = camera_object[:3, 3]75        object_points = (camera_points - translation) @ rotation76        tolerance = np.maximum(0.003, dimensions * box_tolerance_ratio)77        inside = np.all(np.abs(object_points) <= dimensions / 2 + tolerance, axis=1)78        xs_valid, ys_valid = xs[inside], ys[inside]79        camera_valid = camera_points[inside]80        object_valid = object_points[inside]