Exemple #1
0
    def instance(self,
                 model_type='bfm',
                 alpha=None,
                 beta=None,
                 landmark_group='ibug68'):
        if alpha is None:
            alpha = np.zeros(len(self.shape_model.eigenvalues))
        if beta is None:
            beta = np.zeros(len(self.texture_model.eigenvalues))

        # Generate instance
        shape = self.shape_model.instance(alpha, normalized_weights=True)
        texture = self.texture_model.instance(beta, normalized_weights=True)

        if model_type == 'bfm':
            tex_scale = UniformScale(1. / 255, 3)
            lms_scale = UniformScale(1e-5, 3)
            texture = tex_scale.apply(texture.reshape([-1, 3]))
            landmarks = lms_scale.apply(self.landmarks)
        elif model_type == 'lsfm':
            pass

        trimesh = ColouredTriMesh(shape.points,
                                  trilist=shape.trilist,
                                  colours=texture)

        trimesh.landmarks[landmark_group] = landmarks

        return trimesh
Exemple #2
0
def detect(detector_callable, image, greyscale=True, image_diagonal=None, group_prefix="object", channels_at_back=True):
    r"""
    Apply the general detection framework.

    This involves converting the image to greyscale if necessary, rescaling
    the image to a given diagonal, performing the detection, and attaching
    the scaled landmarks back onto the original image.

    uint8 images cannot be converted to greyscale by this framework, so must
    already be greyscale or ``greyscale=False``.

    Parameters
    ----------
    detector_callable : `callable` or `function`
        A callable object that will perform detection given a single parameter,
        a `uint8` numpy array with either no channels, or channels as the
        *last* axis.
    image : `menpo.image.Image`
        A Menpo image to detect. The bounding boxes of the detected objects
        will be attached to this image.
    greyscale : `bool`, optional
        Convert the image to greyscale or not.
    image_diagonal : `int`, optional
        The total size of the diagonal of the image that should be used for
        detection. This is useful for scaling images up and down for detection.
    group_prefix : `str`, optional
        The prefix string to be appended to each each landmark group that is
        stored on the image. Each detection will be stored as group_prefix_#
        where # is a count starting from 0.
    channels_at_back : `bool`, optional
        If ``True``, the image channels are placed onto the last axis (the back)
        as is common in many imaging packages. This is contrary to the Menpo
        default where channels are the first axis (at the front).

    Returns
    -------
    bounding_boxes : `list` of `menpo.shape.PointDirectedGraph`
        A list of bounding boxes representing the detections found.
    """
    d_image = image

    if greyscale:
        d_image = _greyscale(d_image)

    if image_diagonal is not None:
        scale_factor = image_diagonal / image.diagonal()
        d_image = d_image.rescale(scale_factor)

    pcs = detector_callable(menpo_image_to_uint8(d_image, channels_at_back=channels_at_back))

    if image_diagonal is not None:
        s = UniformScale(1 / scale_factor, n_dims=2)
        pcs = [s.apply(pc) for pc in pcs]

    padding_magnitude = len(str(len(pcs)))
    for i, pc in enumerate(pcs):
        key = "{prefix}_{num:0{mag}d}".format(mag=padding_magnitude, prefix=group_prefix, num=i)
        image.landmarks[key] = pc
    return pcs
Exemple #3
0
def test_align_2d_uniform_scale():
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and target
    estimate = AlignmentUniformScale(source, target)
    # check the estimates is correct
    assert_allclose(scale.h_matrix, estimate.h_matrix)
Exemple #4
0
def test_align_2d_uniform_scale_set_h_matrix_raises_notimplemented_error():
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and source
    estimate = AlignmentUniformScale(source, source)
    # and set the target
    estimate.set_h_matrix(scale.h_matrix)
Exemple #5
0
def test_align_2d_uniform_scale_set_h_matrix_raises_notimplemented_error():
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and source
    estimate = AlignmentUniformScale(source, source)
    # and set the target
    estimate.set_h_matrix(scale.h_matrix)
Exemple #6
0
def test_align_2d_uniform_scale():
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and target
    estimate = AlignmentUniformScale(source, target)
    # check the estimates is correct
    assert_allclose(scale.h_matrix, estimate.h_matrix)
def homog_compose_before_alignment_nonuniformscale_test():
    homog = Homogeneous(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]))
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and target
    s = AlignmentUniformScale(source, target)
    res = homog.compose_before(s)
    assert (type(res) == Homogeneous)
def test_homog_compose_before_alignment_nonuniformscale():
    homog = Homogeneous(np.array([[0, 1, 0],
                                  [1, 0, 0],
                                  [0, 0, 1]]))
    scale = UniformScale(2.5, 2)
    source = PointCloud(np.array([[0, 1],
                                  [1, 1],
                                  [-1, -5],
                                  [3, -5]]))
    target = scale.apply(source)
    # estimate the transform from source and target
    s = AlignmentUniformScale(source, target)
    res = homog.compose_before(s)
    assert(type(res) == Homogeneous)
Exemple #9
0
def mesh_in_unit_sphere(mesh):
    scale = UniformScale(1 / mesh.norm(), mesh.n_dims)
    translation = Translation(-scale.apply(mesh).centre())
    return translation.compose_after(scale)
Exemple #10
0
def detect(detector_callable,
           image,
           greyscale=True,
           image_diagonal=None,
           group_prefix='object',
           channels_at_back=True):
    r"""
    Apply the general detection framework.

    This involves converting the image to greyscale if necessary, rescaling
    the image to a given diagonal, performing the detection, and attaching
    the scaled landmarks back onto the original image.

    uint8 images cannot be converted to greyscale by this framework, so must
    already be greyscale or ``greyscale=False``.

    Parameters
    ----------
    detector_callable : `callable` or `function`
        A callable object that will perform detection given a single parameter,
        a `uint8` numpy array with either no channels, or channels as the
        *last* axis.
    image : `menpo.image.Image`
        A Menpo image to detect. The bounding boxes of the detected objects
        will be attached to this image.
    greyscale : `bool`, optional
        Convert the image to greyscale or not.
    image_diagonal : `int`, optional
        The total size of the diagonal of the image that should be used for
        detection. This is useful for scaling images up and down for detection.
    group_prefix : `str`, optional
        The prefix string to be appended to each each landmark group that is
        stored on the image. Each detection will be stored as group_prefix_#
        where # is a count starting from 0.
    channels_at_back : `bool`, optional
        If ``True``, the image channels are placed onto the last axis (the back)
        as is common in many imaging packages. This is contrary to the Menpo
        default where channels are the first axis (at the front).

    Returns
    -------
    bounding_boxes : `list` of `menpo.shape.PointDirectedGraph`
        A list of bounding boxes representing the detections found.
    """
    d_image = image

    if greyscale:
        d_image = _greyscale(d_image)

    if image_diagonal is not None:
        scale_factor = image_diagonal / image.diagonal()
        d_image = d_image.rescale(scale_factor)

    pcs = detector_callable(
        menpo_image_to_uint8(d_image, channels_at_back=channels_at_back))

    if image_diagonal is not None:
        s = UniformScale(1 / scale_factor, n_dims=2)
        pcs = [s.apply(pc) for pc in pcs]

    padding_magnitude = len(str(len(pcs)))
    for i, pc in enumerate(pcs):
        key = '{prefix}_{num:0{mag}d}'.format(mag=padding_magnitude,
                                              prefix=group_prefix,
                                              num=i)
        image.landmarks[key] = pc
    return pcs