def skew_image(image, theta, phi):
    r"""
    Method that skews the provided image. Note that the output image has the
    same size (shape) as the input.

    Parameters
    ----------
    image : `menpo.image.Image`
        The image to distort.
    theta : `float`
        The skew angle over x axis (tan(theta)).
    phi : `float`
        The skew angle over y axis (tan(phi)).

    Returns
    -------
    skewed_image : `menpo.image.Image`
        The skewed (distorted) image.
    """
    # Get mask of pixels
    mask = BooleanImage(image.pixels[0])
    # Create the bounding box (pointcloud) of the mask
    bbox = bounding_box(*mask.bounds_true())
    # Skew the bounding box
    new_bbox = skew_shape(bbox, theta, phi)
    # Warp the image using TPS
    pwa = ThinPlateSplines(new_bbox, bbox)

    return image.warp_to_shape(image.shape, pwa)
Beispiel #2
0
def warp_face_onto_background(face_to_warp_filepath: str,
                              background_face_filepath: str,
                              do_plot: bool = True,
                              do_all_plots: bool = False) -> Image:
    img_warp_face = get_landmarks(face_to_warp_filepath, do_plot=do_all_plots)
    img_bgd_face = get_landmarks(background_face_filepath,
                                 do_plot=do_all_plots)

    # prep warp_face

    img_warp_face_masked = prep_source_img(img_warp_face, do_plot=do_all_plots)
    img_bgd_face_masked = prep_source_img(img_bgd_face,
                                          crop=False,
                                          do_plot=do_all_plots)

    #

    tps_bgd_face_to_warp_face = ThinPlateSplines(
        img_bgd_face_masked.landmarks['ibug_0'],
        img_warp_face_masked.landmarks['ibug_0'])
    warped_warp_face_to_bgd_face_tps = img_warp_face_masked.as_unmasked(
        copy=False).warp_to_mask(img_bgd_face_masked.mask,
                                 tps_bgd_face_to_warp_face)

    if do_plot:
        warped_warp_face_to_bgd_face_tps.view(new_figure=True)

    return warped_warp_face_to_bgd_face_tps
Beispiel #3
0
def warp_face_image_tps(img, new_shape, lms_grp_name='PTS', warp_mode='constant'):
    """warp image to new landmarks using TPS interpolation"""

    tps = ThinPlateSplines(new_shape, img.landmarks[lms_grp_name])
    try:
        img_warp = img.warp_to_shape(img.shape, tps, mode=warp_mode)
        img_warp.landmarks[lms_grp_name] = new_shape
        return img_warp
    except np.linalg.linalg.LinAlgError as err:
        print ('Error:'+str(err)+'\nUsing original landmarks for:\n'+str(img.path))
        return img
 def __init__(self, source, target, kernel=None):
     if kernel is None:
         kernel = DifferentiableR2LogR2RBF(source.points)
     ThinPlateSplines.__init__(self, source, target, kernel=kernel)
 def __init__(self, source, target, kernel=None):
     if kernel is None:
         kernel = DifferentiableR2LogR2RBF(source.points)
     ThinPlateSplines.__init__(self, source, target, kernel=kernel)
Beispiel #6
0
def warp_face_image_tps(img, new_shape):
    tps = ThinPlateSplines(new_shape, img.landmarks['PTS'])
    img_warp = img.warp_to_shape(img.shape, tps)
    img_warp.landmarks['PTS'] = new_shape
    return img_warp