coastalimagelib/corefunctions.py
def xyz2DistUV(grid, cal):

    """
    This function computes the distorted UV coordinates (UVd)  that
    correspond to a set of world xyz points for for given camera
    extrinsics and intrinsics. Function also
    produces a flag variable to indicate if the UVd point is valid.

    Arguments:
        cal: CameraData object containing the DLT coefficient vector A->L
        grid: XYZGrid object containing real world coords

    Returns:
        DU: Nx1 vector of distorted U coordinates for N points
        DV: Nx1 vector of distorted V coordinates for N points
    """

    # Take Calibration Information, combine it into a sigular P matrix
    # containing both intrinsics and extrinsic information, make
    # homogenous
    xyz = np.vstack((grid.xyz.T, np.ones((len(grid.xyz),))))
    UV = np.matmul(cal.P, xyz)
    UV = UV / np.tile(UV[2, :], (3, 1))

    # Normalize distances
    u = UV[0, :]
    v = UV[1, :]
    x = (u - cal.c0U) / cal.fx
    y = (v - cal.c0V) / cal.fy

    # Radial distortion
    r2 = x * x + y * y
    fr = 1.0 + cal.d1 * r2 + cal.d2 * (r2 * r2) + cal.d3 * (r2 * (r2 * r2))

    # Tangential Distortion
    dx = 2.0 * cal.t1 * x * y + cal.t2 * (r2 + 2.0 * x * x)
    dy = cal.t1 * (r2 + 2.0 * y * y) + 2.0 * cal.t2 * x * y

    #  Apply Correction
    xd = x * fr + dx
    yd = y * fr + dy
    Ud = xd * cal.fx + cal.c0U
    Vd = yd * cal.fy + cal.c0V
    mask = (Ud < 0) | (Ud > cal.NU) | (Vd < 0) | (Vd > cal.NV)
    Ud[mask] = 0
    Vd[mask] = 0

    # Calc maximum tangential distortion
    Um = np.array((0, 0, cal.NU, cal.NU))
    Vm = np.array((0, cal.NV, cal.NV, 0))

    # Normalize
    xm = (Um - cal.c0U) / cal.fx
    ym = (Vm - cal.c0V) / cal.fy
    r2m = xm * xm + ym * ym

    # Tangential Distortion
    dxm = 2.0 * cal.t1 * xm * ym + cal.t2 * (r2m + 2.0 * xm * xm)
    dym = cal.t1 * (r2m + 2.0 * ym * ym) + 2.0 * cal.t2 * xm * ym

    # Flag values outside xy limits
    flag = np.ones_like(Ud)
    flag[np.where(np.abs(dy) > np.max(np.abs(dym)))] = 0.0
    flag[np.where(np.abs(dx) > np.max(np.abs(dxm)))] = 0.0

    DU = Ud.reshape(grid.X.shape, order="F")
    DV = Vd.reshape(grid.Y.shape, order="F")

    # Flag negative Z values
    UV = np.matmul(cal.P, xyz)
    xyzC = np.matmul(cal.R, np.matmul(cal.IC, xyz))
    flag[np.where(xyzC[2, :] <= 0.0)] = 0.0
    flag = flag.reshape(grid.X.shape, order="F")

    # Apply flag to remove invalid points (set points = 0)
    return DU * flag, DV * flag

    This function computes the distorted UV coordinates (UVd)  that
    correspond to a set of world xyz points for for given camera
    extrinsics and intrinsics. Function also
    produces a flag variable to indicate if the UVd point is valid.

    Arguments:
        cal: CameraData object containing the DLT coefficient vector A->L
        grid: XYZGrid object containing real world coords

    Returns:
        DU: Nx1 vector of distorted U coordinates for N points
        DV: Nx1 vector of distorted V coordinates for N points
    
This function computes the distorted UV coordinates (UVd)  that
correspond to a set of world xyz points for for given camera
extrinsics and intrinsics. Function also
produces a flag variable to indicate if the UVd point is valid.
This function computes the distorted UV coordinates ( UVd ) that correspond to a set of world xyz points for for given camera extrinsics and intrinsics . Function also produces a flag variable to indicate if the UVd point is valid .
def xyz2DistUV(grid, cal):
    xyz = np.vstack((grid.xyz.T, np.ones((len(grid.xyz),))))
    UV = np.matmul(cal.P, xyz)
    UV = UV / np.tile(UV[2, :], (3, 1))
    u = UV[0, :]
    v = UV[1, :]
    x = (u - cal.c0U) / cal.fx
    y = (v - cal.c0V) / cal.fy
    r2 = x * x + y * y
    fr = 1.0 + cal.d1 * r2 + cal.d2 * (r2 * r2) + cal.d3 * (r2 * (r2 * r2))
    dx = 2.0 * cal.t1 * x * y + cal.t2 * (r2 + 2.0 * x * x)
    dy = cal.t1 * (r2 + 2.0 * y * y) + 2.0 * cal.t2 * x * y
    xd = x * fr + dx
    yd = y * fr + dy
    Ud = xd * cal.fx + cal.c0U
    Vd = yd * cal.fy + cal.c0V
    mask = (Ud < 0) | (Ud > cal.NU) | (Vd < 0) | (Vd > cal.NV)
    Ud[mask] = 0
    Vd[mask] = 0
    Um = np.array((0, 0, cal.NU, cal.NU))
    Vm = np.array((0, cal.NV, cal.NV, 0))
    xm = (Um - cal.c0U) / cal.fx
    ym = (Vm - cal.c0V) / cal.fy
    r2m = xm * xm + ym * ym
    dxm = 2.0 * cal.t1 * xm * ym + cal.t2 * (r2m + 2.0 * xm * xm)
    dym = cal.t1 * (r2m + 2.0 * ym * ym) + 2.0 * cal.t2 * xm * ym
    flag = np.ones_like(Ud)
    flag[np.where(np.abs(dy) > np.max(np.abs(dym)))] = 0.0
    flag[np.where(np.abs(dx) > np.max(np.abs(dxm)))] = 0.0
    DU = Ud.reshape(grid.X.shape, order="F")
    DV = Vd.reshape(grid.Y.shape, order="F")
    UV = np.matmul(cal.P, xyz)
    xyzC = np.matmul(cal.R, np.matmul(cal.IC, xyz))
    flag[np.where(xyzC[2, :] <= 0.0)] = 0.0
    flag = flag.reshape(grid.X.shape, order="F")
    return DU * flag, DV * flag
def xyz2DistUV ( grid , cal ) : xyz = np . vstack ( ( grid . xyz . T , np . ones ( ( len ( grid . xyz ) , ) ) ) ) UV = np . matmul ( cal . P , xyz ) UV = UV / np . tile ( UV [ 2 , : ] , ( 3 , 1 ) ) u = UV [ 0 , : ] v = UV [ 1 , : ] x = ( u - cal . c0U ) / cal . fx y = ( v - cal . c0V ) / cal . fy r2 = x * x + y * y fr = 1.0 + cal . d1 * r2 + cal . d2 * ( r2 * r2 ) + cal . d3 * ( r2 * ( r2 * r2 ) ) dx = 2.0 * cal . t1 * x * y + cal . t2 * ( r2 + 2.0 * x * x ) dy = cal . t1 * ( r2 + 2.0 * y * y ) + 2.0 * cal . t2 * x * y xd = x * fr + dx yd = y * fr + dy Ud = xd * cal . fx + cal . c0U Vd = yd * cal . fy + cal . c0V mask = ( Ud < 0 ) | ( Ud > cal . NU ) | ( Vd < 0 ) | ( Vd > cal . NV ) Ud [ mask ] = 0 Vd [ mask ] = 0 Um = np . array ( ( 0 , 0 , cal . NU , cal . NU ) ) Vm = np . array ( ( 0 , cal . NV , cal . NV , 0 ) ) xm = ( Um - cal . c0U ) / cal . fx ym = ( Vm - cal . c0V ) / cal . fy r2m = xm * xm + ym * ym dxm = 2.0 * cal . t1 * xm * ym + cal . t2 * ( r2m + 2.0 * xm * xm ) dym = cal . t1 * ( r2m + 2.0 * ym * ym ) + 2.0 * cal . t2 * xm * ym flag = np . ones_like ( Ud ) flag [ np . where ( np . abs ( dy ) > np . max ( np . abs ( dym ) ) ) ] = 0.0 flag [ np . where ( np . abs ( dx ) > np . max ( np . abs ( dxm ) ) ) ] = 0.0 DU = Ud . reshape ( grid . X . shape , order = "F" ) DV = Vd . reshape ( grid . Y . shape , order = "F" ) UV = np . matmul ( cal . P , xyz ) xyzC = np . matmul ( cal . R , np . matmul ( cal . IC , xyz ) ) flag [ np . where ( xyzC [ 2 , : ] <= 0.0 ) ] = 0.0 flag = flag . reshape ( grid . X . shape , order = "F" ) return DU * flag , DV * flag
This function computes the distorted UV coordinates (UVd)  that
correspond to a set of world xyz points for for given camera
extrinsics and intrinsics.
This function computes the distorted UV coordinates ( UVd ) that correspond to a set of world xyz points for for given camera extrinsics and intrinsics .
"""
    This function computes the distorted UV coordinates (UVd)  that
    correspond to a set of world xyz points for for given camera
    extrinsics and intrinsics. Function also
    produces a flag variable to indicate if the UVd point is valid.

    Arguments:
        cal: CameraData object containing the DLT coefficient vector A->L
        grid: XYZGrid object containing real world coords

    Returns:
        DU: Nx1 vector of distorted U coordinates for N points
        DV: Nx1 vector of distorted V coordinates for N points
    """ # Take Calibration Information, combine it into a sigular P matrix # containing both intrinsics and extrinsic information, make # homogenous # Normalize distances # Radial distortion # Tangential Distortion #  Apply Correction # Calc maximum tangential distortion # Normalize # Tangential Distortion # Flag values outside xy limits # Flag negative Z values # Apply flag to remove invalid points (set points = 0)
Nx1 vector of distorted U coordinates for N points
DV: Nx1 vector of distorted V coordinates for N points
Nx1 vector of distorted U coordinates for N points DV : Nx1 vector of distorted V coordinates for N points
DU
grid
XYZGrid object containing real world coords
XYZGrid object containing real world coords
cal
CameraData object containing the DLT coefficient vector A->L
CameraData object containing the DLT coefficient vector A - > L