Esempio n. 1
0
def isbp_walk_for_bps(I, bpi):

    bpi = set(bpi)

    # Use raveled image
    Ir = np.ravel(I)

    # Get edge pixels
    edgeidcs = iu.edge_coords(I.shape, dtype='flat')

    # Get emanators from first bp
    do_first = set()  # These are the 4-connected emanators
    emanators = set()
    for bp in bpi:
        emanators = emanators | set(iu.neighbors_flat(bp, Ir, I.shape[1])[0])
        do_first.update(iu.four_conn([bp], I)[0])

    # Create set containing pixels that have already been visited
    walked = bpi | emanators

    while emanators:

        if do_first:
            idx = do_first.pop()
            emanators.remove(idx)
        else:
            idx = emanators.pop()

        walking = 1
        while walking:

            walked.add(idx)
            neighs = set(iu.neighbors_flat(idx, Ir, I.shape[1])[0])
            neighs = neighs - walked

            if len(neighs) == 0:
                walking = 0

            elif len(neighs) == 1:
                idx = neighs.pop()
                if idx in edgeidcs:
                    walking = 0
            else:
                bpi.add(idx)
                fourconn = iu.four_conn([idx], I)[0]
                fourconn = [f for f in fourconn if f in neighs]
                do_first.update(fourconn)
                emanators = emanators | neighs
                walked.add(idx)
                walked.update(neighs)

    return bpi
Esempio n. 2
0
def get_neighbors(idx, Iskel):
    """
    Get a flattened array of neighboring pixel indices.

    Returns a flattened array of the neighboring pixel indices within Iskel
    that are True. Only looks at 8-connected neighbors (i.e. a 3x3 kernel with
    centered on idx).

    Parameters
    ----------
    idx : np.int
        Index within Iskel to get neighbors.
    Iskel : np.ndarray
        Image of the skeletonized mask, but can be any image array.

    Returns
    -------
    neighbor_idcs_gloal : list
        Indices within Iskel of True pixels bordering idx.

    """
    size = (3, 3)
    cent_idx = 4  # OR int((size[0]*size[1] - 1) / 2)

    # Pull square with idx at center
    I, row_offset, col_offset = iu.get_array(idx, Iskel, size)
    I_flat = np.ravel(I)

    # Find its neighbors (next possible steps)
    neighbor_idcs, _ = iu.neighbors_flat(cent_idx, I_flat, size[1])
    neighbor_idcs_gloal = iu.reglobalize_flat_idx(neighbor_idcs, size,
                                                  row_offset, col_offset,
                                                  Iskel.shape)

    return neighbor_idcs_gloal
Esempio n. 3
0
 def test_neighbors_flatmid(self):
     """Test 3."""
     I = np.zeros((5, 5))
     I[1, 1] = 1.0
     Iflat = np.ravel(I)
     idx = 10
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [5, 6, 11, 15, 16])
     assert np.all(vals == [0., 1., 0., 0., 0.])
Esempio n. 4
0
 def test_neighbors_flatsmall(self):
     """Test 2."""
     I = np.zeros((5, 5))
     I[1, 2] = 1.0
     Iflat = np.ravel(I)
     idx = 3
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [2, 4, 7, 8, 9])
     assert np.all(vals == [0., 0., 1., 0., 0.])
Esempio n. 5
0
 def test_neighbors_bottomwrow(self):
     """Test 9."""
     I = np.zeros((20, 10))
     I[-1, :] = 2.0
     Iflat = np.ravel(I)
     ncols = 10
     idx = 190
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [180, 181, 191])
     assert np.all(vals == [0., 0., 2.])
Esempio n. 6
0
 def test_neighbors_flatbottom(self):
     """Test 8."""
     I = np.zeros((5, 5))
     I[3, 4] = 1.0
     Iflat = np.ravel(I)
     idx = 24
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [18, 19, 23])
     assert np.all(vals == [0., 1., 0.])
Esempio n. 7
0
 def test_neighbors_flatlast(self):
     """Test 6."""
     I = np.zeros((5, 5))
     I[4, 3] = 3.0
     Iflat = np.ravel(I)
     idx = 22
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [16, 17, 18, 21, 23])
     assert np.all(vals == [0., 0., 0., 0., 3.])
Esempio n. 8
0
 def test_neighbors_midcol(self):
     """Test 4."""
     I = np.zeros((5, 5))
     I[4, 3] = 1.0
     Iflat = np.ravel(I)
     idx = 19
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [13, 14, 18, 23, 24])
     assert np.all(vals == [0., 0., 0., 1., 0.])
Esempio n. 9
0
 def test_neighbors_flat(self):
     """Test 1."""
     I = np.zeros((5, 5))
     I[1, 1] = 1.0
     I[2, 2] = 3.0
     Iflat = np.ravel(I)
     idx = 11
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [5, 6, 7, 10, 12, 15, 16, 17])
     assert np.all(vals == [0., 1., 0., 0., 3., 0., 0., 0.])
Esempio n. 10
0
 def test_neighbors_flattop(self):
     """Test 7."""
     I = np.zeros((5, 5))
     I[0, 3] = 1.0
     I[1, 4] = 3.0
     Iflat = np.ravel(I)
     idx = 4
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [3, 8, 9])
     assert np.all(vals == [1., 0., 3.])
Esempio n. 11
0
 def test_neighbors_flatfirst(self):
     """Test 5."""
     I = np.zeros((5, 5))
     I[1, 1] = 1.0
     I[2, 2] = 3.0
     Iflat = np.ravel(I)
     idx = 0
     ncols = 5
     filt = 'none'
     idcs, vals = im_utils.neighbors_flat(idx, Iflat, ncols, filt=filt)
     # make assertion
     assert np.all(idcs == [1, 5, 6])
     assert np.all(vals == [0., 0., 1.])
Esempio n. 12
0
def get_neighbors(idx, Iskel):

    size = (3, 3)
    cent_idx = 4  # OR int((size[0]*size[1] - 1) / 2)

    # Pull square with idx at center
    I, row_offset, col_offset = iu.get_array(idx, Iskel, size)
    I_flat = np.ravel(I)

    # Find its neighbors (next possible steps)
    neighbor_idcs, _ = iu.neighbors_flat(cent_idx, I_flat, size[1])
    neighbor_idcs_gloal = iu.reglobalize_flat_idx(neighbor_idcs, size,
                                                  row_offset, col_offset,
                                                  Iskel.shape)

    return neighbor_idcs_gloal
Esempio n. 13
0
def isbp_walk_for_bps(I, bpi):
    """
    Find branchpoints by walking from a pixel.

    Finds branchpoints by ensuring that all pixels in the sub-skeleton can be
    walked to from the set of already-found branchpoints, without visiting
    the same pixel more than once.

    Parameters
    ----------
    I : np.ndarray
        Binary image of a skeleton. In RivGraph, the skeleton is a reduced and
        padded version of Iskel.
    bpi : list
        Indices within I of the branchpoint to begin walk.

    Returns
    -------
    bpi : list
        Branchpoint indices in I.

    """
    bpi = set(bpi)

    # Use raveled image
    Ir = np.ravel(I)

    # Get edge pixels
    edgeidcs = iu.edge_coords(I.shape, dtype='flat')

    # Get emanators from first bp
    do_first = set()  # These are the 4-connected emanators
    emanators = set()
    for bp in bpi:
        emanators = emanators | set(iu.neighbors_flat(bp, Ir, I.shape[1])[0])
        do_first.update(iu.four_conn([bp], I)[0])

    # Create set containing pixels that have already been visited
    walked = bpi | emanators

    while emanators:

        if do_first:
            idx = do_first.pop()
            emanators.remove(idx)
        else:
            idx = emanators.pop()

        walking = 1
        while walking:

            walked.add(idx)
            neighs = set(iu.neighbors_flat(idx, Ir, I.shape[1])[0])
            neighs = neighs - walked

            if len(neighs) == 0:
                walking = 0

            elif len(neighs) == 1:
                idx = neighs.pop()
                if idx in edgeidcs:
                    walking = 0
            else:
                bpi.add(idx)
                fourconn = iu.four_conn([idx], I)[0]
                fourconn = [f for f in fourconn if f in neighs]
                do_first.update(fourconn)
                emanators = emanators | neighs
                walked.add(idx)
                walked.update(neighs)

    return bpi