def _convert_2d_to_3d_point_undistort(point_group, camera, fbw, fbh, lcox,
                                      lcoy, camera_fov, frame, pos, depth):
    """
    Convert a 2D point (undistorted) into a 3D point, in world space.

    :param point_group: Camera Point Group for camera.
    :param camera: The camera to use for rolling shutter calculations.
    :param fbw: Camera's film back width value.
    :param fbh: Camera's film back height value.
    :param lcox: Camera lens lens center offset X value.
    :param lcoy: Camera lens lens center offset Y value.
    :param camera_fov: Camera Field of View as list of left, right,
                       bottom and top.
    :param frame: The 2D point's frame number (in internal 3DE frame numbers).
    :param pos: Input 2D data.
    :param depth: The content distance to calculate rolling shutter at.

    :return: Corrected 2D point.
    :rtype: vec2d
    """
    focal = tde4.getCameraFocalLength(camera, frame)
    r3d = vl_sdv.mat3d(tde4.getPGroupRotation3D(point_group, camera, frame))
    p3d = vl_sdv.vec3d(tde4.getPGroupPosition3D(point_group, camera, frame))
    left, right, bottom, top = camera_fov

    p2d = [0, 0]
    p2d[0] = (pos[0] - left) / (right - left)
    p2d[1] = (pos[1] - bottom) / (top - bottom)
    p2d = tde4.removeDistortion2D(camera, frame, p2d)

    p2d_cm = vl_sdv.vec2d((p2d[0] - 0.5) * fbw - lcox,
                          (p2d[1] - 0.5) * fbh - lcoy)
    homogeneous_point = r3d * vl_sdv.vec3d(p2d_cm[0], p2d_cm[1], -focal).unit()
    out_point = homogeneous_point * depth + p3d
    return out_point
Exemple #2
0
def _remove_rs_from_2d_point(point_group, camera, frame, input_2d, depth):
    """
    Correct Rolling Shutter for the given input_2d point data, on frame.

    :param point_group: Camera Point Group for camera.
    :param camera: The camera to use for rolling shutter calculations.
    :param frame: The 2D point's frame number (in internal 3DE frame numbers).
    :param input_2d: Input 2D data.
    :param depth: The content distance to calculate rolling shutter at.

    :return: 2D point with corrected position.
    :rtype: [float, float]
    """
    assert isinstance(input_2d, vl_sdv.vec2d)
    num_frames = tde4.getCameraNoFrames(camera)
    if num_frames == 1:
        return input_2d

    # Static camera and lens values.
    camera_fps = tde4.getCameraFPS(camera)
    camera_fov = tde4.getCameraFOV(camera)
    lens = tde4.getCameraLens(camera)
    fbw = tde4.getLensFBackWidth(lens)
    fbh = tde4.getLensFBackHeight(lens)
    lcox = tde4.getLensLensCenterX(lens)
    lcoy = tde4.getLensLensCenterY(lens)
    rs_time_shift = tde4.getCameraRollingShutterTimeShift(camera)
    rs_value = rs_time_shift * camera_fps

    # Sample at previous frame
    prev_pos = vl_sdv.vec3d(0, 0, 0)
    prev_frame = frame - 1
    if frame > 1:
        prev_pos = _convert_2d_to_3d_point_undistort(
            point_group, camera,
            fbw, fbh, lcox, lcoy,
            camera_fov,
            prev_frame, input_2d, depth)

    # Sample at next frame
    next_pos = vl_sdv.vec3d(0, 0, 0)
    next_frame = frame + 1
    if frame < num_frames:
        next_pos = _convert_2d_to_3d_point_undistort(
            point_group, camera,
            fbw, fbh, lcox, lcoy,
            camera_fov,
            next_frame, input_2d, depth)

    # Sample at current frame
    curr_pos = _convert_2d_to_3d_point_undistort(
        point_group, camera,
        fbw, fbh, lcox, lcoy,
        camera_fov,
        frame, input_2d, depth)

    # Blend previous, next and current frame values based on the
    # position of the 2D point vertical position and the rolling
    # shutter value.
    if frame == 1:
        prev_pos = curr_pos + (curr_pos - next_pos)
    if frame == num_frames:
        next_pos = curr_pos + (curr_pos - prev_pos)
    t = rs_value * (1.0 - input_2d[1])
    curr_pos = _apply_rs_correction(-t, prev_pos, curr_pos, next_pos)

    # Back-projection
    focal = tde4.getCameraFocalLength(camera, frame)
    r3d = vl_sdv.mat3d(tde4.getPGroupRotation3D(point_group, camera, frame))
    p3d = vl_sdv.vec3d(tde4.getPGroupPosition3D(point_group, camera, frame))
    d = r3d.trans() * (curr_pos - p3d)
    p2d = [0, 0]
    p2d[0] = (d[0] * focal / (-d[2] * fbw)) + (lcox / fbw) + 0.5
    p2d[1] = (d[1] * focal / (-d[2] * fbh)) + (lcoy / fbh) + 0.5
    p = tde4.applyDistortion2D(camera, frame, p2d)
    left, right, bottom, top = camera_fov
    p = vl_sdv.vec2d((p[0] * (right - left)) + left,
                     (p[1] * (top - bottom)) + bottom)
    v = (input_2d + (input_2d - p)).list()
    return v