Пример #1
0
    def constrain_to_pointcloud(self, pointcloud, trilist=None):
        r"""
        Restricts this mask to be equal to the convex hull around a point cloud

        Parameters
        ----------
        pointcloud : :map:`PointCloud`
            The pointcloud of points that should be constrained to

        trilist: (t, 3) ndarray, Optional
            Triangle list to be used on the points in selecting
            the mask region. If None defaults to performing Delaunay
            triangulation on the points.

            Default: None
        """
        from menpo.transform.piecewiseaffine import PiecewiseAffine
        from menpo.transform.piecewiseaffine import TriangleContainmentError

        if self.n_dims != 2:
            raise ValueError("can only constrain mask on 2D images.")

        if trilist is not None:
            from menpo.shape import TriMesh
            pointcloud = TriMesh(pointcloud.points, trilist)

        pwa = PiecewiseAffine(pointcloud, pointcloud)
        try:
            pwa.apply(self.indices)
        except TriangleContainmentError as e:
            self.from_vector_inplace(~e.points_outside_source_domain)
Пример #2
0
def pwa_point_in_pointcloud(pcloud, indices, batch_size=None):
    """
    Make sure that the decision of whether a point is inside or outside
    the PointCloud is exactly the same as how PWA calculates triangle
    containment. Then, we use the trick of setting the mask to all the
    points that were NOT outside the triangulation. Otherwise, all points
    were inside and we just return those as ``True``. In general, points
    on the boundary are counted as inside the polygon.

    Parameters
    ----------
    pcloud : :map:`PointCloud`
        The pointcloud to use for the containment test.
    indices : (d, n_dims) `ndarray`
        The list of pixel indices to test.
    batch_size : `int` or ``None``, optional
        See constrain_to_pointcloud for more information about the batch_size
        parameter.

    Returns
    -------
    mask : (d,) `bool ndarray`
        Whether each pixel index was in inside the convex hull of the
        pointcloud or not.
    """
    from menpo.transform.piecewiseaffine import PiecewiseAffine
    from menpo.transform.piecewiseaffine import TriangleContainmentError

    try:
        pwa = PiecewiseAffine(pcloud, pcloud)
        pwa.apply(indices, batch_size=batch_size)
        return torch.ones(indices.shape[0], dtype=torch.long)
    except TriangleContainmentError as e:
        return ~e.points_outside_source_domain
Пример #3
0
    def constrain_to_pointcloud(self, pointcloud, trilist=None):
        r"""
        Restricts this mask to be equal to the convex hull around a point cloud

        Parameters
        ----------
        pointcloud : :map:`PointCloud`
            The pointcloud of points that should be constrained to

        trilist: (t, 3) ndarray, Optional
            Triangle list to be used on the points in selecting
            the mask region. If None defaults to performing Delaunay
            triangulation on the points.

            Default: None
        """
        from menpo.transform.piecewiseaffine import PiecewiseAffine
        from menpo.transform.piecewiseaffine import TriangleContainmentError

        if self.n_dims != 2:
            raise ValueError("can only constrain mask on 2D images.")

        if trilist is not None:
            from menpo.shape import TriMesh
            pointcloud = TriMesh(pointcloud.points, trilist)

        pwa = PiecewiseAffine(pointcloud, pointcloud)
        try:
            pwa.apply(self.indices)
        except TriangleContainmentError as e:
            self.from_vector_inplace(~e.points_outside_source_domain)
Пример #4
0
def chain_pwa_after_tps_test():
    a_tm = TriMesh(np.random.random([10, 2]))
    b = PointCloud(np.random.random([10, 2]))
    pwa = PiecewiseAffine(a_tm, b)
    tps = ThinPlateSplines(b, a_tm)
    chain = pwa.compose_after(tps)
    assert(isinstance(chain, TransformChain))
Пример #5
0
def pwa_point_in_pointcloud(pcloud, indices, batch_size=None):
    """
    Make sure that the decision of whether a point is inside or outside
    the PointCloud is exactly the same as how PWA calculates triangle
    containment. Then, we use the trick of setting the mask to all the
    points that were NOT outside the triangulation. Otherwise, all points
    were inside and we just return those as ``True``. In general, points
    on the boundary are counted as inside the polygon.

    Parameters
    ----------
    pcloud : :map:`PointCloud`
        The pointcloud to use for the containment test.
    indices : (d, n_dims) `ndarray`
        The list of pixel indices to test.
    batch_size : `int` or ``None``, optional
        See constrain_to_pointcloud for more information about the batch_size
        parameter.

    Returns
    -------
    mask : (d,) `bool ndarray`
        Whether each pixel index was in inside the convex hull of the
        pointcloud or not.
    """
    from menpo.transform.piecewiseaffine import PiecewiseAffine
    from menpo.transform.piecewiseaffine import TriangleContainmentError

    try:
        pwa = PiecewiseAffine(pcloud, pcloud)
        pwa.apply(indices, batch_size=batch_size)
        return np.ones(indices.shape[0], dtype=np.bool)
    except TriangleContainmentError as e:
        return ~e.points_outside_source_domain
Пример #6
0
def test_chain_tps_before_pwa():
    a_tm = TriMesh(np.random.random([10, 2]))
    b = PointCloud(np.random.random([10, 2]))
    pwa = PiecewiseAffine(a_tm, b)
    tps = ThinPlateSplines(b, a_tm)
    chain = tps.compose_before(pwa)
    assert (isinstance(chain, TransformChain))
Пример #7
0
    def constrain_mask_to_landmarks(self,
                                    group=None,
                                    label='all',
                                    trilist=None):
        r"""
        Restricts this image's mask to be equal to the convex hull
        around the landmarks chosen.

        Parameters
        ----------
        group : string, Optional
            The key of the landmark set that should be used. If None,
            and if there is only one set of landmarks, this set will be used.

            Default: None

        label: string, Optional
            The label of of the landmark manager that you wish to use. If no
            label is passed, the convex hull of all landmarks is used.

            Default: None

        trilist: (t, 3) ndarray, Optional
            Triangle list to be used on the landmarked points in selecting
            the mask region. If None defaults to performing Delaunay
            triangulation on the points.

            Default: None
        """
        from menpo.transform.piecewiseaffine import PiecewiseAffine
        from menpo.transform.piecewiseaffine import TriangleContainmentError

        if self.n_dims != 2:
            raise ValueError("can only constrain mask on 2D images.")

        pc = self.landmarks[group][label]
        if trilist is not None:
            from menpo.shape import TriMesh

            pc = TriMesh(pc.points, trilist)

        pwa = PiecewiseAffine(pc, pc)
        try:
            pwa.apply(self.indices)
        except TriangleContainmentError as e:
            self.mask.from_vector_inplace(~e.points_outside_source_domain)
Пример #8
0
    def constrain_mask_to_landmarks(self, group=None, label='all',
                                    trilist=None):
        r"""
        Restricts this image's mask to be equal to the convex hull
        around the landmarks chosen.

        Parameters
        ----------
        group : string, Optional
            The key of the landmark set that should be used. If None,
            and if there is only one set of landmarks, this set will be used.

            Default: None

        label: string, Optional
            The label of of the landmark manager that you wish to use. If no
            label is passed, the convex hull of all landmarks is used.

            Default: None

        trilist: (t, 3) ndarray, Optional
            Triangle list to be used on the landmarked points in selecting
            the mask region. If None defaults to performing Delaunay
            triangulation on the points.

            Default: None
        """
        from menpo.transform.piecewiseaffine import PiecewiseAffine
        from menpo.transform.piecewiseaffine import TriangleContainmentError

        if self.n_dims != 2:
            raise ValueError("can only constrain mask on 2D images.")

        pc = self.landmarks[group][label]
        if trilist is not None:
            from menpo.shape import TriMesh

            pc = TriMesh(pc.points, trilist)

        pwa = PiecewiseAffine(pc, pc)
        try:
            pwa.apply(self.indices)
        except TriangleContainmentError as e:
            self.mask.from_vector_inplace(~e.points_outside_source_domain)